blob: 637f84f7cce99df364e063779a6af80287e25a0c [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +08005 * (C) 2008-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Sylvain Munaut30a15382009-12-24 00:27:26 +010032#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000037#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080039#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000041#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000042#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000043#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000044#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000045#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020046#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020047#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020048#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010049#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080050#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080051#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080052#include <osmocore/bitvec.h>
53
54#include <osmocore/gsm48.h>
Holger Hans Peter Freyther12eb9b72010-09-30 18:53:47 +080055#include <osmocore/gsm0480.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080056#include <osmocore/gsm_utils.h>
57#include <osmocore/msgb.h>
58#include <osmocore/talloc.h>
59#include <osmocore/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000060
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020061void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010062void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020063
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080064int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080065static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000066 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010067static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080068static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000069
Harald Welte52b1f982008-12-23 20:25:15 +000070struct gsm_lai {
71 u_int16_t mcc;
72 u_int16_t mnc;
73 u_int16_t lac;
74};
75
Harald Welte4bfdfe72009-06-10 23:11:52 +080076static u_int32_t new_callref = 0x80000001;
77
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080078static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
79 struct gsm_trans *trans)
80{
81 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
82
83 /* if we get passed a transaction reference, do some common
84 * work that the caller no longer has to do */
85 if (trans) {
86 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
87 msg->lchan = trans->conn->lchan;
88 }
89
90
91 if (msg->lchan) {
92 msg->trx = msg->lchan->ts->trx;
93 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
94 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
95 "Sending '%s' to MS.\n", msg->trx->bts->nr,
96 msg->trx->nr, msg->lchan->ts->nr,
97 gh->proto_discr & 0xf0,
98 gsm48_cc_msg_name(gh->msg_type));
99 else
100 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
101 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
102 msg->trx->nr, msg->lchan->ts->nr,
103 gh->proto_discr, gh->msg_type);
104 }
105
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100106 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800107}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100108
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800109int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
110{
111 struct gsm48_hdr *gh;
112 struct msgb *ss_notify;
113
114 ss_notify = gsm0480_create_notifySS(message);
115 if (!ss_notify)
116 return -1;
117
118 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
119 uint8_t *data = msgb_push(ss_notify, 1);
120 data[0] = ss_notify->len - 1;
121 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
122 gh->msg_type = GSM48_MT_CC_FACILITY;
123 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
124}
125
Sylvain Munaut30a15382009-12-24 00:27:26 +0100126static void release_security_operation(struct gsm_subscriber_connection *conn)
127{
128 if (!conn->sec_operation)
129 return;
130
131 talloc_free(conn->sec_operation);
132 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800133 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100134}
135
136static void allocate_security_operation(struct gsm_subscriber_connection *conn)
137{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100138 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
139 struct gsm_security_operation);
140}
141
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100142int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
143 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100144{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800145 struct gsm_network *net = conn->bts->network;
146 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147 struct gsm_security_operation *op;
148 struct gsm_auth_tuple atuple;
149 int status = -1, rc;
150
151 /* Check if we _can_ enable encryption. Cases where we can't:
152 * - Encryption disabled in config
153 * - Channel already secured (nothing to do)
154 * - Subscriber equipment doesn't support configured encryption
155 */
156 if (!net->a5_encryption) {
157 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800158 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100159 DEBUGP(DMM, "Requesting to secure an already secure channel");
160 status = GSM_SECURITY_SUCCEEDED;
161 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
162 net->a5_encryption)) {
163 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
164 status = GSM_SECURITY_NOAVAIL;
165 }
166
167 /* If not done yet, try to get info for this user */
168 if (status < 0) {
169 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
170 if (rc <= 0)
171 status = GSM_SECURITY_NOAVAIL;
172 }
173
174 /* Are we done yet ? */
175 if (status >= 0)
176 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800177 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100178 0;
179
180 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800181 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100182 return -EBUSY;
183
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 allocate_security_operation(conn);
185 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186 op->cb = cb;
187 op->cb_data = cb_data;
188 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
189
190 /* FIXME: Should start a timer for completion ... */
191
192 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100193 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100194 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800195 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100196 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100197 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100198 return gsm0808_cipher_mode(conn, net->a5_encryption,
199 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 }
201
202 return -EINVAL; /* not reached */
203}
204
Holger Freyther73487a22008-12-31 18:53:57 +0000205static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
206 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000207{
208 if (!subscriber)
209 return 0;
210
Holger Freyther73487a22008-12-31 18:53:57 +0000211 /*
212 * Do not send accept yet as more information should arrive. Some
213 * phones will not send us the information and we will have to check
214 * what we want to do with that.
215 */
216 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
217 return 0;
218
Jan Luebbe06513f22009-08-12 12:48:00 +0200219 switch (subscriber->net->auth_policy) {
220 case GSM_AUTH_POLICY_CLOSED:
221 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200222 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200223 if (subscriber->authorized)
224 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200225 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200226 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000227 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200228 default:
229 return 0;
230 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000231}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000232
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100233static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000234{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100235 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000236 return;
237
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800238 /* No need to keep the connection up */
239 release_anchor(conn);
240
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100241 bsc_del_timer(&conn->loc_operation->updating_timer);
242 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100243 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800244 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000245}
246
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100247static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000248{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800249 if (conn->loc_operation)
250 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100251 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000252
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100253 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200254 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000255}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000256
Sylvain Munaut267fba02009-12-24 00:28:01 +0100257static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
258 struct msgb *msg, void *data, void *param)
259{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800260 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100261 int rc = 0;
262
263 switch (event) {
264 case GSM_SECURITY_AUTH_FAILED:
265 release_loc_updating_req(conn);
266 break;
267
268 case GSM_SECURITY_NOAVAIL:
269 case GSM_SECURITY_SUCCEEDED:
270 /* We're all good */
271 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800272 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800273 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100274 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800275 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100276 }
277
278 /* call subscr_update after putting the loc_upd_acc
279 * in the transmit queue, since S_SUBSCR_ATTACHED might
280 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800281 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100282 GSM_SUBSCRIBER_UPDATE_ATTACHED);
283
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100284 /*
285 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
286 * MS needs to respond with a TMSI REALLOCATION COMPLETE
287 * (even if the TMSI is the same).
288 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100289 break;
290
291 default:
292 rc = -EINVAL;
293 };
294
295 return rc;
296}
297
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100298static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000299{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100300 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800301 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100302 conn->loc_operation->key_seq,
303 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000304 return 0;
305}
306
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800307void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000308{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800309 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800310
311 /* avoid someone issuing a clear */
312 conn->in_release = 1;
313
Holger Freyther7c19f742009-06-06 13:54:35 +0000314 /*
315 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800316 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000317 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800318 release_loc_updating_req(conn);
319 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800320 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000321
Harald Welte4bfdfe72009-06-10 23:11:52 +0800322 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200323 /* FIXME: this is not neccessarily the right thing to do, we should
324 * only set trans->lchan to NULL and wait for another lchan to be
325 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800326 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
327 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200328 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800329 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000330}
331
Holger Freyther429e7762008-12-30 13:28:30 +0000332/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800333int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000334{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800335 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800336 struct msgb *msg;
337
338 counter_inc(bts->network->stats.loc_upd_resp.reject);
339
340 msg = gsm48_create_loc_upd_rej(cause);
341 if (!msg) {
342 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
343 return -1;
344 }
Harald Welte52b1f982008-12-23 20:25:15 +0000345
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800346 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000347
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100348 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100349 "LAC=%u BTS=%u\n", conn->subscr ?
350 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800351 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100352
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800353 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000354}
355
356/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800357int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000358{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800359 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000360 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000361 struct gsm48_hdr *gh;
362 struct gsm48_loc_area_id *lai;
363 u_int8_t *mid;
364
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800365 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000366
367 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
368 gh->proto_discr = GSM48_PDISC_MM;
369 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
370
371 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100372 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000373 bts->network->network_code, bts->location_area_code);
374
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200375 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200376 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000377
378 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
379
Harald Welteffa55a42009-12-22 19:07:32 +0100380 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100381
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800382 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000383}
384
Harald Weltebf5e8df2009-02-03 12:59:45 +0000385/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800386static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000387{
388 struct msgb *msg = gsm48_msgb_alloc();
389 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000390
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800391 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000392
393 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
394 gh->proto_discr = GSM48_PDISC_MM;
395 gh->msg_type = GSM48_MT_MM_ID_REQ;
396 gh->data[0] = id_type;
397
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800398 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000399}
400
Harald Welte231ad4f2008-12-27 11:15:38 +0000401
Harald Weltebf5e8df2009-02-03 12:59:45 +0000402/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800403static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000404{
405 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000406 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200407 struct gsm_bts *bts = lchan->ts->trx->bts;
408 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000409 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200410 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000411
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200412 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000413 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000414 mi_type, mi_string);
415
Harald Welte7659de12009-12-13 12:39:18 +0100416 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
417
Harald Welte75a983f2008-12-27 21:34:06 +0000418 switch (mi_type) {
419 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200420 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100421 if (!conn->subscr) {
422 conn->subscr = subscr_get_by_imsi(net, mi_string);
423 if (!conn->subscr)
424 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200425 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100426 if (conn->loc_operation)
427 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000428 break;
429 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000430 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000431 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100432 if (conn->subscr) {
433 db_subscriber_assoc_imei(conn->subscr, mi_string);
434 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200435 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100436 if (conn->loc_operation)
437 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000438 break;
439 }
Holger Freyther73487a22008-12-31 18:53:57 +0000440
441 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100442 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000443}
444
Harald Welte255539c2008-12-28 02:26:27 +0000445
446static void loc_upd_rej_cb(void *data)
447{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100448 struct gsm_subscriber_connection *conn = data;
449 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100450 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000451
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800452 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800453 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000454}
455
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100456static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000457{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100458 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
459 conn->loc_operation->updating_timer.data = conn;
460 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000461}
462
Harald Welte2a139372009-02-22 21:14:55 +0000463static const char *lupd_name(u_int8_t type)
464{
465 switch (type) {
466 case GSM48_LUPD_NORMAL:
467 return "NORMAL";
468 case GSM48_LUPD_PERIODIC:
469 return "PEROIDOC";
470 case GSM48_LUPD_IMSI_ATT:
471 return "IMSI ATTACH";
472 default:
473 return "UNKNOWN";
474 }
475}
476
Harald Weltebf5e8df2009-02-03 12:59:45 +0000477/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800478static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000479{
Harald Welte8470bf22008-12-25 23:28:35 +0000480 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000481 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800482 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800483 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000484 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200485 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000486 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000487
Harald Welte8470bf22008-12-25 23:28:35 +0000488 lu = (struct gsm48_loc_upd_req *) gh->data;
489
490 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000491
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200492 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000493
Harald Weltea0368542009-06-27 02:58:43 +0200494 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000495 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000496
Harald Welte7659de12009-12-13 12:39:18 +0100497 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
498
Harald Welte24ff6ee2009-12-22 00:41:05 +0100499 switch (lu->type) {
500 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100501 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100502 break;
503 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100504 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100505 break;
506 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100507 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100508 break;
509 }
510
Holger Freythereaf04692009-06-06 13:54:44 +0000511 /*
512 * Pseudo Spoof detection: Just drop a second/concurrent
513 * location updating request.
514 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100515 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200516 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100517 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800518 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000519 return 0;
520 }
521
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800522 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000523
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200524 conn->loc_operation->key_seq = lu->key_seq;
525
Harald Welte52b1f982008-12-23 20:25:15 +0000526 switch (mi_type) {
527 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200528 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000529 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800530 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100531 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000532
Jan Luebbe370b41d2009-08-12 10:19:34 +0200533 /* look up subscriber based on IMSI, create if not found */
534 subscr = subscr_get_by_imsi(bts->network, mi_string);
535 if (!subscr) {
536 subscr = db_create_subscriber(bts->network, mi_string);
537 }
Harald Welte4b634542008-12-27 01:55:51 +0000538 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000539 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200540 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000541 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200542 subscr = subscr_get_by_tmsi(bts->network,
543 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000544 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000545 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800546 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100547 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000548 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200549 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800550 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200551 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000552 break;
553 case GSM_MI_TYPE_IMEI:
554 case GSM_MI_TYPE_IMEISV:
555 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200556 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000557 break;
558 default:
Harald Weltea0368542009-06-27 02:58:43 +0200559 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000560 break;
561 }
562
Harald Welte24516ea2009-07-04 10:18:00 +0200563 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100564 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200565
Harald Welte4bfdfe72009-06-10 23:11:52 +0800566 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200567 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800568 /* FIXME: request id? close channel? */
569 return -EINVAL;
570 }
571
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 conn->subscr = subscr;
573 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000574
Harald Welte24516ea2009-07-04 10:18:00 +0200575 /* check if we can let the subscriber into our network immediately
576 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100577 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000578}
579
Harald Welte4bfdfe72009-06-10 23:11:52 +0800580#if 0
581static u_int8_t to_bcd8(u_int8_t val)
582{
583 return ((val / 10) << 4) | (val % 10);
584}
585#endif
586
Harald Weltedb253af2008-12-30 17:56:55 +0000587/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800588int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000589{
590 struct msgb *msg = gsm48_msgb_alloc();
591 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800592 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000593 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200594 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800595#if 0
596 time_t cur_t;
597 struct tm* cur_time;
598 int tz15min;
599#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000600
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800601 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000602
603 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
604 gh->proto_discr = GSM48_PDISC_MM;
605 gh->msg_type = GSM48_MT_MM_INFO;
606
607 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200608#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000609 name_len = strlen(net->name_long);
610 /* 10.5.3.5a */
611 ptr8 = msgb_put(msg, 3);
612 ptr8[0] = GSM48_IE_NAME_LONG;
613 ptr8[1] = name_len*2 +1;
614 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
615
616 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
617 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000618 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000619
620 /* FIXME: Use Cell Broadcast, not UCS-2, since
621 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200622#endif
623 name_len = (strlen(net->name_long)*7)/8;
624 name_pad = (8 - strlen(net->name_long)*7)%8;
625 if (name_pad > 0)
626 name_len++;
627 /* 10.5.3.5a */
628 ptr8 = msgb_put(msg, 3);
629 ptr8[0] = GSM48_IE_NAME_LONG;
630 ptr8[1] = name_len +1;
631 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
632
633 ptr8 = msgb_put(msg, name_len);
634 gsm_7bit_encode(ptr8, net->name_long);
635
Harald Weltedb253af2008-12-30 17:56:55 +0000636 }
637
638 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200639#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000640 name_len = strlen(net->name_short);
641 /* 10.5.3.5a */
642 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200643 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000644 ptr8[1] = name_len*2 + 1;
645 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
646
Harald Weltee872cb12009-01-01 00:33:37 +0000647 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000648 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000649 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200650#endif
651 name_len = (strlen(net->name_short)*7)/8;
652 name_pad = (8 - strlen(net->name_short)*7)%8;
653 if (name_pad > 0)
654 name_len++;
655 /* 10.5.3.5a */
656 ptr8 = (u_int8_t *) msgb_put(msg, 3);
657 ptr8[0] = GSM48_IE_NAME_SHORT;
658 ptr8[1] = name_len +1;
659 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
660
661 ptr8 = msgb_put(msg, name_len);
662 gsm_7bit_encode(ptr8, net->name_short);
663
Harald Weltedb253af2008-12-30 17:56:55 +0000664 }
665
666#if 0
667 /* Section 10.5.3.9 */
668 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800669 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000670 ptr8 = msgb_put(msg, 8);
671 ptr8[0] = GSM48_IE_NET_TIME_TZ;
672 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
673 ptr8[2] = to_bcd8(cur_time->tm_mon);
674 ptr8[3] = to_bcd8(cur_time->tm_mday);
675 ptr8[4] = to_bcd8(cur_time->tm_hour);
676 ptr8[5] = to_bcd8(cur_time->tm_min);
677 ptr8[6] = to_bcd8(cur_time->tm_sec);
678 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
679 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800680 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000681 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000683#endif
684
Daniel Willmanneea93372009-08-13 03:42:07 +0200685 DEBUGP(DMM, "-> MM INFO\n");
686
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800687 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000688}
689
Harald Welte7984d5c2009-08-12 22:56:50 +0200690/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800691int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200692{
693 struct msgb *msg = gsm48_msgb_alloc();
694 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200695 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200696
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100697 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200698
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800699 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200700 gh->proto_discr = GSM48_PDISC_MM;
701 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
702
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100703 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200704
Harald Welte7984d5c2009-08-12 22:56:50 +0200705 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200706 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200707 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200708
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800709 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200710}
711
712/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800713int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200714{
715 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800716 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200717}
718
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800719static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000720{
Harald Welte4b634542008-12-27 01:55:51 +0000721 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800722 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000723}
Harald Welteba4cf162009-01-10 01:49:35 +0000724
725/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100726static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000727 enum gsm48_reject_value value)
728{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800729 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000730
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800731 msg = gsm48_create_mm_serv_rej(value);
732 if (!msg) {
733 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
734 return -1;
735 }
Harald Welteba4cf162009-01-10 01:49:35 +0000736
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800737 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100738 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800739 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000740}
741
Sylvain Munautba87f452009-12-24 00:28:46 +0100742static int _gsm48_rx_mm_serv_req_sec_cb(
743 unsigned int hooknum, unsigned int event,
744 struct msgb *msg, void *data, void *param)
745{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800746 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100747 int rc = 0;
748
749 switch (event) {
750 case GSM_SECURITY_AUTH_FAILED:
751 /* Nothing to do */
752 break;
753
754 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800755 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100756 break;
757
758 case GSM_SECURITY_SUCCEEDED:
759 /* nothing to do. CIPHER MODE COMMAND is
760 * implicit CM SERV ACK */
761 break;
762
763 default:
764 rc = -EINVAL;
765 };
766
767 return rc;
768}
769
Harald Welte4ed0e922009-01-10 03:17:30 +0000770/*
771 * Handle CM Service Requests
772 * a) Verify that the packet is long enough to contain the information
773 * we require otherwsie reject with INCORRECT_MESSAGE
774 * b) Try to parse the TMSI. If we do not have one reject
775 * c) Check that we know the subscriber with the TMSI otherwise reject
776 * with a HLR cause
777 * d) Set the subscriber on the gsm_lchan and accept
778 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800779static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000780{
Harald Welteba4cf162009-01-10 01:49:35 +0000781 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200782 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000783
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800784 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000785 struct gsm_subscriber *subscr;
786 struct gsm48_hdr *gh = msgb_l3(msg);
787 struct gsm48_service_request *req =
788 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800789 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000790 u_int8_t classmark2_len = gh->data[1];
791 u_int8_t *classmark2 = gh->data+2;
792 u_int8_t mi_len = *(classmark2 + classmark2_len);
793 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000794
Harald Weltec9e02182009-05-01 19:07:53 +0000795 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000796 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000797 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800798 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000799 GSM48_REJECT_INCORRECT_MESSAGE);
800 }
801
802 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000803 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800804 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000805 GSM48_REJECT_INCORRECT_MESSAGE);
806 }
807
Harald Weltec9e02182009-05-01 19:07:53 +0000808 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000809 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000810 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800811 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000812 GSM48_REJECT_INCORRECT_MESSAGE);
813 }
814
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200815 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000816 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000817 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000818
Harald Welte7659de12009-12-13 12:39:18 +0100819 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
820
Harald Welte3ac7f102009-08-10 10:12:45 +0200821 if (is_siemens_bts(bts))
822 send_siemens_mrpci(msg->lchan, classmark2-1);
823
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200824 subscr = subscr_get_by_tmsi(bts->network,
825 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000826
Harald Welte2a139372009-02-22 21:14:55 +0000827 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000828 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800829 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000830 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
831
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800832 if (!conn->subscr)
833 conn->subscr = subscr;
834 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100835 subscr_put(subscr); /* lchan already has a ref, don't need another one */
836 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000837 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
838 subscr_put(subscr);
839 }
840
Harald Weltec2e302d2009-07-05 14:08:13 +0200841 subscr->equipment.classmark2_len = classmark2_len;
842 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
843 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000844
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800845 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100846 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000847}
848
Harald Welte2a139372009-02-22 21:14:55 +0000849static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
850{
Harald Welte9176bd42009-07-23 18:46:00 +0200851 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000852 struct gsm48_hdr *gh = msgb_l3(msg);
853 struct gsm48_imsi_detach_ind *idi =
854 (struct gsm48_imsi_detach_ind *) gh->data;
855 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200856 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800857 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000858
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200859 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000860 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
861 mi_type, mi_string);
862
Harald Welteffa55a42009-12-22 19:07:32 +0100863 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100864
Harald Welte2a139372009-02-22 21:14:55 +0000865 switch (mi_type) {
866 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200867 subscr = subscr_get_by_tmsi(bts->network,
868 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000869 break;
870 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200871 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000872 break;
873 case GSM_MI_TYPE_IMEI:
874 case GSM_MI_TYPE_IMEISV:
875 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000876 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000877 break;
878 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000879 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000880 break;
881 }
882
Holger Freyther4a49e772009-04-12 05:37:29 +0000883 if (subscr) {
884 subscr_update(subscr, msg->trx->bts,
885 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100886 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200887
888 subscr->equipment.classmark1 = idi->classmark1;
889 db_sync_equipment(&subscr->equipment);
890
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000891 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000892 } else
Harald Welte2a139372009-02-22 21:14:55 +0000893 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
894
Harald Welte31981a02009-12-20 09:58:40 +0100895 /* FIXME: iterate over all transactions and release them,
896 * imagine an IMSI DETACH happening during an active call! */
897
Harald Welteb83d9382009-12-12 21:00:48 +0100898 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000899 return 0;
900}
901
Harald Welted2a7f5a2009-06-05 20:08:20 +0000902static int gsm48_rx_mm_status(struct msgb *msg)
903{
904 struct gsm48_hdr *gh = msgb_l3(msg);
905
906 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
907
908 return 0;
909}
910
Sylvain Munaut30a15382009-12-24 00:27:26 +0100911/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800912static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100913{
914 struct gsm48_hdr *gh = msgb_l3(msg);
915 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800916 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100917
Sylvain Munautc593cf12010-06-10 23:51:41 +0200918 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100919 hexdump(ar->sres, 4));
920
921 /* Safety check */
922 if (!conn->sec_operation) {
923 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
924 return -EIO;
925 }
926
927 /* Validate SRES */
928 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100929 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100930 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200931
932 DEBUGPC(DMM, "Invalid (expected %s)\n",
933 hexdump(conn->sec_operation->atuple.sres, 4));
934
Sylvain Munaut30a15382009-12-24 00:27:26 +0100935 if (cb)
936 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800937 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100938
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100939 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100940 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100941 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100942 }
943
Sylvain Munautc593cf12010-06-10 23:51:41 +0200944 DEBUGPC(DMM, "OK\n");
945
Sylvain Munaut30a15382009-12-24 00:27:26 +0100946 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100947 return gsm0808_cipher_mode(conn, net->a5_encryption,
948 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100949}
950
Harald Weltebf5e8df2009-02-03 12:59:45 +0000951/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800952static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000953{
954 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800955 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000956
957 switch (gh->msg_type & 0xbf) {
958 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200959 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800960 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000961 break;
962 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800963 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000964 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000965 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800966 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000967 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000968 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000969 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000970 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000971 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000972 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800973 conn->subscr ?
974 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000975 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100976 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +0000977 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000978 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000979 rc = gsm48_rx_mm_imsi_detach_ind(msg);
980 break;
981 case GSM48_MT_MM_CM_REEST_REQ:
982 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
983 break;
984 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800985 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000986 break;
987 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100988 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000989 gh->msg_type);
990 break;
991 }
992
993 return rc;
994}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000995
Harald Welte2d35ae62009-02-06 12:02:13 +0000996/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800997static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000998{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800999 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001000 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001001 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +00001002 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001003 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001004 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001005 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001006 int rc = 0;
1007
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001008 resp = (struct gsm48_pag_resp *) &gh->data[0];
1009 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1010 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001011 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1012 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001013
Harald Weltefe18d8f2009-02-22 21:14:24 +00001014 switch (mi_type) {
1015 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001016 subscr = subscr_get_by_tmsi(bts->network,
1017 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001018 break;
1019 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001020 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001021 break;
1022 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001023
1024 if (!subscr) {
1025 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001026 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001027 return -EINVAL;
1028 }
1029 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001030 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001031
Harald Weltec2e302d2009-07-05 14:08:13 +02001032 subscr->equipment.classmark2_len = *classmark2_lv;
1033 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1034 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001035
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001036 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001037 return rc;
1038}
1039
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001041{
1042 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001044 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1045 u_int8_t cm2_len, cm3_len = 0;
1046 u_int8_t *cm2, *cm3 = NULL;
1047
1048 DEBUGP(DRR, "CLASSMARK CHANGE ");
1049
1050 /* classmark 2 */
1051 cm2_len = gh->data[0];
1052 cm2 = &gh->data[1];
1053 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1054
1055 if (payload_len > cm2_len + 1) {
1056 /* we must have a classmark3 */
1057 if (gh->data[cm2_len+1] != 0x20) {
1058 DEBUGPC(DRR, "ERR CM3 TAG\n");
1059 return -EINVAL;
1060 }
1061 if (cm2_len > 3) {
1062 DEBUGPC(DRR, "CM2 too long!\n");
1063 return -EINVAL;
1064 }
1065
1066 cm3_len = gh->data[cm2_len+2];
1067 cm3 = &gh->data[cm2_len+3];
1068 if (cm3_len > 14) {
1069 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1070 return -EINVAL;
1071 }
1072 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1073 }
1074 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001075 subscr->equipment.classmark2_len = cm2_len;
1076 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001077 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001078 subscr->equipment.classmark3_len = cm3_len;
1079 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001080 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001081 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001082 }
1083
Harald Weltef7c43522009-06-09 20:24:21 +00001084 return 0;
1085}
1086
Harald Weltecf5b3592009-05-01 18:28:42 +00001087static int gsm48_rx_rr_status(struct msgb *msg)
1088{
1089 struct gsm48_hdr *gh = msgb_l3(msg);
1090
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001091 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001092 rr_cause_name(gh->data[0]));
1093
1094 return 0;
1095}
1096
Harald Weltef7c43522009-06-09 20:24:21 +00001097static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1098{
Harald Welted12b0fd2009-12-15 21:36:05 +01001099 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001100
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001101 /* This shouldn't actually end up here, as RSL treats
1102 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1103 * directly into gsm48_parse_meas_rep */
1104 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001105 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001106
1107 return 0;
1108}
1109
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001110static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001111{
1112 struct gsm48_hdr *gh = msgb_l3(msg);
1113 u_int8_t apdu_id_flags;
1114 u_int8_t apdu_len;
1115 u_int8_t *apdu_data;
1116
1117 apdu_id_flags = gh->data[0];
1118 apdu_len = gh->data[1];
1119 apdu_data = gh->data+2;
1120
1121 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1122 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1123
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001124 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001125}
1126
Sylvain Munaut30a15382009-12-24 00:27:26 +01001127/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001128static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001129{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001130 gsm_cbfn *cb;
1131 int rc = 0;
1132
1133 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1134
1135 /* Safety check */
1136 if (!conn->sec_operation) {
1137 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1138 return -EIO;
1139 }
1140
1141 /* FIXME: check for MI (if any) */
1142
1143 /* Call back whatever was in progress (if anything) ... */
1144 cb = conn->sec_operation->cb;
1145 if (cb) {
1146 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001147 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001148 }
1149
1150 /* Complete the operation */
1151 release_security_operation(conn);
1152
1153 return rc;
1154}
1155
Harald Welted011e8b2009-11-29 22:45:52 +01001156/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001157static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001158{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001159 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001160 struct gsm48_hdr *gh = msgb_l3(msg);
1161
1162 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1163 rr_cause_name(gh->data[0]));
1164
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001165 sig.lchan = msg->lchan;
1166 sig.mr = NULL;
1167 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001168 /* FIXME: release old channel */
1169
1170 return 0;
1171}
1172
1173/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001174static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001175{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001176 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001177 struct gsm48_hdr *gh = msgb_l3(msg);
1178
1179 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1180 rr_cause_name(gh->data[0]));
1181
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001182 sig.lchan = msg->lchan;
1183 sig.mr = NULL;
1184 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001185 /* FIXME: release allocated new channel */
1186
1187 return 0;
1188}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001189
Harald Weltebf5e8df2009-02-03 12:59:45 +00001190/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001191static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001192{
1193 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001194 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001195
1196 switch (gh->msg_type) {
1197 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001198 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001199 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001200 case GSM48_MT_RR_GPRS_SUSP_REQ:
1201 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1202 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001203 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001204 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001205 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001206 case GSM48_MT_RR_STATUS:
1207 rc = gsm48_rx_rr_status(msg);
1208 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001209 case GSM48_MT_RR_MEAS_REP:
1210 rc = gsm48_rx_rr_meas_rep(msg);
1211 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001212 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001213 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001214 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001215 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001216 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001217 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001218 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001219 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001220 break;
1221 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001222 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001223 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001224 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001225 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1226 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001227 break;
1228 }
1229
Harald Welte2d35ae62009-02-06 12:02:13 +00001230 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001231}
1232
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001233int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001234 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001235{
1236 struct msgb *msg = gsm48_msgb_alloc();
1237 struct gsm48_hdr *gh;
1238
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001239 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001240
1241 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1242 apdu_id, apdu_len);
1243
1244 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1245 gh->proto_discr = GSM48_PDISC_RR;
1246 gh->msg_type = GSM48_MT_RR_APP_INFO;
1247 gh->data[0] = apdu_id;
1248 gh->data[1] = apdu_len;
1249 memcpy(gh->data+2, apdu, apdu_len);
1250
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001251 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001252}
1253
Harald Welte4bc90a12008-12-27 16:32:52 +00001254/* Call Control */
1255
Harald Welte7584aea2009-02-11 11:44:12 +00001256/* The entire call control code is written in accordance with Figure 7.10c
1257 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1258 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1259 * it for voice */
1260
Harald Welte4bfdfe72009-06-10 23:11:52 +08001261static void new_cc_state(struct gsm_trans *trans, int state)
1262{
1263 if (state > 31 || state < 0)
1264 return;
1265
1266 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001267 gsm48_cc_state_name(trans->cc.state),
1268 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269
Harald Weltedcaf5652009-07-23 18:56:43 +02001270 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001271}
1272
1273static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001274{
1275 struct msgb *msg = gsm48_msgb_alloc();
1276 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1277 u_int8_t *cause, *call_state;
1278
Harald Welte4bc90a12008-12-27 16:32:52 +00001279 gh->msg_type = GSM48_MT_CC_STATUS;
1280
1281 cause = msgb_put(msg, 3);
1282 cause[0] = 2;
1283 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1284 cause[2] = 0x80 | 30; /* response to status inquiry */
1285
1286 call_state = msgb_put(msg, 1);
1287 call_state[0] = 0xc0 | 0x00;
1288
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001289 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001290}
1291
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001292static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001293 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001294{
1295 struct msgb *msg = gsm48_msgb_alloc();
1296 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1297
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001298 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001299
Harald Welte6f4b7532008-12-29 00:39:37 +00001300 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001301 gh->msg_type = msg_type;
1302
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001303 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001304}
1305
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1307{
Harald Weltedcaf5652009-07-23 18:56:43 +02001308 if (bsc_timer_pending(&trans->cc.timer)) {
1309 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1310 bsc_del_timer(&trans->cc.timer);
1311 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001312 }
1313}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001314
Harald Welte4bfdfe72009-06-10 23:11:52 +08001315static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1316 int msg_type, struct gsm_mncc *mncc)
1317{
1318 struct msgb *msg;
1319
1320 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001321 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001322 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001324 trans->conn->lchan->ts->trx->bts->nr,
1325 trans->conn->lchan->ts->trx->nr,
1326 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327 (trans->subscr)?(trans->subscr->extension):"-",
1328 get_mncc_name(msg_type));
1329 else
1330 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1331 "Sending '%s' to MNCC.\n",
1332 (trans->subscr)?(trans->subscr->extension):"-",
1333 get_mncc_name(msg_type));
1334 else
1335 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1336 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1337
1338 mncc->msg_type = msg_type;
1339
Harald Welte966636f2009-06-26 19:39:35 +02001340 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341 if (!msg)
1342 return -ENOMEM;
1343 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
Harald Welte434bb3f2010-12-22 19:02:27 +01001344 mncc_upq_enqueue(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345
1346 return 0;
1347}
1348
1349int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1350 u_int32_t callref, int location, int value)
1351{
1352 struct gsm_mncc rel;
1353
Harald Welte92f70c52009-06-12 01:54:08 +08001354 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001356 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1358}
1359
Harald Weltedcaf5652009-07-23 18:56:43 +02001360/* Call Control Specific transaction release.
1361 * gets called by trans_free, DO NOT CALL YOURSELF! */
1362void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 gsm48_stop_cc_timer(trans);
1365
1366 /* send release to L4, if callref still exists */
1367 if (trans->callref) {
1368 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001369 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001370 GSM48_CAUSE_LOC_PRN_S_LU,
1371 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001373 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001375 if (trans->conn)
1376 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377}
1378
1379static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001380
Harald Welte09e38af2009-02-16 22:52:23 +00001381/* call-back from paging the B-end of the connection */
1382static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001383 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001384{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001385 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 struct gsm_subscriber *subscr = param;
1387 struct gsm_trans *transt, *tmp;
1388 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001389
Harald Welte09e38af2009-02-16 22:52:23 +00001390 if (hooknum != GSM_HOOK_RR_PAGING)
1391 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001392
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 if (!subscr)
1394 return -EINVAL;
1395 net = subscr->net;
1396 if (!net) {
1397 DEBUGP(DCC, "Error Network not set!\n");
1398 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001399 }
Harald Welte7584aea2009-02-11 11:44:12 +00001400
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401 /* check all tranactions (without lchan) for subscriber */
1402 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001403 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 continue;
1405 switch (event) {
1406 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001407 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 break;
1409 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1410 subscr->extension);
1411 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001412 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001413 transt->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 }
1415 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001416 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417 break;
1418 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001419 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 DEBUGP(DCC, "Paging subscr %s expired!\n",
1421 subscr->extension);
1422 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001423 mncc_release_ind(transt->subscr->net, transt,
1424 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001425 GSM48_CAUSE_LOC_PRN_S_LU,
1426 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001428 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429 break;
1430 }
1431 }
Harald Welte09e38af2009-02-16 22:52:23 +00001432 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001433}
Harald Welte7584aea2009-02-11 11:44:12 +00001434
Harald Welteda7ab742009-12-19 22:23:05 +01001435static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1436
Harald Welte805f6442009-07-28 18:25:29 +02001437/* some other part of the code sends us a signal */
1438static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1439 void *handler_data, void *signal_data)
1440{
1441 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001442 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001443 struct gsm_network *net;
1444 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001445
1446 if (subsys != SS_ABISIP)
1447 return 0;
1448
1449 /* in case we use direct BTS-to-BTS RTP */
1450 if (ipacc_rtp_direct)
1451 return 0;
1452
Harald Welte805f6442009-07-28 18:25:29 +02001453 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001454 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001455 /* check if any transactions on this lchan still have
1456 * a tch_recv_mncc request pending */
1457 net = lchan->ts->trx->bts->network;
1458 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001459 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001460 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1461 tch_recv_mncc(net, trans->callref, 1);
1462 }
1463 }
Harald Welte805f6442009-07-28 18:25:29 +02001464 break;
Harald Welte805f6442009-07-28 18:25:29 +02001465 }
1466
1467 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001468}
1469
Harald Welte49f48b82009-02-17 15:29:33 +00001470/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001471static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001472{
Harald Welte11fa29c2009-02-19 17:24:39 +00001473 struct gsm_bts *bts = lchan->ts->trx->bts;
1474 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001475 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001476
Harald Welte11fa29c2009-02-19 17:24:39 +00001477 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1478 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1479 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1480
1481 if (bts->type != remote_bts->type) {
1482 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1483 return -EINVAL;
1484 }
Harald Welteda7ab742009-12-19 22:23:05 +01001485
1486 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001487 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001488 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001489 if (!ipacc_rtp_direct) {
1490 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001491 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001492 if (rc < 0)
1493 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001494 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001495#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001496
1497 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301498 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1499 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001500 } else {
1501 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301502 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1503 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301504 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001505 if (rc < 0)
1506 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301507 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1508 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301509 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001510 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001511 break;
1512 case GSM_BTS_TYPE_BS11:
1513 trau_mux_map_lchan(lchan, remote_lchan);
1514 break;
1515 default:
1516 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001517 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001518 }
Harald Welte49f48b82009-02-17 15:29:33 +00001519
1520 return 0;
1521}
1522
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523/* bridge channels of two transactions */
1524static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001525{
Harald Weltedcaf5652009-07-23 18:56:43 +02001526 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1527 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001528
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001530 return -EIO;
1531
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001532 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001533 return -EIO;
1534
1535 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001536 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001537}
1538
Harald Welteda7ab742009-12-19 22:23:05 +01001539/* enable receive of channels to MNCC upqueue */
1540static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541{
1542 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001543 struct gsm_lchan *lchan;
1544 struct gsm_bts *bts;
1545 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001546
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001548 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 if (!trans)
1550 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001551 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001553 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001554 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001555
Harald Welteda7ab742009-12-19 22:23:05 +01001556 switch (bts->type) {
1557 case GSM_BTS_TYPE_NANOBTS:
1558 if (ipacc_rtp_direct) {
1559 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1560 return -EINVAL;
1561 }
1562 /* in case, we don't have a RTP socket yet, we note this
1563 * in the transaction and try later */
1564 if (!lchan->abis_ip.rtp_socket) {
1565 trans->tch_recv = enable;
1566 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1567 return 0;
1568 }
1569 if (enable) {
1570 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001571 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001572 if (rc < 0)
1573 return rc;
1574 /* assign socket to application interface */
1575 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1576 net, callref);
1577 } else
1578 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1579 net, 0);
1580 break;
1581 case GSM_BTS_TYPE_BS11:
1582 if (enable)
1583 return trau_recv_lchan(lchan, callref);
1584 return trau_mux_unmap(NULL, callref);
1585 break;
1586 default:
1587 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1588 return -EINVAL;
1589 }
1590
1591 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592}
1593
Harald Welte4bfdfe72009-06-10 23:11:52 +08001594static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1595{
1596 DEBUGP(DCC, "-> STATUS ENQ\n");
1597 return gsm48_cc_tx_status(trans, msg);
1598}
1599
1600static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1601static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1602
1603static void gsm48_cc_timeout(void *arg)
1604{
1605 struct gsm_trans *trans = arg;
1606 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001607 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1608 int mo_location = GSM48_CAUSE_LOC_USER;
1609 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1610 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611 struct gsm_mncc mo_rel, l4_rel;
1612
1613 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1614 mo_rel.callref = trans->callref;
1615 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1616 l4_rel.callref = trans->callref;
1617
Harald Weltedcaf5652009-07-23 18:56:43 +02001618 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001619 case 0x303:
1620 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001621 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001622 break;
1623 case 0x310:
1624 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001625 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626 break;
1627 case 0x313:
1628 disconnect = 1;
1629 /* unknown, did not find it in the specs */
1630 break;
1631 case 0x301:
1632 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001633 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 break;
1635 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001636 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001638 gsm48_cc_tx_release(trans, &trans->cc.msg);
1639 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 break; /* stay in release state */
1641 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001642 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 return;
1644// release = 1;
1645// l4_cause = 14;
1646// break;
1647 case 0x306:
1648 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001649 mo_cause = trans->cc.msg.cause.value;
1650 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 break;
1652 case 0x323:
1653 disconnect = 1;
1654 break;
1655 default:
1656 release = 1;
1657 }
1658
1659 if (release && trans->callref) {
1660 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001661 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 l4_location, l4_cause);
1663 trans->callref = 0;
1664 }
1665
1666 if (disconnect && trans->callref) {
1667 /* process disconnect towards layer 4 */
1668 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001669 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 }
1671
1672 /* process disconnect towards mobile station */
1673 if (disconnect || release) {
1674 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001675 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1676 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1677 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 mo_rel.cause.diag_len = 3;
1679
1680 if (disconnect)
1681 gsm48_cc_tx_disconnect(trans, &mo_rel);
1682 if (release)
1683 gsm48_cc_tx_release(trans, &mo_rel);
1684 }
1685
1686}
1687
1688static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1689 int sec, int micro)
1690{
1691 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001692 trans->cc.timer.cb = gsm48_cc_timeout;
1693 trans->cc.timer.data = trans;
1694 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1695 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696}
1697
1698static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1699{
1700 struct gsm48_hdr *gh = msgb_l3(msg);
1701 u_int8_t msg_type = gh->msg_type & 0xbf;
1702 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1703 struct tlv_parsed tp;
1704 struct gsm_mncc setup;
1705
1706 memset(&setup, 0, sizeof(struct gsm_mncc));
1707 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001708 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 /* emergency setup is identified by msg_type */
1710 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1711 setup.emergency = 1;
1712
1713 /* use subscriber as calling party number */
1714 if (trans->subscr) {
1715 setup.fields |= MNCC_F_CALLING;
1716 strncpy(setup.calling.number, trans->subscr->extension,
1717 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001718 strncpy(setup.imsi, trans->subscr->imsi,
1719 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 }
1721 /* bearer capability */
1722 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1723 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001724 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1726 }
1727 /* facility */
1728 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1729 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001730 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1732 }
1733 /* called party bcd number */
1734 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1735 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001736 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1738 }
1739 /* user-user */
1740 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1741 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001742 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1744 }
1745 /* ss-version */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1747 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001748 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1750 }
1751 /* CLIR suppression */
1752 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1753 setup.clir.sup = 1;
1754 /* CLIR invocation */
1755 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1756 setup.clir.inv = 1;
1757 /* cc cap */
1758 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1759 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001760 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1762 }
1763
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 new_cc_state(trans, GSM_CSTATE_INITIATED);
1765
Harald Welte (local)47df3992009-12-26 19:45:03 +01001766 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1767 subscr_name(trans->subscr), trans->subscr->extension,
1768 setup.called.number);
1769
Harald Weltea29e43a2010-12-24 16:06:33 +01001770 counter_inc(trans->subscr->net->stats.call.mo_setup);
1771
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001773 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774
Harald Welte13cac662009-07-29 12:10:35 +02001775 /* MNCC code will modify the channel asynchronously, we should
1776 * ipaccess-bind only after the modification has been made to the
1777 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 return 0;
1779}
1780
1781static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001782{
1783 struct msgb *msg = gsm48_msgb_alloc();
1784 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001786 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001787
Harald Welte7ccf7782009-02-17 01:43:01 +00001788 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001789
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 /* transaction id must not be assigned */
1791 if (trans->transaction_id != 0xff) { /* unasssigned */
1792 DEBUGP(DCC, "TX Setup with assigned transaction. "
1793 "This is not allowed!\n");
1794 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001795 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001796 GSM48_CAUSE_LOC_PRN_S_LU,
1797 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001799 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800 return rc;
1801 }
1802
1803 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001804 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1805 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001807 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001808 GSM48_CAUSE_LOC_PRN_S_LU,
1809 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001811 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 return rc;
1813 }
Harald Welte78283ef2009-07-23 21:36:44 +02001814 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001815
Harald Welte65e74cc2008-12-29 01:55:35 +00001816 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001817
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001819
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 /* bearer capability */
1821 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001822 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 /* facility */
1824 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001825 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 /* progress */
1827 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001828 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 /* calling party BCD number */
1830 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001831 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001832 /* called party BCD number */
1833 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001834 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 /* user-user */
1836 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001837 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 /* redirecting party BCD number */
1839 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001840 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 /* signal */
1842 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001843 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844
1845 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001846
Harald Weltea29e43a2010-12-24 16:06:33 +01001847 counter_inc(trans->subscr->net->stats.call.mt_setup);
1848
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001849 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001850}
1851
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1853{
1854 struct gsm48_hdr *gh = msgb_l3(msg);
1855 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1856 struct tlv_parsed tp;
1857 struct gsm_mncc call_conf;
1858
1859 gsm48_stop_cc_timer(trans);
1860 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1861
1862 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1863 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001864 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865#if 0
1866 /* repeat */
1867 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1868 call_conf.repeat = 1;
1869 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1870 call_conf.repeat = 2;
1871#endif
1872 /* bearer capability */
1873 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1874 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001875 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1877 }
1878 /* cause */
1879 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1880 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001881 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1883 }
1884 /* cc cap */
1885 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1886 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001887 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1889 }
1890
1891 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1892
Harald Welte596fed42009-07-23 19:06:52 +02001893 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1894 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895}
1896
1897static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1898{
1899 struct gsm_mncc *proceeding = arg;
1900 struct msgb *msg = gsm48_msgb_alloc();
1901 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1902
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1904
1905 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1906
1907 /* bearer capability */
1908 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001909 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910 /* facility */
1911 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001912 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 /* progress */
1914 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001915 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001917 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918}
1919
1920static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1921{
1922 struct gsm48_hdr *gh = msgb_l3(msg);
1923 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1924 struct tlv_parsed tp;
1925 struct gsm_mncc alerting;
1926
1927 gsm48_stop_cc_timer(trans);
1928 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1929
1930 memset(&alerting, 0, sizeof(struct gsm_mncc));
1931 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001932 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 /* facility */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1935 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001936 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1938 }
1939
1940 /* progress */
1941 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1942 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1945 }
1946 /* ss-version */
1947 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1948 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001949 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1951 }
1952
1953 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1954
Harald Welte596fed42009-07-23 19:06:52 +02001955 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1956 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957}
1958
1959static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1960{
1961 struct gsm_mncc *alerting = arg;
1962 struct msgb *msg = gsm48_msgb_alloc();
1963 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1964
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 gh->msg_type = GSM48_MT_CC_ALERTING;
1966
1967 /* facility */
1968 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 /* progress */
1971 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 /* user-user */
1974 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976
1977 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1978
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001979 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980}
1981
1982static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1983{
1984 struct gsm_mncc *progress = arg;
1985 struct msgb *msg = gsm48_msgb_alloc();
1986 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1987
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 gh->msg_type = GSM48_MT_CC_PROGRESS;
1989
1990 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001991 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 /* user-user */
1993 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001994 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001996 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997}
1998
1999static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2000{
2001 struct gsm_mncc *connect = arg;
2002 struct msgb *msg = gsm48_msgb_alloc();
2003 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2004
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 gh->msg_type = GSM48_MT_CC_CONNECT;
2006
2007 gsm48_stop_cc_timer(trans);
2008 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2009
2010 /* facility */
2011 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 /* progress */
2014 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002015 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016 /* connected number */
2017 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002018 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 /* user-user */
2020 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002021 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022
2023 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2024
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002025 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026}
2027
2028static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2029{
2030 struct gsm48_hdr *gh = msgb_l3(msg);
2031 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2032 struct tlv_parsed tp;
2033 struct gsm_mncc connect;
2034
2035 gsm48_stop_cc_timer(trans);
2036
2037 memset(&connect, 0, sizeof(struct gsm_mncc));
2038 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002039 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 /* use subscriber as connected party number */
2041 if (trans->subscr) {
2042 connect.fields |= MNCC_F_CONNECTED;
2043 strncpy(connect.connected.number, trans->subscr->extension,
2044 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002045 strncpy(connect.imsi, trans->subscr->imsi,
2046 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 }
2048 /* facility */
2049 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2050 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2053 }
2054 /* user-user */
2055 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2056 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002057 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2059 }
2060 /* ss-version */
2061 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2062 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2065 }
2066
2067 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltea29e43a2010-12-24 16:06:33 +01002068 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069
Harald Welte596fed42009-07-23 19:06:52 +02002070 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071}
2072
2073
2074static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2075{
2076 struct gsm_mncc connect_ack;
2077
2078 gsm48_stop_cc_timer(trans);
2079
2080 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltea29e43a2010-12-24 16:06:33 +01002081 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082
2083 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2084 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002085
Harald Welte596fed42009-07-23 19:06:52 +02002086 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 &connect_ack);
2088}
2089
2090static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2091{
2092 struct msgb *msg = gsm48_msgb_alloc();
2093 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2094
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2096
2097 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2098
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002099 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100}
2101
2102static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2103{
2104 struct gsm48_hdr *gh = msgb_l3(msg);
2105 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2106 struct tlv_parsed tp;
2107 struct gsm_mncc disc;
2108
2109 gsm48_stop_cc_timer(trans);
2110
2111 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2112
2113 memset(&disc, 0, sizeof(struct gsm_mncc));
2114 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002115 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116 /* cause */
2117 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2118 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2121 }
2122 /* facility */
2123 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2124 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002125 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2127 }
2128 /* user-user */
2129 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2130 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002131 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2133 }
2134 /* ss-version */
2135 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2136 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2139 }
2140
Harald Welte596fed42009-07-23 19:06:52 +02002141 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142
2143}
2144
Harald Weltec66b71c2009-06-11 14:23:20 +08002145static struct gsm_mncc_cause default_cause = {
2146 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2147 .coding = 0,
2148 .rec = 0,
2149 .rec_val = 0,
2150 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2151 .diag_len = 0,
2152 .diag = { 0 },
2153};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154
2155static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2156{
2157 struct gsm_mncc *disc = arg;
2158 struct msgb *msg = gsm48_msgb_alloc();
2159 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2160
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2162
2163 gsm48_stop_cc_timer(trans);
2164 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2165
2166 /* cause */
2167 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 else
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171
2172 /* facility */
2173 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002174 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 /* progress */
2176 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002177 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 /* user-user */
2179 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181
2182 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002183 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184
2185 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2186
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002187 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188}
2189
2190static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2191{
2192 struct gsm48_hdr *gh = msgb_l3(msg);
2193 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2194 struct tlv_parsed tp;
2195 struct gsm_mncc rel;
2196 int rc;
2197
2198 gsm48_stop_cc_timer(trans);
2199
2200 memset(&rel, 0, sizeof(struct gsm_mncc));
2201 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002202 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 /* cause */
2204 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2205 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002206 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2208 }
2209 /* facility */
2210 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2211 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002212 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2214 }
2215 /* user-user */
2216 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2217 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002218 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2220 }
2221 /* ss-version */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2223 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2226 }
2227
Harald Weltedcaf5652009-07-23 18:56:43 +02002228 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002230 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002232 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002233 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002234 GSM48_MT_CC_RELEASE_COMPL);
2235 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 }
2237
2238 new_cc_state(trans, GSM_CSTATE_NULL);
2239
2240 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002241 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242
2243 return rc;
2244}
2245
2246static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2247{
2248 struct gsm_mncc *rel = arg;
2249 struct msgb *msg = gsm48_msgb_alloc();
2250 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2251
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 gh->msg_type = GSM48_MT_CC_RELEASE;
2253
2254 trans->callref = 0;
2255
2256 gsm48_stop_cc_timer(trans);
2257 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2258
2259 /* cause */
2260 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002261 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 /* facility */
2263 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 /* user-user */
2266 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002267 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268
Harald Weltedcaf5652009-07-23 18:56:43 +02002269 trans->cc.T308_second = 0;
2270 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271
Harald Weltedcaf5652009-07-23 18:56:43 +02002272 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2274
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002275 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276}
2277
2278static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2279{
2280 struct gsm48_hdr *gh = msgb_l3(msg);
2281 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2282 struct tlv_parsed tp;
2283 struct gsm_mncc rel;
2284 int rc = 0;
2285
2286 gsm48_stop_cc_timer(trans);
2287
2288 memset(&rel, 0, sizeof(struct gsm_mncc));
2289 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002290 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 /* cause */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2293 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2296 }
2297 /* facility */
2298 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2299 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002300 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2302 }
2303 /* user-user */
2304 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2305 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2308 }
2309 /* ss-version */
2310 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2311 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002312 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2314 }
2315
2316 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002317 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002319 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 MNCC_REJ_IND, &rel);
2321 break;
2322 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002323 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 MNCC_REL_CNF, &rel);
2325 break;
2326 default:
Harald Welte596fed42009-07-23 19:06:52 +02002327 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 MNCC_REL_IND, &rel);
2329 }
2330 }
2331
2332 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002333 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334
2335 return rc;
2336}
2337
2338static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2339{
2340 struct gsm_mncc *rel = arg;
2341 struct msgb *msg = gsm48_msgb_alloc();
2342 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002343 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2346
2347 trans->callref = 0;
2348
2349 gsm48_stop_cc_timer(trans);
2350
2351 /* cause */
2352 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 /* facility */
2355 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002356 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 /* user-user */
2358 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360
Harald Weltec7782de2010-12-21 19:31:41 +01002361 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2362
Harald Weltedcaf5652009-07-23 18:56:43 +02002363 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364
Harald Weltec7782de2010-12-21 19:31:41 +01002365 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366}
2367
2368static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2369{
2370 struct gsm48_hdr *gh = msgb_l3(msg);
2371 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2372 struct tlv_parsed tp;
2373 struct gsm_mncc fac;
2374
2375 memset(&fac, 0, sizeof(struct gsm_mncc));
2376 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002377 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 /* facility */
2379 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2380 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002381 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2383 }
2384 /* ss-version */
2385 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2386 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2389 }
2390
Harald Welte596fed42009-07-23 19:06:52 +02002391 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392}
2393
2394static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2395{
2396 struct gsm_mncc *fac = 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_FACILITY;
2401
2402 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002403 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002405 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406}
2407
2408static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2409{
2410 struct gsm_mncc hold;
2411
2412 memset(&hold, 0, sizeof(struct gsm_mncc));
2413 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002414 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2418{
2419 struct msgb *msg = gsm48_msgb_alloc();
2420 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2421
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2423
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002424 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *hold_rej = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2434
2435 /* cause */
2436 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002437 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 else
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002441 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2445{
2446 struct gsm_mncc retrieve;
2447
2448 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2449 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002450 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2451 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452}
2453
2454static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2455{
2456 struct msgb *msg = gsm48_msgb_alloc();
2457 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2458
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2460
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002461 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462}
2463
2464static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2465{
2466 struct gsm_mncc *retrieve_rej = arg;
2467 struct msgb *msg = gsm48_msgb_alloc();
2468 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2469
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2471
2472 /* cause */
2473 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002474 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 else
Harald Welte55c8f352010-03-07 23:40:35 +01002476 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002478 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479}
2480
2481static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2482{
2483 struct gsm48_hdr *gh = msgb_l3(msg);
2484 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2485 struct tlv_parsed tp;
2486 struct gsm_mncc dtmf;
2487
2488 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2489 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002490 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 /* keypad facility */
2492 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2493 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2496 }
2497
Harald Welte596fed42009-07-23 19:06:52 +02002498 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2502{
2503 struct gsm_mncc *dtmf = arg;
2504 struct msgb *msg = gsm48_msgb_alloc();
2505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2506
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2508
2509 /* keypad */
2510 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002511 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002513 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2517{
2518 struct gsm_mncc *dtmf = arg;
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2523
2524 /* cause */
2525 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002526 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 else
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002530 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531}
2532
2533static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2534{
2535 struct msgb *msg = gsm48_msgb_alloc();
2536 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2537
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2539
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002540 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541}
2542
2543static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2544{
2545 struct gsm_mncc dtmf;
2546
2547 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2548 dtmf.callref = trans->callref;
2549
Harald Welte596fed42009-07-23 19:06:52 +02002550 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551}
2552
2553static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2554{
2555 struct gsm48_hdr *gh = msgb_l3(msg);
2556 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2557 struct tlv_parsed tp;
2558 struct gsm_mncc modify;
2559
2560 memset(&modify, 0, sizeof(struct gsm_mncc));
2561 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002562 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 /* bearer capability */
2564 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2565 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2568 }
2569
2570 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2571
Harald Welte596fed42009-07-23 19:06:52 +02002572 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573}
2574
2575static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2576{
2577 struct gsm_mncc *modify = arg;
2578 struct msgb *msg = gsm48_msgb_alloc();
2579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2580
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 gh->msg_type = GSM48_MT_CC_MODIFY;
2582
2583 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2584
2585 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002586 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587
2588 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2589
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002590 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591}
2592
2593static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2594{
2595 struct gsm48_hdr *gh = msgb_l3(msg);
2596 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2597 struct tlv_parsed tp;
2598 struct gsm_mncc modify;
2599
2600 gsm48_stop_cc_timer(trans);
2601
2602 memset(&modify, 0, sizeof(struct gsm_mncc));
2603 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002604 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 /* bearer capability */
2606 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2607 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002608 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2610 }
2611
2612 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2613
Harald Welte596fed42009-07-23 19:06:52 +02002614 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615}
2616
2617static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2618{
2619 struct gsm_mncc *modify = arg;
2620 struct msgb *msg = gsm48_msgb_alloc();
2621 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2622
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2624
2625 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627
2628 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2629
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002630 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631}
2632
2633static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2634{
2635 struct gsm48_hdr *gh = msgb_l3(msg);
2636 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2637 struct tlv_parsed tp;
2638 struct gsm_mncc modify;
2639
2640 gsm48_stop_cc_timer(trans);
2641
2642 memset(&modify, 0, sizeof(struct gsm_mncc));
2643 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002644 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 /* bearer capability */
2646 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2647 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002648 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2650 }
2651 /* cause */
2652 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2653 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002654 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2656 }
2657
2658 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2659
Harald Welte596fed42009-07-23 19:06:52 +02002660 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661}
2662
2663static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2664{
2665 struct gsm_mncc *modify = arg;
2666 struct msgb *msg = gsm48_msgb_alloc();
2667 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2668
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2670
2671 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002672 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
2676 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2677
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002678 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679}
2680
2681static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2682{
2683 struct gsm_mncc *notify = arg;
2684 struct msgb *msg = gsm48_msgb_alloc();
2685 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2686
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687 gh->msg_type = GSM48_MT_CC_NOTIFY;
2688
2689 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002690 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002692 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693}
2694
2695static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2696{
2697 struct gsm48_hdr *gh = msgb_l3(msg);
2698 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2699// struct tlv_parsed tp;
2700 struct gsm_mncc notify;
2701
2702 memset(&notify, 0, sizeof(struct gsm_mncc));
2703 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002704// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002706 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707
Harald Welte596fed42009-07-23 19:06:52 +02002708 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709}
2710
2711static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2712{
2713 struct gsm_mncc *user = arg;
2714 struct msgb *msg = gsm48_msgb_alloc();
2715 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2716
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 gh->msg_type = GSM48_MT_CC_USER_INFO;
2718
2719 /* user-user */
2720 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002721 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722 /* more data */
2723 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002724 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002726 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727}
2728
2729static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2730{
2731 struct gsm48_hdr *gh = msgb_l3(msg);
2732 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2733 struct tlv_parsed tp;
2734 struct gsm_mncc user;
2735
2736 memset(&user, 0, sizeof(struct gsm_mncc));
2737 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002738 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 /* user-user */
2740 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2741 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002742 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2744 }
2745 /* more data */
2746 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2747 user.more = 1;
2748
Harald Welte596fed42009-07-23 19:06:52 +02002749 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750}
2751
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002752static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753{
2754 struct gsm_mncc *mode = arg;
2755
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002756 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757}
2758
2759static struct downstate {
2760 u_int32_t states;
2761 int type;
2762 int (*rout) (struct gsm_trans *trans, void *arg);
2763} downstatelist[] = {
2764 /* mobile originating call establishment */
2765 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2766 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2767 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2768 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2769 {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 */
2770 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2771 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2772 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2773 /* mobile terminating call establishment */
2774 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2775 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2776 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2777 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2778 /* signalling during call */
2779 {SBIT(GSM_CSTATE_ACTIVE),
2780 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2781 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2782 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2783 {ALL_STATES,
2784 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2785 {ALL_STATES,
2786 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2787 {ALL_STATES,
2788 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2789 {SBIT(GSM_CSTATE_ACTIVE),
2790 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2791 {SBIT(GSM_CSTATE_ACTIVE),
2792 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2793 {SBIT(GSM_CSTATE_ACTIVE),
2794 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2795 {SBIT(GSM_CSTATE_ACTIVE),
2796 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2797 {SBIT(GSM_CSTATE_ACTIVE),
2798 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2799 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2800 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2801 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2802 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2803 {SBIT(GSM_CSTATE_ACTIVE),
2804 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2805 /* clearing */
2806 {SBIT(GSM_CSTATE_INITIATED),
2807 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2808 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2809 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2810 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2811 MNCC_REL_REQ, gsm48_cc_tx_release},
2812 /* special */
2813 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002814 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815};
2816
2817#define DOWNSLLEN \
2818 (sizeof(downstatelist) / sizeof(struct downstate))
2819
2820
2821int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2822{
Harald Welte1a6f7982009-08-09 18:52:33 +02002823 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002824 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002825 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 struct gsm_mncc *data = arg, rel;
2828
2829 /* handle special messages */
2830 switch(msg_type) {
2831 case MNCC_BRIDGE:
2832 return tch_bridge(net, arg);
2833 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002834 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002836 return tch_recv_mncc(net, data->callref, 1);
2837 case GSM_TCHF_FRAME:
2838 /* Find callref */
2839 trans = trans_find_by_callref(net, data->callref);
2840 if (!trans)
2841 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002842 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002843 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002844 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002845 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002846 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002847 switch (bts->type) {
2848 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002849 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002850 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002851 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002852 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002853 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002854 default:
2855 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2856 }
2857 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 }
2859
2860 memset(&rel, 0, sizeof(struct gsm_mncc));
2861 rel.callref = data->callref;
2862
2863 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002864 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865
2866 /* Callref unknown */
2867 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002868 struct gsm_subscriber *subscr;
2869
Harald Welte4a3464c2009-07-04 10:11:24 +02002870 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2872 "Received '%s' from MNCC with "
2873 "unknown callref %d\n", data->called.number,
2874 get_mncc_name(msg_type), data->callref);
2875 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002876 return mncc_release_ind(net, NULL, data->callref,
2877 GSM48_CAUSE_LOC_PRN_S_LU,
2878 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002880 if (!data->called.number[0] && !data->imsi[0]) {
2881 DEBUGP(DCC, "(bts - trx - ts - ti) "
2882 "Received '%s' from MNCC with "
2883 "no number or IMSI\n", get_mncc_name(msg_type));
2884 /* Invalid number */
2885 return mncc_release_ind(net, NULL, data->callref,
2886 GSM48_CAUSE_LOC_PRN_S_LU,
2887 GSM48_CC_CAUSE_INV_NR_FORMAT);
2888 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002890 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002891 subscr = subscr_get_by_extension(net,
2892 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002893 else
Harald Welte9176bd42009-07-23 18:46:00 +02002894 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895 /* If subscriber is not found */
2896 if (!subscr) {
2897 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2898 "Received '%s' from MNCC with "
2899 "unknown subscriber %s\n", data->called.number,
2900 get_mncc_name(msg_type), data->called.number);
2901 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002902 return mncc_release_ind(net, NULL, data->callref,
2903 GSM48_CAUSE_LOC_PRN_S_LU,
2904 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905 }
2906 /* If subscriber is not "attached" */
2907 if (!subscr->lac) {
2908 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2909 "Received '%s' from MNCC with "
2910 "detached subscriber %s\n", data->called.number,
2911 get_mncc_name(msg_type), data->called.number);
2912 subscr_put(subscr);
2913 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002914 return mncc_release_ind(net, NULL, data->callref,
2915 GSM48_CAUSE_LOC_PRN_S_LU,
2916 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 }
2918 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002919 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2920 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921 DEBUGP(DCC, "No memory for trans.\n");
2922 subscr_put(subscr);
2923 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002924 mncc_release_ind(net, NULL, data->callref,
2925 GSM48_CAUSE_LOC_PRN_S_LU,
2926 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 return -ENOMEM;
2928 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002930 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002931
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002933 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 /* find transaction with this subscriber already paging */
2935 llist_for_each_entry(transt, &net->trans_list, entry) {
2936 /* Transaction of our lchan? */
2937 if (transt == trans ||
2938 transt->subscr != subscr)
2939 continue;
2940 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2941 "Received '%s' from MNCC with "
2942 "unallocated channel, paging already "
2943 "started.\n", bts->nr,
2944 data->called.number,
2945 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002946 subscr_put(subscr);
2947 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 return 0;
2949 }
2950 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002951 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002952
2953 /* Get a channel */
2954 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2955
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002956 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 return 0;
2958 }
2959 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002960 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002961 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002963
2964 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002965 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966
2967 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002968 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002970 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 (trans->subscr)?(trans->subscr->extension):"-",
2972 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002973 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2974 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 if (msg_type == MNCC_REL_REQ)
2976 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2977 else
2978 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2979 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002980 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981 return rc;
2982 }
2983
2984 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2985 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002986 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002988 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002989 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002990 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991
2992 /* Find function for current state and message */
2993 for (i = 0; i < DOWNSLLEN; i++)
2994 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002995 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 break;
2997 if (i == DOWNSLLEN) {
2998 DEBUGP(DCC, "Message unhandled at this state.\n");
2999 return 0;
3000 }
3001
3002 rc = downstatelist[i].rout(trans, arg);
3003
3004 return rc;
3005}
3006
3007
3008static struct datastate {
3009 u_int32_t states;
3010 int type;
3011 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3012} datastatelist[] = {
3013 /* mobile originating call establishment */
3014 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3015 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3016 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3017 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3018 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3019 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3020 /* mobile terminating call establishment */
3021 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3022 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3023 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3024 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003025 {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 +08003026 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3027 /* signalling during call */
3028 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3029 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3030 {SBIT(GSM_CSTATE_ACTIVE),
3031 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3032 {ALL_STATES,
3033 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3034 {ALL_STATES,
3035 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3036 {ALL_STATES,
3037 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3038 {SBIT(GSM_CSTATE_ACTIVE),
3039 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3040 {SBIT(GSM_CSTATE_ACTIVE),
3041 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3042 {SBIT(GSM_CSTATE_ACTIVE),
3043 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3044 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3045 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3046 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3047 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3048 {SBIT(GSM_CSTATE_ACTIVE),
3049 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3050 /* clearing */
3051 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3052 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3053 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3054 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3055 {ALL_STATES, /* 5.4.3.4 */
3056 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3057};
3058
3059#define DATASLLEN \
3060 (sizeof(datastatelist) / sizeof(struct datastate))
3061
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003062static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003063{
3064 struct gsm48_hdr *gh = msgb_l3(msg);
3065 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003066 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003067 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003069
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 if (msg_type & 0x80) {
3071 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3072 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003073 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003074
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003076 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003077
Harald Welte6f5aee02009-07-23 21:21:14 +02003078 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003080 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003081 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003082 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3083 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084
3085 /* Create transaction */
3086 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003087 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 "creating new trans.\n", transaction_id);
3089 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003090 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003091 transaction_id, new_callref++);
3092 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003093 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003094 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003095 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 GSM48_MT_CC_RELEASE_COMPL);
3097 return -ENOMEM;
3098 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003100 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 }
3102
3103 /* find function for current state and message */
3104 for (i = 0; i < DATASLLEN; i++)
3105 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003106 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 break;
3108 if (i == DATASLLEN) {
3109 DEBUGP(DCC, "Message unhandled at this state.\n");
3110 return 0;
3111 }
3112
3113 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003114
3115 return rc;
3116}
3117
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003118/* Create a dummy to wait five seconds */
3119static void release_anchor(struct gsm_subscriber_connection *conn)
3120{
3121 if (!conn->anch_operation)
3122 return;
3123
3124 bsc_del_timer(&conn->anch_operation->timeout);
3125 talloc_free(conn->anch_operation);
3126 conn->anch_operation = NULL;
3127}
3128
3129static void anchor_timeout(void *_data)
3130{
3131 struct gsm_subscriber_connection *con = _data;
3132
3133 release_anchor(con);
3134 msc_release_connection(con);
3135}
3136
3137int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3138{
3139 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3140 if (!conn->anch_operation)
3141 return -1;
3142
3143 conn->anch_operation->timeout.data = conn;
3144 conn->anch_operation->timeout.cb = anchor_timeout;
3145 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3146 return 0;
3147}
3148
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003149/* here we get data from the BSC level... */
3150int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003151{
3152 struct gsm48_hdr *gh = msgb_l3(msg);
3153 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003154 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003155
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003156 if (silent_call_reroute(conn, msg))
3157 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003158
3159 switch (pdisc) {
3160 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003161 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003162 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003163 break;
3164 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003165 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003166 break;
3167 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003168 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003169 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003170 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003171 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003172 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003173 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003174 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003175 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003176 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3177 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003178 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003179 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003180 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003181 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003182 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003183 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003184 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3185 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003186 break;
3187 }
3188
3189 return rc;
3190}
Harald Welte8470bf22008-12-25 23:28:35 +00003191
Harald Welte805f6442009-07-28 18:25:29 +02003192/*
3193 * This will be ran by the linker when loading the DSO. We use it to
3194 * do system initialization, e.g. registration of signal handlers.
3195 */
3196static __attribute__((constructor)) void on_dso_load_0408(void)
3197{
Harald Welte805f6442009-07-28 18:25:29 +02003198 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3199}