blob: fd482e0bd9c622ea6896a34ccbab94d31a44a889 [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +08005 * (C) 2008-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte59b04682009-06-10 05:40:52 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080018 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte59b04682009-06-10 05:40:52 +080021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
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>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020054#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010055#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080056
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010057#include <osmocom/gsm/gsm48.h>
58#include <osmocom/gsm/gsm0480.h>
59#include <osmocom/gsm/gsm_utils.h>
60#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
62#include <osmocom/gsm/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080063
Harald Welte (local)8751ee92009-08-15 02:30:58 +020064void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010065void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020066
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020067int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080068static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020069 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010070static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080071static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080072
73struct gsm_lai {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020074 uint16_t mcc;
75 uint16_t mnc;
76 uint16_t lac;
Harald Welte59b04682009-06-10 05:40:52 +080077};
78
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020079static uint32_t new_callref = 0x80000001;
Harald Welte03740842009-06-10 23:11:52 +080080
Harald Welte4f272cc2011-03-03 23:29:05 +010081void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
82{
83 net->mncc_recv(net, msg);
84}
85
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080086static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
87 struct gsm_trans *trans)
88{
89 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
90
91 /* if we get passed a transaction reference, do some common
92 * work that the caller no longer has to do */
93 if (trans) {
94 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
95 msg->lchan = trans->conn->lchan;
96 }
97
98
99 if (msg->lchan) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200100 struct e1inp_sign_link *sign_link =
101 msg->lchan->ts->trx->rsl_link;
102
103 msg->dst = sign_link;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800104 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
105 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200106 "Sending '%s' to MS.\n",
107 sign_link->trx->bts->nr,
108 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800109 gh->proto_discr & 0xf0,
110 gsm48_cc_msg_name(gh->msg_type));
111 else
112 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200113 "Sending 0x%02x to MS.\n",
114 sign_link->trx->bts->nr,
115 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800116 gh->proto_discr, gh->msg_type);
117 }
118
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100119 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800120}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100121
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800122int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
123{
124 struct gsm48_hdr *gh;
125 struct msgb *ss_notify;
126
127 ss_notify = gsm0480_create_notifySS(message);
128 if (!ss_notify)
129 return -1;
130
131 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
132 uint8_t *data = msgb_push(ss_notify, 1);
133 data[0] = ss_notify->len - 1;
134 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
135 gh->msg_type = GSM48_MT_CC_FACILITY;
136 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
137}
138
Harald Weltefe01ed72012-01-23 16:40:24 +0100139void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100140{
141 if (!conn->sec_operation)
142 return;
143
144 talloc_free(conn->sec_operation);
145 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800146 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100147}
148
Harald Weltefe01ed72012-01-23 16:40:24 +0100149void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100150{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100151 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
152 struct gsm_security_operation);
153}
154
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100155int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
156 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100157{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800158 struct gsm_network *net = conn->bts->network;
159 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100160 struct gsm_security_operation *op;
161 struct gsm_auth_tuple atuple;
162 int status = -1, rc;
163
164 /* Check if we _can_ enable encryption. Cases where we can't:
165 * - Encryption disabled in config
166 * - Channel already secured (nothing to do)
167 * - Subscriber equipment doesn't support configured encryption
168 */
169 if (!net->a5_encryption) {
170 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800171 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100172 DEBUGP(DMM, "Requesting to secure an already secure channel");
173 status = GSM_SECURITY_SUCCEEDED;
174 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
175 net->a5_encryption)) {
176 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
177 status = GSM_SECURITY_NOAVAIL;
178 }
179
180 /* If not done yet, try to get info for this user */
181 if (status < 0) {
182 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
183 if (rc <= 0)
184 status = GSM_SECURITY_NOAVAIL;
185 }
186
187 /* Are we done yet ? */
188 if (status >= 0)
189 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800190 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100191 0;
192
193 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800194 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100195 return -EBUSY;
196
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800197 allocate_security_operation(conn);
198 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100199 op->cb = cb;
200 op->cb_data = cb_data;
201 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
202
203 /* FIXME: Should start a timer for completion ... */
204
205 /* Then do whatever is needed ... */
Harald Welte857e15d2010-12-23 18:07:49 +0100206 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100207 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800208 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100209 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100210 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100211 return gsm0808_cipher_mode(conn, net->a5_encryption,
212 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100213 }
214
215 return -EINVAL; /* not reached */
216}
217
Harald Welte59b04682009-06-10 05:40:52 +0800218static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
219 struct gsm_subscriber *subscriber)
220{
221 if (!subscriber)
222 return 0;
223
224 /*
225 * Do not send accept yet as more information should arrive. Some
226 * phones will not send us the information and we will have to check
227 * what we want to do with that.
228 */
229 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
230 return 0;
231
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200232 switch (subscriber->net->auth_policy) {
233 case GSM_AUTH_POLICY_CLOSED:
234 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200235 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200236 if (subscriber->authorized)
237 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200238 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200239 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800240 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200241 default:
242 return 0;
243 }
Harald Welte59b04682009-06-10 05:40:52 +0800244}
245
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100246static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800247{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100248 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800249 return;
250
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800251 /* No need to keep the connection up */
252 release_anchor(conn);
253
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200254 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100255 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100256 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800257 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800258}
259
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100260static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800261{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800262 if (conn->loc_operation)
263 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100264 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800265
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100266 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200267 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800268}
269
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100270static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
271 struct msgb *msg, void *data, void *param)
272{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800273 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100274 int rc = 0;
275
276 switch (event) {
277 case GSM_SECURITY_AUTH_FAILED:
278 release_loc_updating_req(conn);
279 break;
280
281 case GSM_SECURITY_NOAVAIL:
282 case GSM_SECURITY_SUCCEEDED:
283 /* We're all good */
284 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800285 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800286 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100287 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800288 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100289 }
290
291 /* call subscr_update after putting the loc_upd_acc
292 * in the transmit queue, since S_SUBSCR_ATTACHED might
293 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800294 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100295 GSM_SUBSCRIBER_UPDATE_ATTACHED);
296
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100297 /*
298 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
299 * MS needs to respond with a TMSI REALLOCATION COMPLETE
300 * (even if the TMSI is the same).
301 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100302 break;
303
304 default:
305 rc = -EINVAL;
306 };
307
308 return rc;
309}
310
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100311static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800312{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100313 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800314 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100315 conn->loc_operation->key_seq,
316 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800317 return 0;
318}
319
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800320void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800321{
Harald Welte03740842009-06-10 23:11:52 +0800322 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800323
324 /* avoid someone issuing a clear */
325 conn->in_release = 1;
326
Harald Welte59b04682009-06-10 05:40:52 +0800327 /*
328 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800329 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800330 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800331 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100332
333 /* We might need to cancel the paging response or such. */
334 if (conn->sec_operation && conn->sec_operation->cb) {
335 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
336 NULL, conn, conn->sec_operation->cb_data);
337 }
338
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800339 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800340 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800341
Harald Welte03740842009-06-10 23:11:52 +0800342 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200343 /* FIXME: this is not neccessarily the right thing to do, we should
344 * only set trans->lchan to NULL and wait for another lchan to be
345 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800346 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
347 if (trans->conn == conn)
Harald Weltec2189a62009-07-23 18:56:43 +0200348 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800349 }
Harald Welte59b04682009-06-10 05:40:52 +0800350}
351
Harald Weltee8d16172010-12-22 23:17:50 +0100352void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
353{
354 struct gsm_trans *trans, *temp;
355
356 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
357
358 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100359 if (trans->protocol == protocol) {
360 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100361 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100362 }
Harald Weltee8d16172010-12-22 23:17:50 +0100363 }
364}
365
Harald Welte59b04682009-06-10 05:40:52 +0800366/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200367int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800368{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800369 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800370 struct msgb *msg;
371
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200372 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800373
374 msg = gsm48_create_loc_upd_rej(cause);
375 if (!msg) {
376 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
377 return -1;
378 }
Harald Welte59b04682009-06-10 05:40:52 +0800379
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800380 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800381
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100382 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100383 "LAC=%u BTS=%u\n", conn->subscr ?
384 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800385 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100386
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800387 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800388}
389
390/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200391int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800392{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800393 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800394 struct msgb *msg = gsm48_msgb_alloc();
395 struct gsm48_hdr *gh;
396 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200397 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800398
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800399 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800400
401 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
402 gh->proto_discr = GSM48_PDISC_MM;
403 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
404
405 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100406 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800407 bts->network->network_code, bts->location_area_code);
408
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200409 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200410 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800411
412 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
413
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200414 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100415
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800416 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800417}
418
Harald Welte59b04682009-06-10 05:40:52 +0800419/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200420static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800421{
422 struct msgb *msg = gsm48_msgb_alloc();
423 struct gsm48_hdr *gh;
424
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800425 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800426
427 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
428 gh->proto_discr = GSM48_PDISC_MM;
429 gh->msg_type = GSM48_MT_MM_ID_REQ;
430 gh->data[0] = id_type;
431
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800432 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800433}
434
Harald Welte59b04682009-06-10 05:40:52 +0800435
436/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800437static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800438{
439 struct gsm48_hdr *gh = msgb_l3(msg);
440 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200441 struct gsm_bts *bts = lchan->ts->trx->bts;
442 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200443 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200444 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800445
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200446 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800447 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
448 mi_type, mi_string);
449
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200450 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100451
Harald Welte59b04682009-06-10 05:40:52 +0800452 switch (mi_type) {
453 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200454 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100455 if (!conn->subscr) {
456 conn->subscr = subscr_get_by_imsi(net, mi_string);
457 if (!conn->subscr)
458 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200459 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100460 if (conn->loc_operation)
461 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800462 break;
463 case GSM_MI_TYPE_IMEI:
464 case GSM_MI_TYPE_IMEISV:
465 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100466 if (conn->subscr) {
467 db_subscriber_assoc_imei(conn->subscr, mi_string);
468 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200469 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100470 if (conn->loc_operation)
471 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800472 break;
473 }
474
475 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100476 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800477}
478
479
480static void loc_upd_rej_cb(void *data)
481{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100482 struct gsm_subscriber_connection *conn = data;
483 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100484 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800485
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100486 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800487 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800488 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800489}
490
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100491static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800492{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100493 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
494 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200495 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800496}
497
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200498static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800499{
500 switch (type) {
501 case GSM48_LUPD_NORMAL:
502 return "NORMAL";
503 case GSM48_LUPD_PERIODIC:
504 return "PEROIDOC";
505 case GSM48_LUPD_IMSI_ATT:
506 return "IMSI ATTACH";
507 default:
508 return "UNKNOWN";
509 }
510}
511
Harald Welte59b04682009-06-10 05:40:52 +0800512/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800513static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800514{
515 struct gsm48_hdr *gh = msgb_l3(msg);
516 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800517 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800518 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200519 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200520 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800521
522 lu = (struct gsm48_loc_upd_req *) gh->data;
523
524 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
525
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200526 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800527
Harald Welte79639662009-06-27 02:58:43 +0200528 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800529 lupd_name(lu->type));
530
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200531 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100532
Harald Welte3edc5a92009-12-22 00:41:05 +0100533 switch (lu->type) {
534 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200535 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100536 break;
537 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200538 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100539 break;
540 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200541 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100542 break;
543 }
544
Harald Welte59b04682009-06-10 05:40:52 +0800545 /*
546 * Pseudo Spoof detection: Just drop a second/concurrent
547 * location updating request.
548 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100549 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200550 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100551 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800552 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800553 return 0;
554 }
555
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800556 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800557
Sylvain Munaut360fc582010-06-10 13:36:59 +0200558 conn->loc_operation->key_seq = lu->key_seq;
559
Harald Welte59b04682009-06-10 05:40:52 +0800560 switch (mi_type) {
561 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200562 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800563 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200564 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100565 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800566
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200567 /* look up subscriber based on IMSI, create if not found */
568 subscr = subscr_get_by_imsi(bts->network, mi_string);
569 if (!subscr) {
570 subscr = db_create_subscriber(bts->network, mi_string);
571 }
Harald Welte59b04682009-06-10 05:40:52 +0800572 break;
573 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200574 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800575 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200576 subscr = subscr_get_by_tmsi(bts->network,
577 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800578 if (!subscr) {
579 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200580 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100581 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800582 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200583 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200584 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200585 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800586 break;
587 case GSM_MI_TYPE_IMEI:
588 case GSM_MI_TYPE_IMEISV:
589 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200590 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800591 break;
592 default:
Harald Welte79639662009-06-27 02:58:43 +0200593 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800594 break;
595 }
596
Harald Welteccd69362009-07-04 10:18:00 +0200597 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100598 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200599
Harald Welte03740842009-06-10 23:11:52 +0800600 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200601 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800602 /* FIXME: request id? close channel? */
603 return -EINVAL;
604 }
605
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100606 conn->subscr = subscr;
607 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800608
Harald Welteccd69362009-07-04 10:18:00 +0200609 /* check if we can let the subscriber into our network immediately
610 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100611 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800612}
613
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100614/* Turn int into semi-octet representation: 98 => 0x89 */
615static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800616{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100617 uint8_t ret;
618
619 ret = value / 10;
620 ret |= (value % 10) << 4;
621
622 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800623}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100624
Harald Welte03740842009-06-10 23:11:52 +0800625
Harald Welte59b04682009-06-10 05:40:52 +0800626/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800627int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800628{
629 struct msgb *msg = gsm48_msgb_alloc();
630 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800631 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100632 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200633 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200634 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100635
Harald Welte03740842009-06-10 23:11:52 +0800636 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100637 struct tm* gmt_time;
638 struct tm* local_time;
639 int tzunits;
Harald Welte59b04682009-06-10 05:40:52 +0800640
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800641 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800642
643 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
644 gh->proto_discr = GSM48_PDISC_MM;
645 gh->msg_type = GSM48_MT_MM_INFO;
646
647 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200648#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800649 name_len = strlen(net->name_long);
650 /* 10.5.3.5a */
651 ptr8 = msgb_put(msg, 3);
652 ptr8[0] = GSM48_IE_NAME_LONG;
653 ptr8[1] = name_len*2 +1;
654 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
655
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200656 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800657 for (i = 0; i < name_len; i++)
658 ptr16[i] = htons(net->name_long[i]);
659
660 /* FIXME: Use Cell Broadcast, not UCS-2, since
661 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200662#endif
663 name_len = (strlen(net->name_long)*7)/8;
664 name_pad = (8 - strlen(net->name_long)*7)%8;
665 if (name_pad > 0)
666 name_len++;
667 /* 10.5.3.5a */
668 ptr8 = msgb_put(msg, 3);
669 ptr8[0] = GSM48_IE_NAME_LONG;
670 ptr8[1] = name_len +1;
671 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
672
673 ptr8 = msgb_put(msg, name_len);
674 gsm_7bit_encode(ptr8, net->name_long);
675
Harald Welte59b04682009-06-10 05:40:52 +0800676 }
677
678 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200679#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800680 name_len = strlen(net->name_short);
681 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200682 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200683 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800684 ptr8[1] = name_len*2 + 1;
685 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
686
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200687 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800688 for (i = 0; i < name_len; i++)
689 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200690#endif
691 name_len = (strlen(net->name_short)*7)/8;
692 name_pad = (8 - strlen(net->name_short)*7)%8;
693 if (name_pad > 0)
694 name_len++;
695 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200696 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200697 ptr8[0] = GSM48_IE_NAME_SHORT;
698 ptr8[1] = name_len +1;
699 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
700
701 ptr8 = msgb_put(msg, name_len);
702 gsm_7bit_encode(ptr8, net->name_short);
703
Harald Welte59b04682009-06-10 05:40:52 +0800704 }
705
Harald Welte59b04682009-06-10 05:40:52 +0800706 /* Section 10.5.3.9 */
707 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100708 gmt_time = gmtime(&cur_t);
709
Harald Welte59b04682009-06-10 05:40:52 +0800710 ptr8 = msgb_put(msg, 8);
711 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100712 ptr8[1] = bcdify(gmt_time->tm_year % 100);
713 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
714 ptr8[3] = bcdify(gmt_time->tm_mday);
715 ptr8[4] = bcdify(gmt_time->tm_hour);
716 ptr8[5] = bcdify(gmt_time->tm_min);
717 ptr8[6] = bcdify(gmt_time->tm_sec);
718
Harald Welte5c3abf92012-07-08 16:48:11 +0200719 if (bts->tz.override) {
720 /* Convert tz.hr and tz.mn to units */
721 if (bts->tz.hr < 0) {
722 tzunits = ((bts->tz.hr/-1)*4);
723 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100724 ptr8[7] = bcdify(tzunits);
725 /* Set negative time */
726 ptr8[7] |= 0x08;
727 }
728 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200729 tzunits = bts->tz.hr*4;
730 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100731 ptr8[7] = bcdify(tzunits);
732 }
733 }
734 else {
735 /* Need to get GSM offset and convert into 15 min units */
736 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
737 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200738#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100739 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200740#else
741#warning find a portable way to obtain the timezone offset
742 tzunits = 0;
743#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100744 if (tzunits < 0) {
745 tzunits = tzunits/-1;
746 ptr8[7] = bcdify(tzunits);
747 /* Flip it to negative */
748 ptr8[7] |= 0x08;
749 }
750 else
751 ptr8[7] = bcdify(tzunits);
752 }
Harald Welte59b04682009-06-10 05:40:52 +0800753
Daniel Willmannc8188202009-08-13 03:42:07 +0200754 DEBUGP(DMM, "-> MM INFO\n");
755
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800756 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800757}
758
Harald Welte36970f62009-08-12 22:56:50 +0200759/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200760int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200761{
762 struct msgb *msg = gsm48_msgb_alloc();
763 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200764 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200765
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200766 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200767
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800768 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200769 gh->proto_discr = GSM48_PDISC_MM;
770 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
771
Sylvain Munaut0789c592009-12-24 00:23:46 +0100772 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200773
Harald Welte36970f62009-08-12 22:56:50 +0200774 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200775 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200776 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200777
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800778 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200779}
780
781/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800782int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200783{
784 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800785 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200786}
787
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800788static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800789{
790 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800791 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800792}
793
794/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100795static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800796 enum gsm48_reject_value value)
797{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800798 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800799
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800800 msg = gsm48_create_mm_serv_rej(value);
801 if (!msg) {
802 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
803 return -1;
804 }
Harald Welte59b04682009-06-10 05:40:52 +0800805
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800806 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100807 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800808 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800809}
810
Sylvain Munautbadc7112009-12-24 00:28:46 +0100811static int _gsm48_rx_mm_serv_req_sec_cb(
812 unsigned int hooknum, unsigned int event,
813 struct msgb *msg, void *data, void *param)
814{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800815 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100816 int rc = 0;
817
818 switch (event) {
819 case GSM_SECURITY_AUTH_FAILED:
820 /* Nothing to do */
821 break;
822
823 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800824 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100825 break;
826
827 case GSM_SECURITY_SUCCEEDED:
828 /* nothing to do. CIPHER MODE COMMAND is
829 * implicit CM SERV ACK */
830 break;
831
832 default:
833 rc = -EINVAL;
834 };
835
836 return rc;
837}
838
Harald Welte59b04682009-06-10 05:40:52 +0800839/*
840 * Handle CM Service Requests
841 * a) Verify that the packet is long enough to contain the information
842 * we require otherwsie reject with INCORRECT_MESSAGE
843 * b) Try to parse the TMSI. If we do not have one reject
844 * c) Check that we know the subscriber with the TMSI otherwise reject
845 * with a HLR cause
846 * d) Set the subscriber on the gsm_lchan and accept
847 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800848static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800849{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200850 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200851 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800852
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800853 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800854 struct gsm_subscriber *subscr;
855 struct gsm48_hdr *gh = msgb_l3(msg);
856 struct gsm48_service_request *req =
857 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800858 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200859 uint8_t classmark2_len = gh->data[1];
860 uint8_t *classmark2 = gh->data+2;
861 uint8_t mi_len = *(classmark2 + classmark2_len);
862 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800863
864 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
865 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
866 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800867 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800868 GSM48_REJECT_INCORRECT_MESSAGE);
869 }
870
871 if (msg->data_len < req->mi_len + 6) {
872 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800873 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800874 GSM48_REJECT_INCORRECT_MESSAGE);
875 }
876
877 mi_type = mi[0] & GSM_MI_TYPE_MASK;
878 if (mi_type != GSM_MI_TYPE_TMSI) {
879 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800880 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800881 GSM48_REJECT_INCORRECT_MESSAGE);
882 }
883
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200884 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800885 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
886 req->cm_service_type, mi_type, mi_string);
887
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200888 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100889
Harald Welte583ceaf2009-08-10 10:12:45 +0200890 if (is_siemens_bts(bts))
891 send_siemens_mrpci(msg->lchan, classmark2-1);
892
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200893 subscr = subscr_get_by_tmsi(bts->network,
894 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800895
896 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
897 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800898 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800899 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
900
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800901 if (!conn->subscr)
902 conn->subscr = subscr;
903 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100904 subscr_put(subscr); /* lchan already has a ref, don't need another one */
905 else {
Harald Welte59b04682009-06-10 05:40:52 +0800906 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
907 subscr_put(subscr);
908 }
909
Harald Weltef6845a72009-07-05 14:08:13 +0200910 subscr->equipment.classmark2_len = classmark2_len;
911 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
912 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800913
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800914 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100915 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800916}
917
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200918static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800919{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200920 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800921 struct gsm48_hdr *gh = msgb_l3(msg);
922 struct gsm48_imsi_detach_ind *idi =
923 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200924 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200925 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800926 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800927
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200928 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800929 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
930 mi_type, mi_string);
931
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200932 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100933
Harald Welte59b04682009-06-10 05:40:52 +0800934 switch (mi_type) {
935 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200936 subscr = subscr_get_by_tmsi(bts->network,
937 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800938 break;
939 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200940 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800941 break;
942 case GSM_MI_TYPE_IMEI:
943 case GSM_MI_TYPE_IMEISV:
944 /* no sim card... FIXME: what to do ? */
945 DEBUGPC(DMM, "unimplemented mobile identity type\n");
946 break;
947 default:
948 DEBUGPC(DMM, "unknown mobile identity type\n");
949 break;
950 }
951
952 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200953 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +0800954 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100955 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200956
957 subscr->equipment.classmark1 = idi->classmark1;
958 db_sync_equipment(&subscr->equipment);
959
Harald Welte59b04682009-06-10 05:40:52 +0800960 subscr_put(subscr);
961 } else
962 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
963
Harald Welte43250dd2009-12-20 09:58:40 +0100964 /* FIXME: iterate over all transactions and release them,
965 * imagine an IMSI DETACH happening during an active call! */
966
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200967 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800968 return 0;
969}
970
971static int gsm48_rx_mm_status(struct msgb *msg)
972{
973 struct gsm48_hdr *gh = msgb_l3(msg);
974
975 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
976
977 return 0;
978}
979
Sylvain Munaut28c47162009-12-24 00:27:26 +0100980/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800981static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100982{
983 struct gsm48_hdr *gh = msgb_l3(msg);
984 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800985 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100986
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200987 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200988 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +0100989
990 /* Safety check */
991 if (!conn->sec_operation) {
992 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
993 return -EIO;
994 }
995
996 /* Validate SRES */
997 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100998 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100999 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001000
1001 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001002 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001003
Sylvain Munaut28c47162009-12-24 00:27:26 +01001004 if (cb)
1005 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001006 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001007
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001008 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001009 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001010 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001011 }
1012
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001013 DEBUGPC(DMM, "OK\n");
1014
Sylvain Munaut28c47162009-12-24 00:27:26 +01001015 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001016 return gsm0808_cipher_mode(conn, net->a5_encryption,
1017 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001018}
1019
Harald Welte59b04682009-06-10 05:40:52 +08001020/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001021static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001022{
1023 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001024 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001025
1026 switch (gh->msg_type & 0xbf) {
1027 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001028 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001029 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001030 break;
1031 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001032 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001033 break;
1034 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001035 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001036 break;
1037 case GSM48_MT_MM_STATUS:
1038 rc = gsm48_rx_mm_status(msg);
1039 break;
1040 case GSM48_MT_MM_TMSI_REALL_COMPL:
1041 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001042 conn->subscr ?
1043 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001044 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001045 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001046 break;
1047 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001048 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001049 break;
1050 case GSM48_MT_MM_CM_REEST_REQ:
1051 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1052 break;
1053 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001054 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001055 break;
1056 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001057 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001058 gh->msg_type);
1059 break;
1060 }
1061
1062 return rc;
1063}
1064
1065/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001066static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001067{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001068 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001069 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001070 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001071 uint8_t *classmark2_lv = gh->data + 1;
1072 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001073 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001074 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001075 int rc = 0;
1076
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001077 resp = (struct gsm48_pag_resp *) &gh->data[0];
1078 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1079 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001080 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1081 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001082
Harald Welte59b04682009-06-10 05:40:52 +08001083 switch (mi_type) {
1084 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001085 subscr = subscr_get_by_tmsi(bts->network,
1086 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001087 break;
1088 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001089 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001090 break;
1091 }
1092
1093 if (!subscr) {
1094 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1095 /* FIXME: request id? close channel? */
1096 return -EINVAL;
1097 }
1098 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001099 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001100
Harald Weltef6845a72009-07-05 14:08:13 +02001101 subscr->equipment.classmark2_len = *classmark2_lv;
1102 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1103 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001104
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001105 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001106 return rc;
1107}
1108
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001109static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001110{
1111 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001112 uint8_t apdu_id_flags;
1113 uint8_t apdu_len;
1114 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001115
1116 apdu_id_flags = gh->data[0];
1117 apdu_len = gh->data[1];
1118 apdu_data = gh->data+2;
1119
Harald Welted4f21662012-07-16 13:22:19 +02001120 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001121 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001122
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001123 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001124}
1125
Harald Welte59b04682009-06-10 05:40:52 +08001126/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001127static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001128{
1129 struct gsm48_hdr *gh = msgb_l3(msg);
1130 int rc = 0;
1131
1132 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001133 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001134 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001135 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001136 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001137 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001138 break;
Harald Welte59b04682009-06-10 05:40:52 +08001139 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001140 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001141 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001142 break;
1143 }
1144
1145 return rc;
1146}
1147
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001148int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1149 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001150{
1151 struct msgb *msg = gsm48_msgb_alloc();
1152 struct gsm48_hdr *gh;
1153
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001154 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001155
1156 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1157 apdu_id, apdu_len);
1158
1159 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1160 gh->proto_discr = GSM48_PDISC_RR;
1161 gh->msg_type = GSM48_MT_RR_APP_INFO;
1162 gh->data[0] = apdu_id;
1163 gh->data[1] = apdu_len;
1164 memcpy(gh->data+2, apdu, apdu_len);
1165
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001166 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001167}
1168
Harald Welte59b04682009-06-10 05:40:52 +08001169/* Call Control */
1170
1171/* The entire call control code is written in accordance with Figure 7.10c
1172 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1173 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1174 * it for voice */
1175
Harald Welte03740842009-06-10 23:11:52 +08001176static void new_cc_state(struct gsm_trans *trans, int state)
1177{
1178 if (state > 31 || state < 0)
1179 return;
1180
1181 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001182 gsm48_cc_state_name(trans->cc.state),
1183 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001184
Harald Weltec2189a62009-07-23 18:56:43 +02001185 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001186}
1187
1188static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001189{
1190 struct msgb *msg = gsm48_msgb_alloc();
1191 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001192 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001193
Harald Welte59b04682009-06-10 05:40:52 +08001194 gh->msg_type = GSM48_MT_CC_STATUS;
1195
1196 cause = msgb_put(msg, 3);
1197 cause[0] = 2;
1198 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1199 cause[2] = 0x80 | 30; /* response to status inquiry */
1200
1201 call_state = msgb_put(msg, 1);
1202 call_state[0] = 0xc0 | 0x00;
1203
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001204 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001205}
1206
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001207static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001208 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001209{
1210 struct msgb *msg = gsm48_msgb_alloc();
1211 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1212
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001213 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001214
1215 gh->proto_discr = pdisc;
1216 gh->msg_type = msg_type;
1217
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001218 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001219}
1220
Harald Welte03740842009-06-10 23:11:52 +08001221static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1222{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001223 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001224 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001225 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001226 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001227 }
1228}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001229
Harald Welte03740842009-06-10 23:11:52 +08001230static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1231 int msg_type, struct gsm_mncc *mncc)
1232{
1233 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001234 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001235
1236 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001237 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001238 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001239 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001240 trans->conn->lchan->ts->trx->bts->nr,
1241 trans->conn->lchan->ts->trx->nr,
1242 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001243 (trans->subscr)?(trans->subscr->extension):"-",
1244 get_mncc_name(msg_type));
1245 else
1246 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1247 "Sending '%s' to MNCC.\n",
1248 (trans->subscr)?(trans->subscr->extension):"-",
1249 get_mncc_name(msg_type));
1250 else
1251 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1252 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1253
1254 mncc->msg_type = msg_type;
1255
Harald Welte9cfc9352009-06-26 19:39:35 +02001256 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001257 if (!msg)
1258 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001259
1260 data = msgb_put(msg, sizeof(struct gsm_mncc));
1261 memcpy(data, mncc, sizeof(struct gsm_mncc));
1262
Harald Weltebe0cb682010-12-22 23:43:29 +01001263 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001264
1265 return 0;
1266}
1267
1268int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001269 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001270{
1271 struct gsm_mncc rel;
1272
Harald Weltecb0595f2009-06-12 01:54:08 +08001273 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001274 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001275 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001276 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1277}
1278
Harald Weltec2189a62009-07-23 18:56:43 +02001279/* Call Control Specific transaction release.
1280 * gets called by trans_free, DO NOT CALL YOURSELF! */
1281void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001282{
Harald Welte03740842009-06-10 23:11:52 +08001283 gsm48_stop_cc_timer(trans);
1284
1285 /* send release to L4, if callref still exists */
1286 if (trans->callref) {
1287 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001288 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001289 GSM48_CAUSE_LOC_PRN_S_LU,
1290 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001291 }
Harald Weltec2189a62009-07-23 18:56:43 +02001292 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001293 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001294 if (trans->conn)
1295 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001296}
1297
1298static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001299
Harald Welte59b04682009-06-10 05:40:52 +08001300/* call-back from paging the B-end of the connection */
1301static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001302 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001303{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001304 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001305 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001306 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001307 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001308
Harald Welte59b04682009-06-10 05:40:52 +08001309 if (hooknum != GSM_HOOK_RR_PAGING)
1310 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001311
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001312 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001313 if (!net) {
1314 DEBUGP(DCC, "Error Network not set!\n");
1315 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001316 }
1317
Harald Welte03740842009-06-10 23:11:52 +08001318 /* check all tranactions (without lchan) for subscriber */
1319 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001320 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001321 continue;
1322 switch (event) {
1323 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001324 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001325 break;
1326 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001327 transt->subscr->extension);
1328 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001329 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001330 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001331 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001332 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001333 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001334 }
1335 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001336 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001337 break;
1338 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001339 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001340 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001341 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001342 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001343 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001344 mncc_release_ind(transt->subscr->net, transt,
1345 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001346 GSM48_CAUSE_LOC_PRN_S_LU,
1347 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001348 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001349 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001350 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001351 break;
1352 }
1353 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001354
1355 talloc_free(paging_request);
1356
1357 /*
1358 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1359 * failure and then the subscr will be poke again. This needs a lot of fixing
1360 * in the subscriber queue code.
1361 */
1362 if (!found && conn)
1363 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001364 return 0;
1365}
1366
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001367static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001368
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001369/* handle audio path for handover */
1370static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1371 void *handler_data, void *signal_data)
1372{
1373 struct rtp_socket *old_rs, *new_rs, *other_rs;
1374 struct ho_signal_data *sig = signal_data;
1375
1376 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1377 return 0;
1378
1379 if (ipacc_rtp_direct) {
1380 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1381 return 0;
1382 }
1383
1384 /* RTP Proxy mode */
1385 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1386 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1387
1388 if (!new_rs) {
1389 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1390 return -EIO;
1391 }
1392
1393 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1394
1395 if (!old_rs) {
1396 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1397 return -EIO;
1398 }
1399
1400 /* copy rx_action and reference to other sock */
1401 new_rs->rx_action = old_rs->rx_action;
1402 new_rs->tx_action = old_rs->tx_action;
1403 new_rs->transmit = old_rs->transmit;
1404
1405 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1406 case RTP_PROXY:
1407 other_rs = old_rs->proxy.other_sock;
1408 rtp_socket_proxy(new_rs, other_rs);
1409 /* delete reference to other end socket to prevent
1410 * rtp_socket_free() from removing the inverse reference */
1411 old_rs->proxy.other_sock = NULL;
1412 break;
1413 case RTP_RECV_UPSTREAM:
1414 new_rs->receive = old_rs->receive;
1415 break;
1416 case RTP_NONE:
1417 break;
1418 }
1419
1420 return 0;
1421}
1422
Harald Welte3c062072009-07-28 18:25:29 +02001423/* some other part of the code sends us a signal */
1424static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1425 void *handler_data, void *signal_data)
1426{
1427 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001428 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001429 struct gsm_network *net;
1430 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001431
1432 if (subsys != SS_ABISIP)
1433 return 0;
1434
1435 /* in case we use direct BTS-to-BTS RTP */
1436 if (ipacc_rtp_direct)
1437 return 0;
1438
Harald Welte3c062072009-07-28 18:25:29 +02001439 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001440 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001441 /* in case we don't use direct BTS-to-BTS RTP */
1442 /* the BTS has successfully bound a TCH to a local ip/port,
1443 * which means we can connect our UDP socket to it */
1444 if (lchan->abis_ip.rtp_socket) {
1445 rtp_socket_free(lchan->abis_ip.rtp_socket);
1446 lchan->abis_ip.rtp_socket = NULL;
1447 }
1448
1449 lchan->abis_ip.rtp_socket = rtp_socket_create();
1450 if (!lchan->abis_ip.rtp_socket)
1451 return -EIO;
1452
1453 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1454 lchan->abis_ip.bound_ip,
1455 lchan->abis_ip.bound_port);
1456 if (rc < 0)
1457 return -EIO;
1458
Harald Welte3971ad52009-12-19 22:23:05 +01001459 /* check if any transactions on this lchan still have
1460 * a tch_recv_mncc request pending */
1461 net = lchan->ts->trx->bts->network;
1462 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001463 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001464 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1465 tch_recv_mncc(net, trans->callref, 1);
1466 }
1467 }
Harald Welte3c062072009-07-28 18:25:29 +02001468 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001469 case S_ABISIP_DLCX_IND:
1470 /* the BTS tells us a RTP stream has been disconnected */
1471 if (lchan->abis_ip.rtp_socket) {
1472 rtp_socket_free(lchan->abis_ip.rtp_socket);
1473 lchan->abis_ip.rtp_socket = NULL;
1474 }
1475
1476 break;
Harald Welte3c062072009-07-28 18:25:29 +02001477 }
1478
1479 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001480}
1481
Harald Welte59b04682009-06-10 05:40:52 +08001482/* map two ipaccess RTP streams onto each other */
1483static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1484{
1485 struct gsm_bts *bts = lchan->ts->trx->bts;
1486 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001487 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001488
1489 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1490 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1491 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1492
1493 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001494 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001495 return -EINVAL;
1496 }
Harald Welte3971ad52009-12-19 22:23:05 +01001497
1498 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001499 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001500 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001501 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001502 if (!ipacc_rtp_direct) {
1503 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001504 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001505 if (rc < 0)
1506 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001507 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001508 if (rc < 0)
1509 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001510 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301511 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1512 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001513 } else {
1514 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301515 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1516 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301517 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001518 if (rc < 0)
1519 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301520 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1521 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301522 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001523 }
Harald Welte59b04682009-06-10 05:40:52 +08001524 break;
1525 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001526 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001527 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001528 trau_mux_map_lchan(lchan, remote_lchan);
1529 break;
1530 default:
Harald Welte8099a712011-08-09 21:53:20 +02001531 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001532 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001533 }
1534
1535 return 0;
1536}
1537
Harald Welte03740842009-06-10 23:11:52 +08001538/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001539static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001540{
Harald Weltec2189a62009-07-23 18:56:43 +02001541 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1542 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001543
Harald Welte03740842009-06-10 23:11:52 +08001544 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001545 return -EIO;
1546
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001547 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001548 return -EIO;
1549
1550 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001551 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001552}
1553
Harald Welte3971ad52009-12-19 22:23:05 +01001554/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001555static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001556{
1557 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001558 struct gsm_lchan *lchan;
1559 struct gsm_bts *bts;
1560 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001561
Harald Welte03740842009-06-10 23:11:52 +08001562 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001563 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001564 if (!trans)
1565 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001566 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001567 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001568 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001569 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001570
Harald Welte3971ad52009-12-19 22:23:05 +01001571 switch (bts->type) {
1572 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001573 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001574 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001575 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001576 return -EINVAL;
1577 }
1578 /* in case, we don't have a RTP socket yet, we note this
1579 * in the transaction and try later */
1580 if (!lchan->abis_ip.rtp_socket) {
1581 trans->tch_recv = enable;
1582 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1583 return 0;
1584 }
1585 if (enable) {
1586 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001587 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001588 if (rc < 0)
1589 return rc;
1590 /* assign socket to application interface */
1591 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1592 net, callref);
1593 } else
1594 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1595 net, 0);
1596 break;
1597 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001598 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001599 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001600 if (enable)
1601 return trau_recv_lchan(lchan, callref);
1602 return trau_mux_unmap(NULL, callref);
1603 break;
1604 default:
Harald Welte8099a712011-08-09 21:53:20 +02001605 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001606 return -EINVAL;
1607 }
1608
1609 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001610}
1611
Harald Welte03740842009-06-10 23:11:52 +08001612static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1613{
1614 DEBUGP(DCC, "-> STATUS ENQ\n");
1615 return gsm48_cc_tx_status(trans, msg);
1616}
1617
1618static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1619static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1620
1621static void gsm48_cc_timeout(void *arg)
1622{
1623 struct gsm_trans *trans = arg;
1624 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001625 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1626 int mo_location = GSM48_CAUSE_LOC_USER;
1627 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1628 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001629 struct gsm_mncc mo_rel, l4_rel;
1630
1631 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1632 mo_rel.callref = trans->callref;
1633 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1634 l4_rel.callref = trans->callref;
1635
Harald Weltec2189a62009-07-23 18:56:43 +02001636 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001637 case 0x303:
1638 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001639 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001640 break;
1641 case 0x310:
1642 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001643 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001644 break;
1645 case 0x313:
1646 disconnect = 1;
1647 /* unknown, did not find it in the specs */
1648 break;
1649 case 0x301:
1650 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001651 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001652 break;
1653 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001654 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001655 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001656 gsm48_cc_tx_release(trans, &trans->cc.msg);
1657 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001658 break; /* stay in release state */
1659 }
Harald Weltec2189a62009-07-23 18:56:43 +02001660 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001661 return;
1662// release = 1;
1663// l4_cause = 14;
1664// break;
1665 case 0x306:
1666 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001667 mo_cause = trans->cc.msg.cause.value;
1668 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001669 break;
1670 case 0x323:
1671 disconnect = 1;
1672 break;
1673 default:
1674 release = 1;
1675 }
1676
1677 if (release && trans->callref) {
1678 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001679 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001680 l4_location, l4_cause);
1681 trans->callref = 0;
1682 }
1683
1684 if (disconnect && trans->callref) {
1685 /* process disconnect towards layer 4 */
1686 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001687 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001688 }
1689
1690 /* process disconnect towards mobile station */
1691 if (disconnect || release) {
1692 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001693 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1694 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1695 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001696 mo_rel.cause.diag_len = 3;
1697
1698 if (disconnect)
1699 gsm48_cc_tx_disconnect(trans, &mo_rel);
1700 if (release)
1701 gsm48_cc_tx_release(trans, &mo_rel);
1702 }
1703
1704}
1705
1706static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1707 int sec, int micro)
1708{
1709 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001710 trans->cc.timer.cb = gsm48_cc_timeout;
1711 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001712 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001713 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001714}
1715
1716static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1717{
1718 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001719 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001720 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1721 struct tlv_parsed tp;
1722 struct gsm_mncc setup;
1723
1724 memset(&setup, 0, sizeof(struct gsm_mncc));
1725 setup.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001726 if (trans->conn && trans->conn->lchan)
1727 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001728 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001729 /* emergency setup is identified by msg_type */
1730 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1731 setup.emergency = 1;
1732
1733 /* use subscriber as calling party number */
1734 if (trans->subscr) {
1735 setup.fields |= MNCC_F_CALLING;
1736 strncpy(setup.calling.number, trans->subscr->extension,
1737 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001738 strncpy(setup.imsi, trans->subscr->imsi,
1739 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001740 }
1741 /* bearer capability */
1742 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1743 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001744 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001745 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1746 }
1747 /* facility */
1748 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1749 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001750 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001751 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1752 }
1753 /* called party bcd number */
1754 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1755 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001756 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001757 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1758 }
1759 /* user-user */
1760 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1761 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001762 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001763 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1764 }
1765 /* ss-version */
1766 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1767 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001768 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001769 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1770 }
1771 /* CLIR suppression */
1772 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1773 setup.clir.sup = 1;
1774 /* CLIR invocation */
1775 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1776 setup.clir.inv = 1;
1777 /* cc cap */
1778 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1779 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001780 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001781 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1782 }
1783
Harald Welte03740842009-06-10 23:11:52 +08001784 new_cc_state(trans, GSM_CSTATE_INITIATED);
1785
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001786 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1787 subscr_name(trans->subscr), trans->subscr->extension,
1788 setup.called.number);
1789
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001790 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001791
Harald Welte03740842009-06-10 23:11:52 +08001792 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001793 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001794
Harald Welteca745e22009-07-29 12:10:35 +02001795 /* MNCC code will modify the channel asynchronously, we should
1796 * ipaccess-bind only after the modification has been made to the
1797 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001798 return 0;
1799}
1800
1801static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001802{
1803 struct msgb *msg = gsm48_msgb_alloc();
1804 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001805 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001806 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001807
1808 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1809
Harald Welte03740842009-06-10 23:11:52 +08001810 /* transaction id must not be assigned */
1811 if (trans->transaction_id != 0xff) { /* unasssigned */
1812 DEBUGP(DCC, "TX Setup with assigned transaction. "
1813 "This is not allowed!\n");
1814 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001815 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001816 GSM48_CAUSE_LOC_PRN_S_LU,
1817 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001818 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001819 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001820 return rc;
1821 }
1822
1823 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001824 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1825 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001826 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001827 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001828 GSM48_CAUSE_LOC_PRN_S_LU,
1829 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001830 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001831 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001832 return rc;
1833 }
Harald Welte165fc332009-07-23 21:36:44 +02001834 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001835
Harald Welte59b04682009-06-10 05:40:52 +08001836 gh->msg_type = GSM48_MT_CC_SETUP;
1837
Harald Welte03740842009-06-10 23:11:52 +08001838 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001839
Harald Welte03740842009-06-10 23:11:52 +08001840 /* bearer capability */
1841 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001842 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001843 /* facility */
1844 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001845 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001846 /* progress */
1847 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001848 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001849 /* calling party BCD number */
1850 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001851 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001852 /* called party BCD number */
1853 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001854 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001855 /* user-user */
1856 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001857 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001858 /* redirecting party BCD number */
1859 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001860 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001861 /* signal */
1862 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001863 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001864
1865 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001866
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001867 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001868
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001869 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001870}
1871
Harald Welte03740842009-06-10 23:11:52 +08001872static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1873{
1874 struct gsm48_hdr *gh = msgb_l3(msg);
1875 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1876 struct tlv_parsed tp;
1877 struct gsm_mncc call_conf;
1878
1879 gsm48_stop_cc_timer(trans);
1880 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1881
1882 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1883 call_conf.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001884 if (trans->conn && trans->conn->lchan)
1885 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001886 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001887#if 0
1888 /* repeat */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1890 call_conf.repeat = 1;
1891 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1892 call_conf.repeat = 2;
1893#endif
1894 /* bearer capability */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1896 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001897 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001898 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1899 }
1900 /* cause */
1901 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1902 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001903 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001904 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1905 }
1906 /* cc cap */
1907 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1908 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001909 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001910 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1911 }
1912
1913 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1914
Harald Welte0abe5a62009-07-23 19:06:52 +02001915 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1916 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001917}
1918
1919static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1920{
1921 struct gsm_mncc *proceeding = arg;
1922 struct msgb *msg = gsm48_msgb_alloc();
1923 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1924
Harald Welte03740842009-06-10 23:11:52 +08001925 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1926
1927 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1928
1929 /* bearer capability */
1930 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001931 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001932 /* facility */
1933 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001934 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001935 /* progress */
1936 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001937 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001938
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001939 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001940}
1941
1942static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1943{
1944 struct gsm48_hdr *gh = msgb_l3(msg);
1945 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1946 struct tlv_parsed tp;
1947 struct gsm_mncc alerting;
1948
1949 gsm48_stop_cc_timer(trans);
1950 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1951
1952 memset(&alerting, 0, sizeof(struct gsm_mncc));
1953 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001954 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001955 /* facility */
1956 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1957 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001958 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001959 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1960 }
1961
1962 /* progress */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1964 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001965 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1967 }
1968 /* ss-version */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1970 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001971 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001972 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1973 }
1974
1975 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1976
Harald Welte0abe5a62009-07-23 19:06:52 +02001977 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1978 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001979}
1980
1981static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1982{
1983 struct gsm_mncc *alerting = arg;
1984 struct msgb *msg = gsm48_msgb_alloc();
1985 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1986
Harald Welte03740842009-06-10 23:11:52 +08001987 gh->msg_type = GSM48_MT_CC_ALERTING;
1988
1989 /* facility */
1990 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001991 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001992 /* progress */
1993 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001994 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001995 /* user-user */
1996 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001997 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001998
1999 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2000
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002001 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002002}
2003
2004static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2005{
2006 struct gsm_mncc *progress = arg;
2007 struct msgb *msg = gsm48_msgb_alloc();
2008 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2009
Harald Welte03740842009-06-10 23:11:52 +08002010 gh->msg_type = GSM48_MT_CC_PROGRESS;
2011
2012 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002013 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002014 /* user-user */
2015 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002016 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002017
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002018 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002019}
2020
2021static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2022{
2023 struct gsm_mncc *connect = arg;
2024 struct msgb *msg = gsm48_msgb_alloc();
2025 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2026
Harald Welte03740842009-06-10 23:11:52 +08002027 gh->msg_type = GSM48_MT_CC_CONNECT;
2028
2029 gsm48_stop_cc_timer(trans);
2030 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2031
2032 /* facility */
2033 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002034 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002035 /* progress */
2036 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002037 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002038 /* connected number */
2039 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002040 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002041 /* user-user */
2042 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002043 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002044
2045 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2046
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002047 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002048}
2049
2050static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2051{
2052 struct gsm48_hdr *gh = msgb_l3(msg);
2053 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2054 struct tlv_parsed tp;
2055 struct gsm_mncc connect;
2056
2057 gsm48_stop_cc_timer(trans);
2058
2059 memset(&connect, 0, sizeof(struct gsm_mncc));
2060 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002061 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002062 /* use subscriber as connected party number */
2063 if (trans->subscr) {
2064 connect.fields |= MNCC_F_CONNECTED;
2065 strncpy(connect.connected.number, trans->subscr->extension,
2066 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002067 strncpy(connect.imsi, trans->subscr->imsi,
2068 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002069 }
2070 /* facility */
2071 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2072 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002073 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002074 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2075 }
2076 /* user-user */
2077 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2078 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002079 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002080 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2081 }
2082 /* ss-version */
2083 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2084 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002085 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002086 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2087 }
2088
2089 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002090 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002091
Harald Welte0abe5a62009-07-23 19:06:52 +02002092 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002093}
2094
2095
2096static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2097{
2098 struct gsm_mncc connect_ack;
2099
2100 gsm48_stop_cc_timer(trans);
2101
2102 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002103 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002104
2105 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2106 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002107
Harald Welte0abe5a62009-07-23 19:06:52 +02002108 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002109 &connect_ack);
2110}
2111
2112static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2113{
2114 struct msgb *msg = gsm48_msgb_alloc();
2115 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2116
Harald Welte03740842009-06-10 23:11:52 +08002117 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2118
2119 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2120
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002121 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002122}
2123
2124static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2125{
2126 struct gsm48_hdr *gh = msgb_l3(msg);
2127 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2128 struct tlv_parsed tp;
2129 struct gsm_mncc disc;
2130
2131 gsm48_stop_cc_timer(trans);
2132
2133 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2134
2135 memset(&disc, 0, sizeof(struct gsm_mncc));
2136 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002137 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002138 /* cause */
2139 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2140 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002141 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002142 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2143 }
2144 /* facility */
2145 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2146 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002147 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002148 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2149 }
2150 /* user-user */
2151 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2152 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002153 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002154 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2155 }
2156 /* ss-version */
2157 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2158 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002159 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002160 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2161 }
2162
Harald Welte0abe5a62009-07-23 19:06:52 +02002163 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002164
2165}
2166
Harald Weltebbc636a2009-06-11 14:23:20 +08002167static struct gsm_mncc_cause default_cause = {
2168 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2169 .coding = 0,
2170 .rec = 0,
2171 .rec_val = 0,
2172 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2173 .diag_len = 0,
2174 .diag = { 0 },
2175};
Harald Welte03740842009-06-10 23:11:52 +08002176
2177static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2178{
2179 struct gsm_mncc *disc = arg;
2180 struct msgb *msg = gsm48_msgb_alloc();
2181 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2182
Harald Welte03740842009-06-10 23:11:52 +08002183 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2184
2185 gsm48_stop_cc_timer(trans);
2186 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2187
2188 /* cause */
2189 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002190 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002191 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002192 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002193
2194 /* facility */
2195 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002196 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002197 /* progress */
2198 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002199 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002200 /* user-user */
2201 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002202 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002203
2204 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002205 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002206
2207 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2208
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002209 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002210}
2211
2212static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2213{
2214 struct gsm48_hdr *gh = msgb_l3(msg);
2215 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2216 struct tlv_parsed tp;
2217 struct gsm_mncc rel;
2218 int rc;
2219
2220 gsm48_stop_cc_timer(trans);
2221
2222 memset(&rel, 0, sizeof(struct gsm_mncc));
2223 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002224 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002225 /* cause */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2227 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002228 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2230 }
2231 /* facility */
2232 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2233 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002234 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002235 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2236 }
2237 /* user-user */
2238 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2239 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002240 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002241 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2242 }
2243 /* ss-version */
2244 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2245 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002246 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002247 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2248 }
2249
Harald Weltec2189a62009-07-23 18:56:43 +02002250 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002251 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002252 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002253 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002254 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002255 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002256 GSM48_MT_CC_RELEASE_COMPL);
2257 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002258 }
2259
2260 new_cc_state(trans, GSM_CSTATE_NULL);
2261
2262 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002263 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002264
2265 return rc;
2266}
2267
2268static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2269{
2270 struct gsm_mncc *rel = arg;
2271 struct msgb *msg = gsm48_msgb_alloc();
2272 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2273
Harald Welte03740842009-06-10 23:11:52 +08002274 gh->msg_type = GSM48_MT_CC_RELEASE;
2275
2276 trans->callref = 0;
2277
2278 gsm48_stop_cc_timer(trans);
2279 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2280
2281 /* cause */
2282 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002283 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002284 /* facility */
2285 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002286 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002287 /* user-user */
2288 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002289 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002290
Harald Weltec2189a62009-07-23 18:56:43 +02002291 trans->cc.T308_second = 0;
2292 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002293
Harald Weltec2189a62009-07-23 18:56:43 +02002294 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002295 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2296
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002297 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002298}
2299
2300static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2301{
2302 struct gsm48_hdr *gh = msgb_l3(msg);
2303 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2304 struct tlv_parsed tp;
2305 struct gsm_mncc rel;
2306 int rc = 0;
2307
2308 gsm48_stop_cc_timer(trans);
2309
2310 memset(&rel, 0, sizeof(struct gsm_mncc));
2311 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002312 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002313 /* cause */
2314 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2315 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002316 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002317 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2318 }
2319 /* facility */
2320 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2321 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002322 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002323 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2324 }
2325 /* user-user */
2326 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2327 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002328 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002329 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2330 }
2331 /* ss-version */
2332 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2333 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002334 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002335 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2336 }
2337
2338 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002339 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002340 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002341 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002342 MNCC_REJ_IND, &rel);
2343 break;
2344 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002345 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002346 MNCC_REL_CNF, &rel);
2347 break;
2348 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002349 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002350 MNCC_REL_IND, &rel);
2351 }
2352 }
2353
2354 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002355 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002356
2357 return rc;
2358}
2359
2360static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2361{
2362 struct gsm_mncc *rel = arg;
2363 struct msgb *msg = gsm48_msgb_alloc();
2364 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002365 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002366
Harald Welte03740842009-06-10 23:11:52 +08002367 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2368
2369 trans->callref = 0;
2370
2371 gsm48_stop_cc_timer(trans);
2372
2373 /* cause */
2374 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002375 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002376 /* facility */
2377 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002378 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002379 /* user-user */
2380 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002381 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002382
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002383 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2384
Harald Weltec2189a62009-07-23 18:56:43 +02002385 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002386
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002387 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2391{
2392 struct gsm48_hdr *gh = msgb_l3(msg);
2393 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2394 struct tlv_parsed tp;
2395 struct gsm_mncc fac;
2396
2397 memset(&fac, 0, sizeof(struct gsm_mncc));
2398 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002399 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002400 /* facility */
2401 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2402 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002403 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002404 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2405 }
2406 /* ss-version */
2407 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2408 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002409 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002410 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2411 }
2412
Harald Welte0abe5a62009-07-23 19:06:52 +02002413 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002414}
2415
2416static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2417{
2418 struct gsm_mncc *fac = arg;
2419 struct msgb *msg = gsm48_msgb_alloc();
2420 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2421
Harald Welte03740842009-06-10 23:11:52 +08002422 gh->msg_type = GSM48_MT_CC_FACILITY;
2423
2424 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002425 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002426
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002427 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002428}
2429
2430static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2431{
2432 struct gsm_mncc hold;
2433
2434 memset(&hold, 0, sizeof(struct gsm_mncc));
2435 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002436 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002437}
2438
2439static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2440{
2441 struct msgb *msg = gsm48_msgb_alloc();
2442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2443
Harald Welte03740842009-06-10 23:11:52 +08002444 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2445
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002446 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2450{
2451 struct gsm_mncc *hold_rej = arg;
2452 struct msgb *msg = gsm48_msgb_alloc();
2453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2454
Harald Welte03740842009-06-10 23:11:52 +08002455 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2456
2457 /* cause */
2458 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002459 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002460 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002461 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002462
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002463 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2467{
2468 struct gsm_mncc retrieve;
2469
2470 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2471 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002472 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2473 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2477{
2478 struct msgb *msg = gsm48_msgb_alloc();
2479 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2480
Harald Welte03740842009-06-10 23:11:52 +08002481 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2482
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002483 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002484}
2485
2486static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2487{
2488 struct gsm_mncc *retrieve_rej = arg;
2489 struct msgb *msg = gsm48_msgb_alloc();
2490 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2491
Harald Welte03740842009-06-10 23:11:52 +08002492 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2493
2494 /* cause */
2495 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002496 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002497 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002498 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002499
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002500 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002501}
2502
2503static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2504{
2505 struct gsm48_hdr *gh = msgb_l3(msg);
2506 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2507 struct tlv_parsed tp;
2508 struct gsm_mncc dtmf;
2509
2510 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2511 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002512 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002513 /* keypad facility */
2514 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2515 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002516 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002517 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2518 }
2519
Harald Welte0abe5a62009-07-23 19:06:52 +02002520 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002521}
2522
2523static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2524{
2525 struct gsm_mncc *dtmf = arg;
2526 struct msgb *msg = gsm48_msgb_alloc();
2527 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2528
Harald Welte03740842009-06-10 23:11:52 +08002529 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2530
2531 /* keypad */
2532 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002533 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002534
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002535 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2539{
2540 struct gsm_mncc *dtmf = arg;
2541 struct msgb *msg = gsm48_msgb_alloc();
2542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2543
Harald Welte03740842009-06-10 23:11:52 +08002544 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2545
2546 /* cause */
2547 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002548 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002549 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002550 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002551
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002552 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002553}
2554
2555static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2556{
2557 struct msgb *msg = gsm48_msgb_alloc();
2558 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2559
Harald Welte03740842009-06-10 23:11:52 +08002560 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2561
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_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2566{
2567 struct gsm_mncc dtmf;
2568
2569 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2570 dtmf.callref = trans->callref;
2571
Harald Welte0abe5a62009-07-23 19:06:52 +02002572 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002573}
2574
2575static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2576{
2577 struct gsm48_hdr *gh = msgb_l3(msg);
2578 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2579 struct tlv_parsed tp;
2580 struct gsm_mncc modify;
2581
2582 memset(&modify, 0, sizeof(struct gsm_mncc));
2583 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002584 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002585 /* bearer capability */
2586 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2587 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002588 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002589 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2590 }
2591
2592 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2593
Harald Welte0abe5a62009-07-23 19:06:52 +02002594 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002595}
2596
2597static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2598{
2599 struct gsm_mncc *modify = arg;
2600 struct msgb *msg = gsm48_msgb_alloc();
2601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2602
Harald Welte03740842009-06-10 23:11:52 +08002603 gh->msg_type = GSM48_MT_CC_MODIFY;
2604
2605 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2606
2607 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002608 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002609
2610 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2611
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002612 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2616{
2617 struct gsm48_hdr *gh = msgb_l3(msg);
2618 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2619 struct tlv_parsed tp;
2620 struct gsm_mncc modify;
2621
2622 gsm48_stop_cc_timer(trans);
2623
2624 memset(&modify, 0, sizeof(struct gsm_mncc));
2625 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002626 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002627 /* bearer capability */
2628 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2629 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002630 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002631 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2632 }
2633
2634 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2635
Harald Welte0abe5a62009-07-23 19:06:52 +02002636 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002637}
2638
2639static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2640{
2641 struct gsm_mncc *modify = arg;
2642 struct msgb *msg = gsm48_msgb_alloc();
2643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2644
Harald Welte03740842009-06-10 23:11:52 +08002645 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2646
2647 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002648 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002649
2650 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2651
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002652 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002653}
2654
2655static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2656{
2657 struct gsm48_hdr *gh = msgb_l3(msg);
2658 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2659 struct tlv_parsed tp;
2660 struct gsm_mncc modify;
2661
2662 gsm48_stop_cc_timer(trans);
2663
2664 memset(&modify, 0, sizeof(struct gsm_mncc));
2665 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002666 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002667 /* bearer capability */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2669 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002670 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002671 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2672 }
2673 /* cause */
2674 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2675 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002676 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002677 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2678 }
2679
2680 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2681
Harald Welte0abe5a62009-07-23 19:06:52 +02002682 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002683}
2684
2685static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2686{
2687 struct gsm_mncc *modify = arg;
2688 struct msgb *msg = gsm48_msgb_alloc();
2689 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2690
Harald Welte03740842009-06-10 23:11:52 +08002691 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2692
2693 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002694 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002695 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002696 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002697
2698 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2699
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002700 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002701}
2702
2703static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2704{
2705 struct gsm_mncc *notify = arg;
2706 struct msgb *msg = gsm48_msgb_alloc();
2707 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2708
Harald Welte03740842009-06-10 23:11:52 +08002709 gh->msg_type = GSM48_MT_CC_NOTIFY;
2710
2711 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002712 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002713
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002714 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002715}
2716
2717static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2718{
2719 struct gsm48_hdr *gh = msgb_l3(msg);
2720 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2721// struct tlv_parsed tp;
2722 struct gsm_mncc notify;
2723
2724 memset(&notify, 0, sizeof(struct gsm_mncc));
2725 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002726// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002727 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002728 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002729
Harald Welte0abe5a62009-07-23 19:06:52 +02002730 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002731}
2732
2733static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2734{
2735 struct gsm_mncc *user = arg;
2736 struct msgb *msg = gsm48_msgb_alloc();
2737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2738
Harald Welte03740842009-06-10 23:11:52 +08002739 gh->msg_type = GSM48_MT_CC_USER_INFO;
2740
2741 /* user-user */
2742 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002743 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002744 /* more data */
2745 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002746 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002747
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002748 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002749}
2750
2751static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2752{
2753 struct gsm48_hdr *gh = msgb_l3(msg);
2754 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2755 struct tlv_parsed tp;
2756 struct gsm_mncc user;
2757
2758 memset(&user, 0, sizeof(struct gsm_mncc));
2759 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002760 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002761 /* user-user */
2762 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2763 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002764 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002765 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2766 }
2767 /* more data */
2768 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2769 user.more = 1;
2770
Harald Welte0abe5a62009-07-23 19:06:52 +02002771 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002772}
2773
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002774static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002775{
2776 struct gsm_mncc *mode = arg;
2777
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002778 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002779}
2780
2781static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002782 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002783 int type;
2784 int (*rout) (struct gsm_trans *trans, void *arg);
2785} downstatelist[] = {
2786 /* mobile originating call establishment */
2787 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2788 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2789 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2790 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2791 {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 */
2792 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2793 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2794 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2795 /* mobile terminating call establishment */
2796 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2797 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2798 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2799 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2800 /* signalling during call */
2801 {SBIT(GSM_CSTATE_ACTIVE),
2802 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2803 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2804 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2805 {ALL_STATES,
2806 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2807 {ALL_STATES,
2808 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2809 {ALL_STATES,
2810 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2811 {SBIT(GSM_CSTATE_ACTIVE),
2812 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2813 {SBIT(GSM_CSTATE_ACTIVE),
2814 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2815 {SBIT(GSM_CSTATE_ACTIVE),
2816 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2817 {SBIT(GSM_CSTATE_ACTIVE),
2818 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2819 {SBIT(GSM_CSTATE_ACTIVE),
2820 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2821 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2822 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2823 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2824 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2825 {SBIT(GSM_CSTATE_ACTIVE),
2826 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2827 /* clearing */
2828 {SBIT(GSM_CSTATE_INITIATED),
2829 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2830 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2831 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2832 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2833 MNCC_REL_REQ, gsm48_cc_tx_release},
2834 /* special */
2835 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002836 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002837};
2838
2839#define DOWNSLLEN \
2840 (sizeof(downstatelist) / sizeof(struct downstate))
2841
2842
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002843int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002844{
Harald Welteaa60edb2009-08-09 18:52:33 +02002845 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002846 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002847 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002848 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002849 struct gsm_mncc *data = arg, rel;
2850
Harald Welte17b56522010-12-22 21:45:05 +01002851 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2852
Harald Welte03740842009-06-10 23:11:52 +08002853 /* handle special messages */
2854 switch(msg_type) {
2855 case MNCC_BRIDGE:
2856 return tch_bridge(net, arg);
2857 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002858 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002859 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002860 return tch_recv_mncc(net, data->callref, 1);
2861 case GSM_TCHF_FRAME:
2862 /* Find callref */
2863 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002864 if (!trans) {
2865 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002866 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002867 }
2868 if (!trans->conn) {
2869 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002870 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002871 }
2872 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2873 /* This should be LOGL_ERROR or NOTICE, but
2874 * unfortuantely it happens for a couple of frames at
2875 * the beginning of every RTP connection */
2876 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002877 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002878 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002879 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002880 switch (bts->type) {
2881 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02002882 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01002883 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002884 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002885 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002886 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002887 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002888 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03002889 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02002890 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002891 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002892 default:
Harald Welte8099a712011-08-09 21:53:20 +02002893 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01002894 }
2895 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002896 }
2897
2898 memset(&rel, 0, sizeof(struct gsm_mncc));
2899 rel.callref = data->callref;
2900
2901 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002902 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002903
2904 /* Callref unknown */
2905 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002906 struct gsm_subscriber *subscr;
2907
Harald Welte6e1536e2009-07-04 10:11:24 +02002908 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002909 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2910 "Received '%s' from MNCC with "
2911 "unknown callref %d\n", data->called.number,
2912 get_mncc_name(msg_type), data->callref);
2913 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002914 return mncc_release_ind(net, NULL, data->callref,
2915 GSM48_CAUSE_LOC_PRN_S_LU,
2916 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002917 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002918 if (!data->called.number[0] && !data->imsi[0]) {
2919 DEBUGP(DCC, "(bts - trx - ts - ti) "
2920 "Received '%s' from MNCC with "
2921 "no number or IMSI\n", get_mncc_name(msg_type));
2922 /* Invalid number */
2923 return mncc_release_ind(net, NULL, data->callref,
2924 GSM48_CAUSE_LOC_PRN_S_LU,
2925 GSM48_CC_CAUSE_INV_NR_FORMAT);
2926 }
Harald Welte03740842009-06-10 23:11:52 +08002927 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002928 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002929 subscr = subscr_get_by_extension(net,
2930 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002931 else
Harald Welte75350412009-07-23 18:46:00 +02002932 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002933 /* If subscriber is not found */
2934 if (!subscr) {
2935 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2936 "Received '%s' from MNCC with "
2937 "unknown subscriber %s\n", data->called.number,
2938 get_mncc_name(msg_type), data->called.number);
2939 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002940 return mncc_release_ind(net, NULL, data->callref,
2941 GSM48_CAUSE_LOC_PRN_S_LU,
2942 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002943 }
2944 /* If subscriber is not "attached" */
2945 if (!subscr->lac) {
2946 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2947 "Received '%s' from MNCC with "
2948 "detached subscriber %s\n", data->called.number,
2949 get_mncc_name(msg_type), data->called.number);
2950 subscr_put(subscr);
2951 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002952 return mncc_release_ind(net, NULL, data->callref,
2953 GSM48_CAUSE_LOC_PRN_S_LU,
2954 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002955 }
2956 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002957 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2958 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002959 DEBUGP(DCC, "No memory for trans.\n");
2960 subscr_put(subscr);
2961 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002962 mncc_release_ind(net, NULL, data->callref,
2963 GSM48_CAUSE_LOC_PRN_S_LU,
2964 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002965 return -ENOMEM;
2966 }
Harald Welte03740842009-06-10 23:11:52 +08002967 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002968 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002969
Harald Welte03740842009-06-10 23:11:52 +08002970 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002971 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002972 /* find transaction with this subscriber already paging */
2973 llist_for_each_entry(transt, &net->trans_list, entry) {
2974 /* Transaction of our lchan? */
2975 if (transt == trans ||
2976 transt->subscr != subscr)
2977 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01002978 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08002979 "Received '%s' from MNCC with "
2980 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01002981 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08002982 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01002983 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002984 subscr_put(subscr);
2985 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002986 return 0;
2987 }
2988 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002989 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01002990
2991 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01002992 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
2993 if (!trans->paging_request) {
2994 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
2995 subscr_put(subscr);
2996 trans_free(trans);
2997 return 0;
2998 }
2999
3000 *trans->paging_request = subscr->net;
3001 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003002
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003003 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003004 return 0;
3005 }
3006 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003007 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003008 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003009 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003010
3011 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003012 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003013
3014 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003015 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003016 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003017 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003018 (trans->subscr)?(trans->subscr->extension):"-",
3019 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003020 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3021 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003022 if (msg_type == MNCC_REL_REQ)
3023 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3024 else
3025 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3026 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003027 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003028 return rc;
3029 }
3030
3031 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3032 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003033 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003034 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003035 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003036 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003037 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003038
3039 /* Find function for current state and message */
3040 for (i = 0; i < DOWNSLLEN; i++)
3041 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003042 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003043 break;
3044 if (i == DOWNSLLEN) {
3045 DEBUGP(DCC, "Message unhandled at this state.\n");
3046 return 0;
3047 }
3048
3049 rc = downstatelist[i].rout(trans, arg);
3050
3051 return rc;
3052}
3053
3054
3055static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003056 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003057 int type;
3058 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3059} datastatelist[] = {
3060 /* mobile originating call establishment */
3061 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3062 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3063 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3064 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3065 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3066 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3067 /* mobile terminating call establishment */
3068 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3069 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3070 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3071 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003072 {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 +08003073 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3074 /* signalling during call */
3075 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3076 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3077 {SBIT(GSM_CSTATE_ACTIVE),
3078 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3079 {ALL_STATES,
3080 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3081 {ALL_STATES,
3082 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3083 {ALL_STATES,
3084 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3085 {SBIT(GSM_CSTATE_ACTIVE),
3086 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3087 {SBIT(GSM_CSTATE_ACTIVE),
3088 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3089 {SBIT(GSM_CSTATE_ACTIVE),
3090 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3091 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3092 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3093 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3094 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3095 {SBIT(GSM_CSTATE_ACTIVE),
3096 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3097 /* clearing */
3098 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3099 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3100 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3101 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3102 {ALL_STATES, /* 5.4.3.4 */
3103 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3104};
3105
3106#define DATASLLEN \
3107 (sizeof(datastatelist) / sizeof(struct datastate))
3108
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003109static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003110{
3111 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003112 uint8_t msg_type = gh->msg_type & 0xbf;
3113 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003114 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003115 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003116
Harald Welte03740842009-06-10 23:11:52 +08003117 if (msg_type & 0x80) {
3118 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3119 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003120 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003121
Harald Welte03740842009-06-10 23:11:52 +08003122 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003123 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003124
Harald Welte4861c822009-07-23 21:21:14 +02003125 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003126 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003127 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003128 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003129 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3130 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003131
3132 /* Create transaction */
3133 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003134 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003135 "creating new trans.\n", transaction_id);
3136 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003137 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003138 transaction_id, new_callref++);
3139 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003140 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003141 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003142 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003143 GSM48_MT_CC_RELEASE_COMPL);
3144 return -ENOMEM;
3145 }
Harald Welte03740842009-06-10 23:11:52 +08003146 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003147 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003148 }
3149
3150 /* find function for current state and message */
3151 for (i = 0; i < DATASLLEN; i++)
3152 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003153 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003154 break;
3155 if (i == DATASLLEN) {
3156 DEBUGP(DCC, "Message unhandled at this state.\n");
3157 return 0;
3158 }
3159
3160 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003161
3162 return rc;
3163}
3164
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003165/* Create a dummy to wait five seconds */
3166static void release_anchor(struct gsm_subscriber_connection *conn)
3167{
3168 if (!conn->anch_operation)
3169 return;
3170
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003171 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003172 talloc_free(conn->anch_operation);
3173 conn->anch_operation = NULL;
3174}
3175
3176static void anchor_timeout(void *_data)
3177{
3178 struct gsm_subscriber_connection *con = _data;
3179
3180 release_anchor(con);
3181 msc_release_connection(con);
3182}
3183
3184int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3185{
3186 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3187 if (!conn->anch_operation)
3188 return -1;
3189
3190 conn->anch_operation->timeout.data = conn;
3191 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003192 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003193 return 0;
3194}
3195
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003196/* here we get data from the BSC level... */
3197int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003198{
3199 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003200 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003201 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003202
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003203 if (silent_call_reroute(conn, msg))
3204 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003205
3206 switch (pdisc) {
3207 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003208 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003209 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003210 break;
3211 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003212 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003213 break;
3214 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003215 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003216 break;
3217 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003218 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003219 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003220 break;
3221 case GSM48_PDISC_MM_GPRS:
3222 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003223 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3224 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003225 break;
Harald Welte03115042009-10-16 08:32:58 +02003226 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003227 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003228 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003229 break;
Harald Welte59b04682009-06-10 05:40:52 +08003230 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003231 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3232 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003233 break;
3234 }
3235
3236 return rc;
3237}
3238
Harald Welte3c062072009-07-28 18:25:29 +02003239/*
3240 * This will be ran by the linker when loading the DSO. We use it to
3241 * do system initialization, e.g. registration of signal handlers.
3242 */
3243static __attribute__((constructor)) void on_dso_load_0408(void)
3244{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003245 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3246 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003247}