blob: 9ab8840dbb500de01089b80a3392423c125ed2a5 [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +08005 * (C) 2008-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte59b04682009-06-10 05:40:52 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080018 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte59b04682009-06-10 05:40:52 +080021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
Sylvain Munaut28c47162009-12-24 00:27:26 +010032#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
37#include <openbsc/gsm_04_11.h>
38#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080039#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080040#include <openbsc/abis_rsl.h>
41#include <openbsc/chan_alloc.h>
42#include <openbsc/paging.h>
43#include <openbsc/signal.h>
44#include <openbsc/trau_frame.h>
45#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020046#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020047#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020048#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010049#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080050#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080051#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080052#include <osmocore/bitvec.h>
53
54#include <osmocore/gsm48.h>
Holger Hans Peter Freythera5386d12010-09-30 18:53:47 +080055#include <osmocore/gsm0480.h>
Holger Hans Peter Freyther89253132010-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 Welte59b04682009-06-10 05:40:52 +080060
Harald Welte (local)8751ee92009-08-15 02:30:58 +020061void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010062void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020063
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +080064int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080065static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +080066 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010067static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080068static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080069
70struct gsm_lai {
71 u_int16_t mcc;
72 u_int16_t mnc;
73 u_int16_t lac;
74};
75
Harald Welte03740842009-06-10 23:11:52 +080076static u_int32_t new_callref = 0x80000001;
77
Holger Hans Peter Freyther5bc4d1d2010-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 Freyther6bfcad82010-11-10 10:16:02 +0100106 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800107}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100108
Holger Hans Peter Freyther9aa29b52010-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 Munaut28c47162009-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 Freyther6e5c50f2010-06-28 17:09:29 +0800133 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100134}
135
136static void allocate_security_operation(struct gsm_subscriber_connection *conn)
137{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100138 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
139 struct gsm_security_operation);
140}
141
Sylvain Munautc0bdbb42010-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 Munaut28c47162009-12-24 00:27:26 +0100144{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800145 struct gsm_network *net = conn->bts->network;
146 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-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 Freyther9acd1b12010-06-16 12:47:59 +0800158 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-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 Freyther9acd1b12010-06-16 12:47:59 +0800177 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100178 0;
179
180 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800181 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100182 return -EBUSY;
183
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800184 allocate_security_operation(conn);
185 op = conn->sec_operation;
Sylvain Munaut28c47162009-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 Welte857e15d2010-12-23 18:07:49 +0100193 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100194 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800195 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100196 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100197 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100198 return gsm0808_cipher_mode(conn, net->a5_encryption,
199 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100200 }
201
202 return -EINVAL; /* not reached */
203}
204
Harald Welte59b04682009-06-10 05:40:52 +0800205static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
206 struct gsm_subscriber *subscriber)
207{
208 if (!subscriber)
209 return 0;
210
211 /*
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 Luebbe3d6294e2009-08-12 12:48:00 +0200219 switch (subscriber->net->auth_policy) {
220 case GSM_AUTH_POLICY_CLOSED:
221 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200222 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200223 if (subscriber->authorized)
224 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200225 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200226 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800227 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200228 default:
229 return 0;
230 }
Harald Welte59b04682009-06-10 05:40:52 +0800231}
232
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100233static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800234{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100235 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800236 return;
237
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800238 /* No need to keep the connection up */
239 release_anchor(conn);
240
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100241 bsc_del_timer(&conn->loc_operation->updating_timer);
242 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100243 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800244 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800245}
246
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100247static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800248{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800249 if (conn->loc_operation)
250 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100251 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800252
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100253 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200254 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800255}
256
Sylvain Munautcb321fb2009-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 Freyther9acd1b12010-06-16 12:47:59 +0800260 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-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 Freytherfa2bafd2010-06-15 14:07:27 +0800272 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800273 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100274 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800275 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-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 Freyther9acd1b12010-06-16 12:47:59 +0800281 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100282 GSM_SUBSCRIBER_UPDATE_ATTACHED);
283
Holger Hans Peter Freytherf08a1d72010-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 Munautcb321fb2009-12-24 00:28:01 +0100289 break;
290
291 default:
292 rc = -EINVAL;
293 };
294
295 return rc;
296}
297
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100298static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800299{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100300 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800301 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100302 conn->loc_operation->key_seq,
303 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800304 return 0;
305}
306
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800307void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800308{
Harald Welte03740842009-06-10 23:11:52 +0800309 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800310
311 /* avoid someone issuing a clear */
312 conn->in_release = 1;
313
Harald Welte59b04682009-06-10 05:40:52 +0800314 /*
315 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800316 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800317 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800318 release_loc_updating_req(conn);
319 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800320 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800321
Harald Welte03740842009-06-10 23:11:52 +0800322 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-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 Freythered4e3bd2010-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 Weltec2189a62009-07-23 18:56:43 +0200328 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800329 }
Harald Welte59b04682009-06-10 05:40:52 +0800330}
331
Harald Weltee8d16172010-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) {
339 if (trans->protocol == protocol)
340 trans_free(trans);
341 }
342}
343
Harald Welte59b04682009-06-10 05:40:52 +0800344/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800345int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800346{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800347 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800348 struct msgb *msg;
349
350 counter_inc(bts->network->stats.loc_upd_resp.reject);
351
352 msg = gsm48_create_loc_upd_rej(cause);
353 if (!msg) {
354 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
355 return -1;
356 }
Harald Welte59b04682009-06-10 05:40:52 +0800357
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800358 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800359
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100360 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100361 "LAC=%u BTS=%u\n", conn->subscr ?
362 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800363 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100364
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800365 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800366}
367
368/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800369int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800370{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800371 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800372 struct msgb *msg = gsm48_msgb_alloc();
373 struct gsm48_hdr *gh;
374 struct gsm48_loc_area_id *lai;
375 u_int8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800376
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800377 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800378
379 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
380 gh->proto_discr = GSM48_PDISC_MM;
381 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
382
383 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100384 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800385 bts->network->network_code, bts->location_area_code);
386
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200387 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200388 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800389
390 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
391
Harald Weltebdbb7442009-12-22 19:07:32 +0100392 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100393
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800394 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800395}
396
Harald Welte59b04682009-06-10 05:40:52 +0800397/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800398static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800399{
400 struct msgb *msg = gsm48_msgb_alloc();
401 struct gsm48_hdr *gh;
402
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800403 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800404
405 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
406 gh->proto_discr = GSM48_PDISC_MM;
407 gh->msg_type = GSM48_MT_MM_ID_REQ;
408 gh->data[0] = id_type;
409
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800410 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800411}
412
Harald Welte59b04682009-06-10 05:40:52 +0800413
414/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800415static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800416{
417 struct gsm48_hdr *gh = msgb_l3(msg);
418 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200419 struct gsm_bts *bts = lchan->ts->trx->bts;
420 struct gsm_network *net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800421 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200422 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800423
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200424 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800425 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
426 mi_type, mi_string);
427
Harald Welteedf48af2009-12-13 12:39:18 +0100428 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
429
Harald Welte59b04682009-06-10 05:40:52 +0800430 switch (mi_type) {
431 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200432 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100433 if (!conn->subscr) {
434 conn->subscr = subscr_get_by_imsi(net, mi_string);
435 if (!conn->subscr)
436 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200437 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100438 if (conn->loc_operation)
439 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800440 break;
441 case GSM_MI_TYPE_IMEI:
442 case GSM_MI_TYPE_IMEISV:
443 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100444 if (conn->subscr) {
445 db_subscriber_assoc_imei(conn->subscr, mi_string);
446 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200447 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100448 if (conn->loc_operation)
449 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800450 break;
451 }
452
453 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100454 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800455}
456
457
458static void loc_upd_rej_cb(void *data)
459{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100460 struct gsm_subscriber_connection *conn = data;
461 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100462 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800463
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800464 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800465 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800466}
467
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100468static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800469{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100470 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
471 conn->loc_operation->updating_timer.data = conn;
472 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800473}
474
475static const char *lupd_name(u_int8_t type)
476{
477 switch (type) {
478 case GSM48_LUPD_NORMAL:
479 return "NORMAL";
480 case GSM48_LUPD_PERIODIC:
481 return "PEROIDOC";
482 case GSM48_LUPD_IMSI_ATT:
483 return "IMSI ATTACH";
484 default:
485 return "UNKNOWN";
486 }
487}
488
Harald Welte59b04682009-06-10 05:40:52 +0800489/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800490static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800491{
492 struct gsm48_hdr *gh = msgb_l3(msg);
493 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800494 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800495 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800496 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200497 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800498 int rc;
499
500 lu = (struct gsm48_loc_upd_req *) gh->data;
501
502 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
503
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200504 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800505
Harald Welte79639662009-06-27 02:58:43 +0200506 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800507 lupd_name(lu->type));
508
Harald Welteedf48af2009-12-13 12:39:18 +0100509 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
510
Harald Welte3edc5a92009-12-22 00:41:05 +0100511 switch (lu->type) {
512 case GSM48_LUPD_NORMAL:
Harald Weltebdbb7442009-12-22 19:07:32 +0100513 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100514 break;
515 case GSM48_LUPD_IMSI_ATT:
Harald Weltebdbb7442009-12-22 19:07:32 +0100516 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100517 break;
518 case GSM48_LUPD_PERIODIC:
Harald Weltebdbb7442009-12-22 19:07:32 +0100519 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100520 break;
521 }
522
Harald Welte59b04682009-06-10 05:40:52 +0800523 /*
524 * Pseudo Spoof detection: Just drop a second/concurrent
525 * location updating request.
526 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100527 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200528 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100529 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800530 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800531 return 0;
532 }
533
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800534 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800535
Sylvain Munaut360fc582010-06-10 13:36:59 +0200536 conn->loc_operation->key_seq = lu->key_seq;
537
Harald Welte59b04682009-06-10 05:40:52 +0800538 switch (mi_type) {
539 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200540 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800541 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800542 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100543 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800544
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200545 /* look up subscriber based on IMSI, create if not found */
546 subscr = subscr_get_by_imsi(bts->network, mi_string);
547 if (!subscr) {
548 subscr = db_create_subscriber(bts->network, mi_string);
549 }
Harald Welte59b04682009-06-10 05:40:52 +0800550 break;
551 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200552 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800553 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200554 subscr = subscr_get_by_tmsi(bts->network,
555 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800556 if (!subscr) {
557 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800558 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100559 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800560 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200561 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800562 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200563 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800564 break;
565 case GSM_MI_TYPE_IMEI:
566 case GSM_MI_TYPE_IMEISV:
567 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200568 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800569 break;
570 default:
Harald Welte79639662009-06-27 02:58:43 +0200571 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800572 break;
573 }
574
Harald Welteccd69362009-07-04 10:18:00 +0200575 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100576 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200577
Harald Welte03740842009-06-10 23:11:52 +0800578 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200579 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800580 /* FIXME: request id? close channel? */
581 return -EINVAL;
582 }
583
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100584 conn->subscr = subscr;
585 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800586
Harald Welteccd69362009-07-04 10:18:00 +0200587 /* check if we can let the subscriber into our network immediately
588 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100589 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800590}
591
Harald Welte03740842009-06-10 23:11:52 +0800592#if 0
593static u_int8_t to_bcd8(u_int8_t val)
594{
595 return ((val / 10) << 4) | (val % 10);
596}
597#endif
598
Harald Welte59b04682009-06-10 05:40:52 +0800599/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800600int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800601{
602 struct msgb *msg = gsm48_msgb_alloc();
603 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800604 struct gsm_network *net = conn->bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800605 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200606 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +0800607#if 0
608 time_t cur_t;
609 struct tm* cur_time;
610 int tz15min;
611#endif
Harald Welte59b04682009-06-10 05:40:52 +0800612
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800613 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800614
615 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
616 gh->proto_discr = GSM48_PDISC_MM;
617 gh->msg_type = GSM48_MT_MM_INFO;
618
619 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200620#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800621 name_len = strlen(net->name_long);
622 /* 10.5.3.5a */
623 ptr8 = msgb_put(msg, 3);
624 ptr8[0] = GSM48_IE_NAME_LONG;
625 ptr8[1] = name_len*2 +1;
626 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
627
628 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
629 for (i = 0; i < name_len; i++)
630 ptr16[i] = htons(net->name_long[i]);
631
632 /* FIXME: Use Cell Broadcast, not UCS-2, since
633 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200634#endif
635 name_len = (strlen(net->name_long)*7)/8;
636 name_pad = (8 - strlen(net->name_long)*7)%8;
637 if (name_pad > 0)
638 name_len++;
639 /* 10.5.3.5a */
640 ptr8 = msgb_put(msg, 3);
641 ptr8[0] = GSM48_IE_NAME_LONG;
642 ptr8[1] = name_len +1;
643 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
644
645 ptr8 = msgb_put(msg, name_len);
646 gsm_7bit_encode(ptr8, net->name_long);
647
Harald Welte59b04682009-06-10 05:40:52 +0800648 }
649
650 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200651#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800652 name_len = strlen(net->name_short);
653 /* 10.5.3.5a */
654 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200655 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800656 ptr8[1] = name_len*2 + 1;
657 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
658
659 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
660 for (i = 0; i < name_len; i++)
661 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200662#endif
663 name_len = (strlen(net->name_short)*7)/8;
664 name_pad = (8 - strlen(net->name_short)*7)%8;
665 if (name_pad > 0)
666 name_len++;
667 /* 10.5.3.5a */
668 ptr8 = (u_int8_t *) msgb_put(msg, 3);
669 ptr8[0] = GSM48_IE_NAME_SHORT;
670 ptr8[1] = name_len +1;
671 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
672
673 ptr8 = msgb_put(msg, name_len);
674 gsm_7bit_encode(ptr8, net->name_short);
675
Harald Welte59b04682009-06-10 05:40:52 +0800676 }
677
678#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800679 /* Section 10.5.3.9 */
680 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +0800681 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +0800682 ptr8 = msgb_put(msg, 8);
683 ptr8[0] = GSM48_IE_NET_TIME_TZ;
684 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
685 ptr8[2] = to_bcd8(cur_time->tm_mon);
686 ptr8[3] = to_bcd8(cur_time->tm_mday);
687 ptr8[4] = to_bcd8(cur_time->tm_hour);
688 ptr8[5] = to_bcd8(cur_time->tm_min);
689 ptr8[6] = to_bcd8(cur_time->tm_sec);
690 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
691 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +0800692 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +0800693 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +0800694 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +0800695#endif
696
Daniel Willmannc8188202009-08-13 03:42:07 +0200697 DEBUGP(DMM, "-> MM INFO\n");
698
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800699 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800700}
701
Harald Welte36970f62009-08-12 22:56:50 +0200702/* Section 9.2.2 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800703int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200704{
705 struct msgb *msg = gsm48_msgb_alloc();
706 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200707 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200708
Sylvain Munautb1a4f282009-12-24 00:24:29 +0100709 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200710
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800711 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200712 gh->proto_discr = GSM48_PDISC_MM;
713 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
714
Sylvain Munaut0789c592009-12-24 00:23:46 +0100715 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200716
Harald Welte36970f62009-08-12 22:56:50 +0200717 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200718 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200719 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200720
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800721 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200722}
723
724/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800725int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200726{
727 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800728 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200729}
730
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800731static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800732{
733 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800734 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800735}
736
737/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100738static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800739 enum gsm48_reject_value value)
740{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800741 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800742
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800743 msg = gsm48_create_mm_serv_rej(value);
744 if (!msg) {
745 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
746 return -1;
747 }
Harald Welte59b04682009-06-10 05:40:52 +0800748
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800749 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100750 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800751 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800752}
753
Sylvain Munautbadc7112009-12-24 00:28:46 +0100754static int _gsm48_rx_mm_serv_req_sec_cb(
755 unsigned int hooknum, unsigned int event,
756 struct msgb *msg, void *data, void *param)
757{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800758 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100759 int rc = 0;
760
761 switch (event) {
762 case GSM_SECURITY_AUTH_FAILED:
763 /* Nothing to do */
764 break;
765
766 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800767 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100768 break;
769
770 case GSM_SECURITY_SUCCEEDED:
771 /* nothing to do. CIPHER MODE COMMAND is
772 * implicit CM SERV ACK */
773 break;
774
775 default:
776 rc = -EINVAL;
777 };
778
779 return rc;
780}
781
Harald Welte59b04682009-06-10 05:40:52 +0800782/*
783 * Handle CM Service Requests
784 * a) Verify that the packet is long enough to contain the information
785 * we require otherwsie reject with INCORRECT_MESSAGE
786 * b) Try to parse the TMSI. If we do not have one reject
787 * c) Check that we know the subscriber with the TMSI otherwise reject
788 * with a HLR cause
789 * d) Set the subscriber on the gsm_lchan and accept
790 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800791static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800792{
793 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200794 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800795
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800796 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800797 struct gsm_subscriber *subscr;
798 struct gsm48_hdr *gh = msgb_l3(msg);
799 struct gsm48_service_request *req =
800 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800801 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Welte59b04682009-06-10 05:40:52 +0800802 u_int8_t classmark2_len = gh->data[1];
803 u_int8_t *classmark2 = gh->data+2;
804 u_int8_t mi_len = *(classmark2 + classmark2_len);
805 u_int8_t *mi = (classmark2 + classmark2_len + 1);
806
807 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
808 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
809 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800810 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800811 GSM48_REJECT_INCORRECT_MESSAGE);
812 }
813
814 if (msg->data_len < req->mi_len + 6) {
815 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800816 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800817 GSM48_REJECT_INCORRECT_MESSAGE);
818 }
819
820 mi_type = mi[0] & GSM_MI_TYPE_MASK;
821 if (mi_type != GSM_MI_TYPE_TMSI) {
822 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800823 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800824 GSM48_REJECT_INCORRECT_MESSAGE);
825 }
826
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200827 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800828 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
829 req->cm_service_type, mi_type, mi_string);
830
Harald Welteedf48af2009-12-13 12:39:18 +0100831 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
832
Harald Welte583ceaf2009-08-10 10:12:45 +0200833 if (is_siemens_bts(bts))
834 send_siemens_mrpci(msg->lchan, classmark2-1);
835
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200836 subscr = subscr_get_by_tmsi(bts->network,
837 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800838
839 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
840 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800841 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800842 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
843
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800844 if (!conn->subscr)
845 conn->subscr = subscr;
846 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100847 subscr_put(subscr); /* lchan already has a ref, don't need another one */
848 else {
Harald Welte59b04682009-06-10 05:40:52 +0800849 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
850 subscr_put(subscr);
851 }
852
Harald Weltef6845a72009-07-05 14:08:13 +0200853 subscr->equipment.classmark2_len = classmark2_len;
854 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
855 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800856
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800857 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100858 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800859}
860
861static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
862{
Harald Welte75350412009-07-23 18:46:00 +0200863 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800864 struct gsm48_hdr *gh = msgb_l3(msg);
865 struct gsm48_imsi_detach_ind *idi =
866 (struct gsm48_imsi_detach_ind *) gh->data;
867 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200868 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800869 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800870
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200871 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800872 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
873 mi_type, mi_string);
874
Harald Weltebdbb7442009-12-22 19:07:32 +0100875 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100876
Harald Welte59b04682009-06-10 05:40:52 +0800877 switch (mi_type) {
878 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200879 subscr = subscr_get_by_tmsi(bts->network,
880 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800881 break;
882 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200883 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800884 break;
885 case GSM_MI_TYPE_IMEI:
886 case GSM_MI_TYPE_IMEISV:
887 /* no sim card... FIXME: what to do ? */
888 DEBUGPC(DMM, "unimplemented mobile identity type\n");
889 break;
890 default:
891 DEBUGPC(DMM, "unknown mobile identity type\n");
892 break;
893 }
894
895 if (subscr) {
896 subscr_update(subscr, msg->trx->bts,
897 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100898 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200899
900 subscr->equipment.classmark1 = idi->classmark1;
901 db_sync_equipment(&subscr->equipment);
902
Harald Welte59b04682009-06-10 05:40:52 +0800903 subscr_put(subscr);
904 } else
905 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
906
Harald Welte43250dd2009-12-20 09:58:40 +0100907 /* FIXME: iterate over all transactions and release them,
908 * imagine an IMSI DETACH happening during an active call! */
909
Harald Welte9ed54ff2009-12-12 21:00:48 +0100910 /* subscriber is detached: should we release lchan? */
Harald Welte59b04682009-06-10 05:40:52 +0800911 return 0;
912}
913
914static int gsm48_rx_mm_status(struct msgb *msg)
915{
916 struct gsm48_hdr *gh = msgb_l3(msg);
917
918 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
919
920 return 0;
921}
922
Sylvain Munaut28c47162009-12-24 00:27:26 +0100923/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800924static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100925{
926 struct gsm48_hdr *gh = msgb_l3(msg);
927 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800928 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100929
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200930 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut28c47162009-12-24 00:27:26 +0100931 hexdump(ar->sres, 4));
932
933 /* Safety check */
934 if (!conn->sec_operation) {
935 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
936 return -EIO;
937 }
938
939 /* Validate SRES */
940 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100941 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100942 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200943
944 DEBUGPC(DMM, "Invalid (expected %s)\n",
945 hexdump(conn->sec_operation->atuple.sres, 4));
946
Sylvain Munaut28c47162009-12-24 00:27:26 +0100947 if (cb)
948 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800949 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100950
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100951 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100952 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100953 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100954 }
955
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200956 DEBUGPC(DMM, "OK\n");
957
Sylvain Munaut28c47162009-12-24 00:27:26 +0100958 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100959 return gsm0808_cipher_mode(conn, net->a5_encryption,
960 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100961}
962
Harald Welte59b04682009-06-10 05:40:52 +0800963/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800964static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800965{
966 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800967 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800968
969 switch (gh->msg_type & 0xbf) {
970 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200971 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800972 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800973 break;
974 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800975 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800976 break;
977 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800978 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800979 break;
980 case GSM48_MT_MM_STATUS:
981 rc = gsm48_rx_mm_status(msg);
982 break;
983 case GSM48_MT_MM_TMSI_REALL_COMPL:
984 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800985 conn->subscr ?
986 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +0800987 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100988 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800989 break;
990 case GSM48_MT_MM_IMSI_DETACH_IND:
991 rc = gsm48_rx_mm_imsi_detach_ind(msg);
992 break;
993 case GSM48_MT_MM_CM_REEST_REQ:
994 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
995 break;
996 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800997 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800998 break;
999 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001000 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001001 gh->msg_type);
1002 break;
1003 }
1004
1005 return rc;
1006}
1007
1008/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001009static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001010{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001011 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001012 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001013 struct gsm48_pag_resp *resp;
Harald Welte59b04682009-06-10 05:40:52 +08001014 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001015 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001016 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001017 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001018 int rc = 0;
1019
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001020 resp = (struct gsm48_pag_resp *) &gh->data[0];
1021 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1022 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001023 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1024 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001025
Harald Welte59b04682009-06-10 05:40:52 +08001026 switch (mi_type) {
1027 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001028 subscr = subscr_get_by_tmsi(bts->network,
1029 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001030 break;
1031 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001032 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001033 break;
1034 }
1035
1036 if (!subscr) {
1037 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1038 /* FIXME: request id? close channel? */
1039 return -EINVAL;
1040 }
1041 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001042 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001043
Harald Weltef6845a72009-07-05 14:08:13 +02001044 subscr->equipment.classmark2_len = *classmark2_lv;
1045 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1046 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001047
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001048 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001049 return rc;
1050}
1051
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001052static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001053{
1054 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001055 struct gsm_subscriber *subscr = conn->subscr;
Harald Welte59b04682009-06-10 05:40:52 +08001056 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1057 u_int8_t cm2_len, cm3_len = 0;
1058 u_int8_t *cm2, *cm3 = NULL;
1059
1060 DEBUGP(DRR, "CLASSMARK CHANGE ");
1061
1062 /* classmark 2 */
1063 cm2_len = gh->data[0];
1064 cm2 = &gh->data[1];
1065 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1066
1067 if (payload_len > cm2_len + 1) {
1068 /* we must have a classmark3 */
1069 if (gh->data[cm2_len+1] != 0x20) {
1070 DEBUGPC(DRR, "ERR CM3 TAG\n");
1071 return -EINVAL;
1072 }
1073 if (cm2_len > 3) {
1074 DEBUGPC(DRR, "CM2 too long!\n");
1075 return -EINVAL;
1076 }
1077
1078 cm3_len = gh->data[cm2_len+2];
1079 cm3 = &gh->data[cm2_len+3];
1080 if (cm3_len > 14) {
1081 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1082 return -EINVAL;
1083 }
1084 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1085 }
1086 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001087 subscr->equipment.classmark2_len = cm2_len;
1088 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001089 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001090 subscr->equipment.classmark3_len = cm3_len;
1091 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001092 }
Harald Weltef6845a72009-07-05 14:08:13 +02001093 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001094 }
1095
Harald Welte59b04682009-06-10 05:40:52 +08001096 return 0;
1097}
1098
1099static int gsm48_rx_rr_status(struct msgb *msg)
1100{
1101 struct gsm48_hdr *gh = msgb_l3(msg);
1102
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001103 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Welte59b04682009-06-10 05:40:52 +08001104 rr_cause_name(gh->data[0]));
1105
1106 return 0;
1107}
1108
1109static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1110{
Harald Weltef9476812009-12-15 21:36:05 +01001111 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001112
Harald Weltec20bd1d2009-11-29 19:07:28 +01001113 /* This shouldn't actually end up here, as RSL treats
1114 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1115 * directly into gsm48_parse_meas_rep */
1116 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Weltef9476812009-12-15 21:36:05 +01001117 gsm48_parse_meas_rep(meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001118
1119 return 0;
1120}
1121
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001122static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001123{
1124 struct gsm48_hdr *gh = msgb_l3(msg);
1125 u_int8_t apdu_id_flags;
1126 u_int8_t apdu_len;
1127 u_int8_t *apdu_data;
1128
1129 apdu_id_flags = gh->data[0];
1130 apdu_len = gh->data[1];
1131 apdu_data = gh->data+2;
1132
1133 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1134 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1135
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001136 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001137}
1138
Sylvain Munaut28c47162009-12-24 00:27:26 +01001139/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001140static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001141{
Sylvain Munaut28c47162009-12-24 00:27:26 +01001142 gsm_cbfn *cb;
1143 int rc = 0;
1144
1145 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1146
1147 /* Safety check */
1148 if (!conn->sec_operation) {
1149 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1150 return -EIO;
1151 }
1152
1153 /* FIXME: check for MI (if any) */
1154
1155 /* Call back whatever was in progress (if anything) ... */
1156 cb = conn->sec_operation->cb;
1157 if (cb) {
1158 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001159 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001160 }
1161
1162 /* Complete the operation */
1163 release_security_operation(conn);
1164
1165 return rc;
1166}
1167
Harald Welte6720a432009-11-29 22:45:52 +01001168/* Chapter 9.1.16 Handover complete */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001169static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001170{
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001171 struct lchan_signal_data sig;
Harald Welte6720a432009-11-29 22:45:52 +01001172 struct gsm48_hdr *gh = msgb_l3(msg);
1173
1174 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1175 rr_cause_name(gh->data[0]));
1176
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001177 sig.lchan = msg->lchan;
1178 sig.mr = NULL;
1179 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welte6720a432009-11-29 22:45:52 +01001180 /* FIXME: release old channel */
1181
1182 return 0;
1183}
1184
1185/* Chapter 9.1.17 Handover Failure */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001186static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001187{
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001188 struct lchan_signal_data sig;
Harald Welte6720a432009-11-29 22:45:52 +01001189 struct gsm48_hdr *gh = msgb_l3(msg);
1190
1191 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1192 rr_cause_name(gh->data[0]));
1193
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001194 sig.lchan = msg->lchan;
1195 sig.mr = NULL;
1196 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welte6720a432009-11-29 22:45:52 +01001197 /* FIXME: release allocated new channel */
1198
1199 return 0;
1200}
Harald Welte (local)b5103922009-08-15 23:32:44 +02001201
Harald Welte59b04682009-06-10 05:40:52 +08001202/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001203static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001204{
1205 struct gsm48_hdr *gh = msgb_l3(msg);
1206 int rc = 0;
1207
1208 switch (gh->msg_type) {
1209 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001210 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001211 break;
1212 case GSM48_MT_RR_GPRS_SUSP_REQ:
1213 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1214 break;
1215 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001216 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001217 break;
Harald Welte59b04682009-06-10 05:40:52 +08001218 case GSM48_MT_RR_STATUS:
1219 rc = gsm48_rx_rr_status(msg);
1220 break;
1221 case GSM48_MT_RR_MEAS_REP:
1222 rc = gsm48_rx_rr_meas_rep(msg);
1223 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001224 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001225 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001226 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001227 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001228 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welted2dd9de2009-08-30 15:37:11 +09001229 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001230 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001231 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001232 break;
1233 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001234 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001235 break;
Harald Welte59b04682009-06-10 05:40:52 +08001236 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001237 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1238 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001239 break;
1240 }
1241
1242 return rc;
1243}
1244
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001245int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001246 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001247{
1248 struct msgb *msg = gsm48_msgb_alloc();
1249 struct gsm48_hdr *gh;
1250
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001251 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001252
1253 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1254 apdu_id, apdu_len);
1255
1256 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1257 gh->proto_discr = GSM48_PDISC_RR;
1258 gh->msg_type = GSM48_MT_RR_APP_INFO;
1259 gh->data[0] = apdu_id;
1260 gh->data[1] = apdu_len;
1261 memcpy(gh->data+2, apdu, apdu_len);
1262
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001263 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001264}
1265
Harald Welte59b04682009-06-10 05:40:52 +08001266/* Call Control */
1267
1268/* The entire call control code is written in accordance with Figure 7.10c
1269 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1270 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1271 * it for voice */
1272
Harald Welte03740842009-06-10 23:11:52 +08001273static void new_cc_state(struct gsm_trans *trans, int state)
1274{
1275 if (state > 31 || state < 0)
1276 return;
1277
1278 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001279 gsm48_cc_state_name(trans->cc.state),
1280 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001281
Harald Weltec2189a62009-07-23 18:56:43 +02001282 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001283}
1284
1285static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001286{
1287 struct msgb *msg = gsm48_msgb_alloc();
1288 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1289 u_int8_t *cause, *call_state;
1290
Harald Welte59b04682009-06-10 05:40:52 +08001291 gh->msg_type = GSM48_MT_CC_STATUS;
1292
1293 cause = msgb_put(msg, 3);
1294 cause[0] = 2;
1295 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1296 cause[2] = 0x80 | 30; /* response to status inquiry */
1297
1298 call_state = msgb_put(msg, 1);
1299 call_state[0] = 0xc0 | 0x00;
1300
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001301 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001302}
1303
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001304static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +08001305 u_int8_t pdisc, u_int8_t msg_type)
1306{
1307 struct msgb *msg = gsm48_msgb_alloc();
1308 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1309
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001310 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001311
1312 gh->proto_discr = pdisc;
1313 gh->msg_type = msg_type;
1314
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001315 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001316}
1317
Harald Welte03740842009-06-10 23:11:52 +08001318static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1319{
Harald Weltec2189a62009-07-23 18:56:43 +02001320 if (bsc_timer_pending(&trans->cc.timer)) {
1321 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1322 bsc_del_timer(&trans->cc.timer);
1323 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001324 }
1325}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001326
Harald Welte03740842009-06-10 23:11:52 +08001327static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1328 int msg_type, struct gsm_mncc *mncc)
1329{
1330 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001331 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001332
1333 if (trans)
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001334 if (trans->conn)
Harald Welte4861c822009-07-23 21:21:14 +02001335 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001336 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001337 trans->conn->lchan->ts->trx->bts->nr,
1338 trans->conn->lchan->ts->trx->nr,
1339 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001340 (trans->subscr)?(trans->subscr->extension):"-",
1341 get_mncc_name(msg_type));
1342 else
1343 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1344 "Sending '%s' to MNCC.\n",
1345 (trans->subscr)?(trans->subscr->extension):"-",
1346 get_mncc_name(msg_type));
1347 else
1348 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1349 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1350
1351 mncc->msg_type = msg_type;
1352
Harald Welte9cfc9352009-06-26 19:39:35 +02001353 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001354 if (!msg)
1355 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001356
1357 data = msgb_put(msg, sizeof(struct gsm_mncc));
1358 memcpy(data, mncc, sizeof(struct gsm_mncc));
1359
Harald Weltebe0cb682010-12-22 23:43:29 +01001360 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001361
1362 return 0;
1363}
1364
1365int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1366 u_int32_t callref, int location, int value)
1367{
1368 struct gsm_mncc rel;
1369
Harald Weltecb0595f2009-06-12 01:54:08 +08001370 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001371 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001372 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001373 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1374}
1375
Harald Weltec2189a62009-07-23 18:56:43 +02001376/* Call Control Specific transaction release.
1377 * gets called by trans_free, DO NOT CALL YOURSELF! */
1378void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001379{
Harald Welte03740842009-06-10 23:11:52 +08001380 gsm48_stop_cc_timer(trans);
1381
1382 /* send release to L4, if callref still exists */
1383 if (trans->callref) {
1384 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001385 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001386 GSM48_CAUSE_LOC_PRN_S_LU,
1387 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001388 }
Harald Weltec2189a62009-07-23 18:56:43 +02001389 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001390 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001391 if (trans->conn)
1392 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001393}
1394
1395static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001396
Harald Welte59b04682009-06-10 05:40:52 +08001397/* call-back from paging the B-end of the connection */
1398static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001399 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001400{
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001401 struct gsm_subscriber_connection *conn = _conn;
Harald Welte03740842009-06-10 23:11:52 +08001402 struct gsm_subscriber *subscr = param;
1403 struct gsm_trans *transt, *tmp;
1404 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001405
Harald Welte59b04682009-06-10 05:40:52 +08001406 if (hooknum != GSM_HOOK_RR_PAGING)
1407 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001408
Harald Welte03740842009-06-10 23:11:52 +08001409 if (!subscr)
1410 return -EINVAL;
1411 net = subscr->net;
1412 if (!net) {
1413 DEBUGP(DCC, "Error Network not set!\n");
1414 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001415 }
1416
Harald Welte03740842009-06-10 23:11:52 +08001417 /* check all tranactions (without lchan) for subscriber */
1418 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001419 if (transt->subscr != subscr || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001420 continue;
1421 switch (event) {
1422 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001423 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001424 break;
1425 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1426 subscr->extension);
1427 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001428 if (!transt->conn) {
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001429 transt->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08001430 }
1431 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001432 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001433 break;
1434 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001435 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001436 DEBUGP(DCC, "Paging subscr %s expired!\n",
1437 subscr->extension);
1438 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001439 mncc_release_ind(transt->subscr->net, transt,
1440 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001441 GSM48_CAUSE_LOC_PRN_S_LU,
1442 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001443 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001444 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001445 break;
1446 }
1447 }
Harald Welte59b04682009-06-10 05:40:52 +08001448 return 0;
1449}
1450
Harald Welte3971ad52009-12-19 22:23:05 +01001451static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1452
Harald Welte3c062072009-07-28 18:25:29 +02001453/* some other part of the code sends us a signal */
1454static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1455 void *handler_data, void *signal_data)
1456{
1457 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001458 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001459 struct gsm_network *net;
1460 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001461
1462 if (subsys != SS_ABISIP)
1463 return 0;
1464
1465 /* in case we use direct BTS-to-BTS RTP */
1466 if (ipacc_rtp_direct)
1467 return 0;
1468
Harald Welte3c062072009-07-28 18:25:29 +02001469 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001470 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001471 /* check if any transactions on this lchan still have
1472 * a tch_recv_mncc request pending */
1473 net = lchan->ts->trx->bts->network;
1474 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001475 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001476 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1477 tch_recv_mncc(net, trans->callref, 1);
1478 }
1479 }
Harald Welte3c062072009-07-28 18:25:29 +02001480 break;
Harald Welte3c062072009-07-28 18:25:29 +02001481 }
1482
1483 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001484}
1485
Harald Welte59b04682009-06-10 05:40:52 +08001486/* map two ipaccess RTP streams onto each other */
1487static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1488{
1489 struct gsm_bts *bts = lchan->ts->trx->bts;
1490 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001491 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001492
1493 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1494 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1495 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1496
1497 if (bts->type != remote_bts->type) {
1498 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1499 return -EINVAL;
1500 }
Harald Welte3971ad52009-12-19 22:23:05 +01001501
1502 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001503 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001504 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001505 if (!ipacc_rtp_direct) {
1506 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001507 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001508 if (rc < 0)
1509 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001510 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001511#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001512
1513 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301514 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1515 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001516 } else {
1517 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301518 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1519 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301520 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001521 if (rc < 0)
1522 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301523 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1524 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301525 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001526 }
Harald Welte59b04682009-06-10 05:40:52 +08001527 break;
1528 case GSM_BTS_TYPE_BS11:
1529 trau_mux_map_lchan(lchan, remote_lchan);
1530 break;
1531 default:
1532 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001533 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001534 }
1535
1536 return 0;
1537}
1538
Harald Welte03740842009-06-10 23:11:52 +08001539/* bridge channels of two transactions */
1540static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001541{
Harald Weltec2189a62009-07-23 18:56:43 +02001542 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1543 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001544
Harald Welte03740842009-06-10 23:11:52 +08001545 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001546 return -EIO;
1547
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001548 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001549 return -EIO;
1550
1551 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001552 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001553}
1554
Harald Welte3971ad52009-12-19 22:23:05 +01001555/* enable receive of channels to MNCC upqueue */
1556static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001557{
1558 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001559 struct gsm_lchan *lchan;
1560 struct gsm_bts *bts;
1561 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001562
Harald Welte03740842009-06-10 23:11:52 +08001563 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001564 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001565 if (!trans)
1566 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001567 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001568 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001569 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001570 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001571
Harald Welte3971ad52009-12-19 22:23:05 +01001572 switch (bts->type) {
1573 case GSM_BTS_TYPE_NANOBTS:
1574 if (ipacc_rtp_direct) {
1575 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1576 return -EINVAL;
1577 }
1578 /* in case, we don't have a RTP socket yet, we note this
1579 * in the transaction and try later */
1580 if (!lchan->abis_ip.rtp_socket) {
1581 trans->tch_recv = enable;
1582 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1583 return 0;
1584 }
1585 if (enable) {
1586 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001587 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001588 if (rc < 0)
1589 return rc;
1590 /* assign socket to application interface */
1591 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1592 net, callref);
1593 } else
1594 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1595 net, 0);
1596 break;
1597 case GSM_BTS_TYPE_BS11:
1598 if (enable)
1599 return trau_recv_lchan(lchan, callref);
1600 return trau_mux_unmap(NULL, callref);
1601 break;
1602 default:
1603 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1604 return -EINVAL;
1605 }
1606
1607 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001608}
1609
Harald Welte03740842009-06-10 23:11:52 +08001610static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1611{
1612 DEBUGP(DCC, "-> STATUS ENQ\n");
1613 return gsm48_cc_tx_status(trans, msg);
1614}
1615
1616static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1617static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1618
1619static void gsm48_cc_timeout(void *arg)
1620{
1621 struct gsm_trans *trans = arg;
1622 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001623 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1624 int mo_location = GSM48_CAUSE_LOC_USER;
1625 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1626 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001627 struct gsm_mncc mo_rel, l4_rel;
1628
1629 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1630 mo_rel.callref = trans->callref;
1631 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1632 l4_rel.callref = trans->callref;
1633
Harald Weltec2189a62009-07-23 18:56:43 +02001634 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001635 case 0x303:
1636 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001637 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001638 break;
1639 case 0x310:
1640 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001641 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001642 break;
1643 case 0x313:
1644 disconnect = 1;
1645 /* unknown, did not find it in the specs */
1646 break;
1647 case 0x301:
1648 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001649 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001650 break;
1651 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001652 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001653 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001654 gsm48_cc_tx_release(trans, &trans->cc.msg);
1655 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001656 break; /* stay in release state */
1657 }
Harald Weltec2189a62009-07-23 18:56:43 +02001658 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001659 return;
1660// release = 1;
1661// l4_cause = 14;
1662// break;
1663 case 0x306:
1664 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001665 mo_cause = trans->cc.msg.cause.value;
1666 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001667 break;
1668 case 0x323:
1669 disconnect = 1;
1670 break;
1671 default:
1672 release = 1;
1673 }
1674
1675 if (release && trans->callref) {
1676 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001677 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001678 l4_location, l4_cause);
1679 trans->callref = 0;
1680 }
1681
1682 if (disconnect && trans->callref) {
1683 /* process disconnect towards layer 4 */
1684 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001685 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001686 }
1687
1688 /* process disconnect towards mobile station */
1689 if (disconnect || release) {
1690 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001691 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1692 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1693 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001694 mo_rel.cause.diag_len = 3;
1695
1696 if (disconnect)
1697 gsm48_cc_tx_disconnect(trans, &mo_rel);
1698 if (release)
1699 gsm48_cc_tx_release(trans, &mo_rel);
1700 }
1701
1702}
1703
1704static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1705 int sec, int micro)
1706{
1707 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001708 trans->cc.timer.cb = gsm48_cc_timeout;
1709 trans->cc.timer.data = trans;
1710 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1711 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001712}
1713
1714static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1715{
1716 struct gsm48_hdr *gh = msgb_l3(msg);
1717 u_int8_t msg_type = gh->msg_type & 0xbf;
1718 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1719 struct tlv_parsed tp;
1720 struct gsm_mncc setup;
1721
1722 memset(&setup, 0, sizeof(struct gsm_mncc));
1723 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001724 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001725 /* emergency setup is identified by msg_type */
1726 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1727 setup.emergency = 1;
1728
1729 /* use subscriber as calling party number */
1730 if (trans->subscr) {
1731 setup.fields |= MNCC_F_CALLING;
1732 strncpy(setup.calling.number, trans->subscr->extension,
1733 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001734 strncpy(setup.imsi, trans->subscr->imsi,
1735 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001736 }
1737 /* bearer capability */
1738 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1739 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001740 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001741 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1742 }
1743 /* facility */
1744 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1745 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001746 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001747 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1748 }
1749 /* called party bcd number */
1750 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1751 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001752 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001753 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1754 }
1755 /* user-user */
1756 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1757 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001758 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001759 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1760 }
1761 /* ss-version */
1762 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1763 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001764 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001765 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1766 }
1767 /* CLIR suppression */
1768 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1769 setup.clir.sup = 1;
1770 /* CLIR invocation */
1771 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1772 setup.clir.inv = 1;
1773 /* cc cap */
1774 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1775 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001776 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001777 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1778 }
1779
Harald Welte03740842009-06-10 23:11:52 +08001780 new_cc_state(trans, GSM_CSTATE_INITIATED);
1781
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001782 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1783 subscr_name(trans->subscr), trans->subscr->extension,
1784 setup.called.number);
1785
Harald Weltec56006b2010-12-24 16:06:33 +01001786 counter_inc(trans->subscr->net->stats.call.mo_setup);
1787
Harald Welte03740842009-06-10 23:11:52 +08001788 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001789 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001790
Harald Welteca745e22009-07-29 12:10:35 +02001791 /* MNCC code will modify the channel asynchronously, we should
1792 * ipaccess-bind only after the modification has been made to the
1793 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001794 return 0;
1795}
1796
1797static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001798{
1799 struct msgb *msg = gsm48_msgb_alloc();
1800 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001801 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001802 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001803
1804 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1805
Harald Welte03740842009-06-10 23:11:52 +08001806 /* transaction id must not be assigned */
1807 if (trans->transaction_id != 0xff) { /* unasssigned */
1808 DEBUGP(DCC, "TX Setup with assigned transaction. "
1809 "This is not allowed!\n");
1810 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001811 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001812 GSM48_CAUSE_LOC_PRN_S_LU,
1813 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001814 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001815 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001816 return rc;
1817 }
1818
1819 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001820 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1821 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001822 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001823 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001824 GSM48_CAUSE_LOC_PRN_S_LU,
1825 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001826 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001827 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001828 return rc;
1829 }
Harald Welte165fc332009-07-23 21:36:44 +02001830 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001831
Harald Welte59b04682009-06-10 05:40:52 +08001832 gh->msg_type = GSM48_MT_CC_SETUP;
1833
Harald Welte03740842009-06-10 23:11:52 +08001834 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001835
Harald Welte03740842009-06-10 23:11:52 +08001836 /* bearer capability */
1837 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001838 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001839 /* facility */
1840 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001841 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001842 /* progress */
1843 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001844 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001845 /* calling party BCD number */
1846 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001847 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001848 /* called party BCD number */
1849 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001850 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001851 /* user-user */
1852 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001853 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001854 /* redirecting party BCD number */
1855 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001856 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001857 /* signal */
1858 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001859 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001860
1861 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001862
Harald Weltec56006b2010-12-24 16:06:33 +01001863 counter_inc(trans->subscr->net->stats.call.mt_setup);
1864
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001865 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001866}
1867
Harald Welte03740842009-06-10 23:11:52 +08001868static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1869{
1870 struct gsm48_hdr *gh = msgb_l3(msg);
1871 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1872 struct tlv_parsed tp;
1873 struct gsm_mncc call_conf;
1874
1875 gsm48_stop_cc_timer(trans);
1876 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1877
1878 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1879 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001880 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001881#if 0
1882 /* repeat */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1884 call_conf.repeat = 1;
1885 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1886 call_conf.repeat = 2;
1887#endif
1888 /* bearer capability */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1890 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001891 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001892 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1893 }
1894 /* cause */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1896 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001897 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001898 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1899 }
1900 /* cc cap */
1901 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1902 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001903 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001904 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1905 }
1906
1907 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1908
Harald Welte0abe5a62009-07-23 19:06:52 +02001909 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1910 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001911}
1912
1913static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1914{
1915 struct gsm_mncc *proceeding = arg;
1916 struct msgb *msg = gsm48_msgb_alloc();
1917 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1918
Harald Welte03740842009-06-10 23:11:52 +08001919 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1920
1921 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1922
1923 /* bearer capability */
1924 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001925 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001926 /* facility */
1927 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001928 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001929 /* progress */
1930 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001931 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001932
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001933 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001934}
1935
1936static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1937{
1938 struct gsm48_hdr *gh = msgb_l3(msg);
1939 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1940 struct tlv_parsed tp;
1941 struct gsm_mncc alerting;
1942
1943 gsm48_stop_cc_timer(trans);
1944 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1945
1946 memset(&alerting, 0, sizeof(struct gsm_mncc));
1947 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001948 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001949 /* facility */
1950 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1951 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001952 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001953 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1954 }
1955
1956 /* progress */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1958 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001959 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1961 }
1962 /* ss-version */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1964 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001965 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1967 }
1968
1969 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1970
Harald Welte0abe5a62009-07-23 19:06:52 +02001971 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1972 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001973}
1974
1975static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1976{
1977 struct gsm_mncc *alerting = arg;
1978 struct msgb *msg = gsm48_msgb_alloc();
1979 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1980
Harald Welte03740842009-06-10 23:11:52 +08001981 gh->msg_type = GSM48_MT_CC_ALERTING;
1982
1983 /* facility */
1984 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001985 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001986 /* progress */
1987 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001988 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001989 /* user-user */
1990 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001991 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001992
1993 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1994
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001995 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001996}
1997
1998static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1999{
2000 struct gsm_mncc *progress = arg;
2001 struct msgb *msg = gsm48_msgb_alloc();
2002 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2003
Harald Welte03740842009-06-10 23:11:52 +08002004 gh->msg_type = GSM48_MT_CC_PROGRESS;
2005
2006 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002007 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002008 /* user-user */
2009 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002010 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002011
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002012 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002013}
2014
2015static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2016{
2017 struct gsm_mncc *connect = arg;
2018 struct msgb *msg = gsm48_msgb_alloc();
2019 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2020
Harald Welte03740842009-06-10 23:11:52 +08002021 gh->msg_type = GSM48_MT_CC_CONNECT;
2022
2023 gsm48_stop_cc_timer(trans);
2024 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2025
2026 /* facility */
2027 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002028 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002029 /* progress */
2030 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002031 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002032 /* connected number */
2033 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002034 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002035 /* user-user */
2036 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002037 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002038
2039 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2040
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002041 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002042}
2043
2044static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2045{
2046 struct gsm48_hdr *gh = msgb_l3(msg);
2047 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2048 struct tlv_parsed tp;
2049 struct gsm_mncc connect;
2050
2051 gsm48_stop_cc_timer(trans);
2052
2053 memset(&connect, 0, sizeof(struct gsm_mncc));
2054 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002055 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002056 /* use subscriber as connected party number */
2057 if (trans->subscr) {
2058 connect.fields |= MNCC_F_CONNECTED;
2059 strncpy(connect.connected.number, trans->subscr->extension,
2060 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002061 strncpy(connect.imsi, trans->subscr->imsi,
2062 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002063 }
2064 /* facility */
2065 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2066 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002067 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002068 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2069 }
2070 /* user-user */
2071 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2072 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002073 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002074 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2075 }
2076 /* ss-version */
2077 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2078 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002079 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002080 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2081 }
2082
2083 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltec56006b2010-12-24 16:06:33 +01002084 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002085
Harald Welte0abe5a62009-07-23 19:06:52 +02002086 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002087}
2088
2089
2090static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2091{
2092 struct gsm_mncc connect_ack;
2093
2094 gsm48_stop_cc_timer(trans);
2095
2096 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltec56006b2010-12-24 16:06:33 +01002097 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002098
2099 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2100 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002101
Harald Welte0abe5a62009-07-23 19:06:52 +02002102 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002103 &connect_ack);
2104}
2105
2106static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2107{
2108 struct msgb *msg = gsm48_msgb_alloc();
2109 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2110
Harald Welte03740842009-06-10 23:11:52 +08002111 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2112
2113 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2114
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002115 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002116}
2117
2118static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2119{
2120 struct gsm48_hdr *gh = msgb_l3(msg);
2121 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2122 struct tlv_parsed tp;
2123 struct gsm_mncc disc;
2124
2125 gsm48_stop_cc_timer(trans);
2126
2127 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2128
2129 memset(&disc, 0, sizeof(struct gsm_mncc));
2130 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002131 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002132 /* cause */
2133 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2134 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002135 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002136 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2137 }
2138 /* facility */
2139 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2140 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002141 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002142 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2143 }
2144 /* user-user */
2145 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2146 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002147 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002148 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2149 }
2150 /* ss-version */
2151 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2152 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002153 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002154 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2155 }
2156
Harald Welte0abe5a62009-07-23 19:06:52 +02002157 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002158
2159}
2160
Harald Weltebbc636a2009-06-11 14:23:20 +08002161static struct gsm_mncc_cause default_cause = {
2162 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2163 .coding = 0,
2164 .rec = 0,
2165 .rec_val = 0,
2166 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2167 .diag_len = 0,
2168 .diag = { 0 },
2169};
Harald Welte03740842009-06-10 23:11:52 +08002170
2171static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2172{
2173 struct gsm_mncc *disc = arg;
2174 struct msgb *msg = gsm48_msgb_alloc();
2175 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2176
Harald Welte03740842009-06-10 23:11:52 +08002177 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2178
2179 gsm48_stop_cc_timer(trans);
2180 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2181
2182 /* cause */
2183 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002184 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002185 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002186 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002187
2188 /* facility */
2189 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002190 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002191 /* progress */
2192 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002193 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002194 /* user-user */
2195 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002196 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002197
2198 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002199 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002200
2201 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2202
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002203 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002204}
2205
2206static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2207{
2208 struct gsm48_hdr *gh = msgb_l3(msg);
2209 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2210 struct tlv_parsed tp;
2211 struct gsm_mncc rel;
2212 int rc;
2213
2214 gsm48_stop_cc_timer(trans);
2215
2216 memset(&rel, 0, sizeof(struct gsm_mncc));
2217 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002218 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002219 /* cause */
2220 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2221 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002222 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002223 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2224 }
2225 /* facility */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2227 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002228 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2230 }
2231 /* user-user */
2232 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2233 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002234 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002235 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2236 }
2237 /* ss-version */
2238 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2239 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002240 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002241 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2242 }
2243
Harald Weltec2189a62009-07-23 18:56:43 +02002244 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002245 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002246 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002247 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002248 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002249 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002250 GSM48_MT_CC_RELEASE_COMPL);
2251 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002252 }
2253
2254 new_cc_state(trans, GSM_CSTATE_NULL);
2255
2256 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002257 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002258
2259 return rc;
2260}
2261
2262static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2263{
2264 struct gsm_mncc *rel = arg;
2265 struct msgb *msg = gsm48_msgb_alloc();
2266 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2267
Harald Welte03740842009-06-10 23:11:52 +08002268 gh->msg_type = GSM48_MT_CC_RELEASE;
2269
2270 trans->callref = 0;
2271
2272 gsm48_stop_cc_timer(trans);
2273 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2274
2275 /* cause */
2276 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002277 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002278 /* facility */
2279 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002280 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002281 /* user-user */
2282 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002283 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002284
Harald Weltec2189a62009-07-23 18:56:43 +02002285 trans->cc.T308_second = 0;
2286 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002287
Harald Weltec2189a62009-07-23 18:56:43 +02002288 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002289 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2290
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002291 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002292}
2293
2294static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2295{
2296 struct gsm48_hdr *gh = msgb_l3(msg);
2297 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2298 struct tlv_parsed tp;
2299 struct gsm_mncc rel;
2300 int rc = 0;
2301
2302 gsm48_stop_cc_timer(trans);
2303
2304 memset(&rel, 0, sizeof(struct gsm_mncc));
2305 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002306 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002307 /* cause */
2308 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2309 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002310 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002311 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2312 }
2313 /* facility */
2314 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2315 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002316 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002317 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2318 }
2319 /* user-user */
2320 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2321 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002322 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002323 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2324 }
2325 /* ss-version */
2326 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2327 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002328 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002329 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2330 }
2331
2332 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002333 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002334 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002335 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002336 MNCC_REJ_IND, &rel);
2337 break;
2338 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002339 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002340 MNCC_REL_CNF, &rel);
2341 break;
2342 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002343 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002344 MNCC_REL_IND, &rel);
2345 }
2346 }
2347
2348 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002349 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002350
2351 return rc;
2352}
2353
2354static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2355{
2356 struct gsm_mncc *rel = arg;
2357 struct msgb *msg = gsm48_msgb_alloc();
2358 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002359 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002360
Harald Welte03740842009-06-10 23:11:52 +08002361 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2362
2363 trans->callref = 0;
2364
2365 gsm48_stop_cc_timer(trans);
2366
2367 /* cause */
2368 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002369 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002370 /* facility */
2371 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002372 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002373 /* user-user */
2374 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002375 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002376
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002377 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2378
Harald Weltec2189a62009-07-23 18:56:43 +02002379 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002380
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002381 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002382}
2383
2384static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2385{
2386 struct gsm48_hdr *gh = msgb_l3(msg);
2387 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2388 struct tlv_parsed tp;
2389 struct gsm_mncc fac;
2390
2391 memset(&fac, 0, sizeof(struct gsm_mncc));
2392 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002394 /* facility */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2396 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002397 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002398 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2399 }
2400 /* ss-version */
2401 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2402 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002403 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002404 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2405 }
2406
Harald Welte0abe5a62009-07-23 19:06:52 +02002407 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002408}
2409
2410static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2411{
2412 struct gsm_mncc *fac = arg;
2413 struct msgb *msg = gsm48_msgb_alloc();
2414 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2415
Harald Welte03740842009-06-10 23:11:52 +08002416 gh->msg_type = GSM48_MT_CC_FACILITY;
2417
2418 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002419 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002420
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002421 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002422}
2423
2424static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2425{
2426 struct gsm_mncc hold;
2427
2428 memset(&hold, 0, sizeof(struct gsm_mncc));
2429 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002430 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002431}
2432
2433static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2434{
2435 struct msgb *msg = gsm48_msgb_alloc();
2436 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2437
Harald Welte03740842009-06-10 23:11:52 +08002438 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2439
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002440 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002441}
2442
2443static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2444{
2445 struct gsm_mncc *hold_rej = arg;
2446 struct msgb *msg = gsm48_msgb_alloc();
2447 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2448
Harald Welte03740842009-06-10 23:11:52 +08002449 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2450
2451 /* cause */
2452 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002453 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002454 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002455 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002456
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002457 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002458}
2459
2460static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2461{
2462 struct gsm_mncc retrieve;
2463
2464 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2465 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002466 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2467 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002468}
2469
2470static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2471{
2472 struct msgb *msg = gsm48_msgb_alloc();
2473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2474
Harald Welte03740842009-06-10 23:11:52 +08002475 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2476
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002477 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002478}
2479
2480static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2481{
2482 struct gsm_mncc *retrieve_rej = arg;
2483 struct msgb *msg = gsm48_msgb_alloc();
2484 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2485
Harald Welte03740842009-06-10 23:11:52 +08002486 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2487
2488 /* cause */
2489 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002490 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002491 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002492 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002493
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002494 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002495}
2496
2497static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2498{
2499 struct gsm48_hdr *gh = msgb_l3(msg);
2500 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2501 struct tlv_parsed tp;
2502 struct gsm_mncc dtmf;
2503
2504 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2505 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002506 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002507 /* keypad facility */
2508 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2509 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002510 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002511 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2512 }
2513
Harald Welte0abe5a62009-07-23 19:06:52 +02002514 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002515}
2516
2517static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2518{
2519 struct gsm_mncc *dtmf = arg;
2520 struct msgb *msg = gsm48_msgb_alloc();
2521 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2522
Harald Welte03740842009-06-10 23:11:52 +08002523 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2524
2525 /* keypad */
2526 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002527 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002528
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002529 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002530}
2531
2532static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2533{
2534 struct gsm_mncc *dtmf = arg;
2535 struct msgb *msg = gsm48_msgb_alloc();
2536 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2537
Harald Welte03740842009-06-10 23:11:52 +08002538 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2539
2540 /* cause */
2541 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002542 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002543 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002544 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002545
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002546 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002547}
2548
2549static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2550{
2551 struct msgb *msg = gsm48_msgb_alloc();
2552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2553
Harald Welte03740842009-06-10 23:11:52 +08002554 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2555
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002556 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2560{
2561 struct gsm_mncc dtmf;
2562
2563 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2564 dtmf.callref = trans->callref;
2565
Harald Welte0abe5a62009-07-23 19:06:52 +02002566 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002567}
2568
2569static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2570{
2571 struct gsm48_hdr *gh = msgb_l3(msg);
2572 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2573 struct tlv_parsed tp;
2574 struct gsm_mncc modify;
2575
2576 memset(&modify, 0, sizeof(struct gsm_mncc));
2577 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002578 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002579 /* bearer capability */
2580 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2581 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002582 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002583 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2584 }
2585
2586 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2587
Harald Welte0abe5a62009-07-23 19:06:52 +02002588 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002589}
2590
2591static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2592{
2593 struct gsm_mncc *modify = arg;
2594 struct msgb *msg = gsm48_msgb_alloc();
2595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2596
Harald Welte03740842009-06-10 23:11:52 +08002597 gh->msg_type = GSM48_MT_CC_MODIFY;
2598
2599 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2600
2601 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002602 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002603
2604 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2605
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002606 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2610{
2611 struct gsm48_hdr *gh = msgb_l3(msg);
2612 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2613 struct tlv_parsed tp;
2614 struct gsm_mncc modify;
2615
2616 gsm48_stop_cc_timer(trans);
2617
2618 memset(&modify, 0, sizeof(struct gsm_mncc));
2619 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002620 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002621 /* bearer capability */
2622 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2623 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002624 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002625 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2626 }
2627
2628 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2629
Harald Welte0abe5a62009-07-23 19:06:52 +02002630 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002631}
2632
2633static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2634{
2635 struct gsm_mncc *modify = arg;
2636 struct msgb *msg = gsm48_msgb_alloc();
2637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2638
Harald Welte03740842009-06-10 23:11:52 +08002639 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2640
2641 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002642 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002643
2644 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2645
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002646 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2650{
2651 struct gsm48_hdr *gh = msgb_l3(msg);
2652 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2653 struct tlv_parsed tp;
2654 struct gsm_mncc modify;
2655
2656 gsm48_stop_cc_timer(trans);
2657
2658 memset(&modify, 0, sizeof(struct gsm_mncc));
2659 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002660 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002661 /* bearer capability */
2662 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2663 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002664 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002665 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2666 }
2667 /* cause */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2669 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002670 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002671 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2672 }
2673
2674 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2675
Harald Welte0abe5a62009-07-23 19:06:52 +02002676 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2680{
2681 struct gsm_mncc *modify = arg;
2682 struct msgb *msg = gsm48_msgb_alloc();
2683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2684
Harald Welte03740842009-06-10 23:11:52 +08002685 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2686
2687 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002688 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002689 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002690 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002691
2692 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2693
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002694 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002695}
2696
2697static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2698{
2699 struct gsm_mncc *notify = arg;
2700 struct msgb *msg = gsm48_msgb_alloc();
2701 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2702
Harald Welte03740842009-06-10 23:11:52 +08002703 gh->msg_type = GSM48_MT_CC_NOTIFY;
2704
2705 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002706 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002707
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002708 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002709}
2710
2711static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2712{
2713 struct gsm48_hdr *gh = msgb_l3(msg);
2714 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2715// struct tlv_parsed tp;
2716 struct gsm_mncc notify;
2717
2718 memset(&notify, 0, sizeof(struct gsm_mncc));
2719 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002720// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002721 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002722 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002723
Harald Welte0abe5a62009-07-23 19:06:52 +02002724 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002725}
2726
2727static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2728{
2729 struct gsm_mncc *user = arg;
2730 struct msgb *msg = gsm48_msgb_alloc();
2731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2732
Harald Welte03740842009-06-10 23:11:52 +08002733 gh->msg_type = GSM48_MT_CC_USER_INFO;
2734
2735 /* user-user */
2736 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002737 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002738 /* more data */
2739 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002740 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002741
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002742 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002743}
2744
2745static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2746{
2747 struct gsm48_hdr *gh = msgb_l3(msg);
2748 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2749 struct tlv_parsed tp;
2750 struct gsm_mncc user;
2751
2752 memset(&user, 0, sizeof(struct gsm_mncc));
2753 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002754 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002755 /* user-user */
2756 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2757 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002758 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002759 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2760 }
2761 /* more data */
2762 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2763 user.more = 1;
2764
Harald Welte0abe5a62009-07-23 19:06:52 +02002765 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002766}
2767
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002768static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002769{
2770 struct gsm_mncc *mode = arg;
2771
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002772 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002773}
2774
2775static struct downstate {
2776 u_int32_t states;
2777 int type;
2778 int (*rout) (struct gsm_trans *trans, void *arg);
2779} downstatelist[] = {
2780 /* mobile originating call establishment */
2781 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2782 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2783 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2784 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2785 {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 */
2786 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2787 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2788 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2789 /* mobile terminating call establishment */
2790 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2791 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2792 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2793 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2794 /* signalling during call */
2795 {SBIT(GSM_CSTATE_ACTIVE),
2796 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2797 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2798 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2799 {ALL_STATES,
2800 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2801 {ALL_STATES,
2802 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2803 {ALL_STATES,
2804 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2805 {SBIT(GSM_CSTATE_ACTIVE),
2806 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2807 {SBIT(GSM_CSTATE_ACTIVE),
2808 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2809 {SBIT(GSM_CSTATE_ACTIVE),
2810 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2811 {SBIT(GSM_CSTATE_ACTIVE),
2812 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2813 {SBIT(GSM_CSTATE_ACTIVE),
2814 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2815 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2816 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2817 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2818 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2819 {SBIT(GSM_CSTATE_ACTIVE),
2820 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2821 /* clearing */
2822 {SBIT(GSM_CSTATE_INITIATED),
2823 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2824 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2825 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2826 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2827 MNCC_REL_REQ, gsm48_cc_tx_release},
2828 /* special */
2829 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002830 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002831};
2832
2833#define DOWNSLLEN \
2834 (sizeof(downstatelist) / sizeof(struct downstate))
2835
2836
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002837int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002838{
Harald Welteaa60edb2009-08-09 18:52:33 +02002839 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002840 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002841 struct gsm_subscriber_connection *conn = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002842 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002843 struct gsm_mncc *data = arg, rel;
2844
Harald Welte17b56522010-12-22 21:45:05 +01002845 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2846
Harald Welte03740842009-06-10 23:11:52 +08002847 /* handle special messages */
2848 switch(msg_type) {
2849 case MNCC_BRIDGE:
2850 return tch_bridge(net, arg);
2851 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002852 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002853 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002854 return tch_recv_mncc(net, data->callref, 1);
2855 case GSM_TCHF_FRAME:
2856 /* Find callref */
2857 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002858 if (!trans) {
2859 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002860 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002861 }
2862 if (!trans->conn) {
2863 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002864 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002865 }
2866 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2867 /* This should be LOGL_ERROR or NOTICE, but
2868 * unfortuantely it happens for a couple of frames at
2869 * the beginning of every RTP connection */
2870 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002871 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002872 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002873 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002874 switch (bts->type) {
2875 case GSM_BTS_TYPE_NANOBTS:
Harald Welte17b56522010-12-22 21:45:05 +01002876 if (!trans->conn->lchan->abis_ip.rtp_socket) {
2877 LOGP(DMNCC, LOGL_ERROR, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002878 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002879 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002880 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002881 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002882 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002883 default:
2884 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2885 }
2886 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002887 }
2888
2889 memset(&rel, 0, sizeof(struct gsm_mncc));
2890 rel.callref = data->callref;
2891
2892 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002893 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002894
2895 /* Callref unknown */
2896 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002897 struct gsm_subscriber *subscr;
2898
Harald Welte6e1536e2009-07-04 10:11:24 +02002899 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002900 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2901 "Received '%s' from MNCC with "
2902 "unknown callref %d\n", data->called.number,
2903 get_mncc_name(msg_type), data->callref);
2904 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002905 return mncc_release_ind(net, NULL, data->callref,
2906 GSM48_CAUSE_LOC_PRN_S_LU,
2907 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002908 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002909 if (!data->called.number[0] && !data->imsi[0]) {
2910 DEBUGP(DCC, "(bts - trx - ts - ti) "
2911 "Received '%s' from MNCC with "
2912 "no number or IMSI\n", get_mncc_name(msg_type));
2913 /* Invalid number */
2914 return mncc_release_ind(net, NULL, data->callref,
2915 GSM48_CAUSE_LOC_PRN_S_LU,
2916 GSM48_CC_CAUSE_INV_NR_FORMAT);
2917 }
Harald Welte03740842009-06-10 23:11:52 +08002918 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002919 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002920 subscr = subscr_get_by_extension(net,
2921 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002922 else
Harald Welte75350412009-07-23 18:46:00 +02002923 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002924 /* If subscriber is not found */
2925 if (!subscr) {
2926 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2927 "Received '%s' from MNCC with "
2928 "unknown subscriber %s\n", data->called.number,
2929 get_mncc_name(msg_type), data->called.number);
2930 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002931 return mncc_release_ind(net, NULL, data->callref,
2932 GSM48_CAUSE_LOC_PRN_S_LU,
2933 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002934 }
2935 /* If subscriber is not "attached" */
2936 if (!subscr->lac) {
2937 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2938 "Received '%s' from MNCC with "
2939 "detached subscriber %s\n", data->called.number,
2940 get_mncc_name(msg_type), data->called.number);
2941 subscr_put(subscr);
2942 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002943 return mncc_release_ind(net, NULL, data->callref,
2944 GSM48_CAUSE_LOC_PRN_S_LU,
2945 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002946 }
2947 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002948 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2949 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002950 DEBUGP(DCC, "No memory for trans.\n");
2951 subscr_put(subscr);
2952 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002953 mncc_release_ind(net, NULL, data->callref,
2954 GSM48_CAUSE_LOC_PRN_S_LU,
2955 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002956 return -ENOMEM;
2957 }
Harald Welte03740842009-06-10 23:11:52 +08002958 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002959 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002960
Harald Welte03740842009-06-10 23:11:52 +08002961 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002962 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002963 /* find transaction with this subscriber already paging */
2964 llist_for_each_entry(transt, &net->trans_list, entry) {
2965 /* Transaction of our lchan? */
2966 if (transt == trans ||
2967 transt->subscr != subscr)
2968 continue;
2969 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2970 "Received '%s' from MNCC with "
2971 "unallocated channel, paging already "
2972 "started.\n", bts->nr,
2973 data->called.number,
2974 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002975 subscr_put(subscr);
2976 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002977 return 0;
2978 }
2979 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002980 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01002981
2982 /* Get a channel */
2983 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2984
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002985 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002986 return 0;
2987 }
2988 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002989 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002990 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002991 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002992
2993 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002994 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08002995
2996 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002997 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002998 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002999 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003000 (trans->subscr)?(trans->subscr->extension):"-",
3001 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003002 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3003 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003004 if (msg_type == MNCC_REL_REQ)
3005 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3006 else
3007 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3008 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003009 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003010 return rc;
3011 }
3012
3013 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3014 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003015 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003016 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003017 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003018 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003019 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003020
3021 /* Find function for current state and message */
3022 for (i = 0; i < DOWNSLLEN; i++)
3023 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003024 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003025 break;
3026 if (i == DOWNSLLEN) {
3027 DEBUGP(DCC, "Message unhandled at this state.\n");
3028 return 0;
3029 }
3030
3031 rc = downstatelist[i].rout(trans, arg);
3032
3033 return rc;
3034}
3035
3036
3037static struct datastate {
3038 u_int32_t states;
3039 int type;
3040 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3041} datastatelist[] = {
3042 /* mobile originating call establishment */
3043 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3044 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3045 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3046 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3047 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3048 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3049 /* mobile terminating call establishment */
3050 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3051 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3052 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3053 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003054 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte03740842009-06-10 23:11:52 +08003055 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3056 /* signalling during call */
3057 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3058 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3059 {SBIT(GSM_CSTATE_ACTIVE),
3060 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3061 {ALL_STATES,
3062 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3063 {ALL_STATES,
3064 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3065 {ALL_STATES,
3066 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3067 {SBIT(GSM_CSTATE_ACTIVE),
3068 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3069 {SBIT(GSM_CSTATE_ACTIVE),
3070 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3071 {SBIT(GSM_CSTATE_ACTIVE),
3072 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3073 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3074 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3075 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3076 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3077 {SBIT(GSM_CSTATE_ACTIVE),
3078 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3079 /* clearing */
3080 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3081 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3082 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3083 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3084 {ALL_STATES, /* 5.4.3.4 */
3085 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3086};
3087
3088#define DATASLLEN \
3089 (sizeof(datastatelist) / sizeof(struct datastate))
3090
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003091static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003092{
3093 struct gsm48_hdr *gh = msgb_l3(msg);
3094 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003095 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003096 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003097 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003098
Harald Welte03740842009-06-10 23:11:52 +08003099 if (msg_type & 0x80) {
3100 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3101 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003102 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003103
Harald Welte03740842009-06-10 23:11:52 +08003104 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003105 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003106
Harald Welte4861c822009-07-23 21:21:14 +02003107 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003108 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003109 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003110 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003111 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3112 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003113
3114 /* Create transaction */
3115 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003116 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003117 "creating new trans.\n", transaction_id);
3118 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003119 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003120 transaction_id, new_callref++);
3121 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003122 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08003123 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02003124 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003125 GSM48_MT_CC_RELEASE_COMPL);
3126 return -ENOMEM;
3127 }
Harald Welte03740842009-06-10 23:11:52 +08003128 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003129 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003130 }
3131
3132 /* find function for current state and message */
3133 for (i = 0; i < DATASLLEN; i++)
3134 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003135 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003136 break;
3137 if (i == DATASLLEN) {
3138 DEBUGP(DCC, "Message unhandled at this state.\n");
3139 return 0;
3140 }
3141
3142 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003143
3144 return rc;
3145}
3146
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003147/* Create a dummy to wait five seconds */
3148static void release_anchor(struct gsm_subscriber_connection *conn)
3149{
3150 if (!conn->anch_operation)
3151 return;
3152
3153 bsc_del_timer(&conn->anch_operation->timeout);
3154 talloc_free(conn->anch_operation);
3155 conn->anch_operation = NULL;
3156}
3157
3158static void anchor_timeout(void *_data)
3159{
3160 struct gsm_subscriber_connection *con = _data;
3161
3162 release_anchor(con);
3163 msc_release_connection(con);
3164}
3165
3166int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3167{
3168 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3169 if (!conn->anch_operation)
3170 return -1;
3171
3172 conn->anch_operation->timeout.data = conn;
3173 conn->anch_operation->timeout.cb = anchor_timeout;
3174 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3175 return 0;
3176}
3177
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003178/* here we get data from the BSC level... */
3179int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003180{
3181 struct gsm48_hdr *gh = msgb_l3(msg);
3182 u_int8_t pdisc = gh->proto_discr & 0x0f;
3183 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003184
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003185 if (silent_call_reroute(conn, msg))
3186 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003187
3188 switch (pdisc) {
3189 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003190 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003191 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003192 break;
3193 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003194 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003195 break;
3196 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003197 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003198 break;
3199 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003200 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003201 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003202 break;
3203 case GSM48_PDISC_MM_GPRS:
3204 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003205 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3206 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003207 break;
Harald Welte03115042009-10-16 08:32:58 +02003208 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003209 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003210 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003211 break;
Harald Welte59b04682009-06-10 05:40:52 +08003212 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003213 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3214 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003215 break;
3216 }
3217
3218 return rc;
3219}
3220
Harald Welte3c062072009-07-28 18:25:29 +02003221/*
3222 * This will be ran by the linker when loading the DSO. We use it to
3223 * do system initialization, e.g. registration of signal handlers.
3224 */
3225static __attribute__((constructor)) void on_dso_load_0408(void)
3226{
Harald Welte3c062072009-07-28 18:25:29 +02003227 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3228}