blob: dac574d5dbc9dae46c32dc995a4d0ff50567a85b [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 Freythere0009f12010-08-12 01:41:57 +08005 * (C) 2008-2010 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
Sylvain Munaut30a15382009-12-24 00:27:26 +010032#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000037#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080039#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000041#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000042#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000043#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000044#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000045#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020046#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020047#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020048#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010049#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080050#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080051#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080052#include <osmocore/bitvec.h>
53
54#include <osmocore/gsm48.h>
Holger Hans Peter Freyther12eb9b72010-09-30 18:53:47 +080055#include <osmocore/gsm0480.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080056#include <osmocore/gsm_utils.h>
57#include <osmocore/msgb.h>
58#include <osmocore/talloc.h>
59#include <osmocore/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000060
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020061void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010062void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020063
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080064int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080065static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000066 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010067static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080068static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000069
Harald Welte52b1f982008-12-23 20:25:15 +000070struct gsm_lai {
71 u_int16_t mcc;
72 u_int16_t mnc;
73 u_int16_t lac;
74};
75
Harald Welte4bfdfe72009-06-10 23:11:52 +080076static u_int32_t new_callref = 0x80000001;
77
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080078static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
79 struct gsm_trans *trans)
80{
81 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
82
83 /* if we get passed a transaction reference, do some common
84 * work that the caller no longer has to do */
85 if (trans) {
86 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
87 msg->lchan = trans->conn->lchan;
88 }
89
90
91 if (msg->lchan) {
92 msg->trx = msg->lchan->ts->trx;
93 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
94 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
95 "Sending '%s' to MS.\n", msg->trx->bts->nr,
96 msg->trx->nr, msg->lchan->ts->nr,
97 gh->proto_discr & 0xf0,
98 gsm48_cc_msg_name(gh->msg_type));
99 else
100 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
101 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
102 msg->trx->nr, msg->lchan->ts->nr,
103 gh->proto_discr, gh->msg_type);
104 }
105
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100106 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800107}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100108
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800109int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
110{
111 struct gsm48_hdr *gh;
112 struct msgb *ss_notify;
113
114 ss_notify = gsm0480_create_notifySS(message);
115 if (!ss_notify)
116 return -1;
117
118 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
119 uint8_t *data = msgb_push(ss_notify, 1);
120 data[0] = ss_notify->len - 1;
121 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
122 gh->msg_type = GSM48_MT_CC_FACILITY;
123 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
124}
125
Sylvain Munaut30a15382009-12-24 00:27:26 +0100126static void release_security_operation(struct gsm_subscriber_connection *conn)
127{
128 if (!conn->sec_operation)
129 return;
130
131 talloc_free(conn->sec_operation);
132 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800133 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100134}
135
136static void allocate_security_operation(struct gsm_subscriber_connection *conn)
137{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100138 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
139 struct gsm_security_operation);
140}
141
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100142int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
143 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100144{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800145 struct gsm_network *net = conn->bts->network;
146 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147 struct gsm_security_operation *op;
148 struct gsm_auth_tuple atuple;
149 int status = -1, rc;
150
151 /* Check if we _can_ enable encryption. Cases where we can't:
152 * - Encryption disabled in config
153 * - Channel already secured (nothing to do)
154 * - Subscriber equipment doesn't support configured encryption
155 */
156 if (!net->a5_encryption) {
157 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800158 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100159 DEBUGP(DMM, "Requesting to secure an already secure channel");
160 status = GSM_SECURITY_SUCCEEDED;
161 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
162 net->a5_encryption)) {
163 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
164 status = GSM_SECURITY_NOAVAIL;
165 }
166
167 /* If not done yet, try to get info for this user */
168 if (status < 0) {
169 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
170 if (rc <= 0)
171 status = GSM_SECURITY_NOAVAIL;
172 }
173
174 /* Are we done yet ? */
175 if (status >= 0)
176 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800177 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100178 0;
179
180 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800181 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100182 return -EBUSY;
183
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 allocate_security_operation(conn);
185 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186 op->cb = cb;
187 op->cb_data = cb_data;
188 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
189
190 /* FIXME: Should start a timer for completion ... */
191
192 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100193 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100194 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800195 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100196 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100197 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100198 return gsm0808_cipher_mode(conn, net->a5_encryption,
199 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 }
201
202 return -EINVAL; /* not reached */
203}
204
Holger Freyther73487a22008-12-31 18:53:57 +0000205static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
206 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000207{
208 if (!subscriber)
209 return 0;
210
Holger Freyther73487a22008-12-31 18:53:57 +0000211 /*
212 * Do not send accept yet as more information should arrive. Some
213 * phones will not send us the information and we will have to check
214 * what we want to do with that.
215 */
216 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
217 return 0;
218
Jan Luebbe06513f22009-08-12 12:48:00 +0200219 switch (subscriber->net->auth_policy) {
220 case GSM_AUTH_POLICY_CLOSED:
221 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200222 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200223 if (subscriber->authorized)
224 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200225 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200226 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000227 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200228 default:
229 return 0;
230 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000231}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000232
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100233static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000234{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100235 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000236 return;
237
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800238 /* No need to keep the connection up */
239 release_anchor(conn);
240
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100241 bsc_del_timer(&conn->loc_operation->updating_timer);
242 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100243 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800244 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000245}
246
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100247static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000248{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800249 if (conn->loc_operation)
250 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100251 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000252
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100253 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200254 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000255}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000256
Sylvain Munaut267fba02009-12-24 00:28:01 +0100257static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
258 struct msgb *msg, void *data, void *param)
259{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800260 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100261 int rc = 0;
262
263 switch (event) {
264 case GSM_SECURITY_AUTH_FAILED:
265 release_loc_updating_req(conn);
266 break;
267
268 case GSM_SECURITY_NOAVAIL:
269 case GSM_SECURITY_SUCCEEDED:
270 /* We're all good */
271 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800272 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800273 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100274 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800275 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100276 }
277
278 /* call subscr_update after putting the loc_upd_acc
279 * in the transmit queue, since S_SUBSCR_ATTACHED might
280 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800281 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100282 GSM_SUBSCRIBER_UPDATE_ATTACHED);
283
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100284 /*
285 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
286 * MS needs to respond with a TMSI REALLOCATION COMPLETE
287 * (even if the TMSI is the same).
288 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100289 break;
290
291 default:
292 rc = -EINVAL;
293 };
294
295 return rc;
296}
297
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100298static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000299{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100300 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800301 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100302 conn->loc_operation->key_seq,
303 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000304 return 0;
305}
306
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800307void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000308{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800309 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800310
311 /* avoid someone issuing a clear */
312 conn->in_release = 1;
313
Holger Freyther7c19f742009-06-06 13:54:35 +0000314 /*
315 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800316 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000317 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800318 release_loc_updating_req(conn);
319 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800320 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000321
Harald Welte4bfdfe72009-06-10 23:11:52 +0800322 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200323 /* FIXME: this is not neccessarily the right thing to do, we should
324 * only set trans->lchan to NULL and wait for another lchan to be
325 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800326 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
327 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200328 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800329 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000330}
331
Harald Welte371efe52010-12-22 23:17:50 +0100332void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
333{
334 struct gsm_trans *trans, *temp;
335
336 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
337
338 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100339 if (trans->protocol == protocol) {
340 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100341 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100342 }
Harald Welte371efe52010-12-22 23:17:50 +0100343 }
344}
345
Holger Freyther429e7762008-12-30 13:28:30 +0000346/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800347int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000348{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800349 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800350 struct msgb *msg;
351
352 counter_inc(bts->network->stats.loc_upd_resp.reject);
353
354 msg = gsm48_create_loc_upd_rej(cause);
355 if (!msg) {
356 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
357 return -1;
358 }
Harald Welte52b1f982008-12-23 20:25:15 +0000359
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800360 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000361
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100362 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100363 "LAC=%u BTS=%u\n", conn->subscr ?
364 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800365 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100366
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800367 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000368}
369
370/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800371int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000372{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800373 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000374 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000375 struct gsm48_hdr *gh;
376 struct gsm48_loc_area_id *lai;
377 u_int8_t *mid;
378
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800379 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000380
381 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
382 gh->proto_discr = GSM48_PDISC_MM;
383 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
384
385 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100386 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000387 bts->network->network_code, bts->location_area_code);
388
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200389 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200390 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000391
392 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
393
Harald Welteffa55a42009-12-22 19:07:32 +0100394 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100395
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800396 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000397}
398
Harald Weltebf5e8df2009-02-03 12:59:45 +0000399/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800400static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000401{
402 struct msgb *msg = gsm48_msgb_alloc();
403 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000404
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800405 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000406
407 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
408 gh->proto_discr = GSM48_PDISC_MM;
409 gh->msg_type = GSM48_MT_MM_ID_REQ;
410 gh->data[0] = id_type;
411
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800412 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000413}
414
Harald Welte231ad4f2008-12-27 11:15:38 +0000415
Harald Weltebf5e8df2009-02-03 12:59:45 +0000416/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800417static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000418{
419 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000420 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200421 struct gsm_bts *bts = lchan->ts->trx->bts;
422 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000423 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200424 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000425
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200426 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000427 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000428 mi_type, mi_string);
429
Harald Welte7659de12009-12-13 12:39:18 +0100430 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
431
Harald Welte75a983f2008-12-27 21:34:06 +0000432 switch (mi_type) {
433 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200434 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100435 if (!conn->subscr) {
436 conn->subscr = subscr_get_by_imsi(net, mi_string);
437 if (!conn->subscr)
438 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200439 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100440 if (conn->loc_operation)
441 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000442 break;
443 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000444 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000445 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100446 if (conn->subscr) {
447 db_subscriber_assoc_imei(conn->subscr, mi_string);
448 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200449 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100450 if (conn->loc_operation)
451 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000452 break;
453 }
Holger Freyther73487a22008-12-31 18:53:57 +0000454
455 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100456 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000457}
458
Harald Welte255539c2008-12-28 02:26:27 +0000459
460static void loc_upd_rej_cb(void *data)
461{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100462 struct gsm_subscriber_connection *conn = data;
463 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100464 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000465
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800466 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800467 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000468}
469
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100470static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000471{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100472 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
473 conn->loc_operation->updating_timer.data = conn;
474 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000475}
476
Harald Welte2a139372009-02-22 21:14:55 +0000477static const char *lupd_name(u_int8_t type)
478{
479 switch (type) {
480 case GSM48_LUPD_NORMAL:
481 return "NORMAL";
482 case GSM48_LUPD_PERIODIC:
483 return "PEROIDOC";
484 case GSM48_LUPD_IMSI_ATT:
485 return "IMSI ATTACH";
486 default:
487 return "UNKNOWN";
488 }
489}
490
Harald Weltebf5e8df2009-02-03 12:59:45 +0000491/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800492static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000493{
Harald Welte8470bf22008-12-25 23:28:35 +0000494 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000495 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800496 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800497 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000498 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200499 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000500 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000501
Harald Welte8470bf22008-12-25 23:28:35 +0000502 lu = (struct gsm48_loc_upd_req *) gh->data;
503
504 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000505
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200506 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000507
Harald Weltea0368542009-06-27 02:58:43 +0200508 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000509 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000510
Harald Welte7659de12009-12-13 12:39:18 +0100511 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
512
Harald Welte24ff6ee2009-12-22 00:41:05 +0100513 switch (lu->type) {
514 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100515 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100516 break;
517 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100518 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100519 break;
520 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100521 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100522 break;
523 }
524
Holger Freythereaf04692009-06-06 13:54:44 +0000525 /*
526 * Pseudo Spoof detection: Just drop a second/concurrent
527 * location updating request.
528 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100529 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200530 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100531 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800532 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000533 return 0;
534 }
535
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800536 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000537
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200538 conn->loc_operation->key_seq = lu->key_seq;
539
Harald Welte52b1f982008-12-23 20:25:15 +0000540 switch (mi_type) {
541 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200542 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000543 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800544 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100545 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000546
Jan Luebbe370b41d2009-08-12 10:19:34 +0200547 /* look up subscriber based on IMSI, create if not found */
548 subscr = subscr_get_by_imsi(bts->network, mi_string);
549 if (!subscr) {
550 subscr = db_create_subscriber(bts->network, mi_string);
551 }
Harald Welte4b634542008-12-27 01:55:51 +0000552 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000553 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200554 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000555 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200556 subscr = subscr_get_by_tmsi(bts->network,
557 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000558 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000559 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800560 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000562 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200563 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800564 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200565 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000566 break;
567 case GSM_MI_TYPE_IMEI:
568 case GSM_MI_TYPE_IMEISV:
569 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200570 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000571 break;
572 default:
Harald Weltea0368542009-06-27 02:58:43 +0200573 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000574 break;
575 }
576
Harald Welte24516ea2009-07-04 10:18:00 +0200577 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200579
Harald Welte4bfdfe72009-06-10 23:11:52 +0800580 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200581 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800582 /* FIXME: request id? close channel? */
583 return -EINVAL;
584 }
585
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100586 conn->subscr = subscr;
587 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000588
Harald Welte24516ea2009-07-04 10:18:00 +0200589 /* check if we can let the subscriber into our network immediately
590 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100591 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000592}
593
Harald Welte4bfdfe72009-06-10 23:11:52 +0800594#if 0
595static u_int8_t to_bcd8(u_int8_t val)
596{
597 return ((val / 10) << 4) | (val % 10);
598}
599#endif
600
Harald Weltedb253af2008-12-30 17:56:55 +0000601/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800602int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000603{
604 struct msgb *msg = gsm48_msgb_alloc();
605 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800606 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000607 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200608 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800609#if 0
610 time_t cur_t;
611 struct tm* cur_time;
612 int tz15min;
613#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000614
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800615 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000616
617 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
618 gh->proto_discr = GSM48_PDISC_MM;
619 gh->msg_type = GSM48_MT_MM_INFO;
620
621 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200622#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000623 name_len = strlen(net->name_long);
624 /* 10.5.3.5a */
625 ptr8 = msgb_put(msg, 3);
626 ptr8[0] = GSM48_IE_NAME_LONG;
627 ptr8[1] = name_len*2 +1;
628 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
629
630 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
631 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000632 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000633
634 /* FIXME: Use Cell Broadcast, not UCS-2, since
635 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200636#endif
637 name_len = (strlen(net->name_long)*7)/8;
638 name_pad = (8 - strlen(net->name_long)*7)%8;
639 if (name_pad > 0)
640 name_len++;
641 /* 10.5.3.5a */
642 ptr8 = msgb_put(msg, 3);
643 ptr8[0] = GSM48_IE_NAME_LONG;
644 ptr8[1] = name_len +1;
645 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
646
647 ptr8 = msgb_put(msg, name_len);
648 gsm_7bit_encode(ptr8, net->name_long);
649
Harald Weltedb253af2008-12-30 17:56:55 +0000650 }
651
652 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200653#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000654 name_len = strlen(net->name_short);
655 /* 10.5.3.5a */
656 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200657 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000658 ptr8[1] = name_len*2 + 1;
659 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
660
Harald Weltee872cb12009-01-01 00:33:37 +0000661 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000662 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000663 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200664#endif
665 name_len = (strlen(net->name_short)*7)/8;
666 name_pad = (8 - strlen(net->name_short)*7)%8;
667 if (name_pad > 0)
668 name_len++;
669 /* 10.5.3.5a */
670 ptr8 = (u_int8_t *) msgb_put(msg, 3);
671 ptr8[0] = GSM48_IE_NAME_SHORT;
672 ptr8[1] = name_len +1;
673 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
674
675 ptr8 = msgb_put(msg, name_len);
676 gsm_7bit_encode(ptr8, net->name_short);
677
Harald Weltedb253af2008-12-30 17:56:55 +0000678 }
679
680#if 0
681 /* Section 10.5.3.9 */
682 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000684 ptr8 = msgb_put(msg, 8);
685 ptr8[0] = GSM48_IE_NET_TIME_TZ;
686 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
687 ptr8[2] = to_bcd8(cur_time->tm_mon);
688 ptr8[3] = to_bcd8(cur_time->tm_mday);
689 ptr8[4] = to_bcd8(cur_time->tm_hour);
690 ptr8[5] = to_bcd8(cur_time->tm_min);
691 ptr8[6] = to_bcd8(cur_time->tm_sec);
692 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
693 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800694 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000695 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800696 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000697#endif
698
Daniel Willmanneea93372009-08-13 03:42:07 +0200699 DEBUGP(DMM, "-> MM INFO\n");
700
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800701 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000702}
703
Harald Welte7984d5c2009-08-12 22:56:50 +0200704/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800705int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200706{
707 struct msgb *msg = gsm48_msgb_alloc();
708 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200709 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200710
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100711 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200712
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800713 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200714 gh->proto_discr = GSM48_PDISC_MM;
715 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
716
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100717 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200718
Harald Welte7984d5c2009-08-12 22:56:50 +0200719 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200720 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200721 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200722
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800723 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200724}
725
726/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800727int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200728{
729 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800730 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200731}
732
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800733static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000734{
Harald Welte4b634542008-12-27 01:55:51 +0000735 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800736 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000737}
Harald Welteba4cf162009-01-10 01:49:35 +0000738
739/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100740static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000741 enum gsm48_reject_value value)
742{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800743 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000744
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800745 msg = gsm48_create_mm_serv_rej(value);
746 if (!msg) {
747 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
748 return -1;
749 }
Harald Welteba4cf162009-01-10 01:49:35 +0000750
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800751 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100752 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800753 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000754}
755
Sylvain Munautba87f452009-12-24 00:28:46 +0100756static int _gsm48_rx_mm_serv_req_sec_cb(
757 unsigned int hooknum, unsigned int event,
758 struct msgb *msg, void *data, void *param)
759{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800760 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100761 int rc = 0;
762
763 switch (event) {
764 case GSM_SECURITY_AUTH_FAILED:
765 /* Nothing to do */
766 break;
767
768 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800769 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100770 break;
771
772 case GSM_SECURITY_SUCCEEDED:
773 /* nothing to do. CIPHER MODE COMMAND is
774 * implicit CM SERV ACK */
775 break;
776
777 default:
778 rc = -EINVAL;
779 };
780
781 return rc;
782}
783
Harald Welte4ed0e922009-01-10 03:17:30 +0000784/*
785 * Handle CM Service Requests
786 * a) Verify that the packet is long enough to contain the information
787 * we require otherwsie reject with INCORRECT_MESSAGE
788 * b) Try to parse the TMSI. If we do not have one reject
789 * c) Check that we know the subscriber with the TMSI otherwise reject
790 * with a HLR cause
791 * d) Set the subscriber on the gsm_lchan and accept
792 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800793static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000794{
Harald Welteba4cf162009-01-10 01:49:35 +0000795 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200796 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000797
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800798 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000799 struct gsm_subscriber *subscr;
800 struct gsm48_hdr *gh = msgb_l3(msg);
801 struct gsm48_service_request *req =
802 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800803 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000804 u_int8_t classmark2_len = gh->data[1];
805 u_int8_t *classmark2 = gh->data+2;
806 u_int8_t mi_len = *(classmark2 + classmark2_len);
807 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000808
Harald Weltec9e02182009-05-01 19:07:53 +0000809 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000810 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000811 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800812 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000813 GSM48_REJECT_INCORRECT_MESSAGE);
814 }
815
816 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000817 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800818 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000819 GSM48_REJECT_INCORRECT_MESSAGE);
820 }
821
Harald Weltec9e02182009-05-01 19:07:53 +0000822 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000823 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000824 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800825 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000826 GSM48_REJECT_INCORRECT_MESSAGE);
827 }
828
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200829 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000830 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000831 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000832
Harald Welte7659de12009-12-13 12:39:18 +0100833 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
834
Harald Welte3ac7f102009-08-10 10:12:45 +0200835 if (is_siemens_bts(bts))
836 send_siemens_mrpci(msg->lchan, classmark2-1);
837
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200838 subscr = subscr_get_by_tmsi(bts->network,
839 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000840
Harald Welte2a139372009-02-22 21:14:55 +0000841 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000842 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800843 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000844 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
845
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800846 if (!conn->subscr)
847 conn->subscr = subscr;
848 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100849 subscr_put(subscr); /* lchan already has a ref, don't need another one */
850 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000851 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
852 subscr_put(subscr);
853 }
854
Harald Weltec2e302d2009-07-05 14:08:13 +0200855 subscr->equipment.classmark2_len = classmark2_len;
856 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
857 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000858
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800859 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100860 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000861}
862
Harald Welte2a139372009-02-22 21:14:55 +0000863static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
864{
Harald Welte9176bd42009-07-23 18:46:00 +0200865 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000866 struct gsm48_hdr *gh = msgb_l3(msg);
867 struct gsm48_imsi_detach_ind *idi =
868 (struct gsm48_imsi_detach_ind *) gh->data;
869 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200870 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800871 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000872
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200873 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000874 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
875 mi_type, mi_string);
876
Harald Welteffa55a42009-12-22 19:07:32 +0100877 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100878
Harald Welte2a139372009-02-22 21:14:55 +0000879 switch (mi_type) {
880 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200881 subscr = subscr_get_by_tmsi(bts->network,
882 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000883 break;
884 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200885 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000886 break;
887 case GSM_MI_TYPE_IMEI:
888 case GSM_MI_TYPE_IMEISV:
889 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000890 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000891 break;
892 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000893 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000894 break;
895 }
896
Holger Freyther4a49e772009-04-12 05:37:29 +0000897 if (subscr) {
898 subscr_update(subscr, msg->trx->bts,
899 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100900 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200901
902 subscr->equipment.classmark1 = idi->classmark1;
903 db_sync_equipment(&subscr->equipment);
904
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000905 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000906 } else
Harald Welte2a139372009-02-22 21:14:55 +0000907 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
908
Harald Welte31981a02009-12-20 09:58:40 +0100909 /* FIXME: iterate over all transactions and release them,
910 * imagine an IMSI DETACH happening during an active call! */
911
Harald Welteb83d9382009-12-12 21:00:48 +0100912 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000913 return 0;
914}
915
Harald Welted2a7f5a2009-06-05 20:08:20 +0000916static int gsm48_rx_mm_status(struct msgb *msg)
917{
918 struct gsm48_hdr *gh = msgb_l3(msg);
919
920 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
921
922 return 0;
923}
924
Sylvain Munaut30a15382009-12-24 00:27:26 +0100925/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800926static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100927{
928 struct gsm48_hdr *gh = msgb_l3(msg);
929 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800930 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100931
Sylvain Munautc593cf12010-06-10 23:51:41 +0200932 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100933 hexdump(ar->sres, 4));
934
935 /* Safety check */
936 if (!conn->sec_operation) {
937 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
938 return -EIO;
939 }
940
941 /* Validate SRES */
942 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100943 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100944 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200945
946 DEBUGPC(DMM, "Invalid (expected %s)\n",
947 hexdump(conn->sec_operation->atuple.sres, 4));
948
Sylvain Munaut30a15382009-12-24 00:27:26 +0100949 if (cb)
950 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800951 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100952
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100953 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100954 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100955 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100956 }
957
Sylvain Munautc593cf12010-06-10 23:51:41 +0200958 DEBUGPC(DMM, "OK\n");
959
Sylvain Munaut30a15382009-12-24 00:27:26 +0100960 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100961 return gsm0808_cipher_mode(conn, net->a5_encryption,
962 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100963}
964
Harald Weltebf5e8df2009-02-03 12:59:45 +0000965/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800966static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000967{
968 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800969 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000970
971 switch (gh->msg_type & 0xbf) {
972 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200973 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800974 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000975 break;
976 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800977 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000978 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000979 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800980 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000981 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000982 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000983 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000984 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000985 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000986 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800987 conn->subscr ?
988 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000989 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100990 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +0000991 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000992 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000993 rc = gsm48_rx_mm_imsi_detach_ind(msg);
994 break;
995 case GSM48_MT_MM_CM_REEST_REQ:
996 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
997 break;
998 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800999 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001000 break;
1001 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001002 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001003 gh->msg_type);
1004 break;
1005 }
1006
1007 return rc;
1008}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001009
Harald Welte2d35ae62009-02-06 12:02:13 +00001010/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001011static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001012{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001013 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001014 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001015 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +00001016 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001017 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001018 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001019 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001020 int rc = 0;
1021
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001022 resp = (struct gsm48_pag_resp *) &gh->data[0];
1023 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1024 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001025 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1026 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001027
Harald Weltefe18d8f2009-02-22 21:14:24 +00001028 switch (mi_type) {
1029 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001030 subscr = subscr_get_by_tmsi(bts->network,
1031 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001032 break;
1033 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001034 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001035 break;
1036 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001037
1038 if (!subscr) {
1039 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001040 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001041 return -EINVAL;
1042 }
1043 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001044 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001045
Harald Weltec2e302d2009-07-05 14:08:13 +02001046 subscr->equipment.classmark2_len = *classmark2_lv;
1047 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1048 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001049
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001050 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001051 return rc;
1052}
1053
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001054static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001055{
1056 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001057 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001058 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1059 u_int8_t cm2_len, cm3_len = 0;
1060 u_int8_t *cm2, *cm3 = NULL;
1061
1062 DEBUGP(DRR, "CLASSMARK CHANGE ");
1063
1064 /* classmark 2 */
1065 cm2_len = gh->data[0];
1066 cm2 = &gh->data[1];
1067 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1068
1069 if (payload_len > cm2_len + 1) {
1070 /* we must have a classmark3 */
1071 if (gh->data[cm2_len+1] != 0x20) {
1072 DEBUGPC(DRR, "ERR CM3 TAG\n");
1073 return -EINVAL;
1074 }
1075 if (cm2_len > 3) {
1076 DEBUGPC(DRR, "CM2 too long!\n");
1077 return -EINVAL;
1078 }
1079
1080 cm3_len = gh->data[cm2_len+2];
1081 cm3 = &gh->data[cm2_len+3];
1082 if (cm3_len > 14) {
1083 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1084 return -EINVAL;
1085 }
1086 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1087 }
1088 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001089 subscr->equipment.classmark2_len = cm2_len;
1090 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001091 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001092 subscr->equipment.classmark3_len = cm3_len;
1093 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001094 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001095 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001096 }
1097
Harald Weltef7c43522009-06-09 20:24:21 +00001098 return 0;
1099}
1100
Harald Weltecf5b3592009-05-01 18:28:42 +00001101static int gsm48_rx_rr_status(struct msgb *msg)
1102{
1103 struct gsm48_hdr *gh = msgb_l3(msg);
1104
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001105 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001106 rr_cause_name(gh->data[0]));
1107
1108 return 0;
1109}
1110
Harald Weltef7c43522009-06-09 20:24:21 +00001111static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1112{
Harald Welted12b0fd2009-12-15 21:36:05 +01001113 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001114
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001115 /* This shouldn't actually end up here, as RSL treats
1116 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1117 * directly into gsm48_parse_meas_rep */
1118 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001119 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001120
1121 return 0;
1122}
1123
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001124static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001125{
1126 struct gsm48_hdr *gh = msgb_l3(msg);
1127 u_int8_t apdu_id_flags;
1128 u_int8_t apdu_len;
1129 u_int8_t *apdu_data;
1130
1131 apdu_id_flags = gh->data[0];
1132 apdu_len = gh->data[1];
1133 apdu_data = gh->data+2;
1134
1135 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1136 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1137
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001138 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001139}
1140
Sylvain Munaut30a15382009-12-24 00:27:26 +01001141/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001142static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001143{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001144 gsm_cbfn *cb;
1145 int rc = 0;
1146
1147 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1148
1149 /* Safety check */
1150 if (!conn->sec_operation) {
1151 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1152 return -EIO;
1153 }
1154
1155 /* FIXME: check for MI (if any) */
1156
1157 /* Call back whatever was in progress (if anything) ... */
1158 cb = conn->sec_operation->cb;
1159 if (cb) {
1160 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001161 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001162 }
1163
1164 /* Complete the operation */
1165 release_security_operation(conn);
1166
1167 return rc;
1168}
1169
Harald Welted011e8b2009-11-29 22:45:52 +01001170/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001171static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001172{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001173 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001174 struct gsm48_hdr *gh = msgb_l3(msg);
1175
1176 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1177 rr_cause_name(gh->data[0]));
1178
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001179 sig.lchan = msg->lchan;
1180 sig.mr = NULL;
1181 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001182 /* FIXME: release old channel */
1183
1184 return 0;
1185}
1186
1187/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001188static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001189{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001190 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001191 struct gsm48_hdr *gh = msgb_l3(msg);
1192
1193 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1194 rr_cause_name(gh->data[0]));
1195
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001196 sig.lchan = msg->lchan;
1197 sig.mr = NULL;
1198 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001199 /* FIXME: release allocated new channel */
1200
1201 return 0;
1202}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001203
Harald Weltebf5e8df2009-02-03 12:59:45 +00001204/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001205static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001206{
1207 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001208 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001209
1210 switch (gh->msg_type) {
1211 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001212 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001213 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001214 case GSM48_MT_RR_GPRS_SUSP_REQ:
1215 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1216 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001217 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001218 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001219 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001220 case GSM48_MT_RR_STATUS:
1221 rc = gsm48_rx_rr_status(msg);
1222 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001223 case GSM48_MT_RR_MEAS_REP:
1224 rc = gsm48_rx_rr_meas_rep(msg);
1225 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001227 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001228 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001229 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001230 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001231 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001232 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001233 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001234 break;
1235 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001236 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001237 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001238 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001239 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1240 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001241 break;
1242 }
1243
Harald Welte2d35ae62009-02-06 12:02:13 +00001244 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001245}
1246
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001247int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001248 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001249{
1250 struct msgb *msg = gsm48_msgb_alloc();
1251 struct gsm48_hdr *gh;
1252
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001253 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254
1255 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1256 apdu_id, apdu_len);
1257
1258 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1259 gh->proto_discr = GSM48_PDISC_RR;
1260 gh->msg_type = GSM48_MT_RR_APP_INFO;
1261 gh->data[0] = apdu_id;
1262 gh->data[1] = apdu_len;
1263 memcpy(gh->data+2, apdu, apdu_len);
1264
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001265 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266}
1267
Harald Welte4bc90a12008-12-27 16:32:52 +00001268/* Call Control */
1269
Harald Welte7584aea2009-02-11 11:44:12 +00001270/* The entire call control code is written in accordance with Figure 7.10c
1271 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1272 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1273 * it for voice */
1274
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275static void new_cc_state(struct gsm_trans *trans, int state)
1276{
1277 if (state > 31 || state < 0)
1278 return;
1279
1280 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001281 gsm48_cc_state_name(trans->cc.state),
1282 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001283
Harald Weltedcaf5652009-07-23 18:56:43 +02001284 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285}
1286
1287static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001288{
1289 struct msgb *msg = gsm48_msgb_alloc();
1290 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1291 u_int8_t *cause, *call_state;
1292
Harald Welte4bc90a12008-12-27 16:32:52 +00001293 gh->msg_type = GSM48_MT_CC_STATUS;
1294
1295 cause = msgb_put(msg, 3);
1296 cause[0] = 2;
1297 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1298 cause[2] = 0x80 | 30; /* response to status inquiry */
1299
1300 call_state = msgb_put(msg, 1);
1301 call_state[0] = 0xc0 | 0x00;
1302
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001303 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001304}
1305
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001306static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001307 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001308{
1309 struct msgb *msg = gsm48_msgb_alloc();
1310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1311
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001312 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001313
Harald Welte6f4b7532008-12-29 00:39:37 +00001314 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001315 gh->msg_type = msg_type;
1316
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001317 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001318}
1319
Harald Welte4bfdfe72009-06-10 23:11:52 +08001320static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1321{
Harald Weltedcaf5652009-07-23 18:56:43 +02001322 if (bsc_timer_pending(&trans->cc.timer)) {
1323 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1324 bsc_del_timer(&trans->cc.timer);
1325 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326 }
1327}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001328
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1330 int msg_type, struct gsm_mncc *mncc)
1331{
1332 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001333 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334
1335 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001336 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001337 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001339 trans->conn->lchan->ts->trx->bts->nr,
1340 trans->conn->lchan->ts->trx->nr,
1341 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342 (trans->subscr)?(trans->subscr->extension):"-",
1343 get_mncc_name(msg_type));
1344 else
1345 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1346 "Sending '%s' to MNCC.\n",
1347 (trans->subscr)?(trans->subscr->extension):"-",
1348 get_mncc_name(msg_type));
1349 else
1350 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1351 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1352
1353 mncc->msg_type = msg_type;
1354
Harald Welte966636f2009-06-26 19:39:35 +02001355 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356 if (!msg)
1357 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001358
1359 data = msgb_put(msg, sizeof(struct gsm_mncc));
1360 memcpy(data, mncc, sizeof(struct gsm_mncc));
1361
Harald Welte54209c22010-12-22 23:43:29 +01001362 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363
1364 return 0;
1365}
1366
1367int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1368 u_int32_t callref, int location, int value)
1369{
1370 struct gsm_mncc rel;
1371
Harald Welte92f70c52009-06-12 01:54:08 +08001372 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001373 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001374 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1376}
1377
Harald Weltedcaf5652009-07-23 18:56:43 +02001378/* Call Control Specific transaction release.
1379 * gets called by trans_free, DO NOT CALL YOURSELF! */
1380void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 gsm48_stop_cc_timer(trans);
1383
1384 /* send release to L4, if callref still exists */
1385 if (trans->callref) {
1386 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001387 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001388 GSM48_CAUSE_LOC_PRN_S_LU,
1389 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001393 if (trans->conn)
1394 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395}
1396
1397static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001398
Harald Welte09e38af2009-02-16 22:52:23 +00001399/* call-back from paging the B-end of the connection */
1400static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001401 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001402{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001403 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 struct gsm_subscriber *subscr = param;
1405 struct gsm_trans *transt, *tmp;
1406 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001407
Harald Welte09e38af2009-02-16 22:52:23 +00001408 if (hooknum != GSM_HOOK_RR_PAGING)
1409 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001410
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 if (!subscr)
1412 return -EINVAL;
1413 net = subscr->net;
1414 if (!net) {
1415 DEBUGP(DCC, "Error Network not set!\n");
1416 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001417 }
Harald Welte7584aea2009-02-11 11:44:12 +00001418
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419 /* check all tranactions (without lchan) for subscriber */
1420 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001421 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001422 continue;
1423 switch (event) {
1424 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001425 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 break;
1427 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1428 subscr->extension);
1429 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001430 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001431 transt->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001432 }
1433 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001434 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 break;
1436 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001437 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438 DEBUGP(DCC, "Paging subscr %s expired!\n",
1439 subscr->extension);
1440 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001441 mncc_release_ind(transt->subscr->net, transt,
1442 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001443 GSM48_CAUSE_LOC_PRN_S_LU,
1444 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001446 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001447 break;
1448 }
1449 }
Harald Welte09e38af2009-02-16 22:52:23 +00001450 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001451}
Harald Welte7584aea2009-02-11 11:44:12 +00001452
Harald Welteda7ab742009-12-19 22:23:05 +01001453static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1454
Harald Welte805f6442009-07-28 18:25:29 +02001455/* some other part of the code sends us a signal */
1456static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1457 void *handler_data, void *signal_data)
1458{
1459 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001460 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001461 struct gsm_network *net;
1462 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001463
1464 if (subsys != SS_ABISIP)
1465 return 0;
1466
1467 /* in case we use direct BTS-to-BTS RTP */
1468 if (ipacc_rtp_direct)
1469 return 0;
1470
Harald Welte805f6442009-07-28 18:25:29 +02001471 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001472 case S_ABISIP_CRCX_ACK:
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 }
Harald Welte805f6442009-07-28 18:25:29 +02001482 break;
Harald Welte805f6442009-07-28 18:25:29 +02001483 }
1484
1485 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001486}
1487
Harald Welte49f48b82009-02-17 15:29:33 +00001488/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001489static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001490{
Harald Welte11fa29c2009-02-19 17:24:39 +00001491 struct gsm_bts *bts = lchan->ts->trx->bts;
1492 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001493 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001494
Harald Welte11fa29c2009-02-19 17:24:39 +00001495 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1496 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1497 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1498
1499 if (bts->type != remote_bts->type) {
1500 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1501 return -EINVAL;
1502 }
Harald Welteda7ab742009-12-19 22:23:05 +01001503
1504 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001505 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001506 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001507 if (!ipacc_rtp_direct) {
1508 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001509 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001510 if (rc < 0)
1511 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001512 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001513#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001514
1515 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301516 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1517 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001518 } else {
1519 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301520 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1521 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301522 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001523 if (rc < 0)
1524 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301525 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1526 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301527 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001528 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001529 break;
1530 case GSM_BTS_TYPE_BS11:
1531 trau_mux_map_lchan(lchan, remote_lchan);
1532 break;
1533 default:
1534 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001535 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001536 }
Harald Welte49f48b82009-02-17 15:29:33 +00001537
1538 return 0;
1539}
1540
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541/* bridge channels of two transactions */
1542static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001543{
Harald Weltedcaf5652009-07-23 18:56:43 +02001544 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1545 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001546
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001548 return -EIO;
1549
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001550 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 return -EIO;
1552
1553 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001554 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001555}
1556
Harald Welteda7ab742009-12-19 22:23:05 +01001557/* enable receive of channels to MNCC upqueue */
1558static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001559{
1560 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001561 struct gsm_lchan *lchan;
1562 struct gsm_bts *bts;
1563 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001564
Harald Welte4bfdfe72009-06-10 23:11:52 +08001565 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001566 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567 if (!trans)
1568 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001569 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001570 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001571 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001572 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573
Harald Welteda7ab742009-12-19 22:23:05 +01001574 switch (bts->type) {
1575 case GSM_BTS_TYPE_NANOBTS:
1576 if (ipacc_rtp_direct) {
1577 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1578 return -EINVAL;
1579 }
1580 /* in case, we don't have a RTP socket yet, we note this
1581 * in the transaction and try later */
1582 if (!lchan->abis_ip.rtp_socket) {
1583 trans->tch_recv = enable;
1584 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1585 return 0;
1586 }
1587 if (enable) {
1588 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001589 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001590 if (rc < 0)
1591 return rc;
1592 /* assign socket to application interface */
1593 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1594 net, callref);
1595 } else
1596 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1597 net, 0);
1598 break;
1599 case GSM_BTS_TYPE_BS11:
1600 if (enable)
1601 return trau_recv_lchan(lchan, callref);
1602 return trau_mux_unmap(NULL, callref);
1603 break;
1604 default:
1605 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1606 return -EINVAL;
1607 }
1608
1609 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610}
1611
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1613{
1614 DEBUGP(DCC, "-> STATUS ENQ\n");
1615 return gsm48_cc_tx_status(trans, msg);
1616}
1617
1618static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1619static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1620
1621static void gsm48_cc_timeout(void *arg)
1622{
1623 struct gsm_trans *trans = arg;
1624 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001625 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1626 int mo_location = GSM48_CAUSE_LOC_USER;
1627 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1628 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 struct gsm_mncc mo_rel, l4_rel;
1630
1631 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1632 mo_rel.callref = trans->callref;
1633 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1634 l4_rel.callref = trans->callref;
1635
Harald Weltedcaf5652009-07-23 18:56:43 +02001636 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 case 0x303:
1638 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001639 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 break;
1641 case 0x310:
1642 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001643 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644 break;
1645 case 0x313:
1646 disconnect = 1;
1647 /* unknown, did not find it in the specs */
1648 break;
1649 case 0x301:
1650 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001651 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 break;
1653 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001654 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001656 gsm48_cc_tx_release(trans, &trans->cc.msg);
1657 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 break; /* stay in release state */
1659 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001660 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 return;
1662// release = 1;
1663// l4_cause = 14;
1664// break;
1665 case 0x306:
1666 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001667 mo_cause = trans->cc.msg.cause.value;
1668 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 break;
1670 case 0x323:
1671 disconnect = 1;
1672 break;
1673 default:
1674 release = 1;
1675 }
1676
1677 if (release && trans->callref) {
1678 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001679 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 l4_location, l4_cause);
1681 trans->callref = 0;
1682 }
1683
1684 if (disconnect && trans->callref) {
1685 /* process disconnect towards layer 4 */
1686 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001687 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 }
1689
1690 /* process disconnect towards mobile station */
1691 if (disconnect || release) {
1692 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1694 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1695 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696 mo_rel.cause.diag_len = 3;
1697
1698 if (disconnect)
1699 gsm48_cc_tx_disconnect(trans, &mo_rel);
1700 if (release)
1701 gsm48_cc_tx_release(trans, &mo_rel);
1702 }
1703
1704}
1705
1706static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1707 int sec, int micro)
1708{
1709 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001710 trans->cc.timer.cb = gsm48_cc_timeout;
1711 trans->cc.timer.data = trans;
1712 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1713 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714}
1715
1716static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1717{
1718 struct gsm48_hdr *gh = msgb_l3(msg);
1719 u_int8_t msg_type = gh->msg_type & 0xbf;
1720 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1721 struct tlv_parsed tp;
1722 struct gsm_mncc setup;
1723
1724 memset(&setup, 0, sizeof(struct gsm_mncc));
1725 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001726 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 /* emergency setup is identified by msg_type */
1728 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1729 setup.emergency = 1;
1730
1731 /* use subscriber as calling party number */
1732 if (trans->subscr) {
1733 setup.fields |= MNCC_F_CALLING;
1734 strncpy(setup.calling.number, trans->subscr->extension,
1735 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001736 strncpy(setup.imsi, trans->subscr->imsi,
1737 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738 }
1739 /* bearer capability */
1740 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1741 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001742 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1744 }
1745 /* facility */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1747 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001748 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1750 }
1751 /* called party bcd number */
1752 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1753 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001754 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1756 }
1757 /* user-user */
1758 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1759 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001760 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1762 }
1763 /* ss-version */
1764 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1765 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001766 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1768 }
1769 /* CLIR suppression */
1770 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1771 setup.clir.sup = 1;
1772 /* CLIR invocation */
1773 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1774 setup.clir.inv = 1;
1775 /* cc cap */
1776 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1777 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001778 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1780 }
1781
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 new_cc_state(trans, GSM_CSTATE_INITIATED);
1783
Harald Welte (local)47df3992009-12-26 19:45:03 +01001784 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1785 subscr_name(trans->subscr), trans->subscr->extension,
1786 setup.called.number);
1787
Harald Weltea29e43a2010-12-24 16:06:33 +01001788 counter_inc(trans->subscr->net->stats.call.mo_setup);
1789
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001791 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792
Harald Welte13cac662009-07-29 12:10:35 +02001793 /* MNCC code will modify the channel asynchronously, we should
1794 * ipaccess-bind only after the modification has been made to the
1795 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 return 0;
1797}
1798
1799static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001800{
1801 struct msgb *msg = gsm48_msgb_alloc();
1802 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001804 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001805
Harald Welte7ccf7782009-02-17 01:43:01 +00001806 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001807
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* transaction id must not be assigned */
1809 if (trans->transaction_id != 0xff) { /* unasssigned */
1810 DEBUGP(DCC, "TX Setup with assigned transaction. "
1811 "This is not allowed!\n");
1812 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001813 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001814 GSM48_CAUSE_LOC_PRN_S_LU,
1815 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001817 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 return rc;
1819 }
1820
1821 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001822 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1823 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001825 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001826 GSM48_CAUSE_LOC_PRN_S_LU,
1827 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001828 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001829 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 return rc;
1831 }
Harald Welte78283ef2009-07-23 21:36:44 +02001832 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001833
Harald Welte65e74cc2008-12-29 01:55:35 +00001834 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001835
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001837
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 /* bearer capability */
1839 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001840 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 /* facility */
1842 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001843 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 /* progress */
1845 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 /* calling party BCD number */
1848 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 /* called party BCD number */
1851 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 /* user-user */
1854 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 /* redirecting party BCD number */
1857 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 /* signal */
1860 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001861 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862
1863 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001864
Harald Weltea29e43a2010-12-24 16:06:33 +01001865 counter_inc(trans->subscr->net->stats.call.mt_setup);
1866
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001867 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001868}
1869
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1871{
1872 struct gsm48_hdr *gh = msgb_l3(msg);
1873 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1874 struct tlv_parsed tp;
1875 struct gsm_mncc call_conf;
1876
1877 gsm48_stop_cc_timer(trans);
1878 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1879
1880 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1881 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001882 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883#if 0
1884 /* repeat */
1885 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1886 call_conf.repeat = 1;
1887 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1888 call_conf.repeat = 2;
1889#endif
1890 /* bearer capability */
1891 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1892 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001893 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1895 }
1896 /* cause */
1897 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1898 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001899 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1901 }
1902 /* cc cap */
1903 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1904 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001905 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1907 }
1908
1909 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1910
Harald Welte596fed42009-07-23 19:06:52 +02001911 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1912 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913}
1914
1915static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1916{
1917 struct gsm_mncc *proceeding = arg;
1918 struct msgb *msg = gsm48_msgb_alloc();
1919 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1920
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1922
1923 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1924
1925 /* bearer capability */
1926 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 /* facility */
1929 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 /* progress */
1932 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001933 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001935 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936}
1937
1938static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1939{
1940 struct gsm48_hdr *gh = msgb_l3(msg);
1941 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1942 struct tlv_parsed tp;
1943 struct gsm_mncc alerting;
1944
1945 gsm48_stop_cc_timer(trans);
1946 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1947
1948 memset(&alerting, 0, sizeof(struct gsm_mncc));
1949 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001950 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* facility */
1952 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1953 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1956 }
1957
1958 /* progress */
1959 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1960 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1963 }
1964 /* ss-version */
1965 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1966 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1969 }
1970
1971 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1972
Harald Welte596fed42009-07-23 19:06:52 +02001973 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1974 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975}
1976
1977static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1978{
1979 struct gsm_mncc *alerting = arg;
1980 struct msgb *msg = gsm48_msgb_alloc();
1981 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1982
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 gh->msg_type = GSM48_MT_CC_ALERTING;
1984
1985 /* facility */
1986 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001987 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* progress */
1989 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 /* user-user */
1992 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001993 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994
1995 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1996
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001997 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998}
1999
2000static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2001{
2002 struct gsm_mncc *progress = arg;
2003 struct msgb *msg = gsm48_msgb_alloc();
2004 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2005
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 gh->msg_type = GSM48_MT_CC_PROGRESS;
2007
2008 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 /* user-user */
2011 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002014 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015}
2016
2017static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2018{
2019 struct gsm_mncc *connect = arg;
2020 struct msgb *msg = gsm48_msgb_alloc();
2021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2022
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 gh->msg_type = GSM48_MT_CC_CONNECT;
2024
2025 gsm48_stop_cc_timer(trans);
2026 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2027
2028 /* facility */
2029 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 /* progress */
2032 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002033 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 /* connected number */
2035 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 /* user-user */
2038 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002039 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040
2041 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2042
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_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2047{
2048 struct gsm48_hdr *gh = msgb_l3(msg);
2049 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2050 struct tlv_parsed tp;
2051 struct gsm_mncc connect;
2052
2053 gsm48_stop_cc_timer(trans);
2054
2055 memset(&connect, 0, sizeof(struct gsm_mncc));
2056 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002057 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 /* use subscriber as connected party number */
2059 if (trans->subscr) {
2060 connect.fields |= MNCC_F_CONNECTED;
2061 strncpy(connect.connected.number, trans->subscr->extension,
2062 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002063 strncpy(connect.imsi, trans->subscr->imsi,
2064 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 }
2066 /* facility */
2067 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2068 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002069 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2071 }
2072 /* user-user */
2073 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2074 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2077 }
2078 /* ss-version */
2079 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2080 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002081 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2083 }
2084
2085 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltea29e43a2010-12-24 16:06:33 +01002086 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087
Harald Welte596fed42009-07-23 19:06:52 +02002088 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089}
2090
2091
2092static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2093{
2094 struct gsm_mncc connect_ack;
2095
2096 gsm48_stop_cc_timer(trans);
2097
2098 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltea29e43a2010-12-24 16:06:33 +01002099 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
2101 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2102 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002103
Harald Welte596fed42009-07-23 19:06:52 +02002104 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 &connect_ack);
2106}
2107
2108static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2109{
2110 struct msgb *msg = gsm48_msgb_alloc();
2111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2112
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2114
2115 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2116
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002117 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118}
2119
2120static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2121{
2122 struct gsm48_hdr *gh = msgb_l3(msg);
2123 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2124 struct tlv_parsed tp;
2125 struct gsm_mncc disc;
2126
2127 gsm48_stop_cc_timer(trans);
2128
2129 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2130
2131 memset(&disc, 0, sizeof(struct gsm_mncc));
2132 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002133 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 /* cause */
2135 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2136 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2139 }
2140 /* facility */
2141 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2142 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2145 }
2146 /* user-user */
2147 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2148 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002149 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2151 }
2152 /* ss-version */
2153 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2154 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002155 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2157 }
2158
Harald Welte596fed42009-07-23 19:06:52 +02002159 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160
2161}
2162
Harald Weltec66b71c2009-06-11 14:23:20 +08002163static struct gsm_mncc_cause default_cause = {
2164 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2165 .coding = 0,
2166 .rec = 0,
2167 .rec_val = 0,
2168 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2169 .diag_len = 0,
2170 .diag = { 0 },
2171};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172
2173static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2174{
2175 struct gsm_mncc *disc = arg;
2176 struct msgb *msg = gsm48_msgb_alloc();
2177 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2178
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2180
2181 gsm48_stop_cc_timer(trans);
2182 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2183
2184 /* cause */
2185 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 else
Harald Welte55c8f352010-03-07 23:40:35 +01002188 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189
2190 /* facility */
2191 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 /* progress */
2194 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002195 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 /* user-user */
2197 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002198 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199
2200 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002201 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202
2203 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2204
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002205 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206}
2207
2208static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2209{
2210 struct gsm48_hdr *gh = msgb_l3(msg);
2211 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2212 struct tlv_parsed tp;
2213 struct gsm_mncc rel;
2214 int rc;
2215
2216 gsm48_stop_cc_timer(trans);
2217
2218 memset(&rel, 0, sizeof(struct gsm_mncc));
2219 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002220 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 /* cause */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2223 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2226 }
2227 /* facility */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2229 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2232 }
2233 /* user-user */
2234 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2235 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2238 }
2239 /* ss-version */
2240 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2241 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2244 }
2245
Harald Weltedcaf5652009-07-23 18:56:43 +02002246 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002248 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002250 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002251 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002252 GSM48_MT_CC_RELEASE_COMPL);
2253 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 }
2255
2256 new_cc_state(trans, GSM_CSTATE_NULL);
2257
2258 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002259 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260
2261 return rc;
2262}
2263
2264static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2265{
2266 struct gsm_mncc *rel = arg;
2267 struct msgb *msg = gsm48_msgb_alloc();
2268 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2269
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270 gh->msg_type = GSM48_MT_CC_RELEASE;
2271
2272 trans->callref = 0;
2273
2274 gsm48_stop_cc_timer(trans);
2275 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2276
2277 /* cause */
2278 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 /* facility */
2281 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002282 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 /* user-user */
2284 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002285 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286
Harald Weltedcaf5652009-07-23 18:56:43 +02002287 trans->cc.T308_second = 0;
2288 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
Harald Weltedcaf5652009-07-23 18:56:43 +02002290 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2292
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002293 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294}
2295
2296static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2297{
2298 struct gsm48_hdr *gh = msgb_l3(msg);
2299 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2300 struct tlv_parsed tp;
2301 struct gsm_mncc rel;
2302 int rc = 0;
2303
2304 gsm48_stop_cc_timer(trans);
2305
2306 memset(&rel, 0, sizeof(struct gsm_mncc));
2307 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002308 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 /* cause */
2310 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2311 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002312 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2314 }
2315 /* facility */
2316 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2317 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002318 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2320 }
2321 /* user-user */
2322 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2323 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002324 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2326 }
2327 /* ss-version */
2328 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2329 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002330 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2332 }
2333
2334 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002335 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002337 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 MNCC_REJ_IND, &rel);
2339 break;
2340 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002341 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 MNCC_REL_CNF, &rel);
2343 break;
2344 default:
Harald Welte596fed42009-07-23 19:06:52 +02002345 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 MNCC_REL_IND, &rel);
2347 }
2348 }
2349
2350 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002351 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352
2353 return rc;
2354}
2355
2356static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2357{
2358 struct gsm_mncc *rel = arg;
2359 struct msgb *msg = gsm48_msgb_alloc();
2360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002361 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2364
2365 trans->callref = 0;
2366
2367 gsm48_stop_cc_timer(trans);
2368
2369 /* cause */
2370 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 /* facility */
2373 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002374 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 /* user-user */
2376 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002377 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378
Harald Weltec7782de2010-12-21 19:31:41 +01002379 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2380
Harald Weltedcaf5652009-07-23 18:56:43 +02002381 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382
Harald Weltec7782de2010-12-21 19:31:41 +01002383 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384}
2385
2386static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2387{
2388 struct gsm48_hdr *gh = msgb_l3(msg);
2389 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2390 struct tlv_parsed tp;
2391 struct gsm_mncc fac;
2392
2393 memset(&fac, 0, sizeof(struct gsm_mncc));
2394 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002395 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 /* facility */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2398 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2401 }
2402 /* ss-version */
2403 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2404 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2407 }
2408
Harald Welte596fed42009-07-23 19:06:52 +02002409 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2413{
2414 struct gsm_mncc *fac = arg;
2415 struct msgb *msg = gsm48_msgb_alloc();
2416 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2417
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 gh->msg_type = GSM48_MT_CC_FACILITY;
2419
2420 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002421 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002423 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424}
2425
2426static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2427{
2428 struct gsm_mncc hold;
2429
2430 memset(&hold, 0, sizeof(struct gsm_mncc));
2431 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002432 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433}
2434
2435static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2436{
2437 struct msgb *msg = gsm48_msgb_alloc();
2438 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2439
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2441
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002442 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443}
2444
2445static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2446{
2447 struct gsm_mncc *hold_rej = arg;
2448 struct msgb *msg = gsm48_msgb_alloc();
2449 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2450
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2452
2453 /* cause */
2454 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002455 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456 else
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002459 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460}
2461
2462static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2463{
2464 struct gsm_mncc retrieve;
2465
2466 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2467 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002468 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2469 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470}
2471
2472static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2473{
2474 struct msgb *msg = gsm48_msgb_alloc();
2475 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2476
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2478
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002479 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480}
2481
2482static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2483{
2484 struct gsm_mncc *retrieve_rej = arg;
2485 struct msgb *msg = gsm48_msgb_alloc();
2486 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2487
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2489
2490 /* cause */
2491 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 else
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002496 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497}
2498
2499static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2500{
2501 struct gsm48_hdr *gh = msgb_l3(msg);
2502 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2503 struct tlv_parsed tp;
2504 struct gsm_mncc dtmf;
2505
2506 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2507 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002508 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509 /* keypad facility */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2511 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002512 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2514 }
2515
Harald Welte596fed42009-07-23 19:06:52 +02002516 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2520{
2521 struct gsm_mncc *dtmf = arg;
2522 struct msgb *msg = gsm48_msgb_alloc();
2523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2526
2527 /* keypad */
2528 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002529 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002531 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532}
2533
2534static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2535{
2536 struct gsm_mncc *dtmf = arg;
2537 struct msgb *msg = gsm48_msgb_alloc();
2538 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2539
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2541
2542 /* cause */
2543 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 else
Harald Welte55c8f352010-03-07 23:40:35 +01002546 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002548 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549}
2550
2551static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2552{
2553 struct msgb *msg = gsm48_msgb_alloc();
2554 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2555
Harald Welte4bfdfe72009-06-10 23:11:52 +08002556 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2557
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002558 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2562{
2563 struct gsm_mncc dtmf;
2564
2565 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2566 dtmf.callref = trans->callref;
2567
Harald Welte596fed42009-07-23 19:06:52 +02002568 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569}
2570
2571static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2572{
2573 struct gsm48_hdr *gh = msgb_l3(msg);
2574 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2575 struct tlv_parsed tp;
2576 struct gsm_mncc modify;
2577
2578 memset(&modify, 0, sizeof(struct gsm_mncc));
2579 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002580 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 /* bearer capability */
2582 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2583 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002584 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2586 }
2587
2588 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2589
Harald Welte596fed42009-07-23 19:06:52 +02002590 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591}
2592
2593static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2594{
2595 struct gsm_mncc *modify = arg;
2596 struct msgb *msg = gsm48_msgb_alloc();
2597 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2598
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599 gh->msg_type = GSM48_MT_CC_MODIFY;
2600
2601 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2602
2603 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002604 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605
2606 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2607
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002608 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609}
2610
2611static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2612{
2613 struct gsm48_hdr *gh = msgb_l3(msg);
2614 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2615 struct tlv_parsed tp;
2616 struct gsm_mncc modify;
2617
2618 gsm48_stop_cc_timer(trans);
2619
2620 memset(&modify, 0, sizeof(struct gsm_mncc));
2621 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002622 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 /* bearer capability */
2624 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2625 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2628 }
2629
2630 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2631
Harald Welte596fed42009-07-23 19:06:52 +02002632 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *modify = arg;
2638 struct msgb *msg = gsm48_msgb_alloc();
2639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2642
2643 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002644 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
2646 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2647
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002648 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649}
2650
2651static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2652{
2653 struct gsm48_hdr *gh = msgb_l3(msg);
2654 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2655 struct tlv_parsed tp;
2656 struct gsm_mncc modify;
2657
2658 gsm48_stop_cc_timer(trans);
2659
2660 memset(&modify, 0, sizeof(struct gsm_mncc));
2661 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002662 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 /* bearer capability */
2664 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2665 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002666 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2668 }
2669 /* cause */
2670 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2671 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002672 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2674 }
2675
2676 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2677
Harald Welte596fed42009-07-23 19:06:52 +02002678 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679}
2680
2681static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2682{
2683 struct gsm_mncc *modify = arg;
2684 struct msgb *msg = gsm48_msgb_alloc();
2685 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2686
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2688
2689 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002690 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002692 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693
2694 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2695
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002696 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697}
2698
2699static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2700{
2701 struct gsm_mncc *notify = arg;
2702 struct msgb *msg = gsm48_msgb_alloc();
2703 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2704
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 gh->msg_type = GSM48_MT_CC_NOTIFY;
2706
2707 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002708 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002710 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711}
2712
2713static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2714{
2715 struct gsm48_hdr *gh = msgb_l3(msg);
2716 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2717// struct tlv_parsed tp;
2718 struct gsm_mncc notify;
2719
2720 memset(&notify, 0, sizeof(struct gsm_mncc));
2721 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002722// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002724 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725
Harald Welte596fed42009-07-23 19:06:52 +02002726 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727}
2728
2729static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2730{
2731 struct gsm_mncc *user = arg;
2732 struct msgb *msg = gsm48_msgb_alloc();
2733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2734
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 gh->msg_type = GSM48_MT_CC_USER_INFO;
2736
2737 /* user-user */
2738 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002739 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 /* more data */
2741 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002742 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002744 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745}
2746
2747static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2748{
2749 struct gsm48_hdr *gh = msgb_l3(msg);
2750 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2751 struct tlv_parsed tp;
2752 struct gsm_mncc user;
2753
2754 memset(&user, 0, sizeof(struct gsm_mncc));
2755 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002756 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 /* user-user */
2758 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2759 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002760 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2762 }
2763 /* more data */
2764 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2765 user.more = 1;
2766
Harald Welte596fed42009-07-23 19:06:52 +02002767 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768}
2769
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002770static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002771{
2772 struct gsm_mncc *mode = arg;
2773
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002774 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775}
2776
2777static struct downstate {
2778 u_int32_t states;
2779 int type;
2780 int (*rout) (struct gsm_trans *trans, void *arg);
2781} downstatelist[] = {
2782 /* mobile originating call establishment */
2783 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2784 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2785 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2786 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2787 {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 */
2788 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2789 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2790 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2791 /* mobile terminating call establishment */
2792 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2793 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2794 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2795 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2796 /* signalling during call */
2797 {SBIT(GSM_CSTATE_ACTIVE),
2798 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2799 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2800 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2801 {ALL_STATES,
2802 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2803 {ALL_STATES,
2804 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2805 {ALL_STATES,
2806 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2807 {SBIT(GSM_CSTATE_ACTIVE),
2808 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2809 {SBIT(GSM_CSTATE_ACTIVE),
2810 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2811 {SBIT(GSM_CSTATE_ACTIVE),
2812 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2813 {SBIT(GSM_CSTATE_ACTIVE),
2814 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2815 {SBIT(GSM_CSTATE_ACTIVE),
2816 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2817 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2818 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2819 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2820 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2821 {SBIT(GSM_CSTATE_ACTIVE),
2822 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2823 /* clearing */
2824 {SBIT(GSM_CSTATE_INITIATED),
2825 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2826 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2827 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2828 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2829 MNCC_REL_REQ, gsm48_cc_tx_release},
2830 /* special */
2831 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002832 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833};
2834
2835#define DOWNSLLEN \
2836 (sizeof(downstatelist) / sizeof(struct downstate))
2837
2838
Harald Welte76556372010-12-22 23:57:45 +01002839int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840{
Harald Welte1a6f7982009-08-09 18:52:33 +02002841 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002843 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845 struct gsm_mncc *data = arg, rel;
2846
Harald Welte04dc88f2010-12-22 21:45:05 +01002847 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2848
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849 /* handle special messages */
2850 switch(msg_type) {
2851 case MNCC_BRIDGE:
2852 return tch_bridge(net, arg);
2853 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002854 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002856 return tch_recv_mncc(net, data->callref, 1);
2857 case GSM_TCHF_FRAME:
2858 /* Find callref */
2859 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002860 if (!trans) {
2861 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002862 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002863 }
2864 if (!trans->conn) {
2865 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002866 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002867 }
2868 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2869 /* This should be LOGL_ERROR or NOTICE, but
2870 * unfortuantely it happens for a couple of frames at
2871 * the beginning of every RTP connection */
2872 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002873 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002874 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002875 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002876 switch (bts->type) {
2877 case GSM_BTS_TYPE_NANOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01002878 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002879 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002880 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002881 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002882 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002883 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002884 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002885 default:
2886 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2887 }
2888 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 }
2890
2891 memset(&rel, 0, sizeof(struct gsm_mncc));
2892 rel.callref = data->callref;
2893
2894 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002895 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896
2897 /* Callref unknown */
2898 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002899 struct gsm_subscriber *subscr;
2900
Harald Welte4a3464c2009-07-04 10:11:24 +02002901 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2903 "Received '%s' from MNCC with "
2904 "unknown callref %d\n", data->called.number,
2905 get_mncc_name(msg_type), data->callref);
2906 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002907 return mncc_release_ind(net, NULL, data->callref,
2908 GSM48_CAUSE_LOC_PRN_S_LU,
2909 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002911 if (!data->called.number[0] && !data->imsi[0]) {
2912 DEBUGP(DCC, "(bts - trx - ts - ti) "
2913 "Received '%s' from MNCC with "
2914 "no number or IMSI\n", get_mncc_name(msg_type));
2915 /* Invalid number */
2916 return mncc_release_ind(net, NULL, data->callref,
2917 GSM48_CAUSE_LOC_PRN_S_LU,
2918 GSM48_CC_CAUSE_INV_NR_FORMAT);
2919 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002921 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002922 subscr = subscr_get_by_extension(net,
2923 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002924 else
Harald Welte9176bd42009-07-23 18:46:00 +02002925 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926 /* If subscriber is not found */
2927 if (!subscr) {
2928 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2929 "Received '%s' from MNCC with "
2930 "unknown subscriber %s\n", data->called.number,
2931 get_mncc_name(msg_type), data->called.number);
2932 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002933 return mncc_release_ind(net, NULL, data->callref,
2934 GSM48_CAUSE_LOC_PRN_S_LU,
2935 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 }
2937 /* If subscriber is not "attached" */
2938 if (!subscr->lac) {
2939 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2940 "Received '%s' from MNCC with "
2941 "detached subscriber %s\n", data->called.number,
2942 get_mncc_name(msg_type), data->called.number);
2943 subscr_put(subscr);
2944 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002945 return mncc_release_ind(net, NULL, data->callref,
2946 GSM48_CAUSE_LOC_PRN_S_LU,
2947 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 }
2949 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002950 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2951 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 DEBUGP(DCC, "No memory for trans.\n");
2953 subscr_put(subscr);
2954 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002955 mncc_release_ind(net, NULL, data->callref,
2956 GSM48_CAUSE_LOC_PRN_S_LU,
2957 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958 return -ENOMEM;
2959 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002961 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002962
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002964 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 /* find transaction with this subscriber already paging */
2966 llist_for_each_entry(transt, &net->trans_list, entry) {
2967 /* Transaction of our lchan? */
2968 if (transt == trans ||
2969 transt->subscr != subscr)
2970 continue;
2971 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2972 "Received '%s' from MNCC with "
2973 "unallocated channel, paging already "
2974 "started.\n", bts->nr,
2975 data->called.number,
2976 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002977 subscr_put(subscr);
2978 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979 return 0;
2980 }
2981 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002982 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002983
2984 /* Get a channel */
2985 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2986
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002987 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 return 0;
2989 }
2990 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002991 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002992 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002994
2995 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002996 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997
2998 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002999 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003001 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 (trans->subscr)?(trans->subscr->extension):"-",
3003 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003004 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3005 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 if (msg_type == MNCC_REL_REQ)
3007 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3008 else
3009 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3010 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003011 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 return rc;
3013 }
3014
3015 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3016 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003017 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003019 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003020 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003021 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022
3023 /* Find function for current state and message */
3024 for (i = 0; i < DOWNSLLEN; i++)
3025 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003026 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 break;
3028 if (i == DOWNSLLEN) {
3029 DEBUGP(DCC, "Message unhandled at this state.\n");
3030 return 0;
3031 }
3032
3033 rc = downstatelist[i].rout(trans, arg);
3034
3035 return rc;
3036}
3037
3038
3039static struct datastate {
3040 u_int32_t states;
3041 int type;
3042 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3043} datastatelist[] = {
3044 /* mobile originating call establishment */
3045 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3046 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3047 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3048 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3049 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3050 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3051 /* mobile terminating call establishment */
3052 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3053 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3054 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3055 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003056 {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 +08003057 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3058 /* signalling during call */
3059 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3060 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3061 {SBIT(GSM_CSTATE_ACTIVE),
3062 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3063 {ALL_STATES,
3064 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3065 {ALL_STATES,
3066 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3067 {ALL_STATES,
3068 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3069 {SBIT(GSM_CSTATE_ACTIVE),
3070 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3071 {SBIT(GSM_CSTATE_ACTIVE),
3072 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3073 {SBIT(GSM_CSTATE_ACTIVE),
3074 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3075 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3076 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3077 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3078 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3079 {SBIT(GSM_CSTATE_ACTIVE),
3080 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3081 /* clearing */
3082 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3083 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3084 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3085 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3086 {ALL_STATES, /* 5.4.3.4 */
3087 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3088};
3089
3090#define DATASLLEN \
3091 (sizeof(datastatelist) / sizeof(struct datastate))
3092
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003093static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003094{
3095 struct gsm48_hdr *gh = msgb_l3(msg);
3096 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003097 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003098 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003100
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 if (msg_type & 0x80) {
3102 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3103 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003104 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003105
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003107 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003108
Harald Welte6f5aee02009-07-23 21:21:14 +02003109 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003111 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003112 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003113 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3114 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003115
3116 /* Create transaction */
3117 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003118 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 "creating new trans.\n", transaction_id);
3120 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003121 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003122 transaction_id, new_callref++);
3123 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003124 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003125 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003126 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127 GSM48_MT_CC_RELEASE_COMPL);
3128 return -ENOMEM;
3129 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003131 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 }
3133
3134 /* find function for current state and message */
3135 for (i = 0; i < DATASLLEN; i++)
3136 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003137 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 break;
3139 if (i == DATASLLEN) {
3140 DEBUGP(DCC, "Message unhandled at this state.\n");
3141 return 0;
3142 }
3143
3144 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003145
3146 return rc;
3147}
3148
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003149/* Create a dummy to wait five seconds */
3150static void release_anchor(struct gsm_subscriber_connection *conn)
3151{
3152 if (!conn->anch_operation)
3153 return;
3154
3155 bsc_del_timer(&conn->anch_operation->timeout);
3156 talloc_free(conn->anch_operation);
3157 conn->anch_operation = NULL;
3158}
3159
3160static void anchor_timeout(void *_data)
3161{
3162 struct gsm_subscriber_connection *con = _data;
3163
3164 release_anchor(con);
3165 msc_release_connection(con);
3166}
3167
3168int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3169{
3170 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3171 if (!conn->anch_operation)
3172 return -1;
3173
3174 conn->anch_operation->timeout.data = conn;
3175 conn->anch_operation->timeout.cb = anchor_timeout;
3176 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3177 return 0;
3178}
3179
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003180/* here we get data from the BSC level... */
3181int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003182{
3183 struct gsm48_hdr *gh = msgb_l3(msg);
3184 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003185 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003186
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003187 if (silent_call_reroute(conn, msg))
3188 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003189
3190 switch (pdisc) {
3191 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003192 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003193 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003194 break;
3195 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003196 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003197 break;
3198 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003199 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003200 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003201 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003202 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003203 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003204 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003205 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003206 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003207 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3208 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003209 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003210 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003211 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003212 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003213 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003214 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003215 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3216 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003217 break;
3218 }
3219
3220 return rc;
3221}
Harald Welte8470bf22008-12-25 23:28:35 +00003222
Harald Welte805f6442009-07-28 18:25:29 +02003223/*
3224 * This will be ran by the linker when loading the DSO. We use it to
3225 * do system initialization, e.g. registration of signal handlers.
3226 */
3227static __attribute__((constructor)) void on_dso_load_0408(void)
3228{
Harald Welte805f6442009-07-28 18:25:29 +02003229 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3230}