blob: 1f26cdd48dfdfe0f676420513bf498fb3daa0c9d [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-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 Welte52b1f982008-12-23 20:25:15 +000012 * (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 Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-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 Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000064
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020065void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010066void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020067
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020068int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080069static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020070 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010071static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080072static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000073
Harald Welte52b1f982008-12-23 20:25:15 +000074struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020075 uint16_t mcc;
76 uint16_t mnc;
77 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000078};
79
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020080static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080081
Harald Welte31c00f72011-03-03 23:29:05 +010082void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
83{
84 net->mncc_recv(net, msg);
85}
86
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080087static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
88 struct gsm_trans *trans)
89{
90 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
91
92 /* if we get passed a transaction reference, do some common
93 * work that the caller no longer has to do */
94 if (trans) {
95 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
96 msg->lchan = trans->conn->lchan;
97 }
98
99
100 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200101 struct e1inp_sign_link *sign_link =
102 msg->lchan->ts->trx->rsl_link;
103
104 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800105 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
106 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200107 "Sending '%s' to MS.\n",
108 sign_link->trx->bts->nr,
109 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800110 gh->proto_discr & 0xf0,
111 gsm48_cc_msg_name(gh->msg_type));
112 else
113 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200114 "Sending 0x%02x to MS.\n",
115 sign_link->trx->bts->nr,
116 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800117 gh->proto_discr, gh->msg_type);
118 }
119
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100120 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800121}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100122
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800123int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
124{
125 struct gsm48_hdr *gh;
126 struct msgb *ss_notify;
127
128 ss_notify = gsm0480_create_notifySS(message);
129 if (!ss_notify)
130 return -1;
131
132 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
133 uint8_t *data = msgb_push(ss_notify, 1);
134 data[0] = ss_notify->len - 1;
135 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
136 gh->msg_type = GSM48_MT_CC_FACILITY;
137 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
138}
139
Harald Weltecf149ee2012-01-23 16:40:24 +0100140void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100141{
142 if (!conn->sec_operation)
143 return;
144
145 talloc_free(conn->sec_operation);
146 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800147 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100148}
149
Harald Weltecf149ee2012-01-23 16:40:24 +0100150void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100151{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100152 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
153 struct gsm_security_operation);
154}
155
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100156int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
157 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100158{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800159 struct gsm_network *net = conn->bts->network;
160 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100161 struct gsm_security_operation *op;
162 struct gsm_auth_tuple atuple;
163 int status = -1, rc;
164
165 /* Check if we _can_ enable encryption. Cases where we can't:
166 * - Encryption disabled in config
167 * - Channel already secured (nothing to do)
168 * - Subscriber equipment doesn't support configured encryption
169 */
170 if (!net->a5_encryption) {
171 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800172 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173 DEBUGP(DMM, "Requesting to secure an already secure channel");
174 status = GSM_SECURITY_SUCCEEDED;
175 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
176 net->a5_encryption)) {
177 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
178 status = GSM_SECURITY_NOAVAIL;
179 }
180
181 /* If not done yet, try to get info for this user */
182 if (status < 0) {
183 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
184 if (rc <= 0)
185 status = GSM_SECURITY_NOAVAIL;
186 }
187
188 /* Are we done yet ? */
189 if (status >= 0)
190 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800191 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100192 0;
193
194 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800195 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100196 return -EBUSY;
197
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800198 allocate_security_operation(conn);
199 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 op->cb = cb;
201 op->cb_data = cb_data;
202 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
203
204 /* FIXME: Should start a timer for completion ... */
205
206 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100207 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100208 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800209 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100210 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100211 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100212 return gsm0808_cipher_mode(conn, net->a5_encryption,
213 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100214 }
215
216 return -EINVAL; /* not reached */
217}
218
Holger Freyther73487a22008-12-31 18:53:57 +0000219static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
220 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000221{
222 if (!subscriber)
223 return 0;
224
Holger Freyther73487a22008-12-31 18:53:57 +0000225 /*
226 * Do not send accept yet as more information should arrive. Some
227 * phones will not send us the information and we will have to check
228 * what we want to do with that.
229 */
230 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
231 return 0;
232
Jan Luebbe06513f22009-08-12 12:48:00 +0200233 switch (subscriber->net->auth_policy) {
234 case GSM_AUTH_POLICY_CLOSED:
235 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200236 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200237 if (subscriber->authorized)
238 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200239 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200240 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000241 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200242 default:
243 return 0;
244 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000245}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000246
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100247static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000248{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100249 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000250 return;
251
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800252 /* No need to keep the connection up */
253 release_anchor(conn);
254
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200255 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100256 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100257 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800258 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000259}
260
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100261static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000262{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800263 if (conn->loc_operation)
264 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100265 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000266
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100267 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200268 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000269}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000270
Sylvain Munaut267fba02009-12-24 00:28:01 +0100271static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
272 struct msgb *msg, void *data, void *param)
273{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800274 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100275 int rc = 0;
276
277 switch (event) {
278 case GSM_SECURITY_AUTH_FAILED:
279 release_loc_updating_req(conn);
280 break;
281
282 case GSM_SECURITY_NOAVAIL:
283 case GSM_SECURITY_SUCCEEDED:
284 /* We're all good */
285 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800286 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800287 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100288 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800289 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100290 }
291
292 /* call subscr_update after putting the loc_upd_acc
293 * in the transmit queue, since S_SUBSCR_ATTACHED might
294 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800295 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100296 GSM_SUBSCRIBER_UPDATE_ATTACHED);
297
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100298 /*
299 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
300 * MS needs to respond with a TMSI REALLOCATION COMPLETE
301 * (even if the TMSI is the same).
302 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100303 break;
304
305 default:
306 rc = -EINVAL;
307 };
308
309 return rc;
310}
311
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100312static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000313{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100314 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800315 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100316 conn->loc_operation->key_seq,
317 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000318 return 0;
319}
320
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800321void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000322{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800323 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800324
325 /* avoid someone issuing a clear */
326 conn->in_release = 1;
327
Holger Freyther7c19f742009-06-06 13:54:35 +0000328 /*
329 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800330 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000331 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800332 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100333
334 /* We might need to cancel the paging response or such. */
335 if (conn->sec_operation && conn->sec_operation->cb) {
336 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
337 NULL, conn, conn->sec_operation->cb_data);
338 }
339
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800340 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800341 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000342
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100343 /*
344 * Free all transactions that are associated with the released
345 * connection. The transaction code will inform the CC or SMS
346 * facilities that will send the release indications. As part of
347 * the CC REL_IND the remote leg might be released and this will
348 * trigger the call to trans_free. This is something the llist
349 * macro can not handle and we will need to re-iterate the list.
350 *
351 * TODO: Move the trans_list into the subscriber connection and
352 * create a pending list for MT transactions. These exist before
353 * we have a subscriber connection.
354 */
355restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800356 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100357 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200358 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100359 goto restart;
360 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800361 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000362}
363
Harald Welte371efe52010-12-22 23:17:50 +0100364void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
365{
366 struct gsm_trans *trans, *temp;
367
368 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
369
370 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100371 if (trans->protocol == protocol) {
372 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100373 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100374 }
Harald Welte371efe52010-12-22 23:17:50 +0100375 }
376}
377
Holger Freyther429e7762008-12-30 13:28:30 +0000378/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200379int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000380{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800381 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800382 struct msgb *msg;
383
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200384 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800385
386 msg = gsm48_create_loc_upd_rej(cause);
387 if (!msg) {
388 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
389 return -1;
390 }
Harald Welte52b1f982008-12-23 20:25:15 +0000391
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800392 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000393
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100394 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100395 "LAC=%u BTS=%u\n", conn->subscr ?
396 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800397 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100398
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800399 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000400}
401
402/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200403int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000404{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800405 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000406 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000407 struct gsm48_hdr *gh;
408 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200409 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000410
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800411 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000412
413 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
414 gh->proto_discr = GSM48_PDISC_MM;
415 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
416
417 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100418 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000419 bts->network->network_code, bts->location_area_code);
420
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200421 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200422 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000423
424 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
425
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200426 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100427
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800428 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000429}
430
Harald Weltebf5e8df2009-02-03 12:59:45 +0000431/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200432static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000433{
434 struct msgb *msg = gsm48_msgb_alloc();
435 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000436
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800437 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000438
439 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
440 gh->proto_discr = GSM48_PDISC_MM;
441 gh->msg_type = GSM48_MT_MM_ID_REQ;
442 gh->data[0] = id_type;
443
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800444 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000445}
446
Harald Welte231ad4f2008-12-27 11:15:38 +0000447
Harald Weltebf5e8df2009-02-03 12:59:45 +0000448/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800449static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000450{
451 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000452 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200453 struct gsm_bts *bts = lchan->ts->trx->bts;
454 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200456 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000457
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200458 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000459 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000460 mi_type, mi_string);
461
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200462 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100463
Harald Welte75a983f2008-12-27 21:34:06 +0000464 switch (mi_type) {
465 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200466 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100467 if (!conn->subscr) {
468 conn->subscr = subscr_get_by_imsi(net, mi_string);
469 if (!conn->subscr)
470 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200471 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100472 if (conn->loc_operation)
473 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000474 break;
475 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000476 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000477 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100478 if (conn->subscr) {
479 db_subscriber_assoc_imei(conn->subscr, mi_string);
480 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200481 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100482 if (conn->loc_operation)
483 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000484 break;
485 }
Holger Freyther73487a22008-12-31 18:53:57 +0000486
487 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100488 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000489}
490
Harald Welte255539c2008-12-28 02:26:27 +0000491
492static void loc_upd_rej_cb(void *data)
493{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100494 struct gsm_subscriber_connection *conn = data;
495 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100496 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000497
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100498 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800499 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800500 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000501}
502
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100503static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000504{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
506 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200507 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000508}
509
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200510static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000511{
512 switch (type) {
513 case GSM48_LUPD_NORMAL:
514 return "NORMAL";
515 case GSM48_LUPD_PERIODIC:
516 return "PEROIDOC";
517 case GSM48_LUPD_IMSI_ATT:
518 return "IMSI ATTACH";
519 default:
520 return "UNKNOWN";
521 }
522}
523
Harald Weltebf5e8df2009-02-03 12:59:45 +0000524/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800525static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000526{
Harald Welte8470bf22008-12-25 23:28:35 +0000527 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000528 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800529 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800530 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200531 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200532 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000533
Harald Welte8470bf22008-12-25 23:28:35 +0000534 lu = (struct gsm48_loc_upd_req *) gh->data;
535
536 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000537
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200538 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000539
Harald Weltea0368542009-06-27 02:58:43 +0200540 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000541 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000542
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200543 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100544
Harald Welte24ff6ee2009-12-22 00:41:05 +0100545 switch (lu->type) {
546 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200547 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100548 break;
549 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200550 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100551 break;
552 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200553 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100554 break;
555 }
556
Holger Freythereaf04692009-06-06 13:54:44 +0000557 /*
558 * Pseudo Spoof detection: Just drop a second/concurrent
559 * location updating request.
560 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200562 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100563 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800564 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000565 return 0;
566 }
567
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800568 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000569
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200570 conn->loc_operation->key_seq = lu->key_seq;
571
Harald Welte52b1f982008-12-23 20:25:15 +0000572 switch (mi_type) {
573 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200574 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000575 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200576 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100577 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000578
Jan Luebbe370b41d2009-08-12 10:19:34 +0200579 /* look up subscriber based on IMSI, create if not found */
580 subscr = subscr_get_by_imsi(bts->network, mi_string);
581 if (!subscr) {
582 subscr = db_create_subscriber(bts->network, mi_string);
583 }
Harald Welte4b634542008-12-27 01:55:51 +0000584 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000585 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200586 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000587 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200588 subscr = subscr_get_by_tmsi(bts->network,
589 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000590 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000591 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200592 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100593 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000594 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200595 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200596 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200597 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000598 break;
599 case GSM_MI_TYPE_IMEI:
600 case GSM_MI_TYPE_IMEISV:
601 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200602 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000603 break;
604 default:
Harald Weltea0368542009-06-27 02:58:43 +0200605 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000606 break;
607 }
608
Harald Welte24516ea2009-07-04 10:18:00 +0200609 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100610 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200611
Harald Welte4bfdfe72009-06-10 23:11:52 +0800612 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200613 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800614 /* FIXME: request id? close channel? */
615 return -EINVAL;
616 }
617
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100618 conn->subscr = subscr;
619 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000620
Harald Welte24516ea2009-07-04 10:18:00 +0200621 /* check if we can let the subscriber into our network immediately
622 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100623 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000624}
625
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100626/* Turn int into semi-octet representation: 98 => 0x89 */
627static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800628{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100629 uint8_t ret;
630
631 ret = value / 10;
632 ret |= (value % 10) << 4;
633
634 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100636
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637
Harald Weltedb253af2008-12-30 17:56:55 +0000638/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800639int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000640{
641 struct msgb *msg = gsm48_msgb_alloc();
642 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800643 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100644 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200645 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200646 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100647
Harald Welte4bfdfe72009-06-10 23:11:52 +0800648 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100649 struct tm* gmt_time;
650 struct tm* local_time;
651 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000652
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800653 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000654
655 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
656 gh->proto_discr = GSM48_PDISC_MM;
657 gh->msg_type = GSM48_MT_MM_INFO;
658
659 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200660#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000661 name_len = strlen(net->name_long);
662 /* 10.5.3.5a */
663 ptr8 = msgb_put(msg, 3);
664 ptr8[0] = GSM48_IE_NAME_LONG;
665 ptr8[1] = name_len*2 +1;
666 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
667
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200668 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000669 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000670 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000671
672 /* FIXME: Use Cell Broadcast, not UCS-2, since
673 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200674#endif
675 name_len = (strlen(net->name_long)*7)/8;
676 name_pad = (8 - strlen(net->name_long)*7)%8;
677 if (name_pad > 0)
678 name_len++;
679 /* 10.5.3.5a */
680 ptr8 = msgb_put(msg, 3);
681 ptr8[0] = GSM48_IE_NAME_LONG;
682 ptr8[1] = name_len +1;
683 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
684
685 ptr8 = msgb_put(msg, name_len);
686 gsm_7bit_encode(ptr8, net->name_long);
687
Harald Weltedb253af2008-12-30 17:56:55 +0000688 }
689
690 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200691#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000692 name_len = strlen(net->name_short);
693 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200694 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200695 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000696 ptr8[1] = name_len*2 + 1;
697 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
698
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200699 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000700 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000701 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200702#endif
703 name_len = (strlen(net->name_short)*7)/8;
704 name_pad = (8 - strlen(net->name_short)*7)%8;
705 if (name_pad > 0)
706 name_len++;
707 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200708 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200709 ptr8[0] = GSM48_IE_NAME_SHORT;
710 ptr8[1] = name_len +1;
711 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
712
713 ptr8 = msgb_put(msg, name_len);
714 gsm_7bit_encode(ptr8, net->name_short);
715
Harald Weltedb253af2008-12-30 17:56:55 +0000716 }
717
Harald Weltedb253af2008-12-30 17:56:55 +0000718 /* Section 10.5.3.9 */
719 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100720 gmt_time = gmtime(&cur_t);
721
Harald Weltedb253af2008-12-30 17:56:55 +0000722 ptr8 = msgb_put(msg, 8);
723 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100724 ptr8[1] = bcdify(gmt_time->tm_year % 100);
725 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
726 ptr8[3] = bcdify(gmt_time->tm_mday);
727 ptr8[4] = bcdify(gmt_time->tm_hour);
728 ptr8[5] = bcdify(gmt_time->tm_min);
729 ptr8[6] = bcdify(gmt_time->tm_sec);
730
Harald Welte45f91712012-07-08 16:48:11 +0200731 if (bts->tz.override) {
732 /* Convert tz.hr and tz.mn to units */
733 if (bts->tz.hr < 0) {
734 tzunits = ((bts->tz.hr/-1)*4);
735 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100736 ptr8[7] = bcdify(tzunits);
737 /* Set negative time */
738 ptr8[7] |= 0x08;
739 }
740 else {
Harald Welte45f91712012-07-08 16:48:11 +0200741 tzunits = bts->tz.hr*4;
742 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100743 ptr8[7] = bcdify(tzunits);
744 }
745 }
746 else {
747 /* Need to get GSM offset and convert into 15 min units */
748 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
749 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200750#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100751 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200752#else
753#warning find a portable way to obtain the timezone offset
754 tzunits = 0;
755#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100756 if (tzunits < 0) {
757 tzunits = tzunits/-1;
758 ptr8[7] = bcdify(tzunits);
759 /* Flip it to negative */
760 ptr8[7] |= 0x08;
761 }
762 else
763 ptr8[7] = bcdify(tzunits);
764 }
Harald Weltedb253af2008-12-30 17:56:55 +0000765
Daniel Willmanneea93372009-08-13 03:42:07 +0200766 DEBUGP(DMM, "-> MM INFO\n");
767
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800768 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000769}
770
Harald Welte7984d5c2009-08-12 22:56:50 +0200771/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200772int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200773{
774 struct msgb *msg = gsm48_msgb_alloc();
775 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200776 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200777
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200778 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200779
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800780 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200781 gh->proto_discr = GSM48_PDISC_MM;
782 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
783
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100784 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200785
Harald Welte7984d5c2009-08-12 22:56:50 +0200786 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200787 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200788 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200789
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800790 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200791}
792
793/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800794int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200795{
796 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800797 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200798}
799
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800800static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000801{
Harald Welte4b634542008-12-27 01:55:51 +0000802 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800803 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000804}
Harald Welteba4cf162009-01-10 01:49:35 +0000805
806/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100807static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000808 enum gsm48_reject_value value)
809{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800810 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000811
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800812 msg = gsm48_create_mm_serv_rej(value);
813 if (!msg) {
814 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
815 return -1;
816 }
Harald Welteba4cf162009-01-10 01:49:35 +0000817
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800818 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100819 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800820 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000821}
822
Sylvain Munautba87f452009-12-24 00:28:46 +0100823static int _gsm48_rx_mm_serv_req_sec_cb(
824 unsigned int hooknum, unsigned int event,
825 struct msgb *msg, void *data, void *param)
826{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800827 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100828 int rc = 0;
829
830 switch (event) {
831 case GSM_SECURITY_AUTH_FAILED:
832 /* Nothing to do */
833 break;
834
835 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800836 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100837 break;
838
839 case GSM_SECURITY_SUCCEEDED:
840 /* nothing to do. CIPHER MODE COMMAND is
841 * implicit CM SERV ACK */
842 break;
843
844 default:
845 rc = -EINVAL;
846 };
847
848 return rc;
849}
850
Harald Welte4ed0e922009-01-10 03:17:30 +0000851/*
852 * Handle CM Service Requests
853 * a) Verify that the packet is long enough to contain the information
854 * we require otherwsie reject with INCORRECT_MESSAGE
855 * b) Try to parse the TMSI. If we do not have one reject
856 * c) Check that we know the subscriber with the TMSI otherwise reject
857 * with a HLR cause
858 * d) Set the subscriber on the gsm_lchan and accept
859 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800860static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000861{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200862 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200863 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000864
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800865 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000866 struct gsm_subscriber *subscr;
867 struct gsm48_hdr *gh = msgb_l3(msg);
868 struct gsm48_service_request *req =
869 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800870 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200871 uint8_t classmark2_len = gh->data[1];
872 uint8_t *classmark2 = gh->data+2;
873 uint8_t mi_len = *(classmark2 + classmark2_len);
874 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000875
Harald Weltec9e02182009-05-01 19:07:53 +0000876 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000877 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000878 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800879 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000880 GSM48_REJECT_INCORRECT_MESSAGE);
881 }
882
883 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000884 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800885 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000886 GSM48_REJECT_INCORRECT_MESSAGE);
887 }
888
Harald Weltec9e02182009-05-01 19:07:53 +0000889 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000890 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000891 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800892 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000893 GSM48_REJECT_INCORRECT_MESSAGE);
894 }
895
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200896 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000897 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000898 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000899
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200900 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100901
Harald Welte3ac7f102009-08-10 10:12:45 +0200902 if (is_siemens_bts(bts))
903 send_siemens_mrpci(msg->lchan, classmark2-1);
904
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200905 subscr = subscr_get_by_tmsi(bts->network,
906 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000907
Harald Welte2a139372009-02-22 21:14:55 +0000908 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000909 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800910 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000911 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freyther4d2a68c2013-01-01 17:18:27 +0100912 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munaut11c1b6e2012-12-28 00:33:04 +0100913 /* If the subscriber is not attached, reject service */
914 return gsm48_tx_mm_serv_rej(conn,
915 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +0000916
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800917 if (!conn->subscr)
918 conn->subscr = subscr;
919 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100920 subscr_put(subscr); /* lchan already has a ref, don't need another one */
921 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000922 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
923 subscr_put(subscr);
924 }
925
Harald Weltec2e302d2009-07-05 14:08:13 +0200926 subscr->equipment.classmark2_len = classmark2_len;
927 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
928 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000929
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800930 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100931 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000932}
933
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200934static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000935{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200936 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000937 struct gsm48_hdr *gh = msgb_l3(msg);
938 struct gsm48_imsi_detach_ind *idi =
939 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200940 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200941 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800942 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000943
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200944 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100945 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000946 mi_type, mi_string);
947
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200948 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100949
Harald Welte2a139372009-02-22 21:14:55 +0000950 switch (mi_type) {
951 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100952 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200953 subscr = subscr_get_by_tmsi(bts->network,
954 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000955 break;
956 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100957 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200958 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000959 break;
960 case GSM_MI_TYPE_IMEI:
961 case GSM_MI_TYPE_IMEISV:
962 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100963 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000964 break;
965 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100966 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000967 break;
968 }
969
Holger Freyther4a49e772009-04-12 05:37:29 +0000970 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200971 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000972 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100973 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200974
975 subscr->equipment.classmark1 = idi->classmark1;
976 db_sync_equipment(&subscr->equipment);
977
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000978 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000979 } else
Harald Welte2a139372009-02-22 21:14:55 +0000980 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
981
Harald Welte31981a02009-12-20 09:58:40 +0100982 /* FIXME: iterate over all transactions and release them,
983 * imagine an IMSI DETACH happening during an active call! */
984
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200985 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000986 return 0;
987}
988
Harald Welted2a7f5a2009-06-05 20:08:20 +0000989static int gsm48_rx_mm_status(struct msgb *msg)
990{
991 struct gsm48_hdr *gh = msgb_l3(msg);
992
993 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
994
995 return 0;
996}
997
Sylvain Munaut30a15382009-12-24 00:27:26 +0100998/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800999static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001000{
1001 struct gsm48_hdr *gh = msgb_l3(msg);
1002 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001003 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001004
Sylvain Munautc593cf12010-06-10 23:51:41 +02001005 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001006 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001007
1008 /* Safety check */
1009 if (!conn->sec_operation) {
1010 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1011 return -EIO;
1012 }
1013
1014 /* Validate SRES */
1015 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001016 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001017 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001018
1019 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001020 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001021
Sylvain Munaut30a15382009-12-24 00:27:26 +01001022 if (cb)
1023 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001024 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001025
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001026 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001027 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001028 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001029 }
1030
Sylvain Munautc593cf12010-06-10 23:51:41 +02001031 DEBUGPC(DMM, "OK\n");
1032
Sylvain Munaut30a15382009-12-24 00:27:26 +01001033 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001034 return gsm0808_cipher_mode(conn, net->a5_encryption,
1035 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001036}
1037
Harald Weltebf5e8df2009-02-03 12:59:45 +00001038/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001039static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001040{
1041 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001042 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001043
1044 switch (gh->msg_type & 0xbf) {
1045 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001046 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001047 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001048 break;
1049 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001050 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001051 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001052 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001053 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001054 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001055 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001056 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001057 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001059 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001060 conn->subscr ?
1061 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001062 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001063 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001064 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001065 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001066 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001067 break;
1068 case GSM48_MT_MM_CM_REEST_REQ:
1069 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1070 break;
1071 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001072 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001073 break;
1074 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001075 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001076 gh->msg_type);
1077 break;
1078 }
1079
1080 return rc;
1081}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001082
Harald Welte2d35ae62009-02-06 12:02:13 +00001083/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001084static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001085{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001086 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001087 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001088 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001089 uint8_t *classmark2_lv = gh->data + 1;
1090 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001091 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001092 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001093 int rc = 0;
1094
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001095 resp = (struct gsm48_pag_resp *) &gh->data[0];
1096 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1097 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001098 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1099 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001100
Harald Weltefe18d8f2009-02-22 21:14:24 +00001101 switch (mi_type) {
1102 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001103 subscr = subscr_get_by_tmsi(bts->network,
1104 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001105 break;
1106 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001107 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001108 break;
1109 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001110
1111 if (!subscr) {
1112 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001113 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001114 return -EINVAL;
1115 }
1116 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001117 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001118
Harald Weltec2e302d2009-07-05 14:08:13 +02001119 subscr->equipment.classmark2_len = *classmark2_lv;
1120 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1121 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001122
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001123 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001124 return rc;
1125}
1126
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001127static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001128{
1129 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001130 uint8_t apdu_id_flags;
1131 uint8_t apdu_len;
1132 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001133
1134 apdu_id_flags = gh->data[0];
1135 apdu_len = gh->data[1];
1136 apdu_data = gh->data+2;
1137
Harald Welte70f92052012-07-16 13:22:19 +02001138 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001139 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001141 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001142}
1143
Harald Weltebf5e8df2009-02-03 12:59:45 +00001144/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001146{
1147 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001148 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001149
1150 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001151 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001152 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001153 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001154 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001155 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001156 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001157 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001158 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001159 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001160 break;
1161 }
1162
Harald Welte2d35ae62009-02-06 12:02:13 +00001163 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001164}
1165
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001166int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1167 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001168{
1169 struct msgb *msg = gsm48_msgb_alloc();
1170 struct gsm48_hdr *gh;
1171
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001172 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001173
1174 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1175 apdu_id, apdu_len);
1176
1177 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1178 gh->proto_discr = GSM48_PDISC_RR;
1179 gh->msg_type = GSM48_MT_RR_APP_INFO;
1180 gh->data[0] = apdu_id;
1181 gh->data[1] = apdu_len;
1182 memcpy(gh->data+2, apdu, apdu_len);
1183
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001184 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001185}
1186
Harald Welte4bc90a12008-12-27 16:32:52 +00001187/* Call Control */
1188
Harald Welte7584aea2009-02-11 11:44:12 +00001189/* The entire call control code is written in accordance with Figure 7.10c
1190 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1191 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1192 * it for voice */
1193
Harald Welte4bfdfe72009-06-10 23:11:52 +08001194static void new_cc_state(struct gsm_trans *trans, int state)
1195{
1196 if (state > 31 || state < 0)
1197 return;
1198
1199 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001200 gsm48_cc_state_name(trans->cc.state),
1201 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001202
Harald Weltedcaf5652009-07-23 18:56:43 +02001203 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001204}
1205
1206static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001207{
1208 struct msgb *msg = gsm48_msgb_alloc();
1209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001210 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001211
Harald Welte4bc90a12008-12-27 16:32:52 +00001212 gh->msg_type = GSM48_MT_CC_STATUS;
1213
1214 cause = msgb_put(msg, 3);
1215 cause[0] = 2;
1216 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1217 cause[2] = 0x80 | 30; /* response to status inquiry */
1218
1219 call_state = msgb_put(msg, 1);
1220 call_state[0] = 0xc0 | 0x00;
1221
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001222 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001223}
1224
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001225static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001226 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001227{
1228 struct msgb *msg = gsm48_msgb_alloc();
1229 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1230
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001231 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001232
Harald Welte6f4b7532008-12-29 00:39:37 +00001233 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001234 gh->msg_type = msg_type;
1235
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001236 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001237}
1238
Harald Welte4bfdfe72009-06-10 23:11:52 +08001239static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1240{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001241 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001242 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001243 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001244 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001245 }
1246}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001247
Harald Welte4bfdfe72009-06-10 23:11:52 +08001248static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1249 int msg_type, struct gsm_mncc *mncc)
1250{
1251 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001252 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001253
1254 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001255 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001256 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001257 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001258 trans->conn->lchan->ts->trx->bts->nr,
1259 trans->conn->lchan->ts->trx->nr,
1260 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001261 (trans->subscr)?(trans->subscr->extension):"-",
1262 get_mncc_name(msg_type));
1263 else
1264 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1265 "Sending '%s' to MNCC.\n",
1266 (trans->subscr)?(trans->subscr->extension):"-",
1267 get_mncc_name(msg_type));
1268 else
1269 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1270 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1271
1272 mncc->msg_type = msg_type;
1273
Harald Welte966636f2009-06-26 19:39:35 +02001274 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275 if (!msg)
1276 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001277
1278 data = msgb_put(msg, sizeof(struct gsm_mncc));
1279 memcpy(data, mncc, sizeof(struct gsm_mncc));
1280
Harald Welte54209c22010-12-22 23:43:29 +01001281 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282
1283 return 0;
1284}
1285
1286int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001287 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001288{
1289 struct gsm_mncc rel;
1290
Harald Welte92f70c52009-06-12 01:54:08 +08001291 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001293 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1295}
1296
Harald Weltedcaf5652009-07-23 18:56:43 +02001297/* Call Control Specific transaction release.
1298 * gets called by trans_free, DO NOT CALL YOURSELF! */
1299void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301 gsm48_stop_cc_timer(trans);
1302
1303 /* send release to L4, if callref still exists */
1304 if (trans->callref) {
1305 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001306 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001307 GSM48_CAUSE_LOC_PRN_S_LU,
1308 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001310 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001312 if (trans->conn)
1313 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314}
1315
1316static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001317
Harald Welte09e38af2009-02-16 22:52:23 +00001318/* call-back from paging the B-end of the connection */
1319static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001320 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001321{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001322 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001323 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001324 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001325 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001326
Harald Welte09e38af2009-02-16 22:52:23 +00001327 if (hooknum != GSM_HOOK_RR_PAGING)
1328 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001329
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001330 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001331 if (!net) {
1332 DEBUGP(DCC, "Error Network not set!\n");
1333 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001334 }
Harald Welte7584aea2009-02-11 11:44:12 +00001335
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336 /* check all tranactions (without lchan) for subscriber */
1337 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001338 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 continue;
1340 switch (event) {
1341 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001342 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343 break;
1344 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001345 transt->subscr->extension);
1346 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001348 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001349 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001350 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001351 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 }
1353 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001354 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 break;
1356 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001357 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001359 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001361 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001362 mncc_release_ind(transt->subscr->net, transt,
1363 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001364 GSM48_CAUSE_LOC_PRN_S_LU,
1365 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001367 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001368 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 break;
1370 }
1371 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001372
1373 talloc_free(paging_request);
1374
1375 /*
1376 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1377 * failure and then the subscr will be poke again. This needs a lot of fixing
1378 * in the subscriber queue code.
1379 */
1380 if (!found && conn)
1381 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001382 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001383}
Harald Welte7584aea2009-02-11 11:44:12 +00001384
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001385static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001386
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001387/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001388static int switch_for_handover(struct gsm_lchan *old_lchan,
1389 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001390{
1391 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001392
1393 if (ipacc_rtp_direct) {
1394 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1395 return 0;
1396 }
1397
1398 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001399 new_rs = new_lchan->abis_ip.rtp_socket;
1400 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001401
1402 if (!new_rs) {
1403 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1404 return -EIO;
1405 }
1406
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001407 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001408
1409 if (!old_rs) {
1410 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1411 return -EIO;
1412 }
1413
1414 /* copy rx_action and reference to other sock */
1415 new_rs->rx_action = old_rs->rx_action;
1416 new_rs->tx_action = old_rs->tx_action;
1417 new_rs->transmit = old_rs->transmit;
1418
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001419 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001420 case RTP_PROXY:
1421 other_rs = old_rs->proxy.other_sock;
1422 rtp_socket_proxy(new_rs, other_rs);
1423 /* delete reference to other end socket to prevent
1424 * rtp_socket_free() from removing the inverse reference */
1425 old_rs->proxy.other_sock = NULL;
1426 break;
1427 case RTP_RECV_UPSTREAM:
1428 new_rs->receive = old_rs->receive;
1429 break;
1430 case RTP_NONE:
1431 break;
1432 }
1433
1434 return 0;
1435}
1436
Harald Welte805f6442009-07-28 18:25:29 +02001437/* some other part of the code sends us a signal */
1438static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1439 void *handler_data, void *signal_data)
1440{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001441 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001442 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001443 struct gsm_network *net;
1444 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001445
1446 if (subsys != SS_ABISIP)
1447 return 0;
1448
1449 /* in case we use direct BTS-to-BTS RTP */
1450 if (ipacc_rtp_direct)
1451 return 0;
1452
Harald Welte805f6442009-07-28 18:25:29 +02001453 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001454 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001455 /* in case we don't use direct BTS-to-BTS RTP */
1456 /* the BTS has successfully bound a TCH to a local ip/port,
1457 * which means we can connect our UDP socket to it */
1458 if (lchan->abis_ip.rtp_socket) {
1459 rtp_socket_free(lchan->abis_ip.rtp_socket);
1460 lchan->abis_ip.rtp_socket = NULL;
1461 }
1462
1463 lchan->abis_ip.rtp_socket = rtp_socket_create();
1464 if (!lchan->abis_ip.rtp_socket)
1465 return -EIO;
1466
1467 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1468 lchan->abis_ip.bound_ip,
1469 lchan->abis_ip.bound_port);
1470 if (rc < 0)
1471 return -EIO;
1472
Harald Welteda7ab742009-12-19 22:23:05 +01001473 /* check if any transactions on this lchan still have
1474 * a tch_recv_mncc request pending */
1475 net = lchan->ts->trx->bts->network;
1476 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001477 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001478 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1479 tch_recv_mncc(net, trans->callref, 1);
1480 }
1481 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001482
1483 /*
1484 * TODO: this appears to be too early? Why not until after
1485 * the handover detect or the handover complete?
1486 *
1487 * Do we have a handover pending for this new lchan? In that
1488 * case re-route the audio from the old channel to the new one.
1489 */
1490 old_lchan = bsc_handover_pending(lchan);
1491 if (old_lchan)
1492 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001493 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001494 case S_ABISIP_DLCX_IND:
1495 /* the BTS tells us a RTP stream has been disconnected */
1496 if (lchan->abis_ip.rtp_socket) {
1497 rtp_socket_free(lchan->abis_ip.rtp_socket);
1498 lchan->abis_ip.rtp_socket = NULL;
1499 }
1500
1501 break;
Harald Welte805f6442009-07-28 18:25:29 +02001502 }
1503
1504 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001505}
1506
Harald Welte49f48b82009-02-17 15:29:33 +00001507/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001508static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001509{
Harald Welte11fa29c2009-02-19 17:24:39 +00001510 struct gsm_bts *bts = lchan->ts->trx->bts;
1511 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001512 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001513
Harald Welte11fa29c2009-02-19 17:24:39 +00001514 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1515 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1516 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1517
1518 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001519 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001520 return -EINVAL;
1521 }
Harald Welteda7ab742009-12-19 22:23:05 +01001522
1523 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001524 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001525 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001526 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001527 if (!ipacc_rtp_direct) {
1528 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001529 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001530 if (rc < 0)
1531 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001532 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001533 if (rc < 0)
1534 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001535 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301536 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1537 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001538 } else {
1539 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301540 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1541 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301542 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001543 if (rc < 0)
1544 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301545 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1546 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301547 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001548 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001549 break;
1550 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001551 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001552 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001553 trau_mux_map_lchan(lchan, remote_lchan);
1554 break;
1555 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001556 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001557 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001558 }
Harald Welte49f48b82009-02-17 15:29:33 +00001559
1560 return 0;
1561}
1562
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001564static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001565{
Harald Weltedcaf5652009-07-23 18:56:43 +02001566 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1567 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001568
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001570 return -EIO;
1571
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001572 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 return -EIO;
1574
1575 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001576 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001577}
1578
Harald Welteda7ab742009-12-19 22:23:05 +01001579/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001580static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581{
1582 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001583 struct gsm_lchan *lchan;
1584 struct gsm_bts *bts;
1585 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001586
Harald Welte4bfdfe72009-06-10 23:11:52 +08001587 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001588 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 if (!trans)
1590 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001591 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001593 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001594 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001595
Harald Welteda7ab742009-12-19 22:23:05 +01001596 switch (bts->type) {
1597 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001598 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001599 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001600 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001601 return -EINVAL;
1602 }
1603 /* in case, we don't have a RTP socket yet, we note this
1604 * in the transaction and try later */
1605 if (!lchan->abis_ip.rtp_socket) {
1606 trans->tch_recv = enable;
1607 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1608 return 0;
1609 }
1610 if (enable) {
1611 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001612 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001613 if (rc < 0)
1614 return rc;
1615 /* assign socket to application interface */
1616 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1617 net, callref);
1618 } else
1619 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1620 net, 0);
1621 break;
1622 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001623 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001624 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001625 if (enable)
1626 return trau_recv_lchan(lchan, callref);
1627 return trau_mux_unmap(NULL, callref);
1628 break;
1629 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001630 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001631 return -EINVAL;
1632 }
1633
1634 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001635}
1636
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1638{
1639 DEBUGP(DCC, "-> STATUS ENQ\n");
1640 return gsm48_cc_tx_status(trans, msg);
1641}
1642
1643static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1644static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1645
1646static void gsm48_cc_timeout(void *arg)
1647{
1648 struct gsm_trans *trans = arg;
1649 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001650 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1651 int mo_location = GSM48_CAUSE_LOC_USER;
1652 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1653 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 struct gsm_mncc mo_rel, l4_rel;
1655
1656 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1657 mo_rel.callref = trans->callref;
1658 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1659 l4_rel.callref = trans->callref;
1660
Harald Weltedcaf5652009-07-23 18:56:43 +02001661 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 case 0x303:
1663 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001664 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 break;
1666 case 0x310:
1667 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001668 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 break;
1670 case 0x313:
1671 disconnect = 1;
1672 /* unknown, did not find it in the specs */
1673 break;
1674 case 0x301:
1675 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001676 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 break;
1678 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001679 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001681 gsm48_cc_tx_release(trans, &trans->cc.msg);
1682 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 break; /* stay in release state */
1684 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001685 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 return;
1687// release = 1;
1688// l4_cause = 14;
1689// break;
1690 case 0x306:
1691 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001692 mo_cause = trans->cc.msg.cause.value;
1693 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 break;
1695 case 0x323:
1696 disconnect = 1;
1697 break;
1698 default:
1699 release = 1;
1700 }
1701
1702 if (release && trans->callref) {
1703 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001704 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 l4_location, l4_cause);
1706 trans->callref = 0;
1707 }
1708
1709 if (disconnect && trans->callref) {
1710 /* process disconnect towards layer 4 */
1711 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001712 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001713 }
1714
1715 /* process disconnect towards mobile station */
1716 if (disconnect || release) {
1717 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001718 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1719 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1720 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 mo_rel.cause.diag_len = 3;
1722
1723 if (disconnect)
1724 gsm48_cc_tx_disconnect(trans, &mo_rel);
1725 if (release)
1726 gsm48_cc_tx_release(trans, &mo_rel);
1727 }
1728
1729}
1730
1731static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1732 int sec, int micro)
1733{
1734 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001735 trans->cc.timer.cb = gsm48_cc_timeout;
1736 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001737 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001738 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739}
1740
1741static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1742{
1743 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001744 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1746 struct tlv_parsed tp;
1747 struct gsm_mncc setup;
1748
1749 memset(&setup, 0, sizeof(struct gsm_mncc));
1750 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001751 if (trans->conn && trans->conn->lchan)
1752 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001753 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 /* emergency setup is identified by msg_type */
1755 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1756 setup.emergency = 1;
1757
1758 /* use subscriber as calling party number */
1759 if (trans->subscr) {
1760 setup.fields |= MNCC_F_CALLING;
1761 strncpy(setup.calling.number, trans->subscr->extension,
1762 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001763 strncpy(setup.imsi, trans->subscr->imsi,
1764 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 }
1766 /* bearer capability */
1767 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1768 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001769 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1771 }
1772 /* facility */
1773 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1774 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001775 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1777 }
1778 /* called party bcd number */
1779 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1780 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1783 }
1784 /* user-user */
1785 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1786 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001787 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1789 }
1790 /* ss-version */
1791 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1792 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001793 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1795 }
1796 /* CLIR suppression */
1797 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1798 setup.clir.sup = 1;
1799 /* CLIR invocation */
1800 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1801 setup.clir.inv = 1;
1802 /* cc cap */
1803 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1804 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001805 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1807 }
1808
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 new_cc_state(trans, GSM_CSTATE_INITIATED);
1810
Harald Welte (local)47df3992009-12-26 19:45:03 +01001811 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1812 subscr_name(trans->subscr), trans->subscr->extension,
1813 setup.called.number);
1814
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001815 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001816
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001818 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819
Harald Welte13cac662009-07-29 12:10:35 +02001820 /* MNCC code will modify the channel asynchronously, we should
1821 * ipaccess-bind only after the modification has been made to the
1822 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 return 0;
1824}
1825
1826static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001827{
1828 struct msgb *msg = gsm48_msgb_alloc();
1829 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001831 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001832
Harald Welte7ccf7782009-02-17 01:43:01 +00001833 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001834
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 /* transaction id must not be assigned */
1836 if (trans->transaction_id != 0xff) { /* unasssigned */
1837 DEBUGP(DCC, "TX Setup with assigned transaction. "
1838 "This is not allowed!\n");
1839 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001840 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001841 GSM48_CAUSE_LOC_PRN_S_LU,
1842 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001844 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 return rc;
1846 }
1847
1848 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001849 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1850 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001852 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001853 GSM48_CAUSE_LOC_PRN_S_LU,
1854 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001856 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857 return rc;
1858 }
Harald Welte78283ef2009-07-23 21:36:44 +02001859 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001860
Harald Welte65e74cc2008-12-29 01:55:35 +00001861 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001862
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001864
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 /* bearer capability */
1866 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 /* facility */
1869 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001870 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 /* progress */
1872 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 /* calling party BCD number */
1875 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001876 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 /* called party BCD number */
1878 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001879 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 /* user-user */
1881 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001882 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 /* redirecting party BCD number */
1884 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 /* signal */
1887 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001888 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889
1890 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001891
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001892 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001893
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001894 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001895}
1896
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1898{
1899 struct gsm48_hdr *gh = msgb_l3(msg);
1900 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1901 struct tlv_parsed tp;
1902 struct gsm_mncc call_conf;
1903
1904 gsm48_stop_cc_timer(trans);
1905 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1906
1907 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1908 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001909 if (trans->conn && trans->conn->lchan)
1910 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001911 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912#if 0
1913 /* repeat */
1914 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1915 call_conf.repeat = 1;
1916 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1917 call_conf.repeat = 2;
1918#endif
1919 /* bearer capability */
1920 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1921 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001922 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1924 }
1925 /* cause */
1926 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1927 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001928 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1930 }
1931 /* cc cap */
1932 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1933 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001934 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1936 }
1937
1938 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1939
Harald Welte596fed42009-07-23 19:06:52 +02001940 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1941 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942}
1943
1944static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1945{
1946 struct gsm_mncc *proceeding = arg;
1947 struct msgb *msg = gsm48_msgb_alloc();
1948 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1949
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1951
1952 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1953
1954 /* bearer capability */
1955 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* facility */
1958 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* progress */
1961 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001964 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965}
1966
1967static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1968{
1969 struct gsm48_hdr *gh = msgb_l3(msg);
1970 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1971 struct tlv_parsed tp;
1972 struct gsm_mncc alerting;
1973
1974 gsm48_stop_cc_timer(trans);
1975 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1976
1977 memset(&alerting, 0, sizeof(struct gsm_mncc));
1978 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001979 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 /* facility */
1981 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1982 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001983 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1985 }
1986
1987 /* progress */
1988 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1989 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1992 }
1993 /* ss-version */
1994 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1995 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001996 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1998 }
1999
2000 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2001
Harald Welte596fed42009-07-23 19:06:52 +02002002 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2003 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004}
2005
2006static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2007{
2008 struct gsm_mncc *alerting = arg;
2009 struct msgb *msg = gsm48_msgb_alloc();
2010 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2011
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012 gh->msg_type = GSM48_MT_CC_ALERTING;
2013
2014 /* facility */
2015 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002016 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017 /* progress */
2018 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002019 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 /* user-user */
2021 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002022 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023
2024 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2025
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002026 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027}
2028
2029static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2030{
2031 struct gsm_mncc *progress = arg;
2032 struct msgb *msg = gsm48_msgb_alloc();
2033 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2034
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 gh->msg_type = GSM48_MT_CC_PROGRESS;
2036
2037 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002038 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 /* user-user */
2040 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002041 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002043 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044}
2045
2046static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2047{
2048 struct gsm_mncc *connect = arg;
2049 struct msgb *msg = gsm48_msgb_alloc();
2050 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2051
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 gh->msg_type = GSM48_MT_CC_CONNECT;
2053
2054 gsm48_stop_cc_timer(trans);
2055 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2056
2057 /* facility */
2058 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002059 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 /* progress */
2061 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002062 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 /* connected number */
2064 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002065 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 /* user-user */
2067 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069
2070 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2071
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002072 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073}
2074
2075static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2076{
2077 struct gsm48_hdr *gh = msgb_l3(msg);
2078 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2079 struct tlv_parsed tp;
2080 struct gsm_mncc connect;
2081
2082 gsm48_stop_cc_timer(trans);
2083
2084 memset(&connect, 0, sizeof(struct gsm_mncc));
2085 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002086 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 /* use subscriber as connected party number */
2088 if (trans->subscr) {
2089 connect.fields |= MNCC_F_CONNECTED;
2090 strncpy(connect.connected.number, trans->subscr->extension,
2091 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002092 strncpy(connect.imsi, trans->subscr->imsi,
2093 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 }
2095 /* facility */
2096 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2097 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002098 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2100 }
2101 /* user-user */
2102 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2103 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2106 }
2107 /* ss-version */
2108 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2109 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2112 }
2113
2114 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002115 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116
Harald Welte596fed42009-07-23 19:06:52 +02002117 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118}
2119
2120
2121static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2122{
2123 struct gsm_mncc connect_ack;
2124
2125 gsm48_stop_cc_timer(trans);
2126
2127 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002128 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129
2130 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2131 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002132
Harald Welte596fed42009-07-23 19:06:52 +02002133 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 &connect_ack);
2135}
2136
2137static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2138{
2139 struct msgb *msg = gsm48_msgb_alloc();
2140 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2141
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2143
2144 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2145
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002146 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147}
2148
2149static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2150{
2151 struct gsm48_hdr *gh = msgb_l3(msg);
2152 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2153 struct tlv_parsed tp;
2154 struct gsm_mncc disc;
2155
2156 gsm48_stop_cc_timer(trans);
2157
2158 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2159
2160 memset(&disc, 0, sizeof(struct gsm_mncc));
2161 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002162 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 /* cause */
2164 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2165 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2168 }
2169 /* facility */
2170 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2171 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2174 }
2175 /* user-user */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2177 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2180 }
2181 /* ss-version */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2183 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002184 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2186 }
2187
Harald Welte596fed42009-07-23 19:06:52 +02002188 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189
2190}
2191
Harald Weltec66b71c2009-06-11 14:23:20 +08002192static struct gsm_mncc_cause default_cause = {
2193 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2194 .coding = 0,
2195 .rec = 0,
2196 .rec_val = 0,
2197 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2198 .diag_len = 0,
2199 .diag = { 0 },
2200};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201
2202static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2203{
2204 struct gsm_mncc *disc = arg;
2205 struct msgb *msg = gsm48_msgb_alloc();
2206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2207
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2209
2210 gsm48_stop_cc_timer(trans);
2211 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2212
2213 /* cause */
2214 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002215 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 else
Harald Welte55c8f352010-03-07 23:40:35 +01002217 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218
2219 /* facility */
2220 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002221 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 /* progress */
2223 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 /* user-user */
2226 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002227 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228
2229 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002230 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231
2232 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2233
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002234 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235}
2236
2237static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2238{
2239 struct gsm48_hdr *gh = msgb_l3(msg);
2240 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2241 struct tlv_parsed tp;
2242 struct gsm_mncc rel;
2243 int rc;
2244
2245 gsm48_stop_cc_timer(trans);
2246
2247 memset(&rel, 0, sizeof(struct gsm_mncc));
2248 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002249 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 /* cause */
2251 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2252 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002253 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2255 }
2256 /* facility */
2257 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2258 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2261 }
2262 /* user-user */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2264 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2267 }
2268 /* ss-version */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2270 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2273 }
2274
Harald Weltedcaf5652009-07-23 18:56:43 +02002275 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002277 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002279 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002280 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002281 GSM48_MT_CC_RELEASE_COMPL);
2282 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 }
2284
2285 new_cc_state(trans, GSM_CSTATE_NULL);
2286
2287 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002288 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
2290 return rc;
2291}
2292
2293static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2294{
2295 struct gsm_mncc *rel = arg;
2296 struct msgb *msg = gsm48_msgb_alloc();
2297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2298
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 gh->msg_type = GSM48_MT_CC_RELEASE;
2300
2301 trans->callref = 0;
2302
2303 gsm48_stop_cc_timer(trans);
2304 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2305
2306 /* cause */
2307 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002308 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 /* facility */
2310 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002311 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 /* user-user */
2313 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002314 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315
Harald Weltedcaf5652009-07-23 18:56:43 +02002316 trans->cc.T308_second = 0;
2317 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318
Harald Weltedcaf5652009-07-23 18:56:43 +02002319 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2321
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002322 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323}
2324
2325static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2326{
2327 struct gsm48_hdr *gh = msgb_l3(msg);
2328 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2329 struct tlv_parsed tp;
2330 struct gsm_mncc rel;
2331 int rc = 0;
2332
2333 gsm48_stop_cc_timer(trans);
2334
2335 memset(&rel, 0, sizeof(struct gsm_mncc));
2336 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002337 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 /* cause */
2339 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2340 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002341 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2343 }
2344 /* facility */
2345 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2346 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002347 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2349 }
2350 /* user-user */
2351 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2352 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2355 }
2356 /* ss-version */
2357 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2358 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2361 }
2362
2363 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002364 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002366 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 MNCC_REJ_IND, &rel);
2368 break;
2369 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002370 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 MNCC_REL_CNF, &rel);
2372 break;
2373 default:
Harald Welte596fed42009-07-23 19:06:52 +02002374 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 MNCC_REL_IND, &rel);
2376 }
2377 }
2378
2379 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002380 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381
2382 return rc;
2383}
2384
2385static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2386{
2387 struct gsm_mncc *rel = arg;
2388 struct msgb *msg = gsm48_msgb_alloc();
2389 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002390 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2393
2394 trans->callref = 0;
2395
2396 gsm48_stop_cc_timer(trans);
2397
2398 /* cause */
2399 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 /* facility */
2402 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002403 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 /* user-user */
2405 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002406 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407
Harald Weltec7782de2010-12-21 19:31:41 +01002408 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2409
Harald Weltedcaf5652009-07-23 18:56:43 +02002410 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411
Harald Weltec7782de2010-12-21 19:31:41 +01002412 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413}
2414
2415static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2416{
2417 struct gsm48_hdr *gh = msgb_l3(msg);
2418 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2419 struct tlv_parsed tp;
2420 struct gsm_mncc fac;
2421
2422 memset(&fac, 0, sizeof(struct gsm_mncc));
2423 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002424 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 /* facility */
2426 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2427 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002428 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2430 }
2431 /* ss-version */
2432 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2433 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002434 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2436 }
2437
Harald Welte596fed42009-07-23 19:06:52 +02002438 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439}
2440
2441static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2442{
2443 struct gsm_mncc *fac = arg;
2444 struct msgb *msg = gsm48_msgb_alloc();
2445 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2446
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447 gh->msg_type = GSM48_MT_CC_FACILITY;
2448
2449 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002450 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002452 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453}
2454
2455static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2456{
2457 struct gsm_mncc hold;
2458
2459 memset(&hold, 0, sizeof(struct gsm_mncc));
2460 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002461 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462}
2463
2464static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2465{
2466 struct msgb *msg = gsm48_msgb_alloc();
2467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2468
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2470
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002471 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472}
2473
2474static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2475{
2476 struct gsm_mncc *hold_rej = arg;
2477 struct msgb *msg = gsm48_msgb_alloc();
2478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2479
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2481
2482 /* cause */
2483 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002484 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485 else
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002488 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489}
2490
2491static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2492{
2493 struct gsm_mncc retrieve;
2494
2495 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2496 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002497 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2498 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2502{
2503 struct msgb *msg = gsm48_msgb_alloc();
2504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2505
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2507
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002508 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509}
2510
2511static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2512{
2513 struct gsm_mncc *retrieve_rej = arg;
2514 struct msgb *msg = gsm48_msgb_alloc();
2515 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2516
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2518
2519 /* cause */
2520 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002521 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 else
Harald Welte55c8f352010-03-07 23:40:35 +01002523 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002525 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002526}
2527
2528static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2529{
2530 struct gsm48_hdr *gh = msgb_l3(msg);
2531 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2532 struct tlv_parsed tp;
2533 struct gsm_mncc dtmf;
2534
2535 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2536 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002537 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 /* keypad facility */
2539 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2540 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002541 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2543 }
2544
Harald Welte596fed42009-07-23 19:06:52 +02002545 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546}
2547
2548static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2549{
2550 struct gsm_mncc *dtmf = arg;
2551 struct msgb *msg = gsm48_msgb_alloc();
2552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2553
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2555
2556 /* keypad */
2557 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002558 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002560 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561}
2562
2563static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2564{
2565 struct gsm_mncc *dtmf = arg;
2566 struct msgb *msg = gsm48_msgb_alloc();
2567 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2568
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2570
2571 /* cause */
2572 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002573 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 else
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002577 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578}
2579
2580static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2581{
2582 struct msgb *msg = gsm48_msgb_alloc();
2583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2584
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2586
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002587 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588}
2589
2590static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2591{
2592 struct gsm_mncc dtmf;
2593
2594 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2595 dtmf.callref = trans->callref;
2596
Harald Welte596fed42009-07-23 19:06:52 +02002597 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm48_hdr *gh = msgb_l3(msg);
2603 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2604 struct tlv_parsed tp;
2605 struct gsm_mncc modify;
2606
2607 memset(&modify, 0, sizeof(struct gsm_mncc));
2608 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002609 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610 /* bearer capability */
2611 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2612 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002613 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2615 }
2616
2617 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2618
Harald Welte596fed42009-07-23 19:06:52 +02002619 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620}
2621
2622static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2623{
2624 struct gsm_mncc *modify = arg;
2625 struct msgb *msg = gsm48_msgb_alloc();
2626 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2627
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628 gh->msg_type = GSM48_MT_CC_MODIFY;
2629
2630 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2631
2632 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002633 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634
2635 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2636
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002637 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638}
2639
2640static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2641{
2642 struct gsm48_hdr *gh = msgb_l3(msg);
2643 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2644 struct tlv_parsed tp;
2645 struct gsm_mncc modify;
2646
2647 gsm48_stop_cc_timer(trans);
2648
2649 memset(&modify, 0, sizeof(struct gsm_mncc));
2650 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002651 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 /* bearer capability */
2653 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2654 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002655 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2657 }
2658
2659 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2660
Harald Welte596fed42009-07-23 19:06:52 +02002661 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662}
2663
2664static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2665{
2666 struct gsm_mncc *modify = arg;
2667 struct msgb *msg = gsm48_msgb_alloc();
2668 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2669
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2671
2672 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674
2675 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2676
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002677 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678}
2679
2680static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2681{
2682 struct gsm48_hdr *gh = msgb_l3(msg);
2683 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2684 struct tlv_parsed tp;
2685 struct gsm_mncc modify;
2686
2687 gsm48_stop_cc_timer(trans);
2688
2689 memset(&modify, 0, sizeof(struct gsm_mncc));
2690 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002691 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 /* bearer capability */
2693 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2694 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002695 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2697 }
2698 /* cause */
2699 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2700 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002701 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2703 }
2704
2705 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2706
Harald Welte596fed42009-07-23 19:06:52 +02002707 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708}
2709
2710static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2711{
2712 struct gsm_mncc *modify = arg;
2713 struct msgb *msg = gsm48_msgb_alloc();
2714 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2715
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2717
2718 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002719 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002721 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722
2723 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2724
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002725 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726}
2727
2728static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2729{
2730 struct gsm_mncc *notify = arg;
2731 struct msgb *msg = gsm48_msgb_alloc();
2732 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2733
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 gh->msg_type = GSM48_MT_CC_NOTIFY;
2735
2736 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002737 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002739 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740}
2741
2742static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2743{
2744 struct gsm48_hdr *gh = msgb_l3(msg);
2745 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2746// struct tlv_parsed tp;
2747 struct gsm_mncc notify;
2748
2749 memset(&notify, 0, sizeof(struct gsm_mncc));
2750 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002751// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002753 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754
Harald Welte596fed42009-07-23 19:06:52 +02002755 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756}
2757
2758static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2759{
2760 struct gsm_mncc *user = arg;
2761 struct msgb *msg = gsm48_msgb_alloc();
2762 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2763
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764 gh->msg_type = GSM48_MT_CC_USER_INFO;
2765
2766 /* user-user */
2767 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002768 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769 /* more data */
2770 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002771 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002773 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774}
2775
2776static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2777{
2778 struct gsm48_hdr *gh = msgb_l3(msg);
2779 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2780 struct tlv_parsed tp;
2781 struct gsm_mncc user;
2782
2783 memset(&user, 0, sizeof(struct gsm_mncc));
2784 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002785 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 /* user-user */
2787 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2788 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002789 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002790 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2791 }
2792 /* more data */
2793 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2794 user.more = 1;
2795
Harald Welte596fed42009-07-23 19:06:52 +02002796 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797}
2798
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002799static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800{
2801 struct gsm_mncc *mode = arg;
2802
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002803 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804}
2805
2806static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002807 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 int type;
2809 int (*rout) (struct gsm_trans *trans, void *arg);
2810} downstatelist[] = {
2811 /* mobile originating call establishment */
2812 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2813 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2814 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2815 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2816 {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 */
2817 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2818 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2819 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2820 /* mobile terminating call establishment */
2821 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2822 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2823 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2824 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2825 /* signalling during call */
2826 {SBIT(GSM_CSTATE_ACTIVE),
2827 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2828 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2829 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2830 {ALL_STATES,
2831 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2832 {ALL_STATES,
2833 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2834 {ALL_STATES,
2835 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2836 {SBIT(GSM_CSTATE_ACTIVE),
2837 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2838 {SBIT(GSM_CSTATE_ACTIVE),
2839 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2840 {SBIT(GSM_CSTATE_ACTIVE),
2841 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2842 {SBIT(GSM_CSTATE_ACTIVE),
2843 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2844 {SBIT(GSM_CSTATE_ACTIVE),
2845 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2846 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2847 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2848 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2849 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2850 {SBIT(GSM_CSTATE_ACTIVE),
2851 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2852 /* clearing */
2853 {SBIT(GSM_CSTATE_INITIATED),
2854 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2855 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2856 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2857 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2858 MNCC_REL_REQ, gsm48_cc_tx_release},
2859 /* special */
2860 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002861 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862};
2863
2864#define DOWNSLLEN \
2865 (sizeof(downstatelist) / sizeof(struct downstate))
2866
2867
Harald Welte76556372010-12-22 23:57:45 +01002868int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869{
Harald Welte1a6f7982009-08-09 18:52:33 +02002870 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002872 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002873 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874 struct gsm_mncc *data = arg, rel;
2875
Harald Welte04dc88f2010-12-22 21:45:05 +01002876 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2877
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878 /* handle special messages */
2879 switch(msg_type) {
2880 case MNCC_BRIDGE:
2881 return tch_bridge(net, arg);
2882 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002883 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002885 return tch_recv_mncc(net, data->callref, 1);
2886 case GSM_TCHF_FRAME:
2887 /* Find callref */
2888 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002889 if (!trans) {
2890 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002891 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002892 }
2893 if (!trans->conn) {
2894 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002895 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002896 }
2897 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2898 /* This should be LOGL_ERROR or NOTICE, but
2899 * unfortuantely it happens for a couple of frames at
2900 * the beginning of every RTP connection */
2901 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002902 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002903 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002904 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002905 switch (bts->type) {
2906 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002907 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002908 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002909 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002910 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002911 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002912 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002913 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002914 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002915 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002916 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002917 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002918 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002919 }
2920 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921 }
2922
2923 memset(&rel, 0, sizeof(struct gsm_mncc));
2924 rel.callref = data->callref;
2925
2926 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002927 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928
2929 /* Callref unknown */
2930 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002931 struct gsm_subscriber *subscr;
2932
Harald Welte4a3464c2009-07-04 10:11:24 +02002933 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2935 "Received '%s' from MNCC with "
2936 "unknown callref %d\n", data->called.number,
2937 get_mncc_name(msg_type), data->callref);
2938 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002939 return mncc_release_ind(net, NULL, data->callref,
2940 GSM48_CAUSE_LOC_PRN_S_LU,
2941 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002943 if (!data->called.number[0] && !data->imsi[0]) {
2944 DEBUGP(DCC, "(bts - trx - ts - ti) "
2945 "Received '%s' from MNCC with "
2946 "no number or IMSI\n", get_mncc_name(msg_type));
2947 /* Invalid number */
2948 return mncc_release_ind(net, NULL, data->callref,
2949 GSM48_CAUSE_LOC_PRN_S_LU,
2950 GSM48_CC_CAUSE_INV_NR_FORMAT);
2951 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002953 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002954 subscr = subscr_get_by_extension(net,
2955 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002956 else
Harald Welte9176bd42009-07-23 18:46:00 +02002957 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958 /* If subscriber is not found */
2959 if (!subscr) {
2960 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2961 "Received '%s' from MNCC with "
2962 "unknown subscriber %s\n", data->called.number,
2963 get_mncc_name(msg_type), data->called.number);
2964 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002965 return mncc_release_ind(net, NULL, data->callref,
2966 GSM48_CAUSE_LOC_PRN_S_LU,
2967 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 }
2969 /* If subscriber is not "attached" */
2970 if (!subscr->lac) {
2971 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2972 "Received '%s' from MNCC with "
2973 "detached subscriber %s\n", data->called.number,
2974 get_mncc_name(msg_type), data->called.number);
2975 subscr_put(subscr);
2976 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002977 return mncc_release_ind(net, NULL, data->callref,
2978 GSM48_CAUSE_LOC_PRN_S_LU,
2979 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002980 }
2981 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002982 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2983 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 DEBUGP(DCC, "No memory for trans.\n");
2985 subscr_put(subscr);
2986 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002987 mncc_release_ind(net, NULL, data->callref,
2988 GSM48_CAUSE_LOC_PRN_S_LU,
2989 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 return -ENOMEM;
2991 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002993 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002994
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002996 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 /* find transaction with this subscriber already paging */
2998 llist_for_each_entry(transt, &net->trans_list, entry) {
2999 /* Transaction of our lchan? */
3000 if (transt == trans ||
3001 transt->subscr != subscr)
3002 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003003 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004 "Received '%s' from MNCC with "
3005 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003006 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003008 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003009 subscr_put(subscr);
3010 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 return 0;
3012 }
3013 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003014 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003015
3016 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003017 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3018 if (!trans->paging_request) {
3019 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3020 subscr_put(subscr);
3021 trans_free(trans);
3022 return 0;
3023 }
3024
3025 *trans->paging_request = subscr->net;
3026 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003027
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003028 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 return 0;
3030 }
3031 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003032 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003033 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003035
3036 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003037 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038
3039 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003040 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003041 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003042 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003043 (trans->subscr)?(trans->subscr->extension):"-",
3044 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003045 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3046 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 if (msg_type == MNCC_REL_REQ)
3048 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3049 else
3050 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3051 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003052 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 return rc;
3054 }
3055
3056 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3057 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003058 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003060 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003061 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003062 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063
3064 /* Find function for current state and message */
3065 for (i = 0; i < DOWNSLLEN; i++)
3066 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003067 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 break;
3069 if (i == DOWNSLLEN) {
3070 DEBUGP(DCC, "Message unhandled at this state.\n");
3071 return 0;
3072 }
3073
3074 rc = downstatelist[i].rout(trans, arg);
3075
3076 return rc;
3077}
3078
3079
3080static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003081 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003082 int type;
3083 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3084} datastatelist[] = {
3085 /* mobile originating call establishment */
3086 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3087 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3088 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3089 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3090 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3091 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3092 /* mobile terminating call establishment */
3093 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3094 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3095 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3096 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003097 {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 Welte4bfdfe72009-06-10 23:11:52 +08003098 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3099 /* signalling during call */
3100 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3101 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3102 {SBIT(GSM_CSTATE_ACTIVE),
3103 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3104 {ALL_STATES,
3105 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3106 {ALL_STATES,
3107 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3108 {ALL_STATES,
3109 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3110 {SBIT(GSM_CSTATE_ACTIVE),
3111 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3112 {SBIT(GSM_CSTATE_ACTIVE),
3113 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3114 {SBIT(GSM_CSTATE_ACTIVE),
3115 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3116 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3117 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3118 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3119 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3120 {SBIT(GSM_CSTATE_ACTIVE),
3121 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3122 /* clearing */
3123 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3124 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3125 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3126 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3127 {ALL_STATES, /* 5.4.3.4 */
3128 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3129};
3130
3131#define DATASLLEN \
3132 (sizeof(datastatelist) / sizeof(struct datastate))
3133
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003134static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003135{
3136 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003137 uint8_t msg_type = gh->msg_type & 0xbf;
3138 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003139 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003140 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003141
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142 if (msg_type & 0x80) {
3143 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3144 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003145 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003146
Harald Welte4bfdfe72009-06-10 23:11:52 +08003147 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003148 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003149
Harald Welte6f5aee02009-07-23 21:21:14 +02003150 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003152 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003153 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003154 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3155 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003156
3157 /* Create transaction */
3158 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003159 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003160 "creating new trans.\n", transaction_id);
3161 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003162 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003163 transaction_id, new_callref++);
3164 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003165 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003166 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003167 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003168 GSM48_MT_CC_RELEASE_COMPL);
3169 return -ENOMEM;
3170 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003172 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 }
3174
3175 /* find function for current state and message */
3176 for (i = 0; i < DATASLLEN; i++)
3177 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003178 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003179 break;
3180 if (i == DATASLLEN) {
3181 DEBUGP(DCC, "Message unhandled at this state.\n");
3182 return 0;
3183 }
3184
3185 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003186
3187 return rc;
3188}
3189
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003190/* Create a dummy to wait five seconds */
3191static void release_anchor(struct gsm_subscriber_connection *conn)
3192{
3193 if (!conn->anch_operation)
3194 return;
3195
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003196 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003197 talloc_free(conn->anch_operation);
3198 conn->anch_operation = NULL;
3199}
3200
3201static void anchor_timeout(void *_data)
3202{
3203 struct gsm_subscriber_connection *con = _data;
3204
3205 release_anchor(con);
3206 msc_release_connection(con);
3207}
3208
3209int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3210{
3211 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3212 if (!conn->anch_operation)
3213 return -1;
3214
3215 conn->anch_operation->timeout.data = conn;
3216 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003217 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003218 return 0;
3219}
3220
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003221/* here we get data from the BSC level... */
3222int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003223{
3224 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003225 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003226 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003227
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003228 if (silent_call_reroute(conn, msg))
3229 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003230
3231 switch (pdisc) {
3232 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003233 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003234 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003235 break;
3236 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003237 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003238 break;
3239 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003240 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003241 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003242 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003243 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003244 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003245 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003246 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003247 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003248 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3249 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003250 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003251 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003252 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003253 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003254 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003255 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003256 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3257 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003258 break;
3259 }
3260
3261 return rc;
3262}
Harald Welte8470bf22008-12-25 23:28:35 +00003263
Harald Welte805f6442009-07-28 18:25:29 +02003264/*
3265 * This will be ran by the linker when loading the DSO. We use it to
3266 * do system initialization, e.g. registration of signal handlers.
3267 */
3268static __attribute__((constructor)) void on_dso_load_0408(void)
3269{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003270 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003271}