blob: 4ab61ba89bf72c0722c3682613be237e9b9c8f97 [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;
1331
1332 if (trans)
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001333 if (trans->conn)
Harald Welte4861c822009-07-23 21:21:14 +02001334 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001335 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001336 trans->conn->lchan->ts->trx->bts->nr,
1337 trans->conn->lchan->ts->trx->nr,
1338 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001339 (trans->subscr)?(trans->subscr->extension):"-",
1340 get_mncc_name(msg_type));
1341 else
1342 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1343 "Sending '%s' to MNCC.\n",
1344 (trans->subscr)?(trans->subscr->extension):"-",
1345 get_mncc_name(msg_type));
1346 else
1347 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1348 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1349
1350 mncc->msg_type = msg_type;
1351
Harald Welte9cfc9352009-06-26 19:39:35 +02001352 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001353 if (!msg)
1354 return -ENOMEM;
1355 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
Harald Weltebe0cb682010-12-22 23:43:29 +01001356 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001357
1358 return 0;
1359}
1360
1361int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1362 u_int32_t callref, int location, int value)
1363{
1364 struct gsm_mncc rel;
1365
Harald Weltecb0595f2009-06-12 01:54:08 +08001366 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001367 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001368 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001369 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1370}
1371
Harald Weltec2189a62009-07-23 18:56:43 +02001372/* Call Control Specific transaction release.
1373 * gets called by trans_free, DO NOT CALL YOURSELF! */
1374void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001375{
Harald Welte03740842009-06-10 23:11:52 +08001376 gsm48_stop_cc_timer(trans);
1377
1378 /* send release to L4, if callref still exists */
1379 if (trans->callref) {
1380 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001381 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001382 GSM48_CAUSE_LOC_PRN_S_LU,
1383 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001384 }
Harald Weltec2189a62009-07-23 18:56:43 +02001385 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001386 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001387 if (trans->conn)
1388 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001389}
1390
1391static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001392
Harald Welte59b04682009-06-10 05:40:52 +08001393/* call-back from paging the B-end of the connection */
1394static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001395 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001396{
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001397 struct gsm_subscriber_connection *conn = _conn;
Harald Welte03740842009-06-10 23:11:52 +08001398 struct gsm_subscriber *subscr = param;
1399 struct gsm_trans *transt, *tmp;
1400 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001401
Harald Welte59b04682009-06-10 05:40:52 +08001402 if (hooknum != GSM_HOOK_RR_PAGING)
1403 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001404
Harald Welte03740842009-06-10 23:11:52 +08001405 if (!subscr)
1406 return -EINVAL;
1407 net = subscr->net;
1408 if (!net) {
1409 DEBUGP(DCC, "Error Network not set!\n");
1410 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001411 }
1412
Harald Welte03740842009-06-10 23:11:52 +08001413 /* check all tranactions (without lchan) for subscriber */
1414 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001415 if (transt->subscr != subscr || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001416 continue;
1417 switch (event) {
1418 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001419 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001420 break;
1421 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1422 subscr->extension);
1423 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001424 if (!transt->conn) {
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001425 transt->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08001426 }
1427 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001428 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001429 break;
1430 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001431 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001432 DEBUGP(DCC, "Paging subscr %s expired!\n",
1433 subscr->extension);
1434 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001435 mncc_release_ind(transt->subscr->net, transt,
1436 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001437 GSM48_CAUSE_LOC_PRN_S_LU,
1438 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001439 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001440 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001441 break;
1442 }
1443 }
Harald Welte59b04682009-06-10 05:40:52 +08001444 return 0;
1445}
1446
Harald Welte3971ad52009-12-19 22:23:05 +01001447static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1448
Harald Welte3c062072009-07-28 18:25:29 +02001449/* some other part of the code sends us a signal */
1450static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1451 void *handler_data, void *signal_data)
1452{
1453 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001454 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001455 struct gsm_network *net;
1456 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001457
1458 if (subsys != SS_ABISIP)
1459 return 0;
1460
1461 /* in case we use direct BTS-to-BTS RTP */
1462 if (ipacc_rtp_direct)
1463 return 0;
1464
Harald Welte3c062072009-07-28 18:25:29 +02001465 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001466 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001467 /* check if any transactions on this lchan still have
1468 * a tch_recv_mncc request pending */
1469 net = lchan->ts->trx->bts->network;
1470 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001471 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001472 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1473 tch_recv_mncc(net, trans->callref, 1);
1474 }
1475 }
Harald Welte3c062072009-07-28 18:25:29 +02001476 break;
Harald Welte3c062072009-07-28 18:25:29 +02001477 }
1478
1479 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001480}
1481
Harald Welte59b04682009-06-10 05:40:52 +08001482/* map two ipaccess RTP streams onto each other */
1483static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1484{
1485 struct gsm_bts *bts = lchan->ts->trx->bts;
1486 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001487 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001488
1489 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1490 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1491 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1492
1493 if (bts->type != remote_bts->type) {
1494 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1495 return -EINVAL;
1496 }
Harald Welte3971ad52009-12-19 22:23:05 +01001497
1498 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001499 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001500 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001501 if (!ipacc_rtp_direct) {
1502 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001503 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001504 if (rc < 0)
1505 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001506 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001507#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001508
1509 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301510 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1511 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001512 } else {
1513 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301514 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1515 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301516 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001517 if (rc < 0)
1518 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301519 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1520 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301521 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001522 }
Harald Welte59b04682009-06-10 05:40:52 +08001523 break;
1524 case GSM_BTS_TYPE_BS11:
1525 trau_mux_map_lchan(lchan, remote_lchan);
1526 break;
1527 default:
1528 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001529 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001530 }
1531
1532 return 0;
1533}
1534
Harald Welte03740842009-06-10 23:11:52 +08001535/* bridge channels of two transactions */
1536static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001537{
Harald Weltec2189a62009-07-23 18:56:43 +02001538 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1539 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001540
Harald Welte03740842009-06-10 23:11:52 +08001541 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001542 return -EIO;
1543
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001544 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001545 return -EIO;
1546
1547 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001548 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001549}
1550
Harald Welte3971ad52009-12-19 22:23:05 +01001551/* enable receive of channels to MNCC upqueue */
1552static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001553{
1554 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001555 struct gsm_lchan *lchan;
1556 struct gsm_bts *bts;
1557 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001558
Harald Welte03740842009-06-10 23:11:52 +08001559 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001560 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001561 if (!trans)
1562 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001563 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001564 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001565 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001566 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001567
Harald Welte3971ad52009-12-19 22:23:05 +01001568 switch (bts->type) {
1569 case GSM_BTS_TYPE_NANOBTS:
1570 if (ipacc_rtp_direct) {
1571 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1572 return -EINVAL;
1573 }
1574 /* in case, we don't have a RTP socket yet, we note this
1575 * in the transaction and try later */
1576 if (!lchan->abis_ip.rtp_socket) {
1577 trans->tch_recv = enable;
1578 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1579 return 0;
1580 }
1581 if (enable) {
1582 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001583 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001584 if (rc < 0)
1585 return rc;
1586 /* assign socket to application interface */
1587 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1588 net, callref);
1589 } else
1590 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1591 net, 0);
1592 break;
1593 case GSM_BTS_TYPE_BS11:
1594 if (enable)
1595 return trau_recv_lchan(lchan, callref);
1596 return trau_mux_unmap(NULL, callref);
1597 break;
1598 default:
1599 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1600 return -EINVAL;
1601 }
1602
1603 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001604}
1605
Harald Welte03740842009-06-10 23:11:52 +08001606static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1607{
1608 DEBUGP(DCC, "-> STATUS ENQ\n");
1609 return gsm48_cc_tx_status(trans, msg);
1610}
1611
1612static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1613static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1614
1615static void gsm48_cc_timeout(void *arg)
1616{
1617 struct gsm_trans *trans = arg;
1618 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001619 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1620 int mo_location = GSM48_CAUSE_LOC_USER;
1621 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1622 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001623 struct gsm_mncc mo_rel, l4_rel;
1624
1625 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1626 mo_rel.callref = trans->callref;
1627 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1628 l4_rel.callref = trans->callref;
1629
Harald Weltec2189a62009-07-23 18:56:43 +02001630 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001631 case 0x303:
1632 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001633 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001634 break;
1635 case 0x310:
1636 disconnect = 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 0x313:
1640 disconnect = 1;
1641 /* unknown, did not find it in the specs */
1642 break;
1643 case 0x301:
1644 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001645 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001646 break;
1647 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001648 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001649 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001650 gsm48_cc_tx_release(trans, &trans->cc.msg);
1651 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001652 break; /* stay in release state */
1653 }
Harald Weltec2189a62009-07-23 18:56:43 +02001654 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001655 return;
1656// release = 1;
1657// l4_cause = 14;
1658// break;
1659 case 0x306:
1660 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001661 mo_cause = trans->cc.msg.cause.value;
1662 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001663 break;
1664 case 0x323:
1665 disconnect = 1;
1666 break;
1667 default:
1668 release = 1;
1669 }
1670
1671 if (release && trans->callref) {
1672 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001673 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001674 l4_location, l4_cause);
1675 trans->callref = 0;
1676 }
1677
1678 if (disconnect && trans->callref) {
1679 /* process disconnect towards layer 4 */
1680 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001681 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001682 }
1683
1684 /* process disconnect towards mobile station */
1685 if (disconnect || release) {
1686 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001687 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1688 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1689 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001690 mo_rel.cause.diag_len = 3;
1691
1692 if (disconnect)
1693 gsm48_cc_tx_disconnect(trans, &mo_rel);
1694 if (release)
1695 gsm48_cc_tx_release(trans, &mo_rel);
1696 }
1697
1698}
1699
1700static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1701 int sec, int micro)
1702{
1703 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001704 trans->cc.timer.cb = gsm48_cc_timeout;
1705 trans->cc.timer.data = trans;
1706 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1707 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001708}
1709
1710static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1711{
1712 struct gsm48_hdr *gh = msgb_l3(msg);
1713 u_int8_t msg_type = gh->msg_type & 0xbf;
1714 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1715 struct tlv_parsed tp;
1716 struct gsm_mncc setup;
1717
1718 memset(&setup, 0, sizeof(struct gsm_mncc));
1719 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001720 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001721 /* emergency setup is identified by msg_type */
1722 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1723 setup.emergency = 1;
1724
1725 /* use subscriber as calling party number */
1726 if (trans->subscr) {
1727 setup.fields |= MNCC_F_CALLING;
1728 strncpy(setup.calling.number, trans->subscr->extension,
1729 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001730 strncpy(setup.imsi, trans->subscr->imsi,
1731 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001732 }
1733 /* bearer capability */
1734 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1735 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001736 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001737 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1738 }
1739 /* facility */
1740 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1741 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001742 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001743 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1744 }
1745 /* called party bcd number */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1747 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001748 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001749 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1750 }
1751 /* user-user */
1752 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1753 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001754 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001755 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1756 }
1757 /* ss-version */
1758 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1759 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001760 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001761 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1762 }
1763 /* CLIR suppression */
1764 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1765 setup.clir.sup = 1;
1766 /* CLIR invocation */
1767 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1768 setup.clir.inv = 1;
1769 /* cc cap */
1770 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1771 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001772 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001773 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1774 }
1775
Harald Welte03740842009-06-10 23:11:52 +08001776 new_cc_state(trans, GSM_CSTATE_INITIATED);
1777
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001778 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1779 subscr_name(trans->subscr), trans->subscr->extension,
1780 setup.called.number);
1781
Harald Weltec56006b2010-12-24 16:06:33 +01001782 counter_inc(trans->subscr->net->stats.call.mo_setup);
1783
Harald Welte03740842009-06-10 23:11:52 +08001784 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001785 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001786
Harald Welteca745e22009-07-29 12:10:35 +02001787 /* MNCC code will modify the channel asynchronously, we should
1788 * ipaccess-bind only after the modification has been made to the
1789 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001790 return 0;
1791}
1792
1793static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001794{
1795 struct msgb *msg = gsm48_msgb_alloc();
1796 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001797 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001798 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001799
1800 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1801
Harald Welte03740842009-06-10 23:11:52 +08001802 /* transaction id must not be assigned */
1803 if (trans->transaction_id != 0xff) { /* unasssigned */
1804 DEBUGP(DCC, "TX Setup with assigned transaction. "
1805 "This is not allowed!\n");
1806 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001807 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001808 GSM48_CAUSE_LOC_PRN_S_LU,
1809 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001810 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001811 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001812 return rc;
1813 }
1814
1815 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001816 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1817 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001818 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001819 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001820 GSM48_CAUSE_LOC_PRN_S_LU,
1821 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001822 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001823 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001824 return rc;
1825 }
Harald Welte165fc332009-07-23 21:36:44 +02001826 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001827
Harald Welte59b04682009-06-10 05:40:52 +08001828 gh->msg_type = GSM48_MT_CC_SETUP;
1829
Harald Welte03740842009-06-10 23:11:52 +08001830 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001831
Harald Welte03740842009-06-10 23:11:52 +08001832 /* bearer capability */
1833 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001834 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001835 /* facility */
1836 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001837 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001838 /* progress */
1839 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001840 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001841 /* calling party BCD number */
1842 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001843 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001844 /* called party BCD number */
1845 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001846 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001847 /* user-user */
1848 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001849 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001850 /* redirecting party BCD number */
1851 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001852 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001853 /* signal */
1854 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001855 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001856
1857 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001858
Harald Weltec56006b2010-12-24 16:06:33 +01001859 counter_inc(trans->subscr->net->stats.call.mt_setup);
1860
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001861 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001862}
1863
Harald Welte03740842009-06-10 23:11:52 +08001864static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1865{
1866 struct gsm48_hdr *gh = msgb_l3(msg);
1867 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1868 struct tlv_parsed tp;
1869 struct gsm_mncc call_conf;
1870
1871 gsm48_stop_cc_timer(trans);
1872 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1873
1874 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1875 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001876 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001877#if 0
1878 /* repeat */
1879 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1880 call_conf.repeat = 1;
1881 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1882 call_conf.repeat = 2;
1883#endif
1884 /* bearer capability */
1885 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1886 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001887 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001888 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1889 }
1890 /* cause */
1891 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1892 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001893 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001894 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1895 }
1896 /* cc cap */
1897 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1898 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001899 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001900 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1901 }
1902
1903 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1904
Harald Welte0abe5a62009-07-23 19:06:52 +02001905 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1906 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001907}
1908
1909static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1910{
1911 struct gsm_mncc *proceeding = arg;
1912 struct msgb *msg = gsm48_msgb_alloc();
1913 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1914
Harald Welte03740842009-06-10 23:11:52 +08001915 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1916
1917 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1918
1919 /* bearer capability */
1920 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001921 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001922 /* facility */
1923 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001924 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001925 /* progress */
1926 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001927 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001928
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001929 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001930}
1931
1932static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1933{
1934 struct gsm48_hdr *gh = msgb_l3(msg);
1935 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1936 struct tlv_parsed tp;
1937 struct gsm_mncc alerting;
1938
1939 gsm48_stop_cc_timer(trans);
1940 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1941
1942 memset(&alerting, 0, sizeof(struct gsm_mncc));
1943 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001944 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001945 /* facility */
1946 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1947 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001948 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001949 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1950 }
1951
1952 /* progress */
1953 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1954 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001955 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001956 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1957 }
1958 /* ss-version */
1959 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1960 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001961 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001962 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1963 }
1964
1965 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1966
Harald Welte0abe5a62009-07-23 19:06:52 +02001967 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1968 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001969}
1970
1971static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1972{
1973 struct gsm_mncc *alerting = arg;
1974 struct msgb *msg = gsm48_msgb_alloc();
1975 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1976
Harald Welte03740842009-06-10 23:11:52 +08001977 gh->msg_type = GSM48_MT_CC_ALERTING;
1978
1979 /* facility */
1980 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001981 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001982 /* progress */
1983 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001984 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001985 /* user-user */
1986 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001987 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001988
1989 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1990
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001991 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001992}
1993
1994static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1995{
1996 struct gsm_mncc *progress = arg;
1997 struct msgb *msg = gsm48_msgb_alloc();
1998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1999
Harald Welte03740842009-06-10 23:11:52 +08002000 gh->msg_type = GSM48_MT_CC_PROGRESS;
2001
2002 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002003 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002004 /* user-user */
2005 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002006 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002007
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002008 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002009}
2010
2011static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2012{
2013 struct gsm_mncc *connect = arg;
2014 struct msgb *msg = gsm48_msgb_alloc();
2015 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2016
Harald Welte03740842009-06-10 23:11:52 +08002017 gh->msg_type = GSM48_MT_CC_CONNECT;
2018
2019 gsm48_stop_cc_timer(trans);
2020 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2021
2022 /* facility */
2023 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002024 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002025 /* progress */
2026 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002027 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002028 /* connected number */
2029 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002030 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002031 /* user-user */
2032 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002033 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002034
2035 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2036
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002037 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002038}
2039
2040static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2041{
2042 struct gsm48_hdr *gh = msgb_l3(msg);
2043 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2044 struct tlv_parsed tp;
2045 struct gsm_mncc connect;
2046
2047 gsm48_stop_cc_timer(trans);
2048
2049 memset(&connect, 0, sizeof(struct gsm_mncc));
2050 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002051 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002052 /* use subscriber as connected party number */
2053 if (trans->subscr) {
2054 connect.fields |= MNCC_F_CONNECTED;
2055 strncpy(connect.connected.number, trans->subscr->extension,
2056 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002057 strncpy(connect.imsi, trans->subscr->imsi,
2058 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002059 }
2060 /* facility */
2061 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2062 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002063 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002064 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2065 }
2066 /* user-user */
2067 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2068 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002069 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002070 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2071 }
2072 /* ss-version */
2073 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2074 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002075 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002076 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2077 }
2078
2079 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltec56006b2010-12-24 16:06:33 +01002080 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002081
Harald Welte0abe5a62009-07-23 19:06:52 +02002082 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002083}
2084
2085
2086static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2087{
2088 struct gsm_mncc connect_ack;
2089
2090 gsm48_stop_cc_timer(trans);
2091
2092 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltec56006b2010-12-24 16:06:33 +01002093 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002094
2095 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2096 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002097
Harald Welte0abe5a62009-07-23 19:06:52 +02002098 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002099 &connect_ack);
2100}
2101
2102static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2103{
2104 struct msgb *msg = gsm48_msgb_alloc();
2105 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2106
Harald Welte03740842009-06-10 23:11:52 +08002107 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2108
2109 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2110
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002111 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002112}
2113
2114static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2115{
2116 struct gsm48_hdr *gh = msgb_l3(msg);
2117 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2118 struct tlv_parsed tp;
2119 struct gsm_mncc disc;
2120
2121 gsm48_stop_cc_timer(trans);
2122
2123 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2124
2125 memset(&disc, 0, sizeof(struct gsm_mncc));
2126 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002127 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002128 /* cause */
2129 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2130 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002131 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002132 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2133 }
2134 /* facility */
2135 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2136 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002137 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002138 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2139 }
2140 /* user-user */
2141 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2142 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002143 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002144 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2145 }
2146 /* ss-version */
2147 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2148 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002149 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002150 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2151 }
2152
Harald Welte0abe5a62009-07-23 19:06:52 +02002153 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002154
2155}
2156
Harald Weltebbc636a2009-06-11 14:23:20 +08002157static struct gsm_mncc_cause default_cause = {
2158 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2159 .coding = 0,
2160 .rec = 0,
2161 .rec_val = 0,
2162 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2163 .diag_len = 0,
2164 .diag = { 0 },
2165};
Harald Welte03740842009-06-10 23:11:52 +08002166
2167static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2168{
2169 struct gsm_mncc *disc = arg;
2170 struct msgb *msg = gsm48_msgb_alloc();
2171 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2172
Harald Welte03740842009-06-10 23:11:52 +08002173 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2174
2175 gsm48_stop_cc_timer(trans);
2176 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2177
2178 /* cause */
2179 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002180 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002181 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002182 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002183
2184 /* facility */
2185 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002186 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002187 /* progress */
2188 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002189 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002190 /* user-user */
2191 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002192 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002193
2194 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002195 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002196
2197 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2198
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002199 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002200}
2201
2202static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2203{
2204 struct gsm48_hdr *gh = msgb_l3(msg);
2205 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2206 struct tlv_parsed tp;
2207 struct gsm_mncc rel;
2208 int rc;
2209
2210 gsm48_stop_cc_timer(trans);
2211
2212 memset(&rel, 0, sizeof(struct gsm_mncc));
2213 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002214 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002215 /* cause */
2216 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2217 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002218 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002219 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2220 }
2221 /* facility */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2223 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002224 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2226 }
2227 /* user-user */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2229 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002230 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2232 }
2233 /* ss-version */
2234 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2235 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002236 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002237 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2238 }
2239
Harald Weltec2189a62009-07-23 18:56:43 +02002240 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002241 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002242 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002243 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002244 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002245 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002246 GSM48_MT_CC_RELEASE_COMPL);
2247 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002248 }
2249
2250 new_cc_state(trans, GSM_CSTATE_NULL);
2251
2252 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002253 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002254
2255 return rc;
2256}
2257
2258static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2259{
2260 struct gsm_mncc *rel = arg;
2261 struct msgb *msg = gsm48_msgb_alloc();
2262 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2263
Harald Welte03740842009-06-10 23:11:52 +08002264 gh->msg_type = GSM48_MT_CC_RELEASE;
2265
2266 trans->callref = 0;
2267
2268 gsm48_stop_cc_timer(trans);
2269 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2270
2271 /* cause */
2272 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002273 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002274 /* facility */
2275 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002276 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002277 /* user-user */
2278 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002279 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002280
Harald Weltec2189a62009-07-23 18:56:43 +02002281 trans->cc.T308_second = 0;
2282 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002283
Harald Weltec2189a62009-07-23 18:56:43 +02002284 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002285 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2286
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002287 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002288}
2289
2290static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2291{
2292 struct gsm48_hdr *gh = msgb_l3(msg);
2293 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2294 struct tlv_parsed tp;
2295 struct gsm_mncc rel;
2296 int rc = 0;
2297
2298 gsm48_stop_cc_timer(trans);
2299
2300 memset(&rel, 0, sizeof(struct gsm_mncc));
2301 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002302 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002303 /* cause */
2304 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2305 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002306 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002307 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2308 }
2309 /* facility */
2310 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2311 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002312 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002313 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2314 }
2315 /* user-user */
2316 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2317 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002318 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002319 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2320 }
2321 /* ss-version */
2322 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2323 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002324 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002325 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2326 }
2327
2328 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002329 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002330 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002331 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002332 MNCC_REJ_IND, &rel);
2333 break;
2334 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002335 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002336 MNCC_REL_CNF, &rel);
2337 break;
2338 default:
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_IND, &rel);
2341 }
2342 }
2343
2344 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002345 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002346
2347 return rc;
2348}
2349
2350static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2351{
2352 struct gsm_mncc *rel = arg;
2353 struct msgb *msg = gsm48_msgb_alloc();
2354 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002355 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002356
Harald Welte03740842009-06-10 23:11:52 +08002357 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2358
2359 trans->callref = 0;
2360
2361 gsm48_stop_cc_timer(trans);
2362
2363 /* cause */
2364 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002365 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002366 /* facility */
2367 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002368 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002369 /* user-user */
2370 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002371 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002372
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002373 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2374
Harald Weltec2189a62009-07-23 18:56:43 +02002375 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002376
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002377 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002378}
2379
2380static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2381{
2382 struct gsm48_hdr *gh = msgb_l3(msg);
2383 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2384 struct tlv_parsed tp;
2385 struct gsm_mncc fac;
2386
2387 memset(&fac, 0, sizeof(struct gsm_mncc));
2388 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002389 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002390 /* facility */
2391 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2392 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002393 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002394 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2395 }
2396 /* ss-version */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2398 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002399 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2401 }
2402
Harald Welte0abe5a62009-07-23 19:06:52 +02002403 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002404}
2405
2406static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2407{
2408 struct gsm_mncc *fac = arg;
2409 struct msgb *msg = gsm48_msgb_alloc();
2410 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2411
Harald Welte03740842009-06-10 23:11:52 +08002412 gh->msg_type = GSM48_MT_CC_FACILITY;
2413
2414 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002415 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002416
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002417 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002418}
2419
2420static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2421{
2422 struct gsm_mncc hold;
2423
2424 memset(&hold, 0, sizeof(struct gsm_mncc));
2425 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002426 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2430{
2431 struct msgb *msg = gsm48_msgb_alloc();
2432 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2433
Harald Welte03740842009-06-10 23:11:52 +08002434 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2435
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002436 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002437}
2438
2439static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2440{
2441 struct gsm_mncc *hold_rej = arg;
2442 struct msgb *msg = gsm48_msgb_alloc();
2443 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2444
Harald Welte03740842009-06-10 23:11:52 +08002445 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2446
2447 /* cause */
2448 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002449 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002450 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002451 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002452
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002453 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm_mncc retrieve;
2459
2460 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2461 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002462 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2463 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2467{
2468 struct msgb *msg = gsm48_msgb_alloc();
2469 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2470
Harald Welte03740842009-06-10 23:11:52 +08002471 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2472
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002473 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2477{
2478 struct gsm_mncc *retrieve_rej = arg;
2479 struct msgb *msg = gsm48_msgb_alloc();
2480 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2481
Harald Welte03740842009-06-10 23:11:52 +08002482 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2483
2484 /* cause */
2485 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002486 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002487 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002488 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002489
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002490 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002491}
2492
2493static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2494{
2495 struct gsm48_hdr *gh = msgb_l3(msg);
2496 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2497 struct tlv_parsed tp;
2498 struct gsm_mncc dtmf;
2499
2500 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2501 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002502 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002503 /* keypad facility */
2504 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2505 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002506 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002507 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2508 }
2509
Harald Welte0abe5a62009-07-23 19:06:52 +02002510 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2514{
2515 struct gsm_mncc *dtmf = arg;
2516 struct msgb *msg = gsm48_msgb_alloc();
2517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2518
Harald Welte03740842009-06-10 23:11:52 +08002519 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2520
2521 /* keypad */
2522 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002523 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002524
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002525 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002526}
2527
2528static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2529{
2530 struct gsm_mncc *dtmf = arg;
2531 struct msgb *msg = gsm48_msgb_alloc();
2532 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2533
Harald Welte03740842009-06-10 23:11:52 +08002534 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2535
2536 /* cause */
2537 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002538 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002539 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002540 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002541
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002542 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2546{
2547 struct msgb *msg = gsm48_msgb_alloc();
2548 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2549
Harald Welte03740842009-06-10 23:11:52 +08002550 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2551
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002552 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002553}
2554
2555static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2556{
2557 struct gsm_mncc dtmf;
2558
2559 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2560 dtmf.callref = trans->callref;
2561
Harald Welte0abe5a62009-07-23 19:06:52 +02002562 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002563}
2564
2565static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2566{
2567 struct gsm48_hdr *gh = msgb_l3(msg);
2568 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2569 struct tlv_parsed tp;
2570 struct gsm_mncc modify;
2571
2572 memset(&modify, 0, sizeof(struct gsm_mncc));
2573 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002574 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002575 /* bearer capability */
2576 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2577 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002578 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002579 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2580 }
2581
2582 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2583
Harald Welte0abe5a62009-07-23 19:06:52 +02002584 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002585}
2586
2587static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2588{
2589 struct gsm_mncc *modify = arg;
2590 struct msgb *msg = gsm48_msgb_alloc();
2591 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2592
Harald Welte03740842009-06-10 23:11:52 +08002593 gh->msg_type = GSM48_MT_CC_MODIFY;
2594
2595 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2596
2597 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002598 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002599
2600 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2601
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002602 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2606{
2607 struct gsm48_hdr *gh = msgb_l3(msg);
2608 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2609 struct tlv_parsed tp;
2610 struct gsm_mncc modify;
2611
2612 gsm48_stop_cc_timer(trans);
2613
2614 memset(&modify, 0, sizeof(struct gsm_mncc));
2615 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002616 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002617 /* bearer capability */
2618 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2619 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002620 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002621 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2622 }
2623
2624 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2625
Harald Welte0abe5a62009-07-23 19:06:52 +02002626 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002627}
2628
2629static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2630{
2631 struct gsm_mncc *modify = arg;
2632 struct msgb *msg = gsm48_msgb_alloc();
2633 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2634
Harald Welte03740842009-06-10 23:11:52 +08002635 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2636
2637 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002638 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002639
2640 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2641
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002642 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002643}
2644
2645static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2646{
2647 struct gsm48_hdr *gh = msgb_l3(msg);
2648 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2649 struct tlv_parsed tp;
2650 struct gsm_mncc modify;
2651
2652 gsm48_stop_cc_timer(trans);
2653
2654 memset(&modify, 0, sizeof(struct gsm_mncc));
2655 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002656 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002657 /* bearer capability */
2658 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2659 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002660 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002661 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2662 }
2663 /* cause */
2664 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2665 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002666 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002667 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2668 }
2669
2670 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2671
Harald Welte0abe5a62009-07-23 19:06:52 +02002672 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002673}
2674
2675static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2676{
2677 struct gsm_mncc *modify = arg;
2678 struct msgb *msg = gsm48_msgb_alloc();
2679 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2680
Harald Welte03740842009-06-10 23:11:52 +08002681 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2682
2683 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002684 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002685 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002686 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002687
2688 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2689
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002690 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002691}
2692
2693static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2694{
2695 struct gsm_mncc *notify = arg;
2696 struct msgb *msg = gsm48_msgb_alloc();
2697 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2698
Harald Welte03740842009-06-10 23:11:52 +08002699 gh->msg_type = GSM48_MT_CC_NOTIFY;
2700
2701 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002702 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002703
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002704 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002705}
2706
2707static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2708{
2709 struct gsm48_hdr *gh = msgb_l3(msg);
2710 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2711// struct tlv_parsed tp;
2712 struct gsm_mncc notify;
2713
2714 memset(&notify, 0, sizeof(struct gsm_mncc));
2715 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002716// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002717 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002718 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002719
Harald Welte0abe5a62009-07-23 19:06:52 +02002720 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002721}
2722
2723static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2724{
2725 struct gsm_mncc *user = arg;
2726 struct msgb *msg = gsm48_msgb_alloc();
2727 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2728
Harald Welte03740842009-06-10 23:11:52 +08002729 gh->msg_type = GSM48_MT_CC_USER_INFO;
2730
2731 /* user-user */
2732 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002733 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002734 /* more data */
2735 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002736 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002737
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002738 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002739}
2740
2741static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2742{
2743 struct gsm48_hdr *gh = msgb_l3(msg);
2744 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2745 struct tlv_parsed tp;
2746 struct gsm_mncc user;
2747
2748 memset(&user, 0, sizeof(struct gsm_mncc));
2749 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002750 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002751 /* user-user */
2752 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2753 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002754 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002755 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2756 }
2757 /* more data */
2758 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2759 user.more = 1;
2760
Harald Welte0abe5a62009-07-23 19:06:52 +02002761 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002762}
2763
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002764static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002765{
2766 struct gsm_mncc *mode = arg;
2767
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002768 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002769}
2770
2771static struct downstate {
2772 u_int32_t states;
2773 int type;
2774 int (*rout) (struct gsm_trans *trans, void *arg);
2775} downstatelist[] = {
2776 /* mobile originating call establishment */
2777 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2778 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2779 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2780 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2781 {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 */
2782 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2783 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2784 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2785 /* mobile terminating call establishment */
2786 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2787 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2788 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2789 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2790 /* signalling during call */
2791 {SBIT(GSM_CSTATE_ACTIVE),
2792 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2793 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2794 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2795 {ALL_STATES,
2796 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2797 {ALL_STATES,
2798 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2799 {ALL_STATES,
2800 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2801 {SBIT(GSM_CSTATE_ACTIVE),
2802 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2803 {SBIT(GSM_CSTATE_ACTIVE),
2804 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2805 {SBIT(GSM_CSTATE_ACTIVE),
2806 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2807 {SBIT(GSM_CSTATE_ACTIVE),
2808 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2809 {SBIT(GSM_CSTATE_ACTIVE),
2810 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2811 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2812 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2813 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2814 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2815 {SBIT(GSM_CSTATE_ACTIVE),
2816 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2817 /* clearing */
2818 {SBIT(GSM_CSTATE_INITIATED),
2819 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2820 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2821 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2822 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2823 MNCC_REL_REQ, gsm48_cc_tx_release},
2824 /* special */
2825 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002826 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002827};
2828
2829#define DOWNSLLEN \
2830 (sizeof(downstatelist) / sizeof(struct downstate))
2831
2832
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002833int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002834{
Harald Welteaa60edb2009-08-09 18:52:33 +02002835 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002836 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002837 struct gsm_subscriber_connection *conn = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002838 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002839 struct gsm_mncc *data = arg, rel;
2840
2841 /* handle special messages */
2842 switch(msg_type) {
2843 case MNCC_BRIDGE:
2844 return tch_bridge(net, arg);
2845 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002846 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002847 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002848 return tch_recv_mncc(net, data->callref, 1);
2849 case GSM_TCHF_FRAME:
2850 /* Find callref */
2851 trans = trans_find_by_callref(net, data->callref);
2852 if (!trans)
2853 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002854 if (!trans->conn)
Harald Welte3971ad52009-12-19 22:23:05 +01002855 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002856 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welte3971ad52009-12-19 22:23:05 +01002857 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002858 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002859 switch (bts->type) {
2860 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002861 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welte3971ad52009-12-19 22:23:05 +01002862 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002863 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002864 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002865 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002866 default:
2867 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2868 }
2869 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002870 }
2871
2872 memset(&rel, 0, sizeof(struct gsm_mncc));
2873 rel.callref = data->callref;
2874
2875 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002876 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002877
2878 /* Callref unknown */
2879 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002880 struct gsm_subscriber *subscr;
2881
Harald Welte6e1536e2009-07-04 10:11:24 +02002882 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002883 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2884 "Received '%s' from MNCC with "
2885 "unknown callref %d\n", data->called.number,
2886 get_mncc_name(msg_type), data->callref);
2887 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002888 return mncc_release_ind(net, NULL, data->callref,
2889 GSM48_CAUSE_LOC_PRN_S_LU,
2890 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002891 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002892 if (!data->called.number[0] && !data->imsi[0]) {
2893 DEBUGP(DCC, "(bts - trx - ts - ti) "
2894 "Received '%s' from MNCC with "
2895 "no number or IMSI\n", get_mncc_name(msg_type));
2896 /* Invalid number */
2897 return mncc_release_ind(net, NULL, data->callref,
2898 GSM48_CAUSE_LOC_PRN_S_LU,
2899 GSM48_CC_CAUSE_INV_NR_FORMAT);
2900 }
Harald Welte03740842009-06-10 23:11:52 +08002901 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002902 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002903 subscr = subscr_get_by_extension(net,
2904 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002905 else
Harald Welte75350412009-07-23 18:46:00 +02002906 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002907 /* If subscriber is not found */
2908 if (!subscr) {
2909 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2910 "Received '%s' from MNCC with "
2911 "unknown subscriber %s\n", data->called.number,
2912 get_mncc_name(msg_type), data->called.number);
2913 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002914 return mncc_release_ind(net, NULL, data->callref,
2915 GSM48_CAUSE_LOC_PRN_S_LU,
2916 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002917 }
2918 /* If subscriber is not "attached" */
2919 if (!subscr->lac) {
2920 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2921 "Received '%s' from MNCC with "
2922 "detached subscriber %s\n", data->called.number,
2923 get_mncc_name(msg_type), data->called.number);
2924 subscr_put(subscr);
2925 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002926 return mncc_release_ind(net, NULL, data->callref,
2927 GSM48_CAUSE_LOC_PRN_S_LU,
2928 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002929 }
2930 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002931 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2932 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002933 DEBUGP(DCC, "No memory for trans.\n");
2934 subscr_put(subscr);
2935 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002936 mncc_release_ind(net, NULL, data->callref,
2937 GSM48_CAUSE_LOC_PRN_S_LU,
2938 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002939 return -ENOMEM;
2940 }
Harald Welte03740842009-06-10 23:11:52 +08002941 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002942 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002943
Harald Welte03740842009-06-10 23:11:52 +08002944 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002945 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002946 /* find transaction with this subscriber already paging */
2947 llist_for_each_entry(transt, &net->trans_list, entry) {
2948 /* Transaction of our lchan? */
2949 if (transt == trans ||
2950 transt->subscr != subscr)
2951 continue;
2952 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2953 "Received '%s' from MNCC with "
2954 "unallocated channel, paging already "
2955 "started.\n", bts->nr,
2956 data->called.number,
2957 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002958 subscr_put(subscr);
2959 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002960 return 0;
2961 }
2962 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002963 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01002964
2965 /* Get a channel */
2966 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2967
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002968 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002969 return 0;
2970 }
2971 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002972 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002973 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002974 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002975
2976 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002977 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08002978
2979 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002980 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002981 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002982 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08002983 (trans->subscr)?(trans->subscr->extension):"-",
2984 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08002985 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2986 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08002987 if (msg_type == MNCC_REL_REQ)
2988 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2989 else
2990 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2991 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002992 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002993 return rc;
2994 }
2995
2996 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2997 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002998 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08002999 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003000 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003001 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003002 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003003
3004 /* Find function for current state and message */
3005 for (i = 0; i < DOWNSLLEN; i++)
3006 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003007 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003008 break;
3009 if (i == DOWNSLLEN) {
3010 DEBUGP(DCC, "Message unhandled at this state.\n");
3011 return 0;
3012 }
3013
3014 rc = downstatelist[i].rout(trans, arg);
3015
3016 return rc;
3017}
3018
3019
3020static struct datastate {
3021 u_int32_t states;
3022 int type;
3023 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3024} datastatelist[] = {
3025 /* mobile originating call establishment */
3026 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3027 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3028 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3029 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3030 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3031 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3032 /* mobile terminating call establishment */
3033 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3034 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3035 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3036 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003037 {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 +08003038 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3039 /* signalling during call */
3040 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3041 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3042 {SBIT(GSM_CSTATE_ACTIVE),
3043 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3044 {ALL_STATES,
3045 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3046 {ALL_STATES,
3047 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3048 {ALL_STATES,
3049 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3050 {SBIT(GSM_CSTATE_ACTIVE),
3051 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3052 {SBIT(GSM_CSTATE_ACTIVE),
3053 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3054 {SBIT(GSM_CSTATE_ACTIVE),
3055 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3056 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3057 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3058 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3059 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3060 {SBIT(GSM_CSTATE_ACTIVE),
3061 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3062 /* clearing */
3063 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3064 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3065 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3066 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3067 {ALL_STATES, /* 5.4.3.4 */
3068 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3069};
3070
3071#define DATASLLEN \
3072 (sizeof(datastatelist) / sizeof(struct datastate))
3073
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003074static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003075{
3076 struct gsm48_hdr *gh = msgb_l3(msg);
3077 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003078 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003079 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003080 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003081
Harald Welte03740842009-06-10 23:11:52 +08003082 if (msg_type & 0x80) {
3083 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3084 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003085 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003086
Harald Welte03740842009-06-10 23:11:52 +08003087 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003088 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003089
Harald Welte4861c822009-07-23 21:21:14 +02003090 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003091 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003092 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003093 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003094 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3095 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003096
3097 /* Create transaction */
3098 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003099 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003100 "creating new trans.\n", transaction_id);
3101 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003102 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003103 transaction_id, new_callref++);
3104 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003105 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08003106 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02003107 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003108 GSM48_MT_CC_RELEASE_COMPL);
3109 return -ENOMEM;
3110 }
Harald Welte03740842009-06-10 23:11:52 +08003111 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003112 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003113 }
3114
3115 /* find function for current state and message */
3116 for (i = 0; i < DATASLLEN; i++)
3117 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003118 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003119 break;
3120 if (i == DATASLLEN) {
3121 DEBUGP(DCC, "Message unhandled at this state.\n");
3122 return 0;
3123 }
3124
3125 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003126
3127 return rc;
3128}
3129
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003130/* Create a dummy to wait five seconds */
3131static void release_anchor(struct gsm_subscriber_connection *conn)
3132{
3133 if (!conn->anch_operation)
3134 return;
3135
3136 bsc_del_timer(&conn->anch_operation->timeout);
3137 talloc_free(conn->anch_operation);
3138 conn->anch_operation = NULL;
3139}
3140
3141static void anchor_timeout(void *_data)
3142{
3143 struct gsm_subscriber_connection *con = _data;
3144
3145 release_anchor(con);
3146 msc_release_connection(con);
3147}
3148
3149int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3150{
3151 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3152 if (!conn->anch_operation)
3153 return -1;
3154
3155 conn->anch_operation->timeout.data = conn;
3156 conn->anch_operation->timeout.cb = anchor_timeout;
3157 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3158 return 0;
3159}
3160
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003161/* here we get data from the BSC level... */
3162int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003163{
3164 struct gsm48_hdr *gh = msgb_l3(msg);
3165 u_int8_t pdisc = gh->proto_discr & 0x0f;
3166 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003167
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003168 if (silent_call_reroute(conn, msg))
3169 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003170
3171 switch (pdisc) {
3172 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003173 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003174 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003175 break;
3176 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003177 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003178 break;
3179 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003180 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003181 break;
3182 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003183 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003184 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003185 break;
3186 case GSM48_PDISC_MM_GPRS:
3187 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003188 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3189 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003190 break;
Harald Welte03115042009-10-16 08:32:58 +02003191 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003192 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003193 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003194 break;
Harald Welte59b04682009-06-10 05:40:52 +08003195 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003196 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3197 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003198 break;
3199 }
3200
3201 return rc;
3202}
3203
Harald Welte3c062072009-07-28 18:25:29 +02003204/*
3205 * This will be ran by the linker when loading the DSO. We use it to
3206 * do system initialization, e.g. registration of signal handlers.
3207 */
3208static __attribute__((constructor)) void on_dso_load_0408(void)
3209{
Harald Welte3c062072009-07-28 18:25:29 +02003210 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3211}