blob: 1ac4b92fa3a38a87d1acce17afc9ff207fd9ae3e [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte59b04682009-06-10 05:40:52 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080018 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte59b04682009-06-10 05:40:52 +080021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
Harald Welteeaf4a522012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut28c47162009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
39#include <openbsc/gsm_04_11.h>
40#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080042#include <openbsc/abis_rsl.h>
43#include <openbsc/chan_alloc.h>
44#include <openbsc/paging.h>
45#include <openbsc/signal.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte59b04682009-06-10 05:40:52 +080047#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080057
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080064
Harald Welte (local)8751ee92009-08-15 02:30:58 +020065void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010066void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020067
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020068int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080069static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020070 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010071static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080072static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080073
74struct gsm_lai {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020075 uint16_t mcc;
76 uint16_t mnc;
77 uint16_t lac;
Harald Welte59b04682009-06-10 05:40:52 +080078};
79
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020080static uint32_t new_callref = 0x80000001;
Harald Welte03740842009-06-10 23:11:52 +080081
Harald Welte4f272cc2011-03-03 23:29:05 +010082void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
83{
84 net->mncc_recv(net, msg);
85}
86
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080087static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
88 struct gsm_trans *trans)
89{
90 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
91
92 /* if we get passed a transaction reference, do some common
93 * work that the caller no longer has to do */
94 if (trans) {
95 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
96 msg->lchan = trans->conn->lchan;
97 }
98
99
100 if (msg->lchan) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200101 struct e1inp_sign_link *sign_link =
102 msg->lchan->ts->trx->rsl_link;
103
104 msg->dst = sign_link;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800105 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
106 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200107 "Sending '%s' to MS.\n",
108 sign_link->trx->bts->nr,
109 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800110 gh->proto_discr & 0xf0,
111 gsm48_cc_msg_name(gh->msg_type));
112 else
113 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200114 "Sending 0x%02x to MS.\n",
115 sign_link->trx->bts->nr,
116 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800117 gh->proto_discr, gh->msg_type);
118 }
119
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100120 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800121}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100122
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800123int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
124{
125 struct gsm48_hdr *gh;
126 struct msgb *ss_notify;
127
128 ss_notify = gsm0480_create_notifySS(message);
129 if (!ss_notify)
130 return -1;
131
132 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
133 uint8_t *data = msgb_push(ss_notify, 1);
134 data[0] = ss_notify->len - 1;
135 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
136 gh->msg_type = GSM48_MT_CC_FACILITY;
137 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
138}
139
Harald Weltefe01ed72012-01-23 16:40:24 +0100140void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100141{
142 if (!conn->sec_operation)
143 return;
144
145 talloc_free(conn->sec_operation);
146 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800147 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100148}
149
Harald Weltefe01ed72012-01-23 16:40:24 +0100150void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100151{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100152 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
153 struct gsm_security_operation);
154}
155
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100156int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
157 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100158{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800159 struct gsm_network *net = conn->bts->network;
160 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100161 struct gsm_security_operation *op;
162 struct gsm_auth_tuple atuple;
163 int status = -1, rc;
164
165 /* Check if we _can_ enable encryption. Cases where we can't:
166 * - Encryption disabled in config
167 * - Channel already secured (nothing to do)
168 * - Subscriber equipment doesn't support configured encryption
169 */
170 if (!net->a5_encryption) {
171 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800172 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100173 DEBUGP(DMM, "Requesting to secure an already secure channel");
174 status = GSM_SECURITY_SUCCEEDED;
175 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
176 net->a5_encryption)) {
177 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
178 status = GSM_SECURITY_NOAVAIL;
179 }
180
181 /* If not done yet, try to get info for this user */
182 if (status < 0) {
183 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
184 if (rc <= 0)
185 status = GSM_SECURITY_NOAVAIL;
186 }
187
188 /* Are we done yet ? */
189 if (status >= 0)
190 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800191 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100192 0;
193
194 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800195 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100196 return -EBUSY;
197
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800198 allocate_security_operation(conn);
199 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100200 op->cb = cb;
201 op->cb_data = cb_data;
202 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
203
204 /* FIXME: Should start a timer for completion ... */
205
206 /* Then do whatever is needed ... */
Harald Welte857e15d2010-12-23 18:07:49 +0100207 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100208 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800209 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100210 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100211 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100212 return gsm0808_cipher_mode(conn, net->a5_encryption,
213 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100214 }
215
216 return -EINVAL; /* not reached */
217}
218
Harald Welte59b04682009-06-10 05:40:52 +0800219static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
220 struct gsm_subscriber *subscriber)
221{
222 if (!subscriber)
223 return 0;
224
225 /*
226 * Do not send accept yet as more information should arrive. Some
227 * phones will not send us the information and we will have to check
228 * what we want to do with that.
229 */
230 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
231 return 0;
232
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200233 switch (subscriber->net->auth_policy) {
234 case GSM_AUTH_POLICY_CLOSED:
235 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200236 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200237 if (subscriber->authorized)
238 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200239 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200240 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800241 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200242 default:
243 return 0;
244 }
Harald Welte59b04682009-06-10 05:40:52 +0800245}
246
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100247static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800248{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100249 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800250 return;
251
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800252 /* No need to keep the connection up */
253 release_anchor(conn);
254
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200255 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100256 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100257 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800258 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800259}
260
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100261static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800262{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800263 if (conn->loc_operation)
264 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100265 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800266
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100267 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200268 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800269}
270
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100271static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
272 struct msgb *msg, void *data, void *param)
273{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800274 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100275 int rc = 0;
276
277 switch (event) {
278 case GSM_SECURITY_AUTH_FAILED:
279 release_loc_updating_req(conn);
280 break;
281
282 case GSM_SECURITY_NOAVAIL:
283 case GSM_SECURITY_SUCCEEDED:
284 /* We're all good */
285 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800286 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800287 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100288 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800289 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100290 }
291
292 /* call subscr_update after putting the loc_upd_acc
293 * in the transmit queue, since S_SUBSCR_ATTACHED might
294 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800295 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100296 GSM_SUBSCRIBER_UPDATE_ATTACHED);
297
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100298 /*
299 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
300 * MS needs to respond with a TMSI REALLOCATION COMPLETE
301 * (even if the TMSI is the same).
302 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100303 break;
304
305 default:
306 rc = -EINVAL;
307 };
308
309 return rc;
310}
311
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100312static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800313{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100314 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800315 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100316 conn->loc_operation->key_seq,
317 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800318 return 0;
319}
320
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800321void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800322{
Harald Welte03740842009-06-10 23:11:52 +0800323 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800324
325 /* avoid someone issuing a clear */
326 conn->in_release = 1;
327
Harald Welte59b04682009-06-10 05:40:52 +0800328 /*
329 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800330 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800331 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800332 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100333
334 /* We might need to cancel the paging response or such. */
335 if (conn->sec_operation && conn->sec_operation->cb) {
336 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
337 NULL, conn, conn->sec_operation->cb_data);
338 }
339
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800340 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800341 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800342
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100343 /*
344 * Free all transactions that are associated with the released
345 * connection. The transaction code will inform the CC or SMS
346 * facilities that will send the release indications. As part of
347 * the CC REL_IND the remote leg might be released and this will
348 * trigger the call to trans_free. This is something the llist
349 * macro can not handle and we will need to re-iterate the list.
350 *
351 * TODO: Move the trans_list into the subscriber connection and
352 * create a pending list for MT transactions. These exist before
353 * we have a subscriber connection.
354 */
355restart:
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800356 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100357 if (trans->conn == conn) {
Harald Weltec2189a62009-07-23 18:56:43 +0200358 trans_free(trans);
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100359 goto restart;
360 }
Harald Welte03740842009-06-10 23:11:52 +0800361 }
Harald Welte59b04682009-06-10 05:40:52 +0800362}
363
Harald Weltee8d16172010-12-22 23:17:50 +0100364void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
365{
366 struct gsm_trans *trans, *temp;
367
368 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
369
370 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100371 if (trans->protocol == protocol) {
372 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100373 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100374 }
Harald Weltee8d16172010-12-22 23:17:50 +0100375 }
376}
377
Harald Welte59b04682009-06-10 05:40:52 +0800378/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200379int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800380{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800381 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800382 struct msgb *msg;
383
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200384 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800385
386 msg = gsm48_create_loc_upd_rej(cause);
387 if (!msg) {
388 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
389 return -1;
390 }
Harald Welte59b04682009-06-10 05:40:52 +0800391
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800392 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800393
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100394 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100395 "LAC=%u BTS=%u\n", conn->subscr ?
396 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800397 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100398
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800399 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800400}
401
402/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200403int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800404{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800405 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800406 struct msgb *msg = gsm48_msgb_alloc();
407 struct gsm48_hdr *gh;
408 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200409 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800410
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800411 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800412
413 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
414 gh->proto_discr = GSM48_PDISC_MM;
415 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
416
417 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100418 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800419 bts->network->network_code, bts->location_area_code);
420
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200421 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200422 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800423
424 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
425
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200426 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100427
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800428 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800429}
430
Harald Welte59b04682009-06-10 05:40:52 +0800431/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200432static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800433{
434 struct msgb *msg = gsm48_msgb_alloc();
435 struct gsm48_hdr *gh;
436
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800437 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800438
439 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
440 gh->proto_discr = GSM48_PDISC_MM;
441 gh->msg_type = GSM48_MT_MM_ID_REQ;
442 gh->data[0] = id_type;
443
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800444 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800445}
446
Harald Welte59b04682009-06-10 05:40:52 +0800447
448/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800449static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800450{
451 struct gsm48_hdr *gh = msgb_l3(msg);
452 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200453 struct gsm_bts *bts = lchan->ts->trx->bts;
454 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200455 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200456 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800457
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200458 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800459 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
460 mi_type, mi_string);
461
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200462 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100463
Harald Welte59b04682009-06-10 05:40:52 +0800464 switch (mi_type) {
465 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200466 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100467 if (!conn->subscr) {
468 conn->subscr = subscr_get_by_imsi(net, mi_string);
469 if (!conn->subscr)
470 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200471 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100472 if (conn->loc_operation)
473 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800474 break;
475 case GSM_MI_TYPE_IMEI:
476 case GSM_MI_TYPE_IMEISV:
477 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100478 if (conn->subscr) {
479 db_subscriber_assoc_imei(conn->subscr, mi_string);
480 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200481 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100482 if (conn->loc_operation)
483 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800484 break;
485 }
486
487 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100488 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800489}
490
491
492static void loc_upd_rej_cb(void *data)
493{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100494 struct gsm_subscriber_connection *conn = data;
495 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100496 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800497
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100498 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800499 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800500 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800501}
502
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100503static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800504{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100505 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
506 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200507 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800508}
509
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200510static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800511{
512 switch (type) {
513 case GSM48_LUPD_NORMAL:
514 return "NORMAL";
515 case GSM48_LUPD_PERIODIC:
516 return "PEROIDOC";
517 case GSM48_LUPD_IMSI_ATT:
518 return "IMSI ATTACH";
519 default:
520 return "UNKNOWN";
521 }
522}
523
Harald Welte59b04682009-06-10 05:40:52 +0800524/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800525static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800526{
527 struct gsm48_hdr *gh = msgb_l3(msg);
528 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800529 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800530 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200531 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200532 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800533
534 lu = (struct gsm48_loc_upd_req *) gh->data;
535
536 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
537
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200538 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800539
Harald Welte79639662009-06-27 02:58:43 +0200540 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800541 lupd_name(lu->type));
542
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200543 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100544
Harald Welte3edc5a92009-12-22 00:41:05 +0100545 switch (lu->type) {
546 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200547 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100548 break;
549 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200550 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100551 break;
552 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200553 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100554 break;
555 }
556
Harald Welte59b04682009-06-10 05:40:52 +0800557 /*
558 * Pseudo Spoof detection: Just drop a second/concurrent
559 * location updating request.
560 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100561 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200562 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100563 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800564 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800565 return 0;
566 }
567
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800568 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800569
Sylvain Munaut360fc582010-06-10 13:36:59 +0200570 conn->loc_operation->key_seq = lu->key_seq;
571
Harald Welte59b04682009-06-10 05:40:52 +0800572 switch (mi_type) {
573 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200574 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800575 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200576 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100577 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800578
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200579 /* look up subscriber based on IMSI, create if not found */
580 subscr = subscr_get_by_imsi(bts->network, mi_string);
581 if (!subscr) {
582 subscr = db_create_subscriber(bts->network, mi_string);
583 }
Harald Welte59b04682009-06-10 05:40:52 +0800584 break;
585 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200586 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800587 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200588 subscr = subscr_get_by_tmsi(bts->network,
589 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800590 if (!subscr) {
591 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200592 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100593 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800594 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200595 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200596 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200597 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800598 break;
599 case GSM_MI_TYPE_IMEI:
600 case GSM_MI_TYPE_IMEISV:
601 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200602 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800603 break;
604 default:
Harald Welte79639662009-06-27 02:58:43 +0200605 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800606 break;
607 }
608
Harald Welteccd69362009-07-04 10:18:00 +0200609 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100610 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200611
Harald Welte03740842009-06-10 23:11:52 +0800612 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200613 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800614 /* FIXME: request id? close channel? */
615 return -EINVAL;
616 }
617
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100618 conn->subscr = subscr;
619 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800620
Harald Welteccd69362009-07-04 10:18:00 +0200621 /* check if we can let the subscriber into our network immediately
622 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100623 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800624}
625
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100626/* Turn int into semi-octet representation: 98 => 0x89 */
627static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800628{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100629 uint8_t ret;
630
631 ret = value / 10;
632 ret |= (value % 10) << 4;
633
634 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800635}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100636
Harald Welte03740842009-06-10 23:11:52 +0800637
Harald Welte59b04682009-06-10 05:40:52 +0800638/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800639int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800640{
641 struct msgb *msg = gsm48_msgb_alloc();
642 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800643 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100644 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200645 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200646 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100647
Harald Welte03740842009-06-10 23:11:52 +0800648 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100649 struct tm* gmt_time;
650 struct tm* local_time;
651 int tzunits;
Harald Welte59b04682009-06-10 05:40:52 +0800652
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800653 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800654
655 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
656 gh->proto_discr = GSM48_PDISC_MM;
657 gh->msg_type = GSM48_MT_MM_INFO;
658
659 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200660#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800661 name_len = strlen(net->name_long);
662 /* 10.5.3.5a */
663 ptr8 = msgb_put(msg, 3);
664 ptr8[0] = GSM48_IE_NAME_LONG;
665 ptr8[1] = name_len*2 +1;
666 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
667
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200668 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800669 for (i = 0; i < name_len; i++)
670 ptr16[i] = htons(net->name_long[i]);
671
672 /* FIXME: Use Cell Broadcast, not UCS-2, since
673 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200674#endif
675 name_len = (strlen(net->name_long)*7)/8;
676 name_pad = (8 - strlen(net->name_long)*7)%8;
677 if (name_pad > 0)
678 name_len++;
679 /* 10.5.3.5a */
680 ptr8 = msgb_put(msg, 3);
681 ptr8[0] = GSM48_IE_NAME_LONG;
682 ptr8[1] = name_len +1;
683 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
684
685 ptr8 = msgb_put(msg, name_len);
686 gsm_7bit_encode(ptr8, net->name_long);
687
Harald Welte59b04682009-06-10 05:40:52 +0800688 }
689
690 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200691#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800692 name_len = strlen(net->name_short);
693 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200694 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200695 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800696 ptr8[1] = name_len*2 + 1;
697 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
698
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200699 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800700 for (i = 0; i < name_len; i++)
701 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200702#endif
703 name_len = (strlen(net->name_short)*7)/8;
704 name_pad = (8 - strlen(net->name_short)*7)%8;
705 if (name_pad > 0)
706 name_len++;
707 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200708 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200709 ptr8[0] = GSM48_IE_NAME_SHORT;
710 ptr8[1] = name_len +1;
711 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
712
713 ptr8 = msgb_put(msg, name_len);
714 gsm_7bit_encode(ptr8, net->name_short);
715
Harald Welte59b04682009-06-10 05:40:52 +0800716 }
717
Harald Welte59b04682009-06-10 05:40:52 +0800718 /* Section 10.5.3.9 */
719 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100720 gmt_time = gmtime(&cur_t);
721
Harald Welte59b04682009-06-10 05:40:52 +0800722 ptr8 = msgb_put(msg, 8);
723 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100724 ptr8[1] = bcdify(gmt_time->tm_year % 100);
725 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
726 ptr8[3] = bcdify(gmt_time->tm_mday);
727 ptr8[4] = bcdify(gmt_time->tm_hour);
728 ptr8[5] = bcdify(gmt_time->tm_min);
729 ptr8[6] = bcdify(gmt_time->tm_sec);
730
Harald Welte5c3abf92012-07-08 16:48:11 +0200731 if (bts->tz.override) {
732 /* Convert tz.hr and tz.mn to units */
733 if (bts->tz.hr < 0) {
734 tzunits = ((bts->tz.hr/-1)*4);
735 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100736 ptr8[7] = bcdify(tzunits);
737 /* Set negative time */
738 ptr8[7] |= 0x08;
739 }
740 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200741 tzunits = bts->tz.hr*4;
742 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100743 ptr8[7] = bcdify(tzunits);
744 }
745 }
746 else {
747 /* Need to get GSM offset and convert into 15 min units */
748 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
749 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200750#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100751 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200752#else
753#warning find a portable way to obtain the timezone offset
754 tzunits = 0;
755#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100756 if (tzunits < 0) {
757 tzunits = tzunits/-1;
758 ptr8[7] = bcdify(tzunits);
759 /* Flip it to negative */
760 ptr8[7] |= 0x08;
761 }
762 else
763 ptr8[7] = bcdify(tzunits);
764 }
Harald Welte59b04682009-06-10 05:40:52 +0800765
Daniel Willmannc8188202009-08-13 03:42:07 +0200766 DEBUGP(DMM, "-> MM INFO\n");
767
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800768 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800769}
770
Harald Welte36970f62009-08-12 22:56:50 +0200771/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200772int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200773{
774 struct msgb *msg = gsm48_msgb_alloc();
775 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200776 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200777
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200778 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200779
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800780 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200781 gh->proto_discr = GSM48_PDISC_MM;
782 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
783
Sylvain Munaut0789c592009-12-24 00:23:46 +0100784 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200785
Harald Welte36970f62009-08-12 22:56:50 +0200786 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200787 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200788 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200789
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800790 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200791}
792
793/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800794int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200795{
796 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800797 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200798}
799
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800800static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800801{
802 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800803 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800804}
805
806/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100807static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800808 enum gsm48_reject_value value)
809{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800810 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800811
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800812 msg = gsm48_create_mm_serv_rej(value);
813 if (!msg) {
814 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
815 return -1;
816 }
Harald Welte59b04682009-06-10 05:40:52 +0800817
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800818 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100819 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800820 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800821}
822
Sylvain Munautbadc7112009-12-24 00:28:46 +0100823static int _gsm48_rx_mm_serv_req_sec_cb(
824 unsigned int hooknum, unsigned int event,
825 struct msgb *msg, void *data, void *param)
826{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800827 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100828 int rc = 0;
829
830 switch (event) {
831 case GSM_SECURITY_AUTH_FAILED:
832 /* Nothing to do */
833 break;
834
835 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800836 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100837 break;
838
839 case GSM_SECURITY_SUCCEEDED:
840 /* nothing to do. CIPHER MODE COMMAND is
841 * implicit CM SERV ACK */
842 break;
843
844 default:
845 rc = -EINVAL;
846 };
847
848 return rc;
849}
850
Harald Welte59b04682009-06-10 05:40:52 +0800851/*
852 * Handle CM Service Requests
853 * a) Verify that the packet is long enough to contain the information
854 * we require otherwsie reject with INCORRECT_MESSAGE
855 * b) Try to parse the TMSI. If we do not have one reject
856 * c) Check that we know the subscriber with the TMSI otherwise reject
857 * with a HLR cause
858 * d) Set the subscriber on the gsm_lchan and accept
859 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800860static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800861{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200862 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200863 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800864
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800865 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800866 struct gsm_subscriber *subscr;
867 struct gsm48_hdr *gh = msgb_l3(msg);
868 struct gsm48_service_request *req =
869 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800870 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200871 uint8_t classmark2_len = gh->data[1];
872 uint8_t *classmark2 = gh->data+2;
873 uint8_t mi_len = *(classmark2 + classmark2_len);
874 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800875
876 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
877 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
878 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800879 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800880 GSM48_REJECT_INCORRECT_MESSAGE);
881 }
882
883 if (msg->data_len < req->mi_len + 6) {
884 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800885 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800886 GSM48_REJECT_INCORRECT_MESSAGE);
887 }
888
889 mi_type = mi[0] & GSM_MI_TYPE_MASK;
890 if (mi_type != GSM_MI_TYPE_TMSI) {
891 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800892 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800893 GSM48_REJECT_INCORRECT_MESSAGE);
894 }
895
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200896 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800897 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
898 req->cm_service_type, mi_type, mi_string);
899
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200900 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100901
Harald Welte583ceaf2009-08-10 10:12:45 +0200902 if (is_siemens_bts(bts))
903 send_siemens_mrpci(msg->lchan, classmark2-1);
904
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200905 subscr = subscr_get_by_tmsi(bts->network,
906 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800907
908 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
909 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800910 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800911 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freytherfe036952013-01-01 17:18:27 +0100912 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munautf1087412012-12-28 00:33:04 +0100913 /* If the subscriber is not attached, reject service */
914 return gsm48_tx_mm_serv_rej(conn,
915 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte59b04682009-06-10 05:40:52 +0800916
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800917 if (!conn->subscr)
918 conn->subscr = subscr;
919 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100920 subscr_put(subscr); /* lchan already has a ref, don't need another one */
921 else {
Harald Welte59b04682009-06-10 05:40:52 +0800922 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
923 subscr_put(subscr);
924 }
925
Harald Weltef6845a72009-07-05 14:08:13 +0200926 subscr->equipment.classmark2_len = classmark2_len;
927 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
928 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800929
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800930 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100931 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800932}
933
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200934static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800935{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200936 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800937 struct gsm48_hdr *gh = msgb_l3(msg);
938 struct gsm48_imsi_detach_ind *idi =
939 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200940 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200941 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800942 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800943
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200944 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100945 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte59b04682009-06-10 05:40:52 +0800946 mi_type, mi_string);
947
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200948 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100949
Harald Welte59b04682009-06-10 05:40:52 +0800950 switch (mi_type) {
951 case GSM_MI_TYPE_TMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100952 DEBUGPC(DMM, "\n");
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200953 subscr = subscr_get_by_tmsi(bts->network,
954 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800955 break;
956 case GSM_MI_TYPE_IMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100957 DEBUGPC(DMM, "\n");
Harald Welte75350412009-07-23 18:46:00 +0200958 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800959 break;
960 case GSM_MI_TYPE_IMEI:
961 case GSM_MI_TYPE_IMEISV:
962 /* no sim card... FIXME: what to do ? */
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100963 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800964 break;
965 default:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100966 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800967 break;
968 }
969
970 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200971 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +0800972 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100973 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200974
975 subscr->equipment.classmark1 = idi->classmark1;
976 db_sync_equipment(&subscr->equipment);
977
Harald Welte59b04682009-06-10 05:40:52 +0800978 subscr_put(subscr);
979 } else
980 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
981
Harald Welte43250dd2009-12-20 09:58:40 +0100982 /* FIXME: iterate over all transactions and release them,
983 * imagine an IMSI DETACH happening during an active call! */
984
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200985 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800986 return 0;
987}
988
989static int gsm48_rx_mm_status(struct msgb *msg)
990{
991 struct gsm48_hdr *gh = msgb_l3(msg);
992
993 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
994
995 return 0;
996}
997
Sylvain Munaut28c47162009-12-24 00:27:26 +0100998/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800999static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001000{
1001 struct gsm48_hdr *gh = msgb_l3(msg);
1002 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001003 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001004
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001005 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001006 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +01001007
1008 /* Safety check */
1009 if (!conn->sec_operation) {
1010 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1011 return -EIO;
1012 }
1013
1014 /* Validate SRES */
1015 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001016 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001017 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001018
1019 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001020 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001021
Sylvain Munaut28c47162009-12-24 00:27:26 +01001022 if (cb)
1023 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001024 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001025
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001026 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001027 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001028 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001029 }
1030
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001031 DEBUGPC(DMM, "OK\n");
1032
Sylvain Munaut28c47162009-12-24 00:27:26 +01001033 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001034 return gsm0808_cipher_mode(conn, net->a5_encryption,
1035 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001036}
1037
Harald Welte59b04682009-06-10 05:40:52 +08001038/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001039static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001040{
1041 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001042 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001043
1044 switch (gh->msg_type & 0xbf) {
1045 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001046 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001047 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001048 break;
1049 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001050 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001051 break;
1052 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001053 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001054 break;
1055 case GSM48_MT_MM_STATUS:
1056 rc = gsm48_rx_mm_status(msg);
1057 break;
1058 case GSM48_MT_MM_TMSI_REALL_COMPL:
1059 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001060 conn->subscr ?
1061 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001062 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001063 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001064 break;
1065 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001066 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001067 break;
1068 case GSM48_MT_MM_CM_REEST_REQ:
1069 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1070 break;
1071 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001072 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001073 break;
1074 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001075 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001076 gh->msg_type);
1077 break;
1078 }
1079
1080 return rc;
1081}
1082
1083/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001084static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001085{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001086 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001087 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001088 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001089 uint8_t *classmark2_lv = gh->data + 1;
1090 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001091 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001092 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001093 int rc = 0;
1094
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001095 resp = (struct gsm48_pag_resp *) &gh->data[0];
1096 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1097 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001098 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1099 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001100
Harald Welte59b04682009-06-10 05:40:52 +08001101 switch (mi_type) {
1102 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001103 subscr = subscr_get_by_tmsi(bts->network,
1104 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001105 break;
1106 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001107 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001108 break;
1109 }
1110
1111 if (!subscr) {
1112 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1113 /* FIXME: request id? close channel? */
1114 return -EINVAL;
1115 }
1116 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001117 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001118
Harald Weltef6845a72009-07-05 14:08:13 +02001119 subscr->equipment.classmark2_len = *classmark2_lv;
1120 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1121 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001122
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001123 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001124 return rc;
1125}
1126
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001127static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001128{
1129 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001130 uint8_t apdu_id_flags;
1131 uint8_t apdu_len;
1132 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001133
1134 apdu_id_flags = gh->data[0];
1135 apdu_len = gh->data[1];
1136 apdu_data = gh->data+2;
1137
Harald Welted4f21662012-07-16 13:22:19 +02001138 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001139 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001140
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001141 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001142}
1143
Harald Welte59b04682009-06-10 05:40:52 +08001144/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001145static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001146{
1147 struct gsm48_hdr *gh = msgb_l3(msg);
1148 int rc = 0;
1149
1150 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001151 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001152 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001153 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001154 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001155 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001156 break;
Harald Welte59b04682009-06-10 05:40:52 +08001157 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001158 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001159 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001160 break;
1161 }
1162
1163 return rc;
1164}
1165
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001166int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1167 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001168{
1169 struct msgb *msg = gsm48_msgb_alloc();
1170 struct gsm48_hdr *gh;
1171
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001172 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001173
1174 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1175 apdu_id, apdu_len);
1176
1177 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1178 gh->proto_discr = GSM48_PDISC_RR;
1179 gh->msg_type = GSM48_MT_RR_APP_INFO;
1180 gh->data[0] = apdu_id;
1181 gh->data[1] = apdu_len;
1182 memcpy(gh->data+2, apdu, apdu_len);
1183
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001184 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001185}
1186
Harald Welte59b04682009-06-10 05:40:52 +08001187/* Call Control */
1188
1189/* The entire call control code is written in accordance with Figure 7.10c
1190 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1191 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1192 * it for voice */
1193
Harald Welte03740842009-06-10 23:11:52 +08001194static void new_cc_state(struct gsm_trans *trans, int state)
1195{
1196 if (state > 31 || state < 0)
1197 return;
1198
1199 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001200 gsm48_cc_state_name(trans->cc.state),
1201 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001202
Harald Weltec2189a62009-07-23 18:56:43 +02001203 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001204}
1205
1206static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001207{
1208 struct msgb *msg = gsm48_msgb_alloc();
1209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001210 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001211
Harald Welte59b04682009-06-10 05:40:52 +08001212 gh->msg_type = GSM48_MT_CC_STATUS;
1213
1214 cause = msgb_put(msg, 3);
1215 cause[0] = 2;
1216 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1217 cause[2] = 0x80 | 30; /* response to status inquiry */
1218
1219 call_state = msgb_put(msg, 1);
1220 call_state[0] = 0xc0 | 0x00;
1221
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001222 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001223}
1224
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001225static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001226 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001227{
1228 struct msgb *msg = gsm48_msgb_alloc();
1229 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1230
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001231 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001232
1233 gh->proto_discr = pdisc;
1234 gh->msg_type = msg_type;
1235
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001236 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001237}
1238
Harald Welte03740842009-06-10 23:11:52 +08001239static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1240{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001241 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001242 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001243 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001244 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001245 }
1246}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001247
Harald Welte03740842009-06-10 23:11:52 +08001248static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1249 int msg_type, struct gsm_mncc *mncc)
1250{
1251 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001252 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001253
1254 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001255 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001256 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001257 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001258 trans->conn->lchan->ts->trx->bts->nr,
1259 trans->conn->lchan->ts->trx->nr,
1260 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001261 (trans->subscr)?(trans->subscr->extension):"-",
1262 get_mncc_name(msg_type));
1263 else
1264 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1265 "Sending '%s' to MNCC.\n",
1266 (trans->subscr)?(trans->subscr->extension):"-",
1267 get_mncc_name(msg_type));
1268 else
1269 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1270 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1271
1272 mncc->msg_type = msg_type;
1273
Harald Welte9cfc9352009-06-26 19:39:35 +02001274 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001275 if (!msg)
1276 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001277
1278 data = msgb_put(msg, sizeof(struct gsm_mncc));
1279 memcpy(data, mncc, sizeof(struct gsm_mncc));
1280
Harald Weltebe0cb682010-12-22 23:43:29 +01001281 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001282
1283 return 0;
1284}
1285
1286int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001287 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001288{
1289 struct gsm_mncc rel;
1290
Harald Weltecb0595f2009-06-12 01:54:08 +08001291 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001292 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001293 mncc_set_cause(&rel, location, value);
Andreas Eversberg59e79ee2013-01-25 08:38:29 +01001294 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1295 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001296 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1297}
1298
Harald Weltec2189a62009-07-23 18:56:43 +02001299/* Call Control Specific transaction release.
1300 * gets called by trans_free, DO NOT CALL YOURSELF! */
1301void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001302{
Harald Welte03740842009-06-10 23:11:52 +08001303 gsm48_stop_cc_timer(trans);
1304
1305 /* send release to L4, if callref still exists */
1306 if (trans->callref) {
1307 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001308 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001309 GSM48_CAUSE_LOC_PRN_S_LU,
1310 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001311 }
Harald Weltec2189a62009-07-23 18:56:43 +02001312 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001313 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001314 if (trans->conn)
1315 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001316}
1317
1318static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001319
Harald Welte59b04682009-06-10 05:40:52 +08001320/* call-back from paging the B-end of the connection */
1321static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001322 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001323{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001324 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001325 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001326 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001327 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001328
Harald Welte59b04682009-06-10 05:40:52 +08001329 if (hooknum != GSM_HOOK_RR_PAGING)
1330 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001331
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001332 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001333 if (!net) {
1334 DEBUGP(DCC, "Error Network not set!\n");
1335 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001336 }
1337
Harald Welte03740842009-06-10 23:11:52 +08001338 /* check all tranactions (without lchan) for subscriber */
1339 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001340 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001341 continue;
1342 switch (event) {
1343 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001344 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001345 break;
1346 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001347 transt->subscr->extension);
1348 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001349 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001350 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001351 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001352 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001353 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001354 }
1355 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001356 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001357 break;
1358 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001359 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001360 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001361 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001362 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001363 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001364 mncc_release_ind(transt->subscr->net, transt,
1365 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001366 GSM48_CAUSE_LOC_PRN_S_LU,
1367 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001368 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001369 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001370 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001371 break;
1372 }
1373 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001374
1375 talloc_free(paging_request);
1376
1377 /*
1378 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1379 * failure and then the subscr will be poke again. This needs a lot of fixing
1380 * in the subscriber queue code.
1381 */
1382 if (!found && conn)
1383 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001384 return 0;
1385}
1386
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001387static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001388
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001389/* handle audio path for handover */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001390static int switch_for_handover(struct gsm_lchan *old_lchan,
1391 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001392{
1393 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001394
1395 if (ipacc_rtp_direct) {
1396 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1397 return 0;
1398 }
1399
1400 /* RTP Proxy mode */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001401 new_rs = new_lchan->abis_ip.rtp_socket;
1402 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001403
1404 if (!new_rs) {
1405 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1406 return -EIO;
1407 }
1408
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001409 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001410
1411 if (!old_rs) {
1412 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1413 return -EIO;
1414 }
1415
1416 /* copy rx_action and reference to other sock */
1417 new_rs->rx_action = old_rs->rx_action;
1418 new_rs->tx_action = old_rs->tx_action;
1419 new_rs->transmit = old_rs->transmit;
1420
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001421 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001422 case RTP_PROXY:
1423 other_rs = old_rs->proxy.other_sock;
1424 rtp_socket_proxy(new_rs, other_rs);
1425 /* delete reference to other end socket to prevent
1426 * rtp_socket_free() from removing the inverse reference */
1427 old_rs->proxy.other_sock = NULL;
1428 break;
1429 case RTP_RECV_UPSTREAM:
1430 new_rs->receive = old_rs->receive;
1431 break;
1432 case RTP_NONE:
1433 break;
1434 }
1435
1436 return 0;
1437}
1438
Harald Welte3c062072009-07-28 18:25:29 +02001439/* some other part of the code sends us a signal */
1440static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1441 void *handler_data, void *signal_data)
1442{
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001443 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte3c062072009-07-28 18:25:29 +02001444 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001445 struct gsm_network *net;
1446 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001447
1448 if (subsys != SS_ABISIP)
1449 return 0;
1450
1451 /* in case we use direct BTS-to-BTS RTP */
1452 if (ipacc_rtp_direct)
1453 return 0;
1454
Harald Welte3c062072009-07-28 18:25:29 +02001455 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001456 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001457 /* in case we don't use direct BTS-to-BTS RTP */
1458 /* the BTS has successfully bound a TCH to a local ip/port,
1459 * which means we can connect our UDP socket to it */
1460 if (lchan->abis_ip.rtp_socket) {
1461 rtp_socket_free(lchan->abis_ip.rtp_socket);
1462 lchan->abis_ip.rtp_socket = NULL;
1463 }
1464
1465 lchan->abis_ip.rtp_socket = rtp_socket_create();
1466 if (!lchan->abis_ip.rtp_socket)
1467 return -EIO;
1468
1469 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1470 lchan->abis_ip.bound_ip,
1471 lchan->abis_ip.bound_port);
1472 if (rc < 0)
1473 return -EIO;
1474
Harald Welte3971ad52009-12-19 22:23:05 +01001475 /* check if any transactions on this lchan still have
1476 * a tch_recv_mncc request pending */
1477 net = lchan->ts->trx->bts->network;
1478 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001479 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001480 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1481 tch_recv_mncc(net, trans->callref, 1);
1482 }
1483 }
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001484
1485 /*
1486 * TODO: this appears to be too early? Why not until after
1487 * the handover detect or the handover complete?
1488 *
1489 * Do we have a handover pending for this new lchan? In that
1490 * case re-route the audio from the old channel to the new one.
1491 */
1492 old_lchan = bsc_handover_pending(lchan);
1493 if (old_lchan)
1494 switch_for_handover(old_lchan, lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001495 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001496 case S_ABISIP_DLCX_IND:
1497 /* the BTS tells us a RTP stream has been disconnected */
1498 if (lchan->abis_ip.rtp_socket) {
1499 rtp_socket_free(lchan->abis_ip.rtp_socket);
1500 lchan->abis_ip.rtp_socket = NULL;
1501 }
1502
1503 break;
Harald Welte3c062072009-07-28 18:25:29 +02001504 }
1505
1506 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001507}
1508
Harald Welte59b04682009-06-10 05:40:52 +08001509/* map two ipaccess RTP streams onto each other */
1510static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1511{
1512 struct gsm_bts *bts = lchan->ts->trx->bts;
1513 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001514 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001515
1516 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1517 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1518 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1519
1520 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001521 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001522 return -EINVAL;
1523 }
Harald Welte3971ad52009-12-19 22:23:05 +01001524
1525 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001526 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001527 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001528 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001529 if (!ipacc_rtp_direct) {
1530 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001531 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001532 if (rc < 0)
1533 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001534 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001535 if (rc < 0)
1536 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001537 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301538 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1539 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001540 } else {
1541 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301542 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1543 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301544 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001545 if (rc < 0)
1546 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301547 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1548 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301549 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001550 }
Harald Welte59b04682009-06-10 05:40:52 +08001551 break;
1552 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001553 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001554 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001555 trau_mux_map_lchan(lchan, remote_lchan);
1556 break;
1557 default:
Harald Welte8099a712011-08-09 21:53:20 +02001558 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001559 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001560 }
1561
1562 return 0;
1563}
1564
Harald Welte03740842009-06-10 23:11:52 +08001565/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001566static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001567{
Harald Weltec2189a62009-07-23 18:56:43 +02001568 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1569 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001570
Harald Welte03740842009-06-10 23:11:52 +08001571 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001572 return -EIO;
1573
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001574 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001575 return -EIO;
1576
1577 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001578 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001579}
1580
Harald Welte3971ad52009-12-19 22:23:05 +01001581/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001582static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001583{
1584 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001585 struct gsm_lchan *lchan;
1586 struct gsm_bts *bts;
1587 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001588
Harald Welte03740842009-06-10 23:11:52 +08001589 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001590 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001591 if (!trans)
1592 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001593 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001594 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001595 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001596 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001597
Harald Welte3971ad52009-12-19 22:23:05 +01001598 switch (bts->type) {
1599 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001600 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001601 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001602 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001603 return -EINVAL;
1604 }
1605 /* in case, we don't have a RTP socket yet, we note this
1606 * in the transaction and try later */
1607 if (!lchan->abis_ip.rtp_socket) {
1608 trans->tch_recv = enable;
1609 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1610 return 0;
1611 }
1612 if (enable) {
1613 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001614 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001615 if (rc < 0)
1616 return rc;
1617 /* assign socket to application interface */
1618 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1619 net, callref);
1620 } else
1621 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1622 net, 0);
1623 break;
1624 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001625 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001626 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001627 if (enable)
1628 return trau_recv_lchan(lchan, callref);
1629 return trau_mux_unmap(NULL, callref);
1630 break;
1631 default:
Harald Welte8099a712011-08-09 21:53:20 +02001632 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001633 return -EINVAL;
1634 }
1635
1636 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001637}
1638
Harald Welte03740842009-06-10 23:11:52 +08001639static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1640{
1641 DEBUGP(DCC, "-> STATUS ENQ\n");
1642 return gsm48_cc_tx_status(trans, msg);
1643}
1644
1645static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1646static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1647
1648static void gsm48_cc_timeout(void *arg)
1649{
1650 struct gsm_trans *trans = arg;
1651 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001652 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1653 int mo_location = GSM48_CAUSE_LOC_USER;
1654 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1655 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001656 struct gsm_mncc mo_rel, l4_rel;
1657
1658 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1659 mo_rel.callref = trans->callref;
1660 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1661 l4_rel.callref = trans->callref;
1662
Harald Weltec2189a62009-07-23 18:56:43 +02001663 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001664 case 0x303:
1665 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001666 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001667 break;
1668 case 0x310:
1669 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001670 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001671 break;
1672 case 0x313:
1673 disconnect = 1;
1674 /* unknown, did not find it in the specs */
1675 break;
1676 case 0x301:
1677 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001678 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001679 break;
1680 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001681 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001682 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001683 gsm48_cc_tx_release(trans, &trans->cc.msg);
1684 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001685 break; /* stay in release state */
1686 }
Harald Weltec2189a62009-07-23 18:56:43 +02001687 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001688 return;
1689// release = 1;
1690// l4_cause = 14;
1691// break;
1692 case 0x306:
1693 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001694 mo_cause = trans->cc.msg.cause.value;
1695 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001696 break;
1697 case 0x323:
1698 disconnect = 1;
1699 break;
1700 default:
1701 release = 1;
1702 }
1703
1704 if (release && trans->callref) {
1705 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001706 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001707 l4_location, l4_cause);
1708 trans->callref = 0;
1709 }
1710
1711 if (disconnect && trans->callref) {
1712 /* process disconnect towards layer 4 */
1713 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001714 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001715 }
1716
1717 /* process disconnect towards mobile station */
1718 if (disconnect || release) {
1719 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001720 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1721 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1722 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001723 mo_rel.cause.diag_len = 3;
1724
1725 if (disconnect)
1726 gsm48_cc_tx_disconnect(trans, &mo_rel);
1727 if (release)
1728 gsm48_cc_tx_release(trans, &mo_rel);
1729 }
1730
1731}
1732
1733static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1734 int sec, int micro)
1735{
1736 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001737 trans->cc.timer.cb = gsm48_cc_timeout;
1738 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001739 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001740 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001741}
1742
1743static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1744{
1745 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001746 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001747 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1748 struct tlv_parsed tp;
1749 struct gsm_mncc setup;
1750
1751 memset(&setup, 0, sizeof(struct gsm_mncc));
1752 setup.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001753 if (trans->conn && trans->conn->lchan)
1754 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001755 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001756 /* emergency setup is identified by msg_type */
1757 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1758 setup.emergency = 1;
1759
1760 /* use subscriber as calling party number */
1761 if (trans->subscr) {
1762 setup.fields |= MNCC_F_CALLING;
1763 strncpy(setup.calling.number, trans->subscr->extension,
1764 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001765 strncpy(setup.imsi, trans->subscr->imsi,
1766 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001767 }
1768 /* bearer capability */
1769 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1770 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001771 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001772 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1773 }
1774 /* facility */
1775 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1776 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001777 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001778 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1779 }
1780 /* called party bcd number */
1781 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1782 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001783 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001784 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1785 }
1786 /* user-user */
1787 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1788 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001789 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001790 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1791 }
1792 /* ss-version */
1793 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1794 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001795 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001796 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1797 }
1798 /* CLIR suppression */
1799 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1800 setup.clir.sup = 1;
1801 /* CLIR invocation */
1802 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1803 setup.clir.inv = 1;
1804 /* cc cap */
1805 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1806 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001807 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001808 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1809 }
1810
Harald Welte03740842009-06-10 23:11:52 +08001811 new_cc_state(trans, GSM_CSTATE_INITIATED);
1812
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001813 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1814 subscr_name(trans->subscr), trans->subscr->extension,
1815 setup.called.number);
1816
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001817 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001818
Harald Welte03740842009-06-10 23:11:52 +08001819 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001820 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001821
Harald Welteca745e22009-07-29 12:10:35 +02001822 /* MNCC code will modify the channel asynchronously, we should
1823 * ipaccess-bind only after the modification has been made to the
1824 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001825 return 0;
1826}
1827
1828static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001829{
1830 struct msgb *msg = gsm48_msgb_alloc();
1831 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001832 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001833 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001834
1835 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1836
Harald Welte03740842009-06-10 23:11:52 +08001837 /* transaction id must not be assigned */
1838 if (trans->transaction_id != 0xff) { /* unasssigned */
1839 DEBUGP(DCC, "TX Setup with assigned transaction. "
1840 "This is not allowed!\n");
1841 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001842 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001843 GSM48_CAUSE_LOC_PRN_S_LU,
1844 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001845 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001846 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001847 return rc;
1848 }
1849
1850 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001851 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1852 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001853 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001854 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001855 GSM48_CAUSE_LOC_PRN_S_LU,
1856 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001857 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001858 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001859 return rc;
1860 }
Harald Welte165fc332009-07-23 21:36:44 +02001861 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001862
Harald Welte59b04682009-06-10 05:40:52 +08001863 gh->msg_type = GSM48_MT_CC_SETUP;
1864
Harald Welte03740842009-06-10 23:11:52 +08001865 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001866
Harald Welte03740842009-06-10 23:11:52 +08001867 /* bearer capability */
1868 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001869 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001870 /* facility */
1871 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001872 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001873 /* progress */
1874 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001875 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001876 /* calling party BCD number */
1877 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001878 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001879 /* called party BCD number */
1880 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001881 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001882 /* user-user */
1883 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001884 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001885 /* redirecting party BCD number */
1886 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001887 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001888 /* signal */
1889 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001890 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001891
1892 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001893
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001894 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001895
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001896 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001897}
1898
Harald Welte03740842009-06-10 23:11:52 +08001899static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1900{
1901 struct gsm48_hdr *gh = msgb_l3(msg);
1902 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1903 struct tlv_parsed tp;
1904 struct gsm_mncc call_conf;
1905
1906 gsm48_stop_cc_timer(trans);
1907 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1908
1909 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1910 call_conf.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001911 if (trans->conn && trans->conn->lchan)
1912 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001913 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001914#if 0
1915 /* repeat */
1916 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1917 call_conf.repeat = 1;
1918 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1919 call_conf.repeat = 2;
1920#endif
1921 /* bearer capability */
1922 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1923 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001924 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001925 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1926 }
1927 /* cause */
1928 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1929 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001930 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001931 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1932 }
1933 /* cc cap */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1935 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001936 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1938 }
1939
1940 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1941
Harald Welte0abe5a62009-07-23 19:06:52 +02001942 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1943 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001944}
1945
1946static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1947{
1948 struct gsm_mncc *proceeding = arg;
1949 struct msgb *msg = gsm48_msgb_alloc();
1950 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1951
Harald Welte03740842009-06-10 23:11:52 +08001952 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1953
1954 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1955
1956 /* bearer capability */
1957 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001958 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001959 /* facility */
1960 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001961 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001962 /* progress */
1963 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001964 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001965
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001966 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001967}
1968
1969static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1970{
1971 struct gsm48_hdr *gh = msgb_l3(msg);
1972 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1973 struct tlv_parsed tp;
1974 struct gsm_mncc alerting;
1975
1976 gsm48_stop_cc_timer(trans);
1977 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1978
1979 memset(&alerting, 0, sizeof(struct gsm_mncc));
1980 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001981 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001982 /* facility */
1983 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1984 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001985 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001986 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1987 }
1988
1989 /* progress */
1990 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1991 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001992 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001993 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1994 }
1995 /* ss-version */
1996 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1997 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001998 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001999 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2000 }
2001
2002 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2003
Harald Welte0abe5a62009-07-23 19:06:52 +02002004 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2005 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002006}
2007
2008static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2009{
2010 struct gsm_mncc *alerting = arg;
2011 struct msgb *msg = gsm48_msgb_alloc();
2012 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2013
Harald Welte03740842009-06-10 23:11:52 +08002014 gh->msg_type = GSM48_MT_CC_ALERTING;
2015
2016 /* facility */
2017 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002018 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002019 /* progress */
2020 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002021 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002022 /* user-user */
2023 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002024 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002025
2026 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2027
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002028 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002029}
2030
2031static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2032{
2033 struct gsm_mncc *progress = 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_PROGRESS;
2038
2039 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002040 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002041 /* user-user */
2042 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002043 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002044
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002045 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002046}
2047
2048static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2049{
2050 struct gsm_mncc *connect = arg;
2051 struct msgb *msg = gsm48_msgb_alloc();
2052 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2053
Harald Welte03740842009-06-10 23:11:52 +08002054 gh->msg_type = GSM48_MT_CC_CONNECT;
2055
2056 gsm48_stop_cc_timer(trans);
2057 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2058
2059 /* facility */
2060 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002061 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002062 /* progress */
2063 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002064 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002065 /* connected number */
2066 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002067 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002068 /* user-user */
2069 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002070 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002071
2072 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2073
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002074 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002075}
2076
2077static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2078{
2079 struct gsm48_hdr *gh = msgb_l3(msg);
2080 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2081 struct tlv_parsed tp;
2082 struct gsm_mncc connect;
2083
2084 gsm48_stop_cc_timer(trans);
2085
2086 memset(&connect, 0, sizeof(struct gsm_mncc));
2087 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002088 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002089 /* use subscriber as connected party number */
2090 if (trans->subscr) {
2091 connect.fields |= MNCC_F_CONNECTED;
2092 strncpy(connect.connected.number, trans->subscr->extension,
2093 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002094 strncpy(connect.imsi, trans->subscr->imsi,
2095 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002096 }
2097 /* facility */
2098 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2099 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002100 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002101 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2102 }
2103 /* user-user */
2104 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2105 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002106 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002107 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2108 }
2109 /* ss-version */
2110 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2111 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002112 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002113 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2114 }
2115
2116 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002117 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002118
Harald Welte0abe5a62009-07-23 19:06:52 +02002119 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002120}
2121
2122
2123static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2124{
2125 struct gsm_mncc connect_ack;
2126
2127 gsm48_stop_cc_timer(trans);
2128
2129 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002130 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002131
2132 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2133 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002134
Harald Welte0abe5a62009-07-23 19:06:52 +02002135 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002136 &connect_ack);
2137}
2138
2139static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2140{
2141 struct msgb *msg = gsm48_msgb_alloc();
2142 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2143
Harald Welte03740842009-06-10 23:11:52 +08002144 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2145
2146 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2147
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002148 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002149}
2150
2151static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2152{
2153 struct gsm48_hdr *gh = msgb_l3(msg);
2154 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2155 struct tlv_parsed tp;
2156 struct gsm_mncc disc;
2157
2158 gsm48_stop_cc_timer(trans);
2159
2160 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2161
2162 memset(&disc, 0, sizeof(struct gsm_mncc));
2163 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002164 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002165 /* cause */
2166 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2167 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002168 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002169 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2170 }
2171 /* facility */
2172 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2173 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002174 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002175 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2176 }
2177 /* user-user */
2178 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2179 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002180 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002181 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2182 }
2183 /* ss-version */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2185 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002186 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002187 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2188 }
2189
Harald Welte0abe5a62009-07-23 19:06:52 +02002190 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002191
2192}
2193
Harald Weltebbc636a2009-06-11 14:23:20 +08002194static struct gsm_mncc_cause default_cause = {
2195 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2196 .coding = 0,
2197 .rec = 0,
2198 .rec_val = 0,
2199 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2200 .diag_len = 0,
2201 .diag = { 0 },
2202};
Harald Welte03740842009-06-10 23:11:52 +08002203
2204static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2205{
2206 struct gsm_mncc *disc = arg;
2207 struct msgb *msg = gsm48_msgb_alloc();
2208 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2209
Harald Welte03740842009-06-10 23:11:52 +08002210 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2211
2212 gsm48_stop_cc_timer(trans);
2213 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2214
2215 /* cause */
2216 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002217 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002218 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002219 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002220
2221 /* facility */
2222 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002223 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002224 /* progress */
2225 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002226 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002227 /* user-user */
2228 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002229 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002230
2231 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002232 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002233
2234 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2235
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002236 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002237}
2238
2239static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2240{
2241 struct gsm48_hdr *gh = msgb_l3(msg);
2242 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2243 struct tlv_parsed tp;
2244 struct gsm_mncc rel;
2245 int rc;
2246
2247 gsm48_stop_cc_timer(trans);
2248
2249 memset(&rel, 0, sizeof(struct gsm_mncc));
2250 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002251 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002252 /* cause */
2253 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2254 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002255 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002256 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2257 }
2258 /* facility */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2260 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002261 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002262 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2263 }
2264 /* user-user */
2265 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2266 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002267 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002268 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2269 }
2270 /* ss-version */
2271 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2272 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002273 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002274 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2275 }
2276
Harald Weltec2189a62009-07-23 18:56:43 +02002277 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002278 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002279 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002280 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002281 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002282 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002283 GSM48_MT_CC_RELEASE_COMPL);
2284 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002285 }
2286
2287 new_cc_state(trans, GSM_CSTATE_NULL);
2288
2289 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002290 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002291
2292 return rc;
2293}
2294
2295static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2296{
2297 struct gsm_mncc *rel = arg;
2298 struct msgb *msg = gsm48_msgb_alloc();
2299 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2300
Harald Welte03740842009-06-10 23:11:52 +08002301 gh->msg_type = GSM48_MT_CC_RELEASE;
2302
2303 trans->callref = 0;
2304
2305 gsm48_stop_cc_timer(trans);
2306 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2307
2308 /* cause */
2309 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002310 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002311 /* facility */
2312 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002313 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002314 /* user-user */
2315 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002316 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002317
Harald Weltec2189a62009-07-23 18:56:43 +02002318 trans->cc.T308_second = 0;
2319 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002320
Harald Weltec2189a62009-07-23 18:56:43 +02002321 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002322 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2323
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002324 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002325}
2326
2327static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2328{
2329 struct gsm48_hdr *gh = msgb_l3(msg);
2330 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2331 struct tlv_parsed tp;
2332 struct gsm_mncc rel;
2333 int rc = 0;
2334
2335 gsm48_stop_cc_timer(trans);
2336
2337 memset(&rel, 0, sizeof(struct gsm_mncc));
2338 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002339 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002340 /* cause */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2342 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002343 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002344 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2345 }
2346 /* facility */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2348 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002349 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002350 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2351 }
2352 /* user-user */
2353 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2354 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002355 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002356 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2357 }
2358 /* ss-version */
2359 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2360 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002361 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002362 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2363 }
2364
2365 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002366 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002367 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002368 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002369 MNCC_REJ_IND, &rel);
2370 break;
2371 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002372 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002373 MNCC_REL_CNF, &rel);
2374 break;
2375 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002376 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002377 MNCC_REL_IND, &rel);
2378 }
2379 }
2380
2381 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002382 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002383
2384 return rc;
2385}
2386
2387static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2388{
2389 struct gsm_mncc *rel = arg;
2390 struct msgb *msg = gsm48_msgb_alloc();
2391 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002392 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002393
Harald Welte03740842009-06-10 23:11:52 +08002394 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2395
2396 trans->callref = 0;
2397
2398 gsm48_stop_cc_timer(trans);
2399
2400 /* cause */
2401 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002402 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002403 /* facility */
2404 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002405 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002406 /* user-user */
2407 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002408 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002409
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002410 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2411
Harald Weltec2189a62009-07-23 18:56:43 +02002412 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002413
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002414 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2418{
2419 struct gsm48_hdr *gh = msgb_l3(msg);
2420 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2421 struct tlv_parsed tp;
2422 struct gsm_mncc fac;
2423
2424 memset(&fac, 0, sizeof(struct gsm_mncc));
2425 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002426 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002427 /* facility */
2428 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2429 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002430 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002431 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2432 }
2433 /* ss-version */
2434 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2435 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002436 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002437 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2438 }
2439
Harald Welte0abe5a62009-07-23 19:06:52 +02002440 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002441}
2442
2443static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2444{
2445 struct gsm_mncc *fac = arg;
2446 struct msgb *msg = gsm48_msgb_alloc();
2447 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2448
Harald Welte03740842009-06-10 23:11:52 +08002449 gh->msg_type = GSM48_MT_CC_FACILITY;
2450
2451 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002452 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002453
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002454 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002455}
2456
2457static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2458{
2459 struct gsm_mncc hold;
2460
2461 memset(&hold, 0, sizeof(struct gsm_mncc));
2462 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002463 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2467{
2468 struct msgb *msg = gsm48_msgb_alloc();
2469 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2470
Harald Welte03740842009-06-10 23:11:52 +08002471 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2472
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002473 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2477{
2478 struct gsm_mncc *hold_rej = arg;
2479 struct msgb *msg = gsm48_msgb_alloc();
2480 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2481
Harald Welte03740842009-06-10 23:11:52 +08002482 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2483
2484 /* cause */
2485 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002486 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002487 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002488 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002489
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002490 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002491}
2492
2493static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2494{
2495 struct gsm_mncc retrieve;
2496
2497 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2498 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002499 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2500 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002501}
2502
2503static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2504{
2505 struct msgb *msg = gsm48_msgb_alloc();
2506 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2507
Harald Welte03740842009-06-10 23:11:52 +08002508 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2509
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002510 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2514{
2515 struct gsm_mncc *retrieve_rej = arg;
2516 struct msgb *msg = gsm48_msgb_alloc();
2517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2518
Harald Welte03740842009-06-10 23:11:52 +08002519 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2520
2521 /* cause */
2522 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002523 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002524 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002525 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002526
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002527 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002528}
2529
2530static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2531{
2532 struct gsm48_hdr *gh = msgb_l3(msg);
2533 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2534 struct tlv_parsed tp;
2535 struct gsm_mncc dtmf;
2536
2537 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2538 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002539 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002540 /* keypad facility */
2541 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2542 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002543 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002544 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2545 }
2546
Harald Welte0abe5a62009-07-23 19:06:52 +02002547 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002548}
2549
2550static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2551{
2552 struct gsm_mncc *dtmf = arg;
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_START_DTMF_ACK;
2557
2558 /* keypad */
2559 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002560 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002561
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002562 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002563}
2564
2565static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2566{
2567 struct gsm_mncc *dtmf = arg;
2568 struct msgb *msg = gsm48_msgb_alloc();
2569 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2570
Harald Welte03740842009-06-10 23:11:52 +08002571 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2572
2573 /* cause */
2574 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002575 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002576 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002577 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002578
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002579 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002580}
2581
2582static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2583{
2584 struct msgb *msg = gsm48_msgb_alloc();
2585 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2586
Harald Welte03740842009-06-10 23:11:52 +08002587 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2588
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002589 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002590}
2591
2592static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2593{
2594 struct gsm_mncc dtmf;
2595
2596 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2597 dtmf.callref = trans->callref;
2598
Harald Welte0abe5a62009-07-23 19:06:52 +02002599 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002600}
2601
2602static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2603{
2604 struct gsm48_hdr *gh = msgb_l3(msg);
2605 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2606 struct tlv_parsed tp;
2607 struct gsm_mncc modify;
2608
2609 memset(&modify, 0, sizeof(struct gsm_mncc));
2610 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002611 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002612 /* bearer capability */
2613 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2614 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002615 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002616 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2617 }
2618
2619 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2620
Harald Welte0abe5a62009-07-23 19:06:52 +02002621 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002622}
2623
2624static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2625{
2626 struct gsm_mncc *modify = arg;
2627 struct msgb *msg = gsm48_msgb_alloc();
2628 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2629
Harald Welte03740842009-06-10 23:11:52 +08002630 gh->msg_type = GSM48_MT_CC_MODIFY;
2631
2632 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2633
2634 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002635 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002636
2637 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2638
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002639 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002640}
2641
2642static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2643{
2644 struct gsm48_hdr *gh = msgb_l3(msg);
2645 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2646 struct tlv_parsed tp;
2647 struct gsm_mncc modify;
2648
2649 gsm48_stop_cc_timer(trans);
2650
2651 memset(&modify, 0, sizeof(struct gsm_mncc));
2652 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002653 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002654 /* bearer capability */
2655 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2656 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002657 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002658 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2659 }
2660
2661 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2662
Harald Welte0abe5a62009-07-23 19:06:52 +02002663 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2667{
2668 struct gsm_mncc *modify = arg;
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_MODIFY_COMPL;
2673
2674 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002675 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002676
2677 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2678
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002679 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002680}
2681
2682static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2683{
2684 struct gsm48_hdr *gh = msgb_l3(msg);
2685 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2686 struct tlv_parsed tp;
2687 struct gsm_mncc modify;
2688
2689 gsm48_stop_cc_timer(trans);
2690
2691 memset(&modify, 0, sizeof(struct gsm_mncc));
2692 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002693 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002694 /* bearer capability */
2695 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2696 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002697 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002698 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2699 }
2700 /* cause */
2701 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2702 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002703 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002704 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2705 }
2706
2707 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2708
Harald Welte0abe5a62009-07-23 19:06:52 +02002709 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002710}
2711
2712static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2713{
2714 struct gsm_mncc *modify = arg;
2715 struct msgb *msg = gsm48_msgb_alloc();
2716 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2717
Harald Welte03740842009-06-10 23:11:52 +08002718 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2719
2720 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002721 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002722 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002723 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002724
2725 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2726
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002727 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002728}
2729
2730static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2731{
2732 struct gsm_mncc *notify = arg;
2733 struct msgb *msg = gsm48_msgb_alloc();
2734 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2735
Harald Welte03740842009-06-10 23:11:52 +08002736 gh->msg_type = GSM48_MT_CC_NOTIFY;
2737
2738 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002739 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002740
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002741 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002742}
2743
2744static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2745{
2746 struct gsm48_hdr *gh = msgb_l3(msg);
2747 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2748// struct tlv_parsed tp;
2749 struct gsm_mncc notify;
2750
2751 memset(&notify, 0, sizeof(struct gsm_mncc));
2752 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002753// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002754 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002755 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002756
Harald Welte0abe5a62009-07-23 19:06:52 +02002757 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002758}
2759
2760static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2761{
2762 struct gsm_mncc *user = arg;
2763 struct msgb *msg = gsm48_msgb_alloc();
2764 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2765
Harald Welte03740842009-06-10 23:11:52 +08002766 gh->msg_type = GSM48_MT_CC_USER_INFO;
2767
2768 /* user-user */
2769 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002770 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002771 /* more data */
2772 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002773 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002774
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002775 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002776}
2777
2778static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2779{
2780 struct gsm48_hdr *gh = msgb_l3(msg);
2781 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2782 struct tlv_parsed tp;
2783 struct gsm_mncc user;
2784
2785 memset(&user, 0, sizeof(struct gsm_mncc));
2786 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002787 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002788 /* user-user */
2789 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2790 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002791 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002792 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2793 }
2794 /* more data */
2795 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2796 user.more = 1;
2797
Harald Welte0abe5a62009-07-23 19:06:52 +02002798 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002799}
2800
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002801static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002802{
2803 struct gsm_mncc *mode = arg;
2804
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002805 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002806}
2807
2808static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002809 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002810 int type;
2811 int (*rout) (struct gsm_trans *trans, void *arg);
2812} downstatelist[] = {
2813 /* mobile originating call establishment */
2814 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2815 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2816 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2817 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2818 {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 */
2819 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2820 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2821 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2822 /* mobile terminating call establishment */
2823 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2824 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2825 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2826 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2827 /* signalling during call */
2828 {SBIT(GSM_CSTATE_ACTIVE),
2829 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2830 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2831 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2832 {ALL_STATES,
2833 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2834 {ALL_STATES,
2835 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2836 {ALL_STATES,
2837 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2838 {SBIT(GSM_CSTATE_ACTIVE),
2839 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2840 {SBIT(GSM_CSTATE_ACTIVE),
2841 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2842 {SBIT(GSM_CSTATE_ACTIVE),
2843 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2844 {SBIT(GSM_CSTATE_ACTIVE),
2845 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2846 {SBIT(GSM_CSTATE_ACTIVE),
2847 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2848 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2849 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2850 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2851 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2852 {SBIT(GSM_CSTATE_ACTIVE),
2853 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2854 /* clearing */
2855 {SBIT(GSM_CSTATE_INITIATED),
2856 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2857 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2858 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2859 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2860 MNCC_REL_REQ, gsm48_cc_tx_release},
2861 /* special */
2862 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002863 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002864};
2865
2866#define DOWNSLLEN \
2867 (sizeof(downstatelist) / sizeof(struct downstate))
2868
2869
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002870int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002871{
Harald Welteaa60edb2009-08-09 18:52:33 +02002872 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002873 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002874 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002875 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002876 struct gsm_mncc *data = arg, rel;
2877
Harald Welte17b56522010-12-22 21:45:05 +01002878 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2879
Harald Welte03740842009-06-10 23:11:52 +08002880 /* handle special messages */
2881 switch(msg_type) {
2882 case MNCC_BRIDGE:
2883 return tch_bridge(net, arg);
2884 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002885 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002886 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002887 return tch_recv_mncc(net, data->callref, 1);
2888 case GSM_TCHF_FRAME:
2889 /* Find callref */
2890 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002891 if (!trans) {
2892 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002893 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002894 }
2895 if (!trans->conn) {
2896 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002897 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002898 }
2899 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2900 /* This should be LOGL_ERROR or NOTICE, but
2901 * unfortuantely it happens for a couple of frames at
2902 * the beginning of every RTP connection */
2903 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002904 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002905 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002906 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002907 switch (bts->type) {
2908 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02002909 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01002910 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002911 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002912 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002913 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002914 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002915 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03002916 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02002917 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002918 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002919 default:
Harald Welte8099a712011-08-09 21:53:20 +02002920 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01002921 }
2922 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002923 }
2924
2925 memset(&rel, 0, sizeof(struct gsm_mncc));
2926 rel.callref = data->callref;
2927
2928 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002929 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002930
2931 /* Callref unknown */
2932 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002933 struct gsm_subscriber *subscr;
2934
Harald Welte6e1536e2009-07-04 10:11:24 +02002935 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002936 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2937 "Received '%s' from MNCC with "
2938 "unknown callref %d\n", data->called.number,
2939 get_mncc_name(msg_type), data->callref);
2940 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002941 return mncc_release_ind(net, NULL, data->callref,
2942 GSM48_CAUSE_LOC_PRN_S_LU,
2943 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002944 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002945 if (!data->called.number[0] && !data->imsi[0]) {
2946 DEBUGP(DCC, "(bts - trx - ts - ti) "
2947 "Received '%s' from MNCC with "
2948 "no number or IMSI\n", get_mncc_name(msg_type));
2949 /* Invalid number */
2950 return mncc_release_ind(net, NULL, data->callref,
2951 GSM48_CAUSE_LOC_PRN_S_LU,
2952 GSM48_CC_CAUSE_INV_NR_FORMAT);
2953 }
Harald Welte03740842009-06-10 23:11:52 +08002954 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002955 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002956 subscr = subscr_get_by_extension(net,
2957 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002958 else
Harald Welte75350412009-07-23 18:46:00 +02002959 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002960 /* If subscriber is not found */
2961 if (!subscr) {
2962 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2963 "Received '%s' from MNCC with "
2964 "unknown subscriber %s\n", data->called.number,
2965 get_mncc_name(msg_type), data->called.number);
2966 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002967 return mncc_release_ind(net, NULL, data->callref,
2968 GSM48_CAUSE_LOC_PRN_S_LU,
2969 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002970 }
2971 /* If subscriber is not "attached" */
2972 if (!subscr->lac) {
2973 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2974 "Received '%s' from MNCC with "
2975 "detached subscriber %s\n", data->called.number,
2976 get_mncc_name(msg_type), data->called.number);
2977 subscr_put(subscr);
2978 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002979 return mncc_release_ind(net, NULL, data->callref,
2980 GSM48_CAUSE_LOC_PRN_S_LU,
2981 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002982 }
2983 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002984 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2985 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002986 DEBUGP(DCC, "No memory for trans.\n");
2987 subscr_put(subscr);
2988 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002989 mncc_release_ind(net, NULL, data->callref,
2990 GSM48_CAUSE_LOC_PRN_S_LU,
2991 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002992 return -ENOMEM;
2993 }
Harald Welte03740842009-06-10 23:11:52 +08002994 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002995 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002996
Harald Welte03740842009-06-10 23:11:52 +08002997 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002998 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002999 /* find transaction with this subscriber already paging */
3000 llist_for_each_entry(transt, &net->trans_list, entry) {
3001 /* Transaction of our lchan? */
3002 if (transt == trans ||
3003 transt->subscr != subscr)
3004 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003005 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003006 "Received '%s' from MNCC with "
3007 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003008 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003009 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003010 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003011 subscr_put(subscr);
3012 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003013 return 0;
3014 }
3015 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003016 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003017
3018 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003019 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3020 if (!trans->paging_request) {
3021 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3022 subscr_put(subscr);
3023 trans_free(trans);
3024 return 0;
3025 }
3026
3027 *trans->paging_request = subscr->net;
3028 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003029
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003030 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003031 return 0;
3032 }
3033 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003034 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003035 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003036 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003037
3038 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003039 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003040
3041 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003042 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003043 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003044 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003045 (trans->subscr)?(trans->subscr->extension):"-",
3046 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003047 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3048 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003049 if (msg_type == MNCC_REL_REQ)
3050 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3051 else
3052 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3053 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003054 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003055 return rc;
3056 }
3057
3058 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3059 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003060 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003061 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003062 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003063 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003064 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003065
3066 /* Find function for current state and message */
3067 for (i = 0; i < DOWNSLLEN; i++)
3068 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003069 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003070 break;
3071 if (i == DOWNSLLEN) {
3072 DEBUGP(DCC, "Message unhandled at this state.\n");
3073 return 0;
3074 }
3075
3076 rc = downstatelist[i].rout(trans, arg);
3077
3078 return rc;
3079}
3080
3081
3082static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003083 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003084 int type;
3085 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3086} datastatelist[] = {
3087 /* mobile originating call establishment */
3088 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3089 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3090 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3091 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3092 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3093 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3094 /* mobile terminating call establishment */
3095 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3096 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3097 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3098 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003099 {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 +08003100 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3101 /* signalling during call */
3102 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3103 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3104 {SBIT(GSM_CSTATE_ACTIVE),
3105 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3106 {ALL_STATES,
3107 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3108 {ALL_STATES,
3109 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3110 {ALL_STATES,
3111 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3112 {SBIT(GSM_CSTATE_ACTIVE),
3113 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3114 {SBIT(GSM_CSTATE_ACTIVE),
3115 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3116 {SBIT(GSM_CSTATE_ACTIVE),
3117 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3118 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3119 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3120 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3121 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3122 {SBIT(GSM_CSTATE_ACTIVE),
3123 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3124 /* clearing */
3125 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3126 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3127 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3128 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3129 {ALL_STATES, /* 5.4.3.4 */
3130 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3131};
3132
3133#define DATASLLEN \
3134 (sizeof(datastatelist) / sizeof(struct datastate))
3135
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003136static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003137{
3138 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003139 uint8_t msg_type = gh->msg_type & 0xbf;
3140 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003141 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003142 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003143
Harald Welte03740842009-06-10 23:11:52 +08003144 if (msg_type & 0x80) {
3145 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3146 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003147 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003148
Harald Welte03740842009-06-10 23:11:52 +08003149 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003150 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003151
Harald Welte4861c822009-07-23 21:21:14 +02003152 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003153 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003154 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003155 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003156 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3157 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003158
3159 /* Create transaction */
3160 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003161 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003162 "creating new trans.\n", transaction_id);
3163 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003164 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003165 transaction_id, new_callref++);
3166 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003167 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003168 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003169 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003170 GSM48_MT_CC_RELEASE_COMPL);
3171 return -ENOMEM;
3172 }
Harald Welte03740842009-06-10 23:11:52 +08003173 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003174 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003175 }
3176
3177 /* find function for current state and message */
3178 for (i = 0; i < DATASLLEN; i++)
3179 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003180 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003181 break;
3182 if (i == DATASLLEN) {
3183 DEBUGP(DCC, "Message unhandled at this state.\n");
3184 return 0;
3185 }
3186
3187 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003188
3189 return rc;
3190}
3191
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003192/* Create a dummy to wait five seconds */
3193static void release_anchor(struct gsm_subscriber_connection *conn)
3194{
3195 if (!conn->anch_operation)
3196 return;
3197
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003198 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003199 talloc_free(conn->anch_operation);
3200 conn->anch_operation = NULL;
3201}
3202
3203static void anchor_timeout(void *_data)
3204{
3205 struct gsm_subscriber_connection *con = _data;
3206
3207 release_anchor(con);
3208 msc_release_connection(con);
3209}
3210
3211int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3212{
3213 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3214 if (!conn->anch_operation)
3215 return -1;
3216
3217 conn->anch_operation->timeout.data = conn;
3218 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003219 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003220 return 0;
3221}
3222
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003223/* here we get data from the BSC level... */
3224int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003225{
3226 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003227 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003228 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003229
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003230 if (silent_call_reroute(conn, msg))
3231 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003232
3233 switch (pdisc) {
3234 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003235 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003236 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003237 break;
3238 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003239 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003240 break;
3241 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003242 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003243 break;
3244 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003245 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003246 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003247 break;
3248 case GSM48_PDISC_MM_GPRS:
3249 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003250 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3251 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003252 break;
Harald Welte03115042009-10-16 08:32:58 +02003253 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003254 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003255 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003256 break;
Harald Welte59b04682009-06-10 05:40:52 +08003257 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003258 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3259 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003260 break;
3261 }
3262
3263 return rc;
3264}
3265
Harald Welte3c062072009-07-28 18:25:29 +02003266/*
3267 * This will be ran by the linker when loading the DSO. We use it to
3268 * do system initialization, e.g. registration of signal handlers.
3269 */
3270static __attribute__((constructor)) void on_dso_load_0408(void)
3271{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003272 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003273}