blob: e8070522d3526ccdbbf2e7409be8ac5989e25036 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Sylvain Munaut30a15382009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
44#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000045#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000046#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000047#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000048#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000049#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020050#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010051#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010052#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080056#include <openbsc/bsc_api.h>
Harald Welte52b1f982008-12-23 20:25:15 +000057
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010059void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020060
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080061int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080062static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000063 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010064static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000065
Harald Welte52b1f982008-12-23 20:25:15 +000066struct gsm_lai {
67 u_int16_t mcc;
68 u_int16_t mnc;
69 u_int16_t lac;
70};
71
Harald Welte4bfdfe72009-06-10 23:11:52 +080072static u_int32_t new_callref = 0x80000001;
73
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080074static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
75 struct gsm_trans *trans)
76{
77 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
78
79 /* if we get passed a transaction reference, do some common
80 * work that the caller no longer has to do */
81 if (trans) {
82 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
83 msg->lchan = trans->conn->lchan;
84 }
85
86
87 if (msg->lchan) {
88 msg->trx = msg->lchan->ts->trx;
89 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
90 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
91 "Sending '%s' to MS.\n", msg->trx->bts->nr,
92 msg->trx->nr, msg->lchan->ts->nr,
93 gh->proto_discr & 0xf0,
94 gsm48_cc_msg_name(gh->msg_type));
95 else
96 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
97 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
98 msg->trx->nr, msg->lchan->ts->nr,
99 gh->proto_discr, gh->msg_type);
100 }
101
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800102 return gsm0808_submit_dtap(conn, msg, 0);
103}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100104
105static void release_security_operation(struct gsm_subscriber_connection *conn)
106{
107 if (!conn->sec_operation)
108 return;
109
110 talloc_free(conn->sec_operation);
111 conn->sec_operation = NULL;
112 put_subscr_con(conn);
113}
114
115static void allocate_security_operation(struct gsm_subscriber_connection *conn)
116{
117 use_subscr_con(conn)
118
119 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
120 struct gsm_security_operation);
121}
122
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800123static int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
124 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100125{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800126 struct gsm_network *net = conn->bts->network;
127 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100128 struct gsm_security_operation *op;
129 struct gsm_auth_tuple atuple;
130 int status = -1, rc;
131
132 /* Check if we _can_ enable encryption. Cases where we can't:
133 * - Encryption disabled in config
134 * - Channel already secured (nothing to do)
135 * - Subscriber equipment doesn't support configured encryption
136 */
137 if (!net->a5_encryption) {
138 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800139 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100140 DEBUGP(DMM, "Requesting to secure an already secure channel");
141 status = GSM_SECURITY_SUCCEEDED;
142 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
143 net->a5_encryption)) {
144 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
145 status = GSM_SECURITY_NOAVAIL;
146 }
147
148 /* If not done yet, try to get info for this user */
149 if (status < 0) {
150 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
151 if (rc <= 0)
152 status = GSM_SECURITY_NOAVAIL;
153 }
154
155 /* Are we done yet ? */
156 if (status >= 0)
157 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800158 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100159 0;
160
161 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800162 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100163 return -EBUSY;
164
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800165 allocate_security_operation(conn);
166 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100167 op->cb = cb;
168 op->cb_data = cb_data;
169 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
170
171 /* FIXME: Should start a timer for completion ... */
172
173 /* Then do whatever is needed ... */
174 if (rc == 1) {
175 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800176 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 } else if (rc == 2) {
178 /* Start ciphering directly */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800179 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
180 conn->lchan->encr.key_len = 8;
181 memcpy(conn->lchan->encr.key, op->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100182
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800183 return gsm48_send_rr_ciph_mode(conn->lchan, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100184 }
185
186 return -EINVAL; /* not reached */
187}
188
Holger Freyther73487a22008-12-31 18:53:57 +0000189static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
190 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000191{
192 if (!subscriber)
193 return 0;
194
Holger Freyther73487a22008-12-31 18:53:57 +0000195 /*
196 * Do not send accept yet as more information should arrive. Some
197 * phones will not send us the information and we will have to check
198 * what we want to do with that.
199 */
200 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
201 return 0;
202
Jan Luebbe06513f22009-08-12 12:48:00 +0200203 switch (subscriber->net->auth_policy) {
204 case GSM_AUTH_POLICY_CLOSED:
205 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200206 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200207 if (subscriber->authorized)
208 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200209 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200210 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000211 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200212 default:
213 return 0;
214 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000215}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000216
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100217static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000218{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100219 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000220 return;
221
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100222 bsc_del_timer(&conn->loc_operation->updating_timer);
223 talloc_free(conn->loc_operation);
224 conn->loc_operation = 0;
225 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000226}
227
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100228static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000229{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100230 use_subscr_con(conn)
231 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000232
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100233 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200234 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000235}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000236
Sylvain Munaut267fba02009-12-24 00:28:01 +0100237static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
238 struct msgb *msg, void *data, void *param)
239{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800240 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100241 int rc = 0;
242
243 switch (event) {
244 case GSM_SECURITY_AUTH_FAILED:
245 release_loc_updating_req(conn);
246 break;
247
248 case GSM_SECURITY_NOAVAIL:
249 case GSM_SECURITY_SUCCEEDED:
250 /* We're all good */
251 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800252 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800253 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100254 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800255 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100256 }
257
258 /* call subscr_update after putting the loc_upd_acc
259 * in the transmit queue, since S_SUBSCR_ATTACHED might
260 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800261 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100262 GSM_SUBSCRIBER_UPDATE_ATTACHED);
263
264 /* try to close channel ASAP */
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800265 release_loc_updating_req(conn);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800266 lchan_auto_release(conn->lchan);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100267 break;
268
269 default:
270 rc = -EINVAL;
271 };
272
273 return rc;
274}
275
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100276static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000277{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100278 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800279 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100280 conn->loc_operation->key_seq,
281 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000282 return 0;
283}
284
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800285void gsm0408_clear_request(struct gsm_subscriber_connection* conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000286{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800287 struct gsm_trans *trans, *temp;
Holger Freyther7c19f742009-06-06 13:54:35 +0000288 /*
289 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800290 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000291 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800292 release_loc_updating_req(conn);
293 release_security_operation(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000294
Harald Welte4bfdfe72009-06-10 23:11:52 +0800295 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200296 /* FIXME: this is not neccessarily the right thing to do, we should
297 * only set trans->lchan to NULL and wait for another lchan to be
298 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800299 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
300 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200301 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800302 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000303}
304
Holger Freyther429e7762008-12-30 13:28:30 +0000305/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800306int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000307{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800308 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800309 struct msgb *msg;
310
311 counter_inc(bts->network->stats.loc_upd_resp.reject);
312
313 msg = gsm48_create_loc_upd_rej(cause);
314 if (!msg) {
315 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
316 return -1;
317 }
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800319 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000320
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100321 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100322 "LAC=%u BTS=%u\n", conn->subscr ?
323 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800324 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100325
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800326 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000327}
328
329/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800330int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000331{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800332 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000333 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000334 struct gsm48_hdr *gh;
335 struct gsm48_loc_area_id *lai;
336 u_int8_t *mid;
337
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800338 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000339
340 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
341 gh->proto_discr = GSM48_PDISC_MM;
342 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
343
344 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100345 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000346 bts->network->network_code, bts->location_area_code);
347
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200348 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200349 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000350
351 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
352
Harald Welteffa55a42009-12-22 19:07:32 +0100353 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100354
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800355 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000356}
357
Harald Weltebf5e8df2009-02-03 12:59:45 +0000358/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800359static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000360{
361 struct msgb *msg = gsm48_msgb_alloc();
362 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000363
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800364 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000365
366 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
367 gh->proto_discr = GSM48_PDISC_MM;
368 gh->msg_type = GSM48_MT_MM_ID_REQ;
369 gh->data[0] = id_type;
370
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800371 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000372}
373
Harald Welte231ad4f2008-12-27 11:15:38 +0000374
Harald Weltebf5e8df2009-02-03 12:59:45 +0000375/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800376static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000377{
378 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000379 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200380 struct gsm_bts *bts = lchan->ts->trx->bts;
381 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000382 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200383 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000384
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200385 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000386 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000387 mi_type, mi_string);
388
Harald Welte7659de12009-12-13 12:39:18 +0100389 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
390
Harald Welte75a983f2008-12-27 21:34:06 +0000391 switch (mi_type) {
392 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200393 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100394 if (!conn->subscr) {
395 conn->subscr = subscr_get_by_imsi(net, mi_string);
396 if (!conn->subscr)
397 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200398 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100399 if (conn->loc_operation)
400 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000401 break;
402 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000403 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000404 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100405 if (conn->subscr) {
406 db_subscriber_assoc_imei(conn->subscr, mi_string);
407 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200408 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100409 if (conn->loc_operation)
410 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000411 break;
412 }
Holger Freyther73487a22008-12-31 18:53:57 +0000413
414 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100415 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000416}
417
Harald Welte255539c2008-12-28 02:26:27 +0000418
419static void loc_upd_rej_cb(void *data)
420{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100421 struct gsm_subscriber_connection *conn = data;
422 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100423 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000424
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800425 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800426 release_loc_updating_req(conn);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000427 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000428}
429
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100430static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000431{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100432 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
433 conn->loc_operation->updating_timer.data = conn;
434 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000435}
436
Harald Welte2a139372009-02-22 21:14:55 +0000437static const char *lupd_name(u_int8_t type)
438{
439 switch (type) {
440 case GSM48_LUPD_NORMAL:
441 return "NORMAL";
442 case GSM48_LUPD_PERIODIC:
443 return "PEROIDOC";
444 case GSM48_LUPD_IMSI_ATT:
445 return "IMSI ATTACH";
446 default:
447 return "UNKNOWN";
448 }
449}
450
Harald Weltebf5e8df2009-02-03 12:59:45 +0000451/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800452static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000453{
Harald Welte8470bf22008-12-25 23:28:35 +0000454 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000455 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800456 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800457 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000458 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200459 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000460 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000461
Harald Welte8470bf22008-12-25 23:28:35 +0000462 lu = (struct gsm48_loc_upd_req *) gh->data;
463
464 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000465
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200466 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000467
Harald Weltea0368542009-06-27 02:58:43 +0200468 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000469 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000470
Harald Welte7659de12009-12-13 12:39:18 +0100471 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
472
Harald Welte24ff6ee2009-12-22 00:41:05 +0100473 switch (lu->type) {
474 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100475 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100476 break;
477 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100478 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100479 break;
480 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100481 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100482 break;
483 }
484
Holger Freythereaf04692009-06-06 13:54:44 +0000485 /*
486 * Pseudo Spoof detection: Just drop a second/concurrent
487 * location updating request.
488 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100489 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200490 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100491 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800492 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000493 return 0;
494 }
495
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800496 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000497
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200498 conn->loc_operation->key_seq = lu->key_seq;
499
Harald Welte52b1f982008-12-23 20:25:15 +0000500 switch (mi_type) {
501 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200502 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000503 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800504 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000506
Jan Luebbe370b41d2009-08-12 10:19:34 +0200507 /* look up subscriber based on IMSI, create if not found */
508 subscr = subscr_get_by_imsi(bts->network, mi_string);
509 if (!subscr) {
510 subscr = db_create_subscriber(bts->network, mi_string);
511 }
Harald Welte4b634542008-12-27 01:55:51 +0000512 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000513 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200514 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000515 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200516 subscr = subscr_get_by_tmsi(bts->network,
517 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000518 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000519 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800520 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100521 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000522 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200523 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800524 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200525 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000526 break;
527 case GSM_MI_TYPE_IMEI:
528 case GSM_MI_TYPE_IMEISV:
529 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200530 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000531 break;
532 default:
Harald Weltea0368542009-06-27 02:58:43 +0200533 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000534 break;
535 }
536
Harald Welte24516ea2009-07-04 10:18:00 +0200537 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100538 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200539
Harald Welte4bfdfe72009-06-10 23:11:52 +0800540 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200541 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800542 /* FIXME: request id? close channel? */
543 return -EINVAL;
544 }
545
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100546 conn->subscr = subscr;
547 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000548
Harald Welte24516ea2009-07-04 10:18:00 +0200549 /* check if we can let the subscriber into our network immediately
550 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100551 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000552}
553
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554#if 0
555static u_int8_t to_bcd8(u_int8_t val)
556{
557 return ((val / 10) << 4) | (val % 10);
558}
559#endif
560
Harald Weltedb253af2008-12-30 17:56:55 +0000561/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800562int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000563{
564 struct msgb *msg = gsm48_msgb_alloc();
565 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800566 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000567 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200568 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800569#if 0
570 time_t cur_t;
571 struct tm* cur_time;
572 int tz15min;
573#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000574
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800575 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000576
577 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
578 gh->proto_discr = GSM48_PDISC_MM;
579 gh->msg_type = GSM48_MT_MM_INFO;
580
581 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200582#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000583 name_len = strlen(net->name_long);
584 /* 10.5.3.5a */
585 ptr8 = msgb_put(msg, 3);
586 ptr8[0] = GSM48_IE_NAME_LONG;
587 ptr8[1] = name_len*2 +1;
588 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
589
590 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
591 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000592 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000593
594 /* FIXME: Use Cell Broadcast, not UCS-2, since
595 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200596#endif
597 name_len = (strlen(net->name_long)*7)/8;
598 name_pad = (8 - strlen(net->name_long)*7)%8;
599 if (name_pad > 0)
600 name_len++;
601 /* 10.5.3.5a */
602 ptr8 = msgb_put(msg, 3);
603 ptr8[0] = GSM48_IE_NAME_LONG;
604 ptr8[1] = name_len +1;
605 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
606
607 ptr8 = msgb_put(msg, name_len);
608 gsm_7bit_encode(ptr8, net->name_long);
609
Harald Weltedb253af2008-12-30 17:56:55 +0000610 }
611
612 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200613#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000614 name_len = strlen(net->name_short);
615 /* 10.5.3.5a */
616 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200617 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000618 ptr8[1] = name_len*2 + 1;
619 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
620
Harald Weltee872cb12009-01-01 00:33:37 +0000621 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000622 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000623 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200624#endif
625 name_len = (strlen(net->name_short)*7)/8;
626 name_pad = (8 - strlen(net->name_short)*7)%8;
627 if (name_pad > 0)
628 name_len++;
629 /* 10.5.3.5a */
630 ptr8 = (u_int8_t *) msgb_put(msg, 3);
631 ptr8[0] = GSM48_IE_NAME_SHORT;
632 ptr8[1] = name_len +1;
633 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
634
635 ptr8 = msgb_put(msg, name_len);
636 gsm_7bit_encode(ptr8, net->name_short);
637
Harald Weltedb253af2008-12-30 17:56:55 +0000638 }
639
640#if 0
641 /* Section 10.5.3.9 */
642 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800643 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000644 ptr8 = msgb_put(msg, 8);
645 ptr8[0] = GSM48_IE_NET_TIME_TZ;
646 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
647 ptr8[2] = to_bcd8(cur_time->tm_mon);
648 ptr8[3] = to_bcd8(cur_time->tm_mday);
649 ptr8[4] = to_bcd8(cur_time->tm_hour);
650 ptr8[5] = to_bcd8(cur_time->tm_min);
651 ptr8[6] = to_bcd8(cur_time->tm_sec);
652 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
653 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800654 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000655 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000657#endif
658
Daniel Willmanneea93372009-08-13 03:42:07 +0200659 DEBUGP(DMM, "-> MM INFO\n");
660
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800661 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000662}
663
Harald Welte7984d5c2009-08-12 22:56:50 +0200664/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800665int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200666{
667 struct msgb *msg = gsm48_msgb_alloc();
668 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200669 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200670
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100671 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200672
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800673 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200674 gh->proto_discr = GSM48_PDISC_MM;
675 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
676
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100677 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200678
Harald Welte7984d5c2009-08-12 22:56:50 +0200679 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200680 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200681 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200682
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800683 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200684}
685
686/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800687int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200688{
689 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800690 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200691}
692
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800693static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000694{
Harald Welte4b634542008-12-27 01:55:51 +0000695 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800696 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000697}
Harald Welteba4cf162009-01-10 01:49:35 +0000698
699/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100700static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000701 enum gsm48_reject_value value)
702{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800703 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000704
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800705 msg = gsm48_create_mm_serv_rej(value);
706 if (!msg) {
707 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
708 return -1;
709 }
Harald Welteba4cf162009-01-10 01:49:35 +0000710
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800711 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100712 msg->lchan = conn->lchan;
713 use_subscr_con(conn);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800714 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000715}
716
Sylvain Munautba87f452009-12-24 00:28:46 +0100717static int _gsm48_rx_mm_serv_req_sec_cb(
718 unsigned int hooknum, unsigned int event,
719 struct msgb *msg, void *data, void *param)
720{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800721 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100722 int rc = 0;
723
724 switch (event) {
725 case GSM_SECURITY_AUTH_FAILED:
726 /* Nothing to do */
727 break;
728
729 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800730 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100731 break;
732
733 case GSM_SECURITY_SUCCEEDED:
734 /* nothing to do. CIPHER MODE COMMAND is
735 * implicit CM SERV ACK */
736 break;
737
738 default:
739 rc = -EINVAL;
740 };
741
742 return rc;
743}
744
Harald Welte4ed0e922009-01-10 03:17:30 +0000745/*
746 * Handle CM Service Requests
747 * a) Verify that the packet is long enough to contain the information
748 * we require otherwsie reject with INCORRECT_MESSAGE
749 * b) Try to parse the TMSI. If we do not have one reject
750 * c) Check that we know the subscriber with the TMSI otherwise reject
751 * with a HLR cause
752 * d) Set the subscriber on the gsm_lchan and accept
753 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800754static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000755{
Harald Welteba4cf162009-01-10 01:49:35 +0000756 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200757 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000758
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800759 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000760 struct gsm_subscriber *subscr;
761 struct gsm48_hdr *gh = msgb_l3(msg);
762 struct gsm48_service_request *req =
763 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800764 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000765 u_int8_t classmark2_len = gh->data[1];
766 u_int8_t *classmark2 = gh->data+2;
767 u_int8_t mi_len = *(classmark2 + classmark2_len);
768 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000769
Harald Weltec9e02182009-05-01 19:07:53 +0000770 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000771 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000772 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800773 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000774 GSM48_REJECT_INCORRECT_MESSAGE);
775 }
776
777 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000778 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800779 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000780 GSM48_REJECT_INCORRECT_MESSAGE);
781 }
782
Harald Weltec9e02182009-05-01 19:07:53 +0000783 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000784 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000785 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800786 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000787 GSM48_REJECT_INCORRECT_MESSAGE);
788 }
789
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200790 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000791 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000792 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000793
Harald Welte7659de12009-12-13 12:39:18 +0100794 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
795
Harald Welte3ac7f102009-08-10 10:12:45 +0200796 if (is_siemens_bts(bts))
797 send_siemens_mrpci(msg->lchan, classmark2-1);
798
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200799 subscr = subscr_get_by_tmsi(bts->network,
800 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000801
Harald Welte2a139372009-02-22 21:14:55 +0000802 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000803 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800804 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000805 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
806
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800807 if (!conn->subscr)
808 conn->subscr = subscr;
809 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100810 subscr_put(subscr); /* lchan already has a ref, don't need another one */
811 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000812 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
813 subscr_put(subscr);
814 }
815
Harald Weltec2e302d2009-07-05 14:08:13 +0200816 subscr->equipment.classmark2_len = classmark2_len;
817 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
818 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000819
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800820 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100821 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000822}
823
Harald Welte2a139372009-02-22 21:14:55 +0000824static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
825{
Harald Welte9176bd42009-07-23 18:46:00 +0200826 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000827 struct gsm48_hdr *gh = msgb_l3(msg);
828 struct gsm48_imsi_detach_ind *idi =
829 (struct gsm48_imsi_detach_ind *) gh->data;
830 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200831 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800832 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000833
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200834 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000835 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
836 mi_type, mi_string);
837
Harald Welteffa55a42009-12-22 19:07:32 +0100838 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100839
Harald Welte2a139372009-02-22 21:14:55 +0000840 switch (mi_type) {
841 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200842 subscr = subscr_get_by_tmsi(bts->network,
843 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000844 break;
845 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200846 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000847 break;
848 case GSM_MI_TYPE_IMEI:
849 case GSM_MI_TYPE_IMEISV:
850 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000851 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000852 break;
853 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000854 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000855 break;
856 }
857
Holger Freyther4a49e772009-04-12 05:37:29 +0000858 if (subscr) {
859 subscr_update(subscr, msg->trx->bts,
860 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100861 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200862
863 subscr->equipment.classmark1 = idi->classmark1;
864 db_sync_equipment(&subscr->equipment);
865
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000866 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000867 } else
Harald Welte2a139372009-02-22 21:14:55 +0000868 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
869
Harald Welte31981a02009-12-20 09:58:40 +0100870 /* FIXME: iterate over all transactions and release them,
871 * imagine an IMSI DETACH happening during an active call! */
872
Harald Welteb83d9382009-12-12 21:00:48 +0100873 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100874 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100875
Harald Welte2a139372009-02-22 21:14:55 +0000876 return 0;
877}
878
Harald Welted2a7f5a2009-06-05 20:08:20 +0000879static int gsm48_rx_mm_status(struct msgb *msg)
880{
881 struct gsm48_hdr *gh = msgb_l3(msg);
882
883 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
884
885 return 0;
886}
887
Sylvain Munaut30a15382009-12-24 00:27:26 +0100888/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800889static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100890{
891 struct gsm48_hdr *gh = msgb_l3(msg);
892 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800893 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100894
Sylvain Munautc593cf12010-06-10 23:51:41 +0200895 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100896 hexdump(ar->sres, 4));
897
898 /* Safety check */
899 if (!conn->sec_operation) {
900 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
901 return -EIO;
902 }
903
904 /* Validate SRES */
905 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
906 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200907
908 DEBUGPC(DMM, "Invalid (expected %s)\n",
909 hexdump(conn->sec_operation->atuple.sres, 4));
910
Sylvain Munaut30a15382009-12-24 00:27:26 +0100911 if (cb)
912 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800913 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100914
915 release_security_operation(conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800916 return gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100917 }
918
Sylvain Munautc593cf12010-06-10 23:51:41 +0200919 DEBUGPC(DMM, "OK\n");
920
Sylvain Munaut30a15382009-12-24 00:27:26 +0100921 /* Start ciphering */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800922 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
923 conn->lchan->encr.key_len = 8;
924 memcpy(conn->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100925
926 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
927}
928
Harald Weltebf5e8df2009-02-03 12:59:45 +0000929/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800930static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000931{
932 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800933 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000934
935 switch (gh->msg_type & 0xbf) {
936 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200937 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800938 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000939 break;
940 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800941 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000942 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000943 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800944 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000945 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000946 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000947 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000948 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000949 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000950 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800951 conn->subscr ?
952 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000953 "unknown subscriber");
954 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000955 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000956 rc = gsm48_rx_mm_imsi_detach_ind(msg);
957 break;
958 case GSM48_MT_MM_CM_REEST_REQ:
959 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
960 break;
961 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800962 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000963 break;
964 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100965 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000966 gh->msg_type);
967 break;
968 }
969
970 return rc;
971}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000972
Harald Welte2d35ae62009-02-06 12:02:13 +0000973/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800974static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000975{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800976 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000977 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800978 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000979 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200980 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200981 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800982 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000983 int rc = 0;
984
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800985 resp = (struct gsm48_pag_resp *) &gh->data[0];
986 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
987 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000988 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
989 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200990
Harald Weltefe18d8f2009-02-22 21:14:24 +0000991 switch (mi_type) {
992 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200993 subscr = subscr_get_by_tmsi(bts->network,
994 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000995 break;
996 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200997 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000998 break;
999 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001000
1001 if (!subscr) {
1002 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001003 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001004 return -EINVAL;
1005 }
1006 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001007 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001008
Harald Weltec2e302d2009-07-05 14:08:13 +02001009 subscr->equipment.classmark2_len = *classmark2_lv;
1010 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1011 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001012
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001013 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001014 return rc;
1015}
1016
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001017static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001018{
1019 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001020 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001021 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1022 u_int8_t cm2_len, cm3_len = 0;
1023 u_int8_t *cm2, *cm3 = NULL;
1024
1025 DEBUGP(DRR, "CLASSMARK CHANGE ");
1026
1027 /* classmark 2 */
1028 cm2_len = gh->data[0];
1029 cm2 = &gh->data[1];
1030 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1031
1032 if (payload_len > cm2_len + 1) {
1033 /* we must have a classmark3 */
1034 if (gh->data[cm2_len+1] != 0x20) {
1035 DEBUGPC(DRR, "ERR CM3 TAG\n");
1036 return -EINVAL;
1037 }
1038 if (cm2_len > 3) {
1039 DEBUGPC(DRR, "CM2 too long!\n");
1040 return -EINVAL;
1041 }
1042
1043 cm3_len = gh->data[cm2_len+2];
1044 cm3 = &gh->data[cm2_len+3];
1045 if (cm3_len > 14) {
1046 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1047 return -EINVAL;
1048 }
1049 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1050 }
1051 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001052 subscr->equipment.classmark2_len = cm2_len;
1053 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001054 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001055 subscr->equipment.classmark3_len = cm3_len;
1056 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001057 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001058 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001059 }
1060
Harald Weltef7c43522009-06-09 20:24:21 +00001061 return 0;
1062}
1063
Harald Weltecf5b3592009-05-01 18:28:42 +00001064static int gsm48_rx_rr_status(struct msgb *msg)
1065{
1066 struct gsm48_hdr *gh = msgb_l3(msg);
1067
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001068 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001069 rr_cause_name(gh->data[0]));
1070
1071 return 0;
1072}
1073
Harald Weltef7c43522009-06-09 20:24:21 +00001074static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1075{
Harald Welted12b0fd2009-12-15 21:36:05 +01001076 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001077
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001078 /* This shouldn't actually end up here, as RSL treats
1079 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1080 * directly into gsm48_parse_meas_rep */
1081 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001082 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001083
1084 return 0;
1085}
1086
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001087static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001088{
1089 struct gsm48_hdr *gh = msgb_l3(msg);
1090 u_int8_t apdu_id_flags;
1091 u_int8_t apdu_len;
1092 u_int8_t *apdu_data;
1093
1094 apdu_id_flags = gh->data[0];
1095 apdu_len = gh->data[1];
1096 apdu_data = gh->data+2;
1097
1098 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1099 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1100
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001101 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001102}
1103
Sylvain Munaut30a15382009-12-24 00:27:26 +01001104/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001105static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001106{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001107 gsm_cbfn *cb;
1108 int rc = 0;
1109
1110 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1111
1112 /* Safety check */
1113 if (!conn->sec_operation) {
1114 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1115 return -EIO;
1116 }
1117
1118 /* FIXME: check for MI (if any) */
1119
1120 /* Call back whatever was in progress (if anything) ... */
1121 cb = conn->sec_operation->cb;
1122 if (cb) {
1123 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001124 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001125 }
1126
1127 /* Complete the operation */
1128 release_security_operation(conn);
1129
1130 return rc;
1131}
1132
Harald Welted011e8b2009-11-29 22:45:52 +01001133/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001134static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001135{
1136 struct gsm48_hdr *gh = msgb_l3(msg);
1137
1138 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1139 rr_cause_name(gh->data[0]));
1140
1141 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1142 /* FIXME: release old channel */
1143
1144 return 0;
1145}
1146
1147/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001148static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001149{
1150 struct gsm48_hdr *gh = msgb_l3(msg);
1151
1152 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1153 rr_cause_name(gh->data[0]));
1154
1155 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1156 /* FIXME: release allocated new channel */
1157
1158 return 0;
1159}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001160
Harald Weltebf5e8df2009-02-03 12:59:45 +00001161/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001163{
1164 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001165 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001166
1167 switch (gh->msg_type) {
1168 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001169 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001170 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001171 case GSM48_MT_RR_GPRS_SUSP_REQ:
1172 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1173 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001174 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001175 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001176 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001177 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001178 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001179 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001180 case GSM48_MT_RR_STATUS:
1181 rc = gsm48_rx_rr_status(msg);
1182 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001183 case GSM48_MT_RR_MEAS_REP:
1184 rc = gsm48_rx_rr_meas_rep(msg);
1185 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001186 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001187 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001188 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001189 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001190 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001191 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001192 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001193 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001194 break;
1195 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001196 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001197 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001198 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001199 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1200 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001201 break;
1202 }
1203
Harald Welte2d35ae62009-02-06 12:02:13 +00001204 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001205}
1206
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001207int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001208 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001209{
1210 struct msgb *msg = gsm48_msgb_alloc();
1211 struct gsm48_hdr *gh;
1212
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001213 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001214
1215 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1216 apdu_id, apdu_len);
1217
1218 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1219 gh->proto_discr = GSM48_PDISC_RR;
1220 gh->msg_type = GSM48_MT_RR_APP_INFO;
1221 gh->data[0] = apdu_id;
1222 gh->data[1] = apdu_len;
1223 memcpy(gh->data+2, apdu, apdu_len);
1224
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001225 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226}
1227
Harald Welte4bc90a12008-12-27 16:32:52 +00001228/* Call Control */
1229
Harald Welte7584aea2009-02-11 11:44:12 +00001230/* The entire call control code is written in accordance with Figure 7.10c
1231 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1232 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1233 * it for voice */
1234
Harald Welte4bfdfe72009-06-10 23:11:52 +08001235static void new_cc_state(struct gsm_trans *trans, int state)
1236{
1237 if (state > 31 || state < 0)
1238 return;
1239
1240 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001241 gsm48_cc_state_name(trans->cc.state),
1242 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001243
Harald Weltedcaf5652009-07-23 18:56:43 +02001244 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001245}
1246
1247static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001248{
1249 struct msgb *msg = gsm48_msgb_alloc();
1250 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1251 u_int8_t *cause, *call_state;
1252
Harald Welte4bc90a12008-12-27 16:32:52 +00001253 gh->msg_type = GSM48_MT_CC_STATUS;
1254
1255 cause = msgb_put(msg, 3);
1256 cause[0] = 2;
1257 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1258 cause[2] = 0x80 | 30; /* response to status inquiry */
1259
1260 call_state = msgb_put(msg, 1);
1261 call_state[0] = 0xc0 | 0x00;
1262
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001263 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001264}
1265
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001266static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001267 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001268{
1269 struct msgb *msg = gsm48_msgb_alloc();
1270 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1271
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001272 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001273
Harald Welte6f4b7532008-12-29 00:39:37 +00001274 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001275 gh->msg_type = msg_type;
1276
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001277 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001278}
1279
Harald Welte4bfdfe72009-06-10 23:11:52 +08001280static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1281{
Harald Weltedcaf5652009-07-23 18:56:43 +02001282 if (bsc_timer_pending(&trans->cc.timer)) {
1283 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1284 bsc_del_timer(&trans->cc.timer);
1285 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 }
1287}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001288
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1290 int msg_type, struct gsm_mncc *mncc)
1291{
1292 struct msgb *msg;
1293
1294 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001295 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001296 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001298 trans->conn->lchan->ts->trx->bts->nr,
1299 trans->conn->lchan->ts->trx->nr,
1300 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301 (trans->subscr)?(trans->subscr->extension):"-",
1302 get_mncc_name(msg_type));
1303 else
1304 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1305 "Sending '%s' to MNCC.\n",
1306 (trans->subscr)?(trans->subscr->extension):"-",
1307 get_mncc_name(msg_type));
1308 else
1309 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1310 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1311
1312 mncc->msg_type = msg_type;
1313
Harald Welte966636f2009-06-26 19:39:35 +02001314 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001315 if (!msg)
1316 return -ENOMEM;
1317 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1318 msgb_enqueue(&net->upqueue, msg);
1319
1320 return 0;
1321}
1322
1323int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1324 u_int32_t callref, int location, int value)
1325{
1326 struct gsm_mncc rel;
1327
Harald Welte92f70c52009-06-12 01:54:08 +08001328 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001330 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001331 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1332}
1333
Harald Weltedcaf5652009-07-23 18:56:43 +02001334/* Call Control Specific transaction release.
1335 * gets called by trans_free, DO NOT CALL YOURSELF! */
1336void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 gsm48_stop_cc_timer(trans);
1339
1340 /* send release to L4, if callref still exists */
1341 if (trans->callref) {
1342 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001343 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001344 GSM48_CAUSE_LOC_PRN_S_LU,
1345 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001347 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001349 if (trans->conn)
1350 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351}
1352
1353static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001354
Harald Welte09e38af2009-02-16 22:52:23 +00001355/* call-back from paging the B-end of the connection */
1356static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001357 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001358{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001359 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360 struct gsm_subscriber *subscr = param;
1361 struct gsm_trans *transt, *tmp;
1362 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001363
Harald Welte09e38af2009-02-16 22:52:23 +00001364 if (hooknum != GSM_HOOK_RR_PAGING)
1365 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001366
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 if (!subscr)
1368 return -EINVAL;
1369 net = subscr->net;
1370 if (!net) {
1371 DEBUGP(DCC, "Error Network not set!\n");
1372 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001373 }
Harald Welte7584aea2009-02-11 11:44:12 +00001374
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 /* check all tranactions (without lchan) for subscriber */
1376 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001377 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 continue;
1379 switch (event) {
1380 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001381 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 break;
1383 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1384 subscr->extension);
1385 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001386 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001387 transt->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001388 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 }
1390 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 break;
1393 case GSM_PAGING_EXPIRED:
1394 DEBUGP(DCC, "Paging subscr %s expired!\n",
1395 subscr->extension);
1396 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001397 mncc_release_ind(transt->subscr->net, transt,
1398 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001399 GSM48_CAUSE_LOC_PRN_S_LU,
1400 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001402 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 break;
1404 }
1405 }
Harald Welte09e38af2009-02-16 22:52:23 +00001406 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001407}
Harald Welte7584aea2009-02-11 11:44:12 +00001408
Harald Welteda7ab742009-12-19 22:23:05 +01001409static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1410
Harald Welte805f6442009-07-28 18:25:29 +02001411/* some other part of the code sends us a signal */
1412static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1413 void *handler_data, void *signal_data)
1414{
1415 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001416 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001417 struct gsm_network *net;
1418 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001419
1420 if (subsys != SS_ABISIP)
1421 return 0;
1422
1423 /* in case we use direct BTS-to-BTS RTP */
1424 if (ipacc_rtp_direct)
1425 return 0;
1426
Harald Welte805f6442009-07-28 18:25:29 +02001427 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001428 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001429 /* check if any transactions on this lchan still have
1430 * a tch_recv_mncc request pending */
1431 net = lchan->ts->trx->bts->network;
1432 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001433 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001434 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1435 tch_recv_mncc(net, trans->callref, 1);
1436 }
1437 }
Harald Welte805f6442009-07-28 18:25:29 +02001438 break;
Harald Welte805f6442009-07-28 18:25:29 +02001439 }
1440
1441 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001442}
1443
Harald Welte49f48b82009-02-17 15:29:33 +00001444/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001445static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001446{
Harald Welte11fa29c2009-02-19 17:24:39 +00001447 struct gsm_bts *bts = lchan->ts->trx->bts;
1448 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001449 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001450
Harald Welte11fa29c2009-02-19 17:24:39 +00001451 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1452 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1453 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1454
1455 if (bts->type != remote_bts->type) {
1456 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1457 return -EINVAL;
1458 }
Harald Welteda7ab742009-12-19 22:23:05 +01001459
1460 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001461 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001462 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001463 if (!ipacc_rtp_direct) {
1464 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001465 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001466 if (rc < 0)
1467 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001468 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001469#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001470
1471 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301472 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1473 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001474 } else {
1475 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301476 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1477 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301478 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001479 if (rc < 0)
1480 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301481 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1482 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301483 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001484 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001485 break;
1486 case GSM_BTS_TYPE_BS11:
1487 trau_mux_map_lchan(lchan, remote_lchan);
1488 break;
1489 default:
1490 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001491 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001492 }
Harald Welte49f48b82009-02-17 15:29:33 +00001493
1494 return 0;
1495}
1496
Harald Welte4bfdfe72009-06-10 23:11:52 +08001497/* bridge channels of two transactions */
1498static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001499{
Harald Weltedcaf5652009-07-23 18:56:43 +02001500 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1501 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001502
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001504 return -EIO;
1505
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001506 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001507 return -EIO;
1508
1509 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001510 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001511}
1512
Harald Welteda7ab742009-12-19 22:23:05 +01001513/* enable receive of channels to MNCC upqueue */
1514static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001515{
1516 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001517 struct gsm_lchan *lchan;
1518 struct gsm_bts *bts;
1519 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001520
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001522 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523 if (!trans)
1524 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001525 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001526 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001527 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001528 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529
Harald Welteda7ab742009-12-19 22:23:05 +01001530 switch (bts->type) {
1531 case GSM_BTS_TYPE_NANOBTS:
1532 if (ipacc_rtp_direct) {
1533 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1534 return -EINVAL;
1535 }
1536 /* in case, we don't have a RTP socket yet, we note this
1537 * in the transaction and try later */
1538 if (!lchan->abis_ip.rtp_socket) {
1539 trans->tch_recv = enable;
1540 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1541 return 0;
1542 }
1543 if (enable) {
1544 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001545 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001546 if (rc < 0)
1547 return rc;
1548 /* assign socket to application interface */
1549 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1550 net, callref);
1551 } else
1552 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1553 net, 0);
1554 break;
1555 case GSM_BTS_TYPE_BS11:
1556 if (enable)
1557 return trau_recv_lchan(lchan, callref);
1558 return trau_mux_unmap(NULL, callref);
1559 break;
1560 default:
1561 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1562 return -EINVAL;
1563 }
1564
1565 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001566}
1567
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1569{
1570 DEBUGP(DCC, "-> STATUS ENQ\n");
1571 return gsm48_cc_tx_status(trans, msg);
1572}
1573
1574static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1575static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1576
1577static void gsm48_cc_timeout(void *arg)
1578{
1579 struct gsm_trans *trans = arg;
1580 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001581 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1582 int mo_location = GSM48_CAUSE_LOC_USER;
1583 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1584 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 struct gsm_mncc mo_rel, l4_rel;
1586
1587 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1588 mo_rel.callref = trans->callref;
1589 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1590 l4_rel.callref = trans->callref;
1591
Harald Weltedcaf5652009-07-23 18:56:43 +02001592 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001593 case 0x303:
1594 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001595 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 break;
1597 case 0x310:
1598 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001599 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600 break;
1601 case 0x313:
1602 disconnect = 1;
1603 /* unknown, did not find it in the specs */
1604 break;
1605 case 0x301:
1606 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001607 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001608 break;
1609 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001610 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001612 gsm48_cc_tx_release(trans, &trans->cc.msg);
1613 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001614 break; /* stay in release state */
1615 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001616 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617 return;
1618// release = 1;
1619// l4_cause = 14;
1620// break;
1621 case 0x306:
1622 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001623 mo_cause = trans->cc.msg.cause.value;
1624 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001625 break;
1626 case 0x323:
1627 disconnect = 1;
1628 break;
1629 default:
1630 release = 1;
1631 }
1632
1633 if (release && trans->callref) {
1634 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001635 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636 l4_location, l4_cause);
1637 trans->callref = 0;
1638 }
1639
1640 if (disconnect && trans->callref) {
1641 /* process disconnect towards layer 4 */
1642 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001643 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644 }
1645
1646 /* process disconnect towards mobile station */
1647 if (disconnect || release) {
1648 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001649 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1650 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1651 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 mo_rel.cause.diag_len = 3;
1653
1654 if (disconnect)
1655 gsm48_cc_tx_disconnect(trans, &mo_rel);
1656 if (release)
1657 gsm48_cc_tx_release(trans, &mo_rel);
1658 }
1659
1660}
1661
1662static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1663 int sec, int micro)
1664{
1665 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001666 trans->cc.timer.cb = gsm48_cc_timeout;
1667 trans->cc.timer.data = trans;
1668 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1669 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670}
1671
1672static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1673{
1674 struct gsm48_hdr *gh = msgb_l3(msg);
1675 u_int8_t msg_type = gh->msg_type & 0xbf;
1676 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1677 struct tlv_parsed tp;
1678 struct gsm_mncc setup;
1679
1680 memset(&setup, 0, sizeof(struct gsm_mncc));
1681 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001682 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 /* emergency setup is identified by msg_type */
1684 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1685 setup.emergency = 1;
1686
1687 /* use subscriber as calling party number */
1688 if (trans->subscr) {
1689 setup.fields |= MNCC_F_CALLING;
1690 strncpy(setup.calling.number, trans->subscr->extension,
1691 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001692 strncpy(setup.imsi, trans->subscr->imsi,
1693 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 }
1695 /* bearer capability */
1696 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1697 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001698 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1700 }
1701 /* facility */
1702 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1703 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001704 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1706 }
1707 /* called party bcd number */
1708 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1709 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001710 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1712 }
1713 /* user-user */
1714 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1715 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001716 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1718 }
1719 /* ss-version */
1720 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1721 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001722 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1724 }
1725 /* CLIR suppression */
1726 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1727 setup.clir.sup = 1;
1728 /* CLIR invocation */
1729 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1730 setup.clir.inv = 1;
1731 /* cc cap */
1732 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1733 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001734 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1736 }
1737
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738 new_cc_state(trans, GSM_CSTATE_INITIATED);
1739
Harald Welte (local)47df3992009-12-26 19:45:03 +01001740 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1741 subscr_name(trans->subscr), trans->subscr->extension,
1742 setup.called.number);
1743
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001745 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746
Harald Welte13cac662009-07-29 12:10:35 +02001747 /* MNCC code will modify the channel asynchronously, we should
1748 * ipaccess-bind only after the modification has been made to the
1749 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750 return 0;
1751}
1752
1753static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001754{
1755 struct msgb *msg = gsm48_msgb_alloc();
1756 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001758 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001759
Harald Welte7ccf7782009-02-17 01:43:01 +00001760 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001761
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 /* transaction id must not be assigned */
1763 if (trans->transaction_id != 0xff) { /* unasssigned */
1764 DEBUGP(DCC, "TX Setup with assigned transaction. "
1765 "This is not allowed!\n");
1766 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001767 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001768 GSM48_CAUSE_LOC_PRN_S_LU,
1769 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001771 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 return rc;
1773 }
1774
1775 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001776 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1777 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001779 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001780 GSM48_CAUSE_LOC_PRN_S_LU,
1781 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001783 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 return rc;
1785 }
Harald Welte78283ef2009-07-23 21:36:44 +02001786 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001787
Harald Welte65e74cc2008-12-29 01:55:35 +00001788 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001789
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001791
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 /* bearer capability */
1793 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001794 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 /* facility */
1796 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 /* progress */
1799 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 /* calling party BCD number */
1802 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001803 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 /* called party BCD number */
1805 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 /* user-user */
1808 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 /* redirecting party BCD number */
1811 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 /* signal */
1814 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001815 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816
1817 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001818
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001819 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001820}
1821
Harald Welte4bfdfe72009-06-10 23:11:52 +08001822static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1823{
1824 struct gsm48_hdr *gh = msgb_l3(msg);
1825 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1826 struct tlv_parsed tp;
1827 struct gsm_mncc call_conf;
1828
1829 gsm48_stop_cc_timer(trans);
1830 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1831
1832 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1833 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001834 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835#if 0
1836 /* repeat */
1837 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1838 call_conf.repeat = 1;
1839 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1840 call_conf.repeat = 2;
1841#endif
1842 /* bearer capability */
1843 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1844 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001845 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1847 }
1848 /* cause */
1849 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1850 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001851 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1853 }
1854 /* cc cap */
1855 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1856 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001857 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1859 }
1860
1861 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1862
Harald Welte596fed42009-07-23 19:06:52 +02001863 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1864 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865}
1866
1867static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1868{
1869 struct gsm_mncc *proceeding = arg;
1870 struct msgb *msg = gsm48_msgb_alloc();
1871 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1872
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1874
1875 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1876
1877 /* bearer capability */
1878 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001879 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 /* facility */
1881 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001882 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 /* progress */
1884 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001887 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888}
1889
1890static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1891{
1892 struct gsm48_hdr *gh = msgb_l3(msg);
1893 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1894 struct tlv_parsed tp;
1895 struct gsm_mncc alerting;
1896
1897 gsm48_stop_cc_timer(trans);
1898 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1899
1900 memset(&alerting, 0, sizeof(struct gsm_mncc));
1901 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001902 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903 /* facility */
1904 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1905 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001906 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1908 }
1909
1910 /* progress */
1911 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1912 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001913 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1915 }
1916 /* ss-version */
1917 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1918 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001919 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1921 }
1922
1923 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1924
Harald Welte596fed42009-07-23 19:06:52 +02001925 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1926 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927}
1928
1929static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1930{
1931 struct gsm_mncc *alerting = arg;
1932 struct msgb *msg = gsm48_msgb_alloc();
1933 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1934
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 gh->msg_type = GSM48_MT_CC_ALERTING;
1936
1937 /* facility */
1938 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001939 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 /* progress */
1941 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* user-user */
1944 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001945 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946
1947 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1948
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001949 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950}
1951
1952static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1953{
1954 struct gsm_mncc *progress = arg;
1955 struct msgb *msg = gsm48_msgb_alloc();
1956 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1957
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 gh->msg_type = GSM48_MT_CC_PROGRESS;
1959
1960 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 /* user-user */
1963 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001966 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967}
1968
1969static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1970{
1971 struct gsm_mncc *connect = arg;
1972 struct msgb *msg = gsm48_msgb_alloc();
1973 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 gh->msg_type = GSM48_MT_CC_CONNECT;
1976
1977 gsm48_stop_cc_timer(trans);
1978 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1979
1980 /* facility */
1981 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 /* progress */
1984 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 /* connected number */
1987 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001988 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 /* user-user */
1990 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001991 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992
1993 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1994
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001995 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996}
1997
1998static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1999{
2000 struct gsm48_hdr *gh = msgb_l3(msg);
2001 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2002 struct tlv_parsed tp;
2003 struct gsm_mncc connect;
2004
2005 gsm48_stop_cc_timer(trans);
2006
2007 memset(&connect, 0, sizeof(struct gsm_mncc));
2008 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002009 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 /* use subscriber as connected party number */
2011 if (trans->subscr) {
2012 connect.fields |= MNCC_F_CONNECTED;
2013 strncpy(connect.connected.number, trans->subscr->extension,
2014 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002015 strncpy(connect.imsi, trans->subscr->imsi,
2016 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017 }
2018 /* facility */
2019 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2020 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002021 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2023 }
2024 /* user-user */
2025 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2026 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2029 }
2030 /* ss-version */
2031 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2032 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002033 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2035 }
2036
2037 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2038
Harald Welte596fed42009-07-23 19:06:52 +02002039 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040}
2041
2042
2043static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2044{
2045 struct gsm_mncc connect_ack;
2046
2047 gsm48_stop_cc_timer(trans);
2048
2049 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2050
2051 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2052 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002053 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054 &connect_ack);
2055}
2056
2057static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2058{
2059 struct msgb *msg = gsm48_msgb_alloc();
2060 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2061
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2063
2064 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2065
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002066 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067}
2068
2069static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2070{
2071 struct gsm48_hdr *gh = msgb_l3(msg);
2072 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2073 struct tlv_parsed tp;
2074 struct gsm_mncc disc;
2075
2076 gsm48_stop_cc_timer(trans);
2077
2078 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2079
2080 memset(&disc, 0, sizeof(struct gsm_mncc));
2081 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002082 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* cause */
2084 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2085 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002086 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2088 }
2089 /* facility */
2090 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2091 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002092 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2094 }
2095 /* user-user */
2096 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2097 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002098 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2100 }
2101 /* ss-version */
2102 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2103 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2106 }
2107
Harald Welte596fed42009-07-23 19:06:52 +02002108 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109
2110}
2111
Harald Weltec66b71c2009-06-11 14:23:20 +08002112static struct gsm_mncc_cause default_cause = {
2113 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2114 .coding = 0,
2115 .rec = 0,
2116 .rec_val = 0,
2117 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2118 .diag_len = 0,
2119 .diag = { 0 },
2120};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121
2122static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2123{
2124 struct gsm_mncc *disc = arg;
2125 struct msgb *msg = gsm48_msgb_alloc();
2126 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2127
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2129
2130 gsm48_stop_cc_timer(trans);
2131 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2132
2133 /* cause */
2134 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002135 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 else
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138
2139 /* facility */
2140 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002141 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 /* progress */
2143 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002144 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 /* user-user */
2146 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002147 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148
2149 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002150 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151
2152 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2153
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002154 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155}
2156
2157static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2158{
2159 struct gsm48_hdr *gh = msgb_l3(msg);
2160 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2161 struct tlv_parsed tp;
2162 struct gsm_mncc rel;
2163 int rc;
2164
2165 gsm48_stop_cc_timer(trans);
2166
2167 memset(&rel, 0, sizeof(struct gsm_mncc));
2168 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002169 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 /* cause */
2171 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2172 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2175 }
2176 /* facility */
2177 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2178 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002179 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2181 }
2182 /* user-user */
2183 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2184 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2187 }
2188 /* ss-version */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2190 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002191 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2193 }
2194
Harald Weltedcaf5652009-07-23 18:56:43 +02002195 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002197 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002199 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002200 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002201 GSM48_MT_CC_RELEASE_COMPL);
2202 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 }
2204
2205 new_cc_state(trans, GSM_CSTATE_NULL);
2206
2207 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002208 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209
2210 return rc;
2211}
2212
2213static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2214{
2215 struct gsm_mncc *rel = arg;
2216 struct msgb *msg = gsm48_msgb_alloc();
2217 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2218
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 gh->msg_type = GSM48_MT_CC_RELEASE;
2220
2221 trans->callref = 0;
2222
2223 gsm48_stop_cc_timer(trans);
2224 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2225
2226 /* cause */
2227 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 /* facility */
2230 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002231 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 /* user-user */
2233 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002234 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235
Harald Weltedcaf5652009-07-23 18:56:43 +02002236 trans->cc.T308_second = 0;
2237 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238
Harald Weltedcaf5652009-07-23 18:56:43 +02002239 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2241
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002242 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243}
2244
2245static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2246{
2247 struct gsm48_hdr *gh = msgb_l3(msg);
2248 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2249 struct tlv_parsed tp;
2250 struct gsm_mncc rel;
2251 int rc = 0;
2252
2253 gsm48_stop_cc_timer(trans);
2254
2255 memset(&rel, 0, sizeof(struct gsm_mncc));
2256 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002257 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 /* cause */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2260 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002261 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2263 }
2264 /* facility */
2265 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2266 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002267 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2269 }
2270 /* user-user */
2271 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2272 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002273 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2275 }
2276 /* ss-version */
2277 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2278 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2281 }
2282
2283 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002284 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002286 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 MNCC_REJ_IND, &rel);
2288 break;
2289 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002290 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002292 /* FIXME: in case of multiple calls, we can't simply
2293 * hang up here ! */
2294 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 break;
2296 default:
Harald Welte596fed42009-07-23 19:06:52 +02002297 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 MNCC_REL_IND, &rel);
2299 }
2300 }
2301
2302 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002303 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304
2305 return rc;
2306}
2307
2308static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2309{
2310 struct gsm_mncc *rel = arg;
2311 struct msgb *msg = gsm48_msgb_alloc();
2312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2313
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2315
2316 trans->callref = 0;
2317
2318 gsm48_stop_cc_timer(trans);
2319
2320 /* cause */
2321 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002322 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323 /* facility */
2324 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002325 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 /* user-user */
2327 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002328 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329
Harald Weltedcaf5652009-07-23 18:56:43 +02002330 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002332 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333}
2334
2335static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2336{
2337 struct gsm48_hdr *gh = msgb_l3(msg);
2338 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2339 struct tlv_parsed tp;
2340 struct gsm_mncc fac;
2341
2342 memset(&fac, 0, sizeof(struct gsm_mncc));
2343 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002344 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 /* facility */
2346 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2347 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2350 }
2351 /* ss-version */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2353 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2356 }
2357
Harald Welte596fed42009-07-23 19:06:52 +02002358 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359}
2360
2361static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2362{
2363 struct gsm_mncc *fac = arg;
2364 struct msgb *msg = gsm48_msgb_alloc();
2365 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2366
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 gh->msg_type = GSM48_MT_CC_FACILITY;
2368
2369 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002370 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002372 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373}
2374
2375static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2376{
2377 struct gsm_mncc hold;
2378
2379 memset(&hold, 0, sizeof(struct gsm_mncc));
2380 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002381 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382}
2383
2384static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2385{
2386 struct msgb *msg = gsm48_msgb_alloc();
2387 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2388
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2390
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002391 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392}
2393
2394static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2395{
2396 struct gsm_mncc *hold_rej = arg;
2397 struct msgb *msg = gsm48_msgb_alloc();
2398 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2399
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2401
2402 /* cause */
2403 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002404 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 else
Harald Welte55c8f352010-03-07 23:40:35 +01002406 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002408 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2412{
2413 struct gsm_mncc retrieve;
2414
2415 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2416 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002417 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2418 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419}
2420
2421static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2422{
2423 struct msgb *msg = gsm48_msgb_alloc();
2424 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2425
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2427
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002428 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429}
2430
2431static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2432{
2433 struct gsm_mncc *retrieve_rej = arg;
2434 struct msgb *msg = gsm48_msgb_alloc();
2435 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2436
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2438
2439 /* cause */
2440 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002441 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 else
Harald Welte55c8f352010-03-07 23:40:35 +01002443 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002445 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446}
2447
2448static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2449{
2450 struct gsm48_hdr *gh = msgb_l3(msg);
2451 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2452 struct tlv_parsed tp;
2453 struct gsm_mncc dtmf;
2454
2455 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2456 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002457 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 /* keypad facility */
2459 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2460 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002461 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2463 }
2464
Harald Welte596fed42009-07-23 19:06:52 +02002465 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466}
2467
2468static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2469{
2470 struct gsm_mncc *dtmf = arg;
2471 struct msgb *msg = gsm48_msgb_alloc();
2472 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2473
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2475
2476 /* keypad */
2477 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002480 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2484{
2485 struct gsm_mncc *dtmf = arg;
2486 struct msgb *msg = gsm48_msgb_alloc();
2487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2488
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2490
2491 /* cause */
2492 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002493 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494 else
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002497 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
2500static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2501{
2502 struct msgb *msg = gsm48_msgb_alloc();
2503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2504
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2506
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002507 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508}
2509
2510static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2511{
2512 struct gsm_mncc dtmf;
2513
2514 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2515 dtmf.callref = trans->callref;
2516
Harald Welte596fed42009-07-23 19:06:52 +02002517 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518}
2519
2520static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2521{
2522 struct gsm48_hdr *gh = msgb_l3(msg);
2523 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2524 struct tlv_parsed tp;
2525 struct gsm_mncc modify;
2526
2527 memset(&modify, 0, sizeof(struct gsm_mncc));
2528 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002529 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530 /* bearer capability */
2531 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2532 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002533 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2535 }
2536
2537 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2538
Harald Welte596fed42009-07-23 19:06:52 +02002539 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540}
2541
2542static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2543{
2544 struct gsm_mncc *modify = arg;
2545 struct msgb *msg = gsm48_msgb_alloc();
2546 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2547
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 gh->msg_type = GSM48_MT_CC_MODIFY;
2549
2550 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2551
2552 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554
2555 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2556
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002557 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558}
2559
2560static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2561{
2562 struct gsm48_hdr *gh = msgb_l3(msg);
2563 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2564 struct tlv_parsed tp;
2565 struct gsm_mncc modify;
2566
2567 gsm48_stop_cc_timer(trans);
2568
2569 memset(&modify, 0, sizeof(struct gsm_mncc));
2570 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002571 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 /* bearer capability */
2573 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2574 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2577 }
2578
2579 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2580
Harald Welte596fed42009-07-23 19:06:52 +02002581 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582}
2583
2584static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2585{
2586 struct gsm_mncc *modify = arg;
2587 struct msgb *msg = gsm48_msgb_alloc();
2588 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2589
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2591
2592 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594
2595 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2596
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002597 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm48_hdr *gh = msgb_l3(msg);
2603 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2604 struct tlv_parsed tp;
2605 struct gsm_mncc modify;
2606
2607 gsm48_stop_cc_timer(trans);
2608
2609 memset(&modify, 0, sizeof(struct gsm_mncc));
2610 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002611 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612 /* bearer capability */
2613 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2614 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002615 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2617 }
2618 /* cause */
2619 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2620 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002621 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2623 }
2624
2625 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2626
Harald Welte596fed42009-07-23 19:06:52 +02002627 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628}
2629
2630static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2631{
2632 struct gsm_mncc *modify = arg;
2633 struct msgb *msg = gsm48_msgb_alloc();
2634 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2635
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2637
2638 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002639 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002641 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642
2643 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2644
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002645 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646}
2647
2648static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2649{
2650 struct gsm_mncc *notify = arg;
2651 struct msgb *msg = gsm48_msgb_alloc();
2652 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2653
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 gh->msg_type = GSM48_MT_CC_NOTIFY;
2655
2656 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002657 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002659 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660}
2661
2662static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2663{
2664 struct gsm48_hdr *gh = msgb_l3(msg);
2665 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2666// struct tlv_parsed tp;
2667 struct gsm_mncc notify;
2668
2669 memset(&notify, 0, sizeof(struct gsm_mncc));
2670 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002671// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674
Harald Welte596fed42009-07-23 19:06:52 +02002675 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676}
2677
2678static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2679{
2680 struct gsm_mncc *user = arg;
2681 struct msgb *msg = gsm48_msgb_alloc();
2682 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2683
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684 gh->msg_type = GSM48_MT_CC_USER_INFO;
2685
2686 /* user-user */
2687 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002688 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 /* more data */
2690 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002691 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002693 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694}
2695
2696static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2697{
2698 struct gsm48_hdr *gh = msgb_l3(msg);
2699 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2700 struct tlv_parsed tp;
2701 struct gsm_mncc user;
2702
2703 memset(&user, 0, sizeof(struct gsm_mncc));
2704 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002705 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 /* user-user */
2707 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2708 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002709 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2711 }
2712 /* more data */
2713 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2714 user.more = 1;
2715
Harald Welte596fed42009-07-23 19:06:52 +02002716 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717}
2718
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002719static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720{
2721 struct gsm_mncc *mode = arg;
2722
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002723 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724}
2725
2726static struct downstate {
2727 u_int32_t states;
2728 int type;
2729 int (*rout) (struct gsm_trans *trans, void *arg);
2730} downstatelist[] = {
2731 /* mobile originating call establishment */
2732 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2733 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2734 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2735 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2736 {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 */
2737 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2738 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2739 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2740 /* mobile terminating call establishment */
2741 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2742 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2743 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2744 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2745 /* signalling during call */
2746 {SBIT(GSM_CSTATE_ACTIVE),
2747 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2748 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2749 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2750 {ALL_STATES,
2751 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2752 {ALL_STATES,
2753 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2754 {ALL_STATES,
2755 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2756 {SBIT(GSM_CSTATE_ACTIVE),
2757 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2758 {SBIT(GSM_CSTATE_ACTIVE),
2759 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2760 {SBIT(GSM_CSTATE_ACTIVE),
2761 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2762 {SBIT(GSM_CSTATE_ACTIVE),
2763 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2764 {SBIT(GSM_CSTATE_ACTIVE),
2765 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2766 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2767 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2768 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2769 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2770 {SBIT(GSM_CSTATE_ACTIVE),
2771 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2772 /* clearing */
2773 {SBIT(GSM_CSTATE_INITIATED),
2774 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2775 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2776 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2777 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2778 MNCC_REL_REQ, gsm48_cc_tx_release},
2779 /* special */
2780 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002781 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782};
2783
2784#define DOWNSLLEN \
2785 (sizeof(downstatelist) / sizeof(struct downstate))
2786
2787
2788int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2789{
Harald Welte1a6f7982009-08-09 18:52:33 +02002790 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002792 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 struct gsm_mncc *data = arg, rel;
2795
2796 /* handle special messages */
2797 switch(msg_type) {
2798 case MNCC_BRIDGE:
2799 return tch_bridge(net, arg);
2800 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002801 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002803 return tch_recv_mncc(net, data->callref, 1);
2804 case GSM_TCHF_FRAME:
2805 /* Find callref */
2806 trans = trans_find_by_callref(net, data->callref);
2807 if (!trans)
2808 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002809 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002810 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002811 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002812 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002813 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002814 switch (bts->type) {
2815 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002816 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002817 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002818 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002819 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002820 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002821 default:
2822 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2823 }
2824 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 }
2826
2827 memset(&rel, 0, sizeof(struct gsm_mncc));
2828 rel.callref = data->callref;
2829
2830 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002831 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832
2833 /* Callref unknown */
2834 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002835 struct gsm_subscriber *subscr;
2836
Harald Welte4a3464c2009-07-04 10:11:24 +02002837 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2839 "Received '%s' from MNCC with "
2840 "unknown callref %d\n", data->called.number,
2841 get_mncc_name(msg_type), data->callref);
2842 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002843 return mncc_release_ind(net, NULL, data->callref,
2844 GSM48_CAUSE_LOC_PRN_S_LU,
2845 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002847 if (!data->called.number[0] && !data->imsi[0]) {
2848 DEBUGP(DCC, "(bts - trx - ts - ti) "
2849 "Received '%s' from MNCC with "
2850 "no number or IMSI\n", get_mncc_name(msg_type));
2851 /* Invalid number */
2852 return mncc_release_ind(net, NULL, data->callref,
2853 GSM48_CAUSE_LOC_PRN_S_LU,
2854 GSM48_CC_CAUSE_INV_NR_FORMAT);
2855 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002857 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002858 subscr = subscr_get_by_extension(net,
2859 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002860 else
Harald Welte9176bd42009-07-23 18:46:00 +02002861 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 /* If subscriber is not found */
2863 if (!subscr) {
2864 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2865 "Received '%s' from MNCC with "
2866 "unknown subscriber %s\n", data->called.number,
2867 get_mncc_name(msg_type), data->called.number);
2868 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002869 return mncc_release_ind(net, NULL, data->callref,
2870 GSM48_CAUSE_LOC_PRN_S_LU,
2871 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872 }
2873 /* If subscriber is not "attached" */
2874 if (!subscr->lac) {
2875 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2876 "Received '%s' from MNCC with "
2877 "detached subscriber %s\n", data->called.number,
2878 get_mncc_name(msg_type), data->called.number);
2879 subscr_put(subscr);
2880 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002881 return mncc_release_ind(net, NULL, data->callref,
2882 GSM48_CAUSE_LOC_PRN_S_LU,
2883 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 }
2885 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002886 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2887 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888 DEBUGP(DCC, "No memory for trans.\n");
2889 subscr_put(subscr);
2890 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002891 mncc_release_ind(net, NULL, data->callref,
2892 GSM48_CAUSE_LOC_PRN_S_LU,
2893 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 return -ENOMEM;
2895 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002897 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002898
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002900 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 /* find transaction with this subscriber already paging */
2902 llist_for_each_entry(transt, &net->trans_list, entry) {
2903 /* Transaction of our lchan? */
2904 if (transt == trans ||
2905 transt->subscr != subscr)
2906 continue;
2907 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2908 "Received '%s' from MNCC with "
2909 "unallocated channel, paging already "
2910 "started.\n", bts->nr,
2911 data->called.number,
2912 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002913 subscr_put(subscr);
2914 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915 return 0;
2916 }
2917 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002918 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002919 /* Trigger paging */
2920 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2921 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002922 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 return 0;
2924 }
2925 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002926 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002927 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002928 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002930
2931 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002932 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933
2934 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002935 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002937 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002938 (trans->subscr)?(trans->subscr->extension):"-",
2939 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002940 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2941 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 if (msg_type == MNCC_REL_REQ)
2943 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2944 else
2945 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2946 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002947 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 return rc;
2949 }
2950
2951 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2952 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002953 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002955 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002956 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08002957 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958
2959 /* Find function for current state and message */
2960 for (i = 0; i < DOWNSLLEN; i++)
2961 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002962 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 break;
2964 if (i == DOWNSLLEN) {
2965 DEBUGP(DCC, "Message unhandled at this state.\n");
2966 return 0;
2967 }
2968
2969 rc = downstatelist[i].rout(trans, arg);
2970
2971 return rc;
2972}
2973
2974
2975static struct datastate {
2976 u_int32_t states;
2977 int type;
2978 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2979} datastatelist[] = {
2980 /* mobile originating call establishment */
2981 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2982 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2983 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2984 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2985 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2986 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2987 /* mobile terminating call establishment */
2988 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2989 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2990 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2991 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002992 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2994 /* signalling during call */
2995 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2996 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2997 {SBIT(GSM_CSTATE_ACTIVE),
2998 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2999 {ALL_STATES,
3000 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3001 {ALL_STATES,
3002 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3003 {ALL_STATES,
3004 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3005 {SBIT(GSM_CSTATE_ACTIVE),
3006 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3007 {SBIT(GSM_CSTATE_ACTIVE),
3008 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3009 {SBIT(GSM_CSTATE_ACTIVE),
3010 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3011 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3012 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3013 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3014 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3015 {SBIT(GSM_CSTATE_ACTIVE),
3016 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3017 /* clearing */
3018 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3019 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3020 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3021 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3022 {ALL_STATES, /* 5.4.3.4 */
3023 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3024};
3025
3026#define DATASLLEN \
3027 (sizeof(datastatelist) / sizeof(struct datastate))
3028
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003029static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003030{
3031 struct gsm48_hdr *gh = msgb_l3(msg);
3032 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003033 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003034 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003036
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037 if (msg_type & 0x80) {
3038 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3039 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003040 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003041
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003043 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003044
Harald Welte6f5aee02009-07-23 21:21:14 +02003045 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003046 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003047 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003048 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003049 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3050 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051
3052 /* Create transaction */
3053 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003054 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055 "creating new trans.\n", transaction_id);
3056 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003057 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003058 transaction_id, new_callref++);
3059 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003061 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003062 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 GSM48_MT_CC_RELEASE_COMPL);
3064 return -ENOMEM;
3065 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003067 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003068 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 }
3070
3071 /* find function for current state and message */
3072 for (i = 0; i < DATASLLEN; i++)
3073 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003074 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 break;
3076 if (i == DATASLLEN) {
3077 DEBUGP(DCC, "Message unhandled at this state.\n");
3078 return 0;
3079 }
3080
3081 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003082
3083 return rc;
3084}
3085
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003086/* here we get data from the BSC level... */
3087int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003088{
3089 struct gsm48_hdr *gh = msgb_l3(msg);
3090 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003091 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003092
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003093 if (silent_call_reroute(conn, msg))
3094 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003095
3096 switch (pdisc) {
3097 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003098 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003099 break;
3100 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003101 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003102 break;
3103 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003104 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003105 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003106 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003107 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003108 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003109 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003110 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003111 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3112 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003113 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003114 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003115 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003116 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003117 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003118 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3119 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003120 break;
3121 }
3122
3123 return rc;
3124}
Harald Welte8470bf22008-12-25 23:28:35 +00003125
Harald Welte805f6442009-07-28 18:25:29 +02003126/*
3127 * This will be ran by the linker when loading the DSO. We use it to
3128 * do system initialization, e.g. registration of signal handlers.
3129 */
3130static __attribute__((constructor)) void on_dso_load_0408(void)
3131{
Harald Welte805f6442009-07-28 18:25:29 +02003132 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3133}