blob: 94a620934d7c476bc26069e194cb1e28e74bab10 [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);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100319
320 /* We might need to cancel the paging response or such. */
321 if (conn->sec_operation && conn->sec_operation->cb) {
322 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
323 NULL, conn, conn->sec_operation->cb_data);
324 }
325
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800326 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800327 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000328
Harald Welte4bfdfe72009-06-10 23:11:52 +0800329 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200330 /* FIXME: this is not neccessarily the right thing to do, we should
331 * only set trans->lchan to NULL and wait for another lchan to be
332 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800333 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
334 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200335 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000337}
338
Harald Welte371efe52010-12-22 23:17:50 +0100339void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
340{
341 struct gsm_trans *trans, *temp;
342
343 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
344
345 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100346 if (trans->protocol == protocol) {
347 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100348 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100349 }
Harald Welte371efe52010-12-22 23:17:50 +0100350 }
351}
352
Holger Freyther429e7762008-12-30 13:28:30 +0000353/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800354int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000355{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800356 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800357 struct msgb *msg;
358
359 counter_inc(bts->network->stats.loc_upd_resp.reject);
360
361 msg = gsm48_create_loc_upd_rej(cause);
362 if (!msg) {
363 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
364 return -1;
365 }
Harald Welte52b1f982008-12-23 20:25:15 +0000366
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800367 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000368
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100369 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100370 "LAC=%u BTS=%u\n", conn->subscr ?
371 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800372 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100373
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800374 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000375}
376
377/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800378int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000379{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800380 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000381 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000382 struct gsm48_hdr *gh;
383 struct gsm48_loc_area_id *lai;
384 u_int8_t *mid;
385
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800386 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000387
388 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
389 gh->proto_discr = GSM48_PDISC_MM;
390 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
391
392 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100393 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000394 bts->network->network_code, bts->location_area_code);
395
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200396 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200397 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000398
399 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
400
Harald Welteffa55a42009-12-22 19:07:32 +0100401 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100402
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800403 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000404}
405
Harald Weltebf5e8df2009-02-03 12:59:45 +0000406/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800407static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000408{
409 struct msgb *msg = gsm48_msgb_alloc();
410 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000411
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800412 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000413
414 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
415 gh->proto_discr = GSM48_PDISC_MM;
416 gh->msg_type = GSM48_MT_MM_ID_REQ;
417 gh->data[0] = id_type;
418
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800419 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000420}
421
Harald Welte231ad4f2008-12-27 11:15:38 +0000422
Harald Weltebf5e8df2009-02-03 12:59:45 +0000423/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800424static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000425{
426 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000427 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200428 struct gsm_bts *bts = lchan->ts->trx->bts;
429 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000430 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200431 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000432
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200433 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000434 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000435 mi_type, mi_string);
436
Harald Welte7659de12009-12-13 12:39:18 +0100437 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
438
Harald Welte75a983f2008-12-27 21:34:06 +0000439 switch (mi_type) {
440 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200441 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100442 if (!conn->subscr) {
443 conn->subscr = subscr_get_by_imsi(net, mi_string);
444 if (!conn->subscr)
445 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200446 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100447 if (conn->loc_operation)
448 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000449 break;
450 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000451 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000452 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100453 if (conn->subscr) {
454 db_subscriber_assoc_imei(conn->subscr, mi_string);
455 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200456 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100457 if (conn->loc_operation)
458 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000459 break;
460 }
Holger Freyther73487a22008-12-31 18:53:57 +0000461
462 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100463 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000464}
465
Harald Welte255539c2008-12-28 02:26:27 +0000466
467static void loc_upd_rej_cb(void *data)
468{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100469 struct gsm_subscriber_connection *conn = data;
470 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100471 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000472
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800473 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800474 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000475}
476
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100477static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000478{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100479 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
480 conn->loc_operation->updating_timer.data = conn;
481 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000482}
483
Harald Welte2a139372009-02-22 21:14:55 +0000484static const char *lupd_name(u_int8_t type)
485{
486 switch (type) {
487 case GSM48_LUPD_NORMAL:
488 return "NORMAL";
489 case GSM48_LUPD_PERIODIC:
490 return "PEROIDOC";
491 case GSM48_LUPD_IMSI_ATT:
492 return "IMSI ATTACH";
493 default:
494 return "UNKNOWN";
495 }
496}
497
Harald Weltebf5e8df2009-02-03 12:59:45 +0000498/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800499static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000500{
Harald Welte8470bf22008-12-25 23:28:35 +0000501 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000502 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800503 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800504 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000505 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200506 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000507 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000508
Harald Welte8470bf22008-12-25 23:28:35 +0000509 lu = (struct gsm48_loc_upd_req *) gh->data;
510
511 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000512
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200513 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000514
Harald Weltea0368542009-06-27 02:58:43 +0200515 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000516 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000517
Harald Welte7659de12009-12-13 12:39:18 +0100518 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
519
Harald Welte24ff6ee2009-12-22 00:41:05 +0100520 switch (lu->type) {
521 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100522 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100523 break;
524 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100525 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100526 break;
527 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100528 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100529 break;
530 }
531
Holger Freythereaf04692009-06-06 13:54:44 +0000532 /*
533 * Pseudo Spoof detection: Just drop a second/concurrent
534 * location updating request.
535 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100536 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200537 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100538 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800539 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000540 return 0;
541 }
542
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800543 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000544
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200545 conn->loc_operation->key_seq = lu->key_seq;
546
Harald Welte52b1f982008-12-23 20:25:15 +0000547 switch (mi_type) {
548 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200549 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000550 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800551 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100552 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000553
Jan Luebbe370b41d2009-08-12 10:19:34 +0200554 /* look up subscriber based on IMSI, create if not found */
555 subscr = subscr_get_by_imsi(bts->network, mi_string);
556 if (!subscr) {
557 subscr = db_create_subscriber(bts->network, mi_string);
558 }
Harald Welte4b634542008-12-27 01:55:51 +0000559 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000560 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200561 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000562 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200563 subscr = subscr_get_by_tmsi(bts->network,
564 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000565 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000566 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800567 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100568 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000569 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200570 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800571 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200572 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000573 break;
574 case GSM_MI_TYPE_IMEI:
575 case GSM_MI_TYPE_IMEISV:
576 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200577 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000578 break;
579 default:
Harald Weltea0368542009-06-27 02:58:43 +0200580 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000581 break;
582 }
583
Harald Welte24516ea2009-07-04 10:18:00 +0200584 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100585 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200586
Harald Welte4bfdfe72009-06-10 23:11:52 +0800587 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200588 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 /* FIXME: request id? close channel? */
590 return -EINVAL;
591 }
592
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100593 conn->subscr = subscr;
594 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000595
Harald Welte24516ea2009-07-04 10:18:00 +0200596 /* check if we can let the subscriber into our network immediately
597 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100598 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000599}
600
Harald Welte4bfdfe72009-06-10 23:11:52 +0800601#if 0
602static u_int8_t to_bcd8(u_int8_t val)
603{
604 return ((val / 10) << 4) | (val % 10);
605}
606#endif
607
Harald Weltedb253af2008-12-30 17:56:55 +0000608/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800609int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000610{
611 struct msgb *msg = gsm48_msgb_alloc();
612 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800613 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000614 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200615 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800616#if 0
617 time_t cur_t;
618 struct tm* cur_time;
619 int tz15min;
620#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000621
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800622 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000623
624 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
625 gh->proto_discr = GSM48_PDISC_MM;
626 gh->msg_type = GSM48_MT_MM_INFO;
627
628 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200629#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000630 name_len = strlen(net->name_long);
631 /* 10.5.3.5a */
632 ptr8 = msgb_put(msg, 3);
633 ptr8[0] = GSM48_IE_NAME_LONG;
634 ptr8[1] = name_len*2 +1;
635 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
636
637 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
638 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000639 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000640
641 /* FIXME: Use Cell Broadcast, not UCS-2, since
642 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200643#endif
644 name_len = (strlen(net->name_long)*7)/8;
645 name_pad = (8 - strlen(net->name_long)*7)%8;
646 if (name_pad > 0)
647 name_len++;
648 /* 10.5.3.5a */
649 ptr8 = msgb_put(msg, 3);
650 ptr8[0] = GSM48_IE_NAME_LONG;
651 ptr8[1] = name_len +1;
652 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
653
654 ptr8 = msgb_put(msg, name_len);
655 gsm_7bit_encode(ptr8, net->name_long);
656
Harald Weltedb253af2008-12-30 17:56:55 +0000657 }
658
659 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200660#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000661 name_len = strlen(net->name_short);
662 /* 10.5.3.5a */
663 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200664 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000665 ptr8[1] = name_len*2 + 1;
666 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
667
Harald Weltee872cb12009-01-01 00:33:37 +0000668 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000669 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000670 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200671#endif
672 name_len = (strlen(net->name_short)*7)/8;
673 name_pad = (8 - strlen(net->name_short)*7)%8;
674 if (name_pad > 0)
675 name_len++;
676 /* 10.5.3.5a */
677 ptr8 = (u_int8_t *) msgb_put(msg, 3);
678 ptr8[0] = GSM48_IE_NAME_SHORT;
679 ptr8[1] = name_len +1;
680 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
681
682 ptr8 = msgb_put(msg, name_len);
683 gsm_7bit_encode(ptr8, net->name_short);
684
Harald Weltedb253af2008-12-30 17:56:55 +0000685 }
686
687#if 0
688 /* Section 10.5.3.9 */
689 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800690 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000691 ptr8 = msgb_put(msg, 8);
692 ptr8[0] = GSM48_IE_NET_TIME_TZ;
693 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
694 ptr8[2] = to_bcd8(cur_time->tm_mon);
695 ptr8[3] = to_bcd8(cur_time->tm_mday);
696 ptr8[4] = to_bcd8(cur_time->tm_hour);
697 ptr8[5] = to_bcd8(cur_time->tm_min);
698 ptr8[6] = to_bcd8(cur_time->tm_sec);
699 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
700 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800701 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000702 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000704#endif
705
Daniel Willmanneea93372009-08-13 03:42:07 +0200706 DEBUGP(DMM, "-> MM INFO\n");
707
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800708 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000709}
710
Harald Welte7984d5c2009-08-12 22:56:50 +0200711/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800712int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200713{
714 struct msgb *msg = gsm48_msgb_alloc();
715 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200716 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200717
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100718 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200719
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800720 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200721 gh->proto_discr = GSM48_PDISC_MM;
722 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
723
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100724 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200725
Harald Welte7984d5c2009-08-12 22:56:50 +0200726 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200727 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200728 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200729
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800730 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200731}
732
733/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800734int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200735{
736 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800737 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200738}
739
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800740static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000741{
Harald Welte4b634542008-12-27 01:55:51 +0000742 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800743 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000744}
Harald Welteba4cf162009-01-10 01:49:35 +0000745
746/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100747static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000748 enum gsm48_reject_value value)
749{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800750 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000751
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800752 msg = gsm48_create_mm_serv_rej(value);
753 if (!msg) {
754 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
755 return -1;
756 }
Harald Welteba4cf162009-01-10 01:49:35 +0000757
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800758 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100759 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800760 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000761}
762
Sylvain Munautba87f452009-12-24 00:28:46 +0100763static int _gsm48_rx_mm_serv_req_sec_cb(
764 unsigned int hooknum, unsigned int event,
765 struct msgb *msg, void *data, void *param)
766{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800767 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100768 int rc = 0;
769
770 switch (event) {
771 case GSM_SECURITY_AUTH_FAILED:
772 /* Nothing to do */
773 break;
774
775 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800776 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100777 break;
778
779 case GSM_SECURITY_SUCCEEDED:
780 /* nothing to do. CIPHER MODE COMMAND is
781 * implicit CM SERV ACK */
782 break;
783
784 default:
785 rc = -EINVAL;
786 };
787
788 return rc;
789}
790
Harald Welte4ed0e922009-01-10 03:17:30 +0000791/*
792 * Handle CM Service Requests
793 * a) Verify that the packet is long enough to contain the information
794 * we require otherwsie reject with INCORRECT_MESSAGE
795 * b) Try to parse the TMSI. If we do not have one reject
796 * c) Check that we know the subscriber with the TMSI otherwise reject
797 * with a HLR cause
798 * d) Set the subscriber on the gsm_lchan and accept
799 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800800static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000801{
Harald Welteba4cf162009-01-10 01:49:35 +0000802 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200803 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000804
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800805 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000806 struct gsm_subscriber *subscr;
807 struct gsm48_hdr *gh = msgb_l3(msg);
808 struct gsm48_service_request *req =
809 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800810 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000811 u_int8_t classmark2_len = gh->data[1];
812 u_int8_t *classmark2 = gh->data+2;
813 u_int8_t mi_len = *(classmark2 + classmark2_len);
814 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000815
Harald Weltec9e02182009-05-01 19:07:53 +0000816 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000817 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000818 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800819 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000820 GSM48_REJECT_INCORRECT_MESSAGE);
821 }
822
823 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000824 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800825 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000826 GSM48_REJECT_INCORRECT_MESSAGE);
827 }
828
Harald Weltec9e02182009-05-01 19:07:53 +0000829 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000830 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000831 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800832 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000833 GSM48_REJECT_INCORRECT_MESSAGE);
834 }
835
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200836 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000837 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000838 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000839
Harald Welte7659de12009-12-13 12:39:18 +0100840 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
841
Harald Welte3ac7f102009-08-10 10:12:45 +0200842 if (is_siemens_bts(bts))
843 send_siemens_mrpci(msg->lchan, classmark2-1);
844
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200845 subscr = subscr_get_by_tmsi(bts->network,
846 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000847
Harald Welte2a139372009-02-22 21:14:55 +0000848 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000849 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800850 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000851 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
852
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800853 if (!conn->subscr)
854 conn->subscr = subscr;
855 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100856 subscr_put(subscr); /* lchan already has a ref, don't need another one */
857 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000858 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
859 subscr_put(subscr);
860 }
861
Harald Weltec2e302d2009-07-05 14:08:13 +0200862 subscr->equipment.classmark2_len = classmark2_len;
863 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
864 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000865
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800866 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100867 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000868}
869
Harald Welte2a139372009-02-22 21:14:55 +0000870static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
871{
Harald Welte9176bd42009-07-23 18:46:00 +0200872 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000873 struct gsm48_hdr *gh = msgb_l3(msg);
874 struct gsm48_imsi_detach_ind *idi =
875 (struct gsm48_imsi_detach_ind *) gh->data;
876 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200877 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800878 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000879
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200880 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000881 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
882 mi_type, mi_string);
883
Harald Welteffa55a42009-12-22 19:07:32 +0100884 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100885
Harald Welte2a139372009-02-22 21:14:55 +0000886 switch (mi_type) {
887 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200888 subscr = subscr_get_by_tmsi(bts->network,
889 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000890 break;
891 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200892 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000893 break;
894 case GSM_MI_TYPE_IMEI:
895 case GSM_MI_TYPE_IMEISV:
896 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000897 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000898 break;
899 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000900 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000901 break;
902 }
903
Holger Freyther4a49e772009-04-12 05:37:29 +0000904 if (subscr) {
905 subscr_update(subscr, msg->trx->bts,
906 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100907 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200908
909 subscr->equipment.classmark1 = idi->classmark1;
910 db_sync_equipment(&subscr->equipment);
911
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000912 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000913 } else
Harald Welte2a139372009-02-22 21:14:55 +0000914 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
915
Harald Welte31981a02009-12-20 09:58:40 +0100916 /* FIXME: iterate over all transactions and release them,
917 * imagine an IMSI DETACH happening during an active call! */
918
Harald Welteb83d9382009-12-12 21:00:48 +0100919 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000920 return 0;
921}
922
Harald Welted2a7f5a2009-06-05 20:08:20 +0000923static int gsm48_rx_mm_status(struct msgb *msg)
924{
925 struct gsm48_hdr *gh = msgb_l3(msg);
926
927 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
928
929 return 0;
930}
931
Sylvain Munaut30a15382009-12-24 00:27:26 +0100932/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800933static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100934{
935 struct gsm48_hdr *gh = msgb_l3(msg);
936 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800937 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100938
Sylvain Munautc593cf12010-06-10 23:51:41 +0200939 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100940 hexdump(ar->sres, 4));
941
942 /* Safety check */
943 if (!conn->sec_operation) {
944 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
945 return -EIO;
946 }
947
948 /* Validate SRES */
949 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100950 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100951 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200952
953 DEBUGPC(DMM, "Invalid (expected %s)\n",
954 hexdump(conn->sec_operation->atuple.sres, 4));
955
Sylvain Munaut30a15382009-12-24 00:27:26 +0100956 if (cb)
957 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800958 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100959
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100960 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100961 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100962 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100963 }
964
Sylvain Munautc593cf12010-06-10 23:51:41 +0200965 DEBUGPC(DMM, "OK\n");
966
Sylvain Munaut30a15382009-12-24 00:27:26 +0100967 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100968 return gsm0808_cipher_mode(conn, net->a5_encryption,
969 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100970}
971
Harald Weltebf5e8df2009-02-03 12:59:45 +0000972/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800973static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000974{
975 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800976 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000977
978 switch (gh->msg_type & 0xbf) {
979 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200980 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800981 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000982 break;
983 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800984 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000985 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000986 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800987 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000988 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000989 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000990 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000991 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000992 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000993 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800994 conn->subscr ?
995 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000996 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100997 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +0000998 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000999 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001000 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1001 break;
1002 case GSM48_MT_MM_CM_REEST_REQ:
1003 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1004 break;
1005 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001006 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001007 break;
1008 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001009 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001010 gh->msg_type);
1011 break;
1012 }
1013
1014 return rc;
1015}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001016
Harald Welte2d35ae62009-02-06 12:02:13 +00001017/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001018static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001019{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001020 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001021 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001022 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +00001023 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001024 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001025 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001026 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001027 int rc = 0;
1028
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001029 resp = (struct gsm48_pag_resp *) &gh->data[0];
1030 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1031 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001032 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1033 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001034
Harald Weltefe18d8f2009-02-22 21:14:24 +00001035 switch (mi_type) {
1036 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001037 subscr = subscr_get_by_tmsi(bts->network,
1038 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001039 break;
1040 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001041 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001042 break;
1043 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001044
1045 if (!subscr) {
1046 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001047 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001048 return -EINVAL;
1049 }
1050 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001051 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001052
Harald Weltec2e302d2009-07-05 14:08:13 +02001053 subscr->equipment.classmark2_len = *classmark2_lv;
1054 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1055 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001056
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001057 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001058 return rc;
1059}
1060
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001061static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001062{
1063 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001064 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001065 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1066 u_int8_t cm2_len, cm3_len = 0;
1067 u_int8_t *cm2, *cm3 = NULL;
1068
1069 DEBUGP(DRR, "CLASSMARK CHANGE ");
1070
1071 /* classmark 2 */
1072 cm2_len = gh->data[0];
1073 cm2 = &gh->data[1];
1074 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1075
1076 if (payload_len > cm2_len + 1) {
1077 /* we must have a classmark3 */
1078 if (gh->data[cm2_len+1] != 0x20) {
1079 DEBUGPC(DRR, "ERR CM3 TAG\n");
1080 return -EINVAL;
1081 }
1082 if (cm2_len > 3) {
1083 DEBUGPC(DRR, "CM2 too long!\n");
1084 return -EINVAL;
1085 }
1086
1087 cm3_len = gh->data[cm2_len+2];
1088 cm3 = &gh->data[cm2_len+3];
1089 if (cm3_len > 14) {
1090 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1091 return -EINVAL;
1092 }
1093 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1094 }
1095 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001096 subscr->equipment.classmark2_len = cm2_len;
1097 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001098 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001099 subscr->equipment.classmark3_len = cm3_len;
1100 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001101 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001102 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001103 }
1104
Harald Weltef7c43522009-06-09 20:24:21 +00001105 return 0;
1106}
1107
Harald Weltecf5b3592009-05-01 18:28:42 +00001108static int gsm48_rx_rr_status(struct msgb *msg)
1109{
1110 struct gsm48_hdr *gh = msgb_l3(msg);
1111
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001112 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001113 rr_cause_name(gh->data[0]));
1114
1115 return 0;
1116}
1117
Harald Weltef7c43522009-06-09 20:24:21 +00001118static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1119{
Harald Welted12b0fd2009-12-15 21:36:05 +01001120 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001121
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001122 /* This shouldn't actually end up here, as RSL treats
1123 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1124 * directly into gsm48_parse_meas_rep */
1125 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001126 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001127
1128 return 0;
1129}
1130
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001131static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001132{
1133 struct gsm48_hdr *gh = msgb_l3(msg);
1134 u_int8_t apdu_id_flags;
1135 u_int8_t apdu_len;
1136 u_int8_t *apdu_data;
1137
1138 apdu_id_flags = gh->data[0];
1139 apdu_len = gh->data[1];
1140 apdu_data = gh->data+2;
1141
1142 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1143 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1144
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001146}
1147
Sylvain Munaut30a15382009-12-24 00:27:26 +01001148/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001149static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001150{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001151 gsm_cbfn *cb;
1152 int rc = 0;
1153
1154 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1155
1156 /* Safety check */
1157 if (!conn->sec_operation) {
1158 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1159 return -EIO;
1160 }
1161
1162 /* FIXME: check for MI (if any) */
1163
1164 /* Call back whatever was in progress (if anything) ... */
1165 cb = conn->sec_operation->cb;
1166 if (cb) {
1167 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001168 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001169 }
1170
1171 /* Complete the operation */
1172 release_security_operation(conn);
1173
1174 return rc;
1175}
1176
Harald Welted011e8b2009-11-29 22:45:52 +01001177/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001178static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001179{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001180 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001181 struct gsm48_hdr *gh = msgb_l3(msg);
1182
1183 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1184 rr_cause_name(gh->data[0]));
1185
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001186 sig.lchan = msg->lchan;
1187 sig.mr = NULL;
1188 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001189 /* FIXME: release old channel */
1190
1191 return 0;
1192}
1193
1194/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001195static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001196{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001197 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001198 struct gsm48_hdr *gh = msgb_l3(msg);
1199
1200 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1201 rr_cause_name(gh->data[0]));
1202
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001203 sig.lchan = msg->lchan;
1204 sig.mr = NULL;
1205 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001206 /* FIXME: release allocated new channel */
1207
1208 return 0;
1209}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210
Harald Weltebf5e8df2009-02-03 12:59:45 +00001211/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001212static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001213{
1214 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001215 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001216
1217 switch (gh->msg_type) {
1218 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001219 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001220 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001221 case GSM48_MT_RR_GPRS_SUSP_REQ:
1222 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1223 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001224 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001225 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001226 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001227 case GSM48_MT_RR_STATUS:
1228 rc = gsm48_rx_rr_status(msg);
1229 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001230 case GSM48_MT_RR_MEAS_REP:
1231 rc = gsm48_rx_rr_meas_rep(msg);
1232 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001233 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001234 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001235 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001236 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001237 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001238 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001239 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001240 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001241 break;
1242 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001243 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001244 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001245 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001246 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1247 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001248 break;
1249 }
1250
Harald Welte2d35ae62009-02-06 12:02:13 +00001251 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001252}
1253
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001254int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001255 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001256{
1257 struct msgb *msg = gsm48_msgb_alloc();
1258 struct gsm48_hdr *gh;
1259
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001260 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261
1262 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1263 apdu_id, apdu_len);
1264
1265 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1266 gh->proto_discr = GSM48_PDISC_RR;
1267 gh->msg_type = GSM48_MT_RR_APP_INFO;
1268 gh->data[0] = apdu_id;
1269 gh->data[1] = apdu_len;
1270 memcpy(gh->data+2, apdu, apdu_len);
1271
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001272 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001273}
1274
Harald Welte4bc90a12008-12-27 16:32:52 +00001275/* Call Control */
1276
Harald Welte7584aea2009-02-11 11:44:12 +00001277/* The entire call control code is written in accordance with Figure 7.10c
1278 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1279 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1280 * it for voice */
1281
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282static void new_cc_state(struct gsm_trans *trans, int state)
1283{
1284 if (state > 31 || state < 0)
1285 return;
1286
1287 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001288 gsm48_cc_state_name(trans->cc.state),
1289 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290
Harald Weltedcaf5652009-07-23 18:56:43 +02001291 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292}
1293
1294static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001295{
1296 struct msgb *msg = gsm48_msgb_alloc();
1297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1298 u_int8_t *cause, *call_state;
1299
Harald Welte4bc90a12008-12-27 16:32:52 +00001300 gh->msg_type = GSM48_MT_CC_STATUS;
1301
1302 cause = msgb_put(msg, 3);
1303 cause[0] = 2;
1304 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1305 cause[2] = 0x80 | 30; /* response to status inquiry */
1306
1307 call_state = msgb_put(msg, 1);
1308 call_state[0] = 0xc0 | 0x00;
1309
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001310 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001311}
1312
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001313static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001314 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001315{
1316 struct msgb *msg = gsm48_msgb_alloc();
1317 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1318
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001319 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001320
Harald Welte6f4b7532008-12-29 00:39:37 +00001321 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001322 gh->msg_type = msg_type;
1323
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001324 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001325}
1326
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1328{
Harald Weltedcaf5652009-07-23 18:56:43 +02001329 if (bsc_timer_pending(&trans->cc.timer)) {
1330 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1331 bsc_del_timer(&trans->cc.timer);
1332 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333 }
1334}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001335
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1337 int msg_type, struct gsm_mncc *mncc)
1338{
1339 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001340 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341
1342 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001343 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001344 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001346 trans->conn->lchan->ts->trx->bts->nr,
1347 trans->conn->lchan->ts->trx->nr,
1348 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349 (trans->subscr)?(trans->subscr->extension):"-",
1350 get_mncc_name(msg_type));
1351 else
1352 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1353 "Sending '%s' to MNCC.\n",
1354 (trans->subscr)?(trans->subscr->extension):"-",
1355 get_mncc_name(msg_type));
1356 else
1357 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1358 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1359
1360 mncc->msg_type = msg_type;
1361
Harald Welte966636f2009-06-26 19:39:35 +02001362 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 if (!msg)
1364 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001365
1366 data = msgb_put(msg, sizeof(struct gsm_mncc));
1367 memcpy(data, mncc, sizeof(struct gsm_mncc));
1368
Harald Welte54209c22010-12-22 23:43:29 +01001369 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370
1371 return 0;
1372}
1373
1374int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1375 u_int32_t callref, int location, int value)
1376{
1377 struct gsm_mncc rel;
1378
Harald Welte92f70c52009-06-12 01:54:08 +08001379 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001381 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1383}
1384
Harald Weltedcaf5652009-07-23 18:56:43 +02001385/* Call Control Specific transaction release.
1386 * gets called by trans_free, DO NOT CALL YOURSELF! */
1387void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 gsm48_stop_cc_timer(trans);
1390
1391 /* send release to L4, if callref still exists */
1392 if (trans->callref) {
1393 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001394 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001395 GSM48_CAUSE_LOC_PRN_S_LU,
1396 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001398 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001400 if (trans->conn)
1401 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402}
1403
1404static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001405
Harald Welte09e38af2009-02-16 22:52:23 +00001406/* call-back from paging the B-end of the connection */
1407static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001408 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001409{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001410 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 struct gsm_subscriber *subscr = param;
1412 struct gsm_trans *transt, *tmp;
1413 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001414
Harald Welte09e38af2009-02-16 22:52:23 +00001415 if (hooknum != GSM_HOOK_RR_PAGING)
1416 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001417
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 if (!subscr)
1419 return -EINVAL;
1420 net = subscr->net;
1421 if (!net) {
1422 DEBUGP(DCC, "Error Network not set!\n");
1423 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001424 }
Harald Welte7584aea2009-02-11 11:44:12 +00001425
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 /* check all tranactions (without lchan) for subscriber */
1427 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001428 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429 continue;
1430 switch (event) {
1431 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001432 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433 break;
1434 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1435 subscr->extension);
1436 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001437 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001438 transt->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439 }
1440 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001441 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001442 break;
1443 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001444 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 DEBUGP(DCC, "Paging subscr %s expired!\n",
1446 subscr->extension);
1447 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001448 mncc_release_ind(transt->subscr->net, transt,
1449 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001450 GSM48_CAUSE_LOC_PRN_S_LU,
1451 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001453 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001454 break;
1455 }
1456 }
Harald Welte09e38af2009-02-16 22:52:23 +00001457 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001458}
Harald Welte7584aea2009-02-11 11:44:12 +00001459
Harald Welteda7ab742009-12-19 22:23:05 +01001460static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1461
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001462/* handle audio path for handover */
1463static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1464 void *handler_data, void *signal_data)
1465{
1466 struct rtp_socket *old_rs, *new_rs, *other_rs;
1467 struct ho_signal_data *sig = signal_data;
1468
1469 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1470 return 0;
1471
1472 if (ipacc_rtp_direct) {
1473 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1474 return 0;
1475 }
1476
1477 /* RTP Proxy mode */
1478 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1479 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1480
1481 if (!new_rs) {
1482 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1483 return -EIO;
1484 }
1485
1486 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1487
1488 if (!old_rs) {
1489 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1490 return -EIO;
1491 }
1492
1493 /* copy rx_action and reference to other sock */
1494 new_rs->rx_action = old_rs->rx_action;
1495 new_rs->tx_action = old_rs->tx_action;
1496 new_rs->transmit = old_rs->transmit;
1497
1498 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1499 case RTP_PROXY:
1500 other_rs = old_rs->proxy.other_sock;
1501 rtp_socket_proxy(new_rs, other_rs);
1502 /* delete reference to other end socket to prevent
1503 * rtp_socket_free() from removing the inverse reference */
1504 old_rs->proxy.other_sock = NULL;
1505 break;
1506 case RTP_RECV_UPSTREAM:
1507 new_rs->receive = old_rs->receive;
1508 break;
1509 case RTP_NONE:
1510 break;
1511 }
1512
1513 return 0;
1514}
1515
Harald Welte805f6442009-07-28 18:25:29 +02001516/* some other part of the code sends us a signal */
1517static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1518 void *handler_data, void *signal_data)
1519{
1520 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001521 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001522 struct gsm_network *net;
1523 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001524
1525 if (subsys != SS_ABISIP)
1526 return 0;
1527
1528 /* in case we use direct BTS-to-BTS RTP */
1529 if (ipacc_rtp_direct)
1530 return 0;
1531
Harald Welte805f6442009-07-28 18:25:29 +02001532 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001533 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001534 /* in case we don't use direct BTS-to-BTS RTP */
1535 /* the BTS has successfully bound a TCH to a local ip/port,
1536 * which means we can connect our UDP socket to it */
1537 if (lchan->abis_ip.rtp_socket) {
1538 rtp_socket_free(lchan->abis_ip.rtp_socket);
1539 lchan->abis_ip.rtp_socket = NULL;
1540 }
1541
1542 lchan->abis_ip.rtp_socket = rtp_socket_create();
1543 if (!lchan->abis_ip.rtp_socket)
1544 return -EIO;
1545
1546 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1547 lchan->abis_ip.bound_ip,
1548 lchan->abis_ip.bound_port);
1549 if (rc < 0)
1550 return -EIO;
1551
Harald Welteda7ab742009-12-19 22:23:05 +01001552 /* check if any transactions on this lchan still have
1553 * a tch_recv_mncc request pending */
1554 net = lchan->ts->trx->bts->network;
1555 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001556 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001557 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1558 tch_recv_mncc(net, trans->callref, 1);
1559 }
1560 }
Harald Welte805f6442009-07-28 18:25:29 +02001561 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001562 case S_ABISIP_DLCX_IND:
1563 /* the BTS tells us a RTP stream has been disconnected */
1564 if (lchan->abis_ip.rtp_socket) {
1565 rtp_socket_free(lchan->abis_ip.rtp_socket);
1566 lchan->abis_ip.rtp_socket = NULL;
1567 }
1568
1569 break;
Harald Welte805f6442009-07-28 18:25:29 +02001570 }
1571
1572 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001573}
1574
Harald Welte49f48b82009-02-17 15:29:33 +00001575/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001576static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001577{
Harald Welte11fa29c2009-02-19 17:24:39 +00001578 struct gsm_bts *bts = lchan->ts->trx->bts;
1579 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001580 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001581
Harald Welte11fa29c2009-02-19 17:24:39 +00001582 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1583 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1584 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1585
1586 if (bts->type != remote_bts->type) {
1587 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1588 return -EINVAL;
1589 }
Harald Welteda7ab742009-12-19 22:23:05 +01001590
1591 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001592 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001593 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001594 if (!ipacc_rtp_direct) {
1595 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001596 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001597 if (rc < 0)
1598 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001599 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001600#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001601
1602 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301603 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1604 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001605 } else {
1606 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301607 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1608 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301609 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001610 if (rc < 0)
1611 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301612 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1613 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301614 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001615 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001616 break;
1617 case GSM_BTS_TYPE_BS11:
1618 trau_mux_map_lchan(lchan, remote_lchan);
1619 break;
1620 default:
1621 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001622 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001623 }
Harald Welte49f48b82009-02-17 15:29:33 +00001624
1625 return 0;
1626}
1627
Harald Welte4bfdfe72009-06-10 23:11:52 +08001628/* bridge channels of two transactions */
1629static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001630{
Harald Weltedcaf5652009-07-23 18:56:43 +02001631 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1632 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001633
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001635 return -EIO;
1636
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001637 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638 return -EIO;
1639
1640 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001641 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001642}
1643
Harald Welteda7ab742009-12-19 22:23:05 +01001644/* enable receive of channels to MNCC upqueue */
1645static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646{
1647 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001648 struct gsm_lchan *lchan;
1649 struct gsm_bts *bts;
1650 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001651
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001653 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 if (!trans)
1655 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001656 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001658 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001659 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660
Harald Welteda7ab742009-12-19 22:23:05 +01001661 switch (bts->type) {
1662 case GSM_BTS_TYPE_NANOBTS:
1663 if (ipacc_rtp_direct) {
1664 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1665 return -EINVAL;
1666 }
1667 /* in case, we don't have a RTP socket yet, we note this
1668 * in the transaction and try later */
1669 if (!lchan->abis_ip.rtp_socket) {
1670 trans->tch_recv = enable;
1671 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1672 return 0;
1673 }
1674 if (enable) {
1675 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001676 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001677 if (rc < 0)
1678 return rc;
1679 /* assign socket to application interface */
1680 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1681 net, callref);
1682 } else
1683 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1684 net, 0);
1685 break;
1686 case GSM_BTS_TYPE_BS11:
1687 if (enable)
1688 return trau_recv_lchan(lchan, callref);
1689 return trau_mux_unmap(NULL, callref);
1690 break;
1691 default:
1692 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1693 return -EINVAL;
1694 }
1695
1696 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697}
1698
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1700{
1701 DEBUGP(DCC, "-> STATUS ENQ\n");
1702 return gsm48_cc_tx_status(trans, msg);
1703}
1704
1705static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1706static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1707
1708static void gsm48_cc_timeout(void *arg)
1709{
1710 struct gsm_trans *trans = arg;
1711 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001712 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1713 int mo_location = GSM48_CAUSE_LOC_USER;
1714 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1715 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 struct gsm_mncc mo_rel, l4_rel;
1717
1718 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1719 mo_rel.callref = trans->callref;
1720 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1721 l4_rel.callref = trans->callref;
1722
Harald Weltedcaf5652009-07-23 18:56:43 +02001723 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 case 0x303:
1725 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001726 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 break;
1728 case 0x310:
1729 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001730 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 break;
1732 case 0x313:
1733 disconnect = 1;
1734 /* unknown, did not find it in the specs */
1735 break;
1736 case 0x301:
1737 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001738 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 break;
1740 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001741 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001743 gsm48_cc_tx_release(trans, &trans->cc.msg);
1744 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 break; /* stay in release state */
1746 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001747 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 return;
1749// release = 1;
1750// l4_cause = 14;
1751// break;
1752 case 0x306:
1753 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001754 mo_cause = trans->cc.msg.cause.value;
1755 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 break;
1757 case 0x323:
1758 disconnect = 1;
1759 break;
1760 default:
1761 release = 1;
1762 }
1763
1764 if (release && trans->callref) {
1765 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001766 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 l4_location, l4_cause);
1768 trans->callref = 0;
1769 }
1770
1771 if (disconnect && trans->callref) {
1772 /* process disconnect towards layer 4 */
1773 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001774 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 }
1776
1777 /* process disconnect towards mobile station */
1778 if (disconnect || release) {
1779 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001780 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1781 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1782 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 mo_rel.cause.diag_len = 3;
1784
1785 if (disconnect)
1786 gsm48_cc_tx_disconnect(trans, &mo_rel);
1787 if (release)
1788 gsm48_cc_tx_release(trans, &mo_rel);
1789 }
1790
1791}
1792
1793static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1794 int sec, int micro)
1795{
1796 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001797 trans->cc.timer.cb = gsm48_cc_timeout;
1798 trans->cc.timer.data = trans;
1799 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1800 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801}
1802
1803static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1804{
1805 struct gsm48_hdr *gh = msgb_l3(msg);
1806 u_int8_t msg_type = gh->msg_type & 0xbf;
1807 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1808 struct tlv_parsed tp;
1809 struct gsm_mncc setup;
1810
1811 memset(&setup, 0, sizeof(struct gsm_mncc));
1812 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001813 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 /* emergency setup is identified by msg_type */
1815 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1816 setup.emergency = 1;
1817
1818 /* use subscriber as calling party number */
1819 if (trans->subscr) {
1820 setup.fields |= MNCC_F_CALLING;
1821 strncpy(setup.calling.number, trans->subscr->extension,
1822 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001823 strncpy(setup.imsi, trans->subscr->imsi,
1824 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 }
1826 /* bearer capability */
1827 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1828 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001829 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1831 }
1832 /* facility */
1833 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1834 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001835 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1837 }
1838 /* called party bcd number */
1839 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1840 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001841 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1843 }
1844 /* user-user */
1845 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1846 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001847 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1849 }
1850 /* ss-version */
1851 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1852 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1855 }
1856 /* CLIR suppression */
1857 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1858 setup.clir.sup = 1;
1859 /* CLIR invocation */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1861 setup.clir.inv = 1;
1862 /* cc cap */
1863 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1864 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1867 }
1868
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 new_cc_state(trans, GSM_CSTATE_INITIATED);
1870
Harald Welte (local)47df3992009-12-26 19:45:03 +01001871 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1872 subscr_name(trans->subscr), trans->subscr->extension,
1873 setup.called.number);
1874
Harald Weltea29e43a2010-12-24 16:06:33 +01001875 counter_inc(trans->subscr->net->stats.call.mo_setup);
1876
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001878 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879
Harald Welte13cac662009-07-29 12:10:35 +02001880 /* MNCC code will modify the channel asynchronously, we should
1881 * ipaccess-bind only after the modification has been made to the
1882 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 return 0;
1884}
1885
1886static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001887{
1888 struct msgb *msg = gsm48_msgb_alloc();
1889 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001891 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001892
Harald Welte7ccf7782009-02-17 01:43:01 +00001893 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001894
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 /* transaction id must not be assigned */
1896 if (trans->transaction_id != 0xff) { /* unasssigned */
1897 DEBUGP(DCC, "TX Setup with assigned transaction. "
1898 "This is not allowed!\n");
1899 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001900 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001901 GSM48_CAUSE_LOC_PRN_S_LU,
1902 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001904 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 return rc;
1906 }
1907
1908 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001909 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1910 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001912 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001913 GSM48_CAUSE_LOC_PRN_S_LU,
1914 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001916 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 return rc;
1918 }
Harald Welte78283ef2009-07-23 21:36:44 +02001919 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001920
Harald Welte65e74cc2008-12-29 01:55:35 +00001921 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001922
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001924
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 /* bearer capability */
1926 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 /* facility */
1929 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 /* progress */
1932 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001933 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934 /* calling party BCD number */
1935 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001936 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 /* called party BCD number */
1938 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001939 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 /* user-user */
1941 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* redirecting party BCD number */
1944 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001945 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* signal */
1947 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001948 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949
1950 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001951
Harald Weltea29e43a2010-12-24 16:06:33 +01001952 counter_inc(trans->subscr->net->stats.call.mt_setup);
1953
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001954 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001955}
1956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1958{
1959 struct gsm48_hdr *gh = msgb_l3(msg);
1960 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1961 struct tlv_parsed tp;
1962 struct gsm_mncc call_conf;
1963
1964 gsm48_stop_cc_timer(trans);
1965 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1966
1967 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1968 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001969 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970#if 0
1971 /* repeat */
1972 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1973 call_conf.repeat = 1;
1974 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1975 call_conf.repeat = 2;
1976#endif
1977 /* bearer capability */
1978 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1979 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001980 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1982 }
1983 /* cause */
1984 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1985 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001986 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1988 }
1989 /* cc cap */
1990 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1991 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001992 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1994 }
1995
1996 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1997
Harald Welte596fed42009-07-23 19:06:52 +02001998 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1999 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000}
2001
2002static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2003{
2004 struct gsm_mncc *proceeding = arg;
2005 struct msgb *msg = gsm48_msgb_alloc();
2006 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2007
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2009
2010 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2011
2012 /* bearer capability */
2013 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002014 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 /* facility */
2016 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002017 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 /* progress */
2019 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002020 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002022 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023}
2024
2025static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2026{
2027 struct gsm48_hdr *gh = msgb_l3(msg);
2028 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2029 struct tlv_parsed tp;
2030 struct gsm_mncc alerting;
2031
2032 gsm48_stop_cc_timer(trans);
2033 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2034
2035 memset(&alerting, 0, sizeof(struct gsm_mncc));
2036 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002037 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 /* facility */
2039 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2040 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002041 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2043 }
2044
2045 /* progress */
2046 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2047 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2050 }
2051 /* ss-version */
2052 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2053 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2056 }
2057
2058 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2059
Harald Welte596fed42009-07-23 19:06:52 +02002060 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2061 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062}
2063
2064static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2065{
2066 struct gsm_mncc *alerting = arg;
2067 struct msgb *msg = gsm48_msgb_alloc();
2068 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2069
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 gh->msg_type = GSM48_MT_CC_ALERTING;
2071
2072 /* facility */
2073 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002074 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 /* progress */
2076 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002077 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 /* user-user */
2079 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002080 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081
2082 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2083
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002084 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085}
2086
2087static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2088{
2089 struct gsm_mncc *progress = arg;
2090 struct msgb *msg = gsm48_msgb_alloc();
2091 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2092
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 gh->msg_type = GSM48_MT_CC_PROGRESS;
2094
2095 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 /* user-user */
2098 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002101 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102}
2103
2104static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2105{
2106 struct gsm_mncc *connect = arg;
2107 struct msgb *msg = gsm48_msgb_alloc();
2108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2109
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 gh->msg_type = GSM48_MT_CC_CONNECT;
2111
2112 gsm48_stop_cc_timer(trans);
2113 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2114
2115 /* facility */
2116 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 /* progress */
2119 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002120 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 /* connected number */
2122 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 /* user-user */
2125 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002126 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127
2128 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2129
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002130 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131}
2132
2133static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2134{
2135 struct gsm48_hdr *gh = msgb_l3(msg);
2136 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2137 struct tlv_parsed tp;
2138 struct gsm_mncc connect;
2139
2140 gsm48_stop_cc_timer(trans);
2141
2142 memset(&connect, 0, sizeof(struct gsm_mncc));
2143 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002144 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 /* use subscriber as connected party number */
2146 if (trans->subscr) {
2147 connect.fields |= MNCC_F_CONNECTED;
2148 strncpy(connect.connected.number, trans->subscr->extension,
2149 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002150 strncpy(connect.imsi, trans->subscr->imsi,
2151 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 }
2153 /* facility */
2154 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2155 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002156 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2158 }
2159 /* user-user */
2160 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2161 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2164 }
2165 /* ss-version */
2166 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2167 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2170 }
2171
2172 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltea29e43a2010-12-24 16:06:33 +01002173 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174
Harald Welte596fed42009-07-23 19:06:52 +02002175 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176}
2177
2178
2179static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2180{
2181 struct gsm_mncc connect_ack;
2182
2183 gsm48_stop_cc_timer(trans);
2184
2185 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltea29e43a2010-12-24 16:06:33 +01002186 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187
2188 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2189 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002190
Harald Welte596fed42009-07-23 19:06:52 +02002191 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 &connect_ack);
2193}
2194
2195static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2196{
2197 struct msgb *msg = gsm48_msgb_alloc();
2198 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2199
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2201
2202 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2203
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002204 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205}
2206
2207static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2208{
2209 struct gsm48_hdr *gh = msgb_l3(msg);
2210 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2211 struct tlv_parsed tp;
2212 struct gsm_mncc disc;
2213
2214 gsm48_stop_cc_timer(trans);
2215
2216 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2217
2218 memset(&disc, 0, sizeof(struct gsm_mncc));
2219 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002220 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 /* cause */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2223 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2226 }
2227 /* facility */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2229 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2232 }
2233 /* user-user */
2234 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2235 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2238 }
2239 /* ss-version */
2240 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2241 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2244 }
2245
Harald Welte596fed42009-07-23 19:06:52 +02002246 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247
2248}
2249
Harald Weltec66b71c2009-06-11 14:23:20 +08002250static struct gsm_mncc_cause default_cause = {
2251 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2252 .coding = 0,
2253 .rec = 0,
2254 .rec_val = 0,
2255 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2256 .diag_len = 0,
2257 .diag = { 0 },
2258};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259
2260static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2261{
2262 struct gsm_mncc *disc = arg;
2263 struct msgb *msg = gsm48_msgb_alloc();
2264 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2265
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2267
2268 gsm48_stop_cc_timer(trans);
2269 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2270
2271 /* cause */
2272 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002273 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 else
Harald Welte55c8f352010-03-07 23:40:35 +01002275 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276
2277 /* facility */
2278 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 /* progress */
2281 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002282 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 /* user-user */
2284 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002285 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286
2287 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002288 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
2290 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2291
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002292 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293}
2294
2295static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2296{
2297 struct gsm48_hdr *gh = msgb_l3(msg);
2298 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2299 struct tlv_parsed tp;
2300 struct gsm_mncc rel;
2301 int rc;
2302
2303 gsm48_stop_cc_timer(trans);
2304
2305 memset(&rel, 0, sizeof(struct gsm_mncc));
2306 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002307 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 /* cause */
2309 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2310 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002311 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2313 }
2314 /* facility */
2315 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2316 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002317 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2319 }
2320 /* user-user */
2321 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2322 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2325 }
2326 /* ss-version */
2327 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2328 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002329 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2331 }
2332
Harald Weltedcaf5652009-07-23 18:56:43 +02002333 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002335 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002337 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002338 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002339 GSM48_MT_CC_RELEASE_COMPL);
2340 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 }
2342
2343 new_cc_state(trans, GSM_CSTATE_NULL);
2344
2345 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002346 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347
2348 return rc;
2349}
2350
2351static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2352{
2353 struct gsm_mncc *rel = arg;
2354 struct msgb *msg = gsm48_msgb_alloc();
2355 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2356
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 gh->msg_type = GSM48_MT_CC_RELEASE;
2358
2359 trans->callref = 0;
2360
2361 gsm48_stop_cc_timer(trans);
2362 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2363
2364 /* cause */
2365 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002366 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 /* facility */
2368 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002369 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 /* user-user */
2371 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002372 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373
Harald Weltedcaf5652009-07-23 18:56:43 +02002374 trans->cc.T308_second = 0;
2375 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376
Harald Weltedcaf5652009-07-23 18:56:43 +02002377 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2379
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002380 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381}
2382
2383static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2384{
2385 struct gsm48_hdr *gh = msgb_l3(msg);
2386 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2387 struct tlv_parsed tp;
2388 struct gsm_mncc rel;
2389 int rc = 0;
2390
2391 gsm48_stop_cc_timer(trans);
2392
2393 memset(&rel, 0, sizeof(struct gsm_mncc));
2394 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002395 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 /* cause */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2398 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2401 }
2402 /* facility */
2403 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2404 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2407 }
2408 /* user-user */
2409 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2410 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2413 }
2414 /* ss-version */
2415 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2416 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002417 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2419 }
2420
2421 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002422 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002424 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 MNCC_REJ_IND, &rel);
2426 break;
2427 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002428 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429 MNCC_REL_CNF, &rel);
2430 break;
2431 default:
Harald Welte596fed42009-07-23 19:06:52 +02002432 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 MNCC_REL_IND, &rel);
2434 }
2435 }
2436
2437 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002438 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
2440 return rc;
2441}
2442
2443static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2444{
2445 struct gsm_mncc *rel = arg;
2446 struct msgb *msg = gsm48_msgb_alloc();
2447 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002448 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2451
2452 trans->callref = 0;
2453
2454 gsm48_stop_cc_timer(trans);
2455
2456 /* cause */
2457 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002458 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 /* facility */
2460 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002461 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 /* user-user */
2463 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002464 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465
Harald Weltec7782de2010-12-21 19:31:41 +01002466 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2467
Harald Weltedcaf5652009-07-23 18:56:43 +02002468 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469
Harald Weltec7782de2010-12-21 19:31:41 +01002470 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471}
2472
2473static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2474{
2475 struct gsm48_hdr *gh = msgb_l3(msg);
2476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2477 struct tlv_parsed tp;
2478 struct gsm_mncc fac;
2479
2480 memset(&fac, 0, sizeof(struct gsm_mncc));
2481 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002482 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483 /* facility */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2485 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2488 }
2489 /* ss-version */
2490 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2491 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2494 }
2495
Harald Welte596fed42009-07-23 19:06:52 +02002496 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497}
2498
2499static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2500{
2501 struct gsm_mncc *fac = arg;
2502 struct msgb *msg = gsm48_msgb_alloc();
2503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2504
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 gh->msg_type = GSM48_MT_CC_FACILITY;
2506
2507 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002508 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002510 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2514{
2515 struct gsm_mncc hold;
2516
2517 memset(&hold, 0, sizeof(struct gsm_mncc));
2518 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002519 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520}
2521
2522static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2523{
2524 struct msgb *msg = gsm48_msgb_alloc();
2525 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2526
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2528
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002529 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530}
2531
2532static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2533{
2534 struct gsm_mncc *hold_rej = arg;
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_HOLD_REJ;
2539
2540 /* cause */
2541 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002542 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 else
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002546 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547}
2548
2549static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2550{
2551 struct gsm_mncc retrieve;
2552
2553 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2554 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002555 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2556 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2560{
2561 struct msgb *msg = gsm48_msgb_alloc();
2562 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2563
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2565
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002566 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567}
2568
2569static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2570{
2571 struct gsm_mncc *retrieve_rej = arg;
2572 struct msgb *msg = gsm48_msgb_alloc();
2573 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2574
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2576
2577 /* cause */
2578 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002579 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580 else
Harald Welte55c8f352010-03-07 23:40:35 +01002581 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002583 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584}
2585
2586static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2587{
2588 struct gsm48_hdr *gh = msgb_l3(msg);
2589 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2590 struct tlv_parsed tp;
2591 struct gsm_mncc dtmf;
2592
2593 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2594 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002595 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596 /* keypad facility */
2597 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2598 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002599 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2601 }
2602
Harald Welte596fed42009-07-23 19:06:52 +02002603 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604}
2605
2606static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2607{
2608 struct gsm_mncc *dtmf = arg;
2609 struct msgb *msg = gsm48_msgb_alloc();
2610 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2611
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2613
2614 /* keypad */
2615 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002616 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002618 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619}
2620
2621static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2622{
2623 struct gsm_mncc *dtmf = arg;
2624 struct msgb *msg = gsm48_msgb_alloc();
2625 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2626
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2628
2629 /* cause */
2630 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002631 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632 else
Harald Welte55c8f352010-03-07 23:40:35 +01002633 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002635 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636}
2637
2638static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2639{
2640 struct msgb *msg = gsm48_msgb_alloc();
2641 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2642
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2644
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002645 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646}
2647
2648static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2649{
2650 struct gsm_mncc dtmf;
2651
2652 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2653 dtmf.callref = trans->callref;
2654
Harald Welte596fed42009-07-23 19:06:52 +02002655 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2659{
2660 struct gsm48_hdr *gh = msgb_l3(msg);
2661 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2662 struct tlv_parsed tp;
2663 struct gsm_mncc modify;
2664
2665 memset(&modify, 0, sizeof(struct gsm_mncc));
2666 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002667 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668 /* bearer capability */
2669 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2670 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002671 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2673 }
2674
2675 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2676
Harald Welte596fed42009-07-23 19:06:52 +02002677 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678}
2679
2680static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2681{
2682 struct gsm_mncc *modify = arg;
2683 struct msgb *msg = gsm48_msgb_alloc();
2684 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2685
Harald Welte4bfdfe72009-06-10 23:11:52 +08002686 gh->msg_type = GSM48_MT_CC_MODIFY;
2687
2688 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2689
2690 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002691 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692
2693 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2694
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002695 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2699{
2700 struct gsm48_hdr *gh = msgb_l3(msg);
2701 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2702 struct tlv_parsed tp;
2703 struct gsm_mncc modify;
2704
2705 gsm48_stop_cc_timer(trans);
2706
2707 memset(&modify, 0, sizeof(struct gsm_mncc));
2708 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002709 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 /* bearer capability */
2711 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2712 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002713 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2715 }
2716
2717 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2718
Harald Welte596fed42009-07-23 19:06:52 +02002719 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720}
2721
2722static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2723{
2724 struct gsm_mncc *modify = arg;
2725 struct msgb *msg = gsm48_msgb_alloc();
2726 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2727
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2729
2730 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002731 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732
2733 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2734
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002735 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736}
2737
2738static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2739{
2740 struct gsm48_hdr *gh = msgb_l3(msg);
2741 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2742 struct tlv_parsed tp;
2743 struct gsm_mncc modify;
2744
2745 gsm48_stop_cc_timer(trans);
2746
2747 memset(&modify, 0, sizeof(struct gsm_mncc));
2748 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002749 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750 /* bearer capability */
2751 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2752 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002753 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2755 }
2756 /* cause */
2757 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2758 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002759 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2761 }
2762
2763 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2764
Harald Welte596fed42009-07-23 19:06:52 +02002765 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *modify = arg;
2771 struct msgb *msg = gsm48_msgb_alloc();
2772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2775
2776 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002777 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002779 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780
2781 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2782
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002783 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784}
2785
2786static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2787{
2788 struct gsm_mncc *notify = arg;
2789 struct msgb *msg = gsm48_msgb_alloc();
2790 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2791
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 gh->msg_type = GSM48_MT_CC_NOTIFY;
2793
2794 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002795 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002797 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798}
2799
2800static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2801{
2802 struct gsm48_hdr *gh = msgb_l3(msg);
2803 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2804// struct tlv_parsed tp;
2805 struct gsm_mncc notify;
2806
2807 memset(&notify, 0, sizeof(struct gsm_mncc));
2808 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002809// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002811 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812
Harald Welte596fed42009-07-23 19:06:52 +02002813 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814}
2815
2816static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2817{
2818 struct gsm_mncc *user = arg;
2819 struct msgb *msg = gsm48_msgb_alloc();
2820 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2821
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 gh->msg_type = GSM48_MT_CC_USER_INFO;
2823
2824 /* user-user */
2825 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002826 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 /* more data */
2828 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002829 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002831 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2835{
2836 struct gsm48_hdr *gh = msgb_l3(msg);
2837 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2838 struct tlv_parsed tp;
2839 struct gsm_mncc user;
2840
2841 memset(&user, 0, sizeof(struct gsm_mncc));
2842 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002843 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 /* user-user */
2845 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2846 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002847 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2849 }
2850 /* more data */
2851 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2852 user.more = 1;
2853
Harald Welte596fed42009-07-23 19:06:52 +02002854 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855}
2856
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002857static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858{
2859 struct gsm_mncc *mode = arg;
2860
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002861 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862}
2863
2864static struct downstate {
2865 u_int32_t states;
2866 int type;
2867 int (*rout) (struct gsm_trans *trans, void *arg);
2868} downstatelist[] = {
2869 /* mobile originating call establishment */
2870 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2871 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2872 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2873 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2874 {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 */
2875 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2876 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2877 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2878 /* mobile terminating call establishment */
2879 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2880 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2881 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2882 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2883 /* signalling during call */
2884 {SBIT(GSM_CSTATE_ACTIVE),
2885 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2886 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2887 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2888 {ALL_STATES,
2889 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2890 {ALL_STATES,
2891 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2892 {ALL_STATES,
2893 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2894 {SBIT(GSM_CSTATE_ACTIVE),
2895 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2896 {SBIT(GSM_CSTATE_ACTIVE),
2897 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2898 {SBIT(GSM_CSTATE_ACTIVE),
2899 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2900 {SBIT(GSM_CSTATE_ACTIVE),
2901 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2902 {SBIT(GSM_CSTATE_ACTIVE),
2903 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2904 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2905 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2906 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2907 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2908 {SBIT(GSM_CSTATE_ACTIVE),
2909 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2910 /* clearing */
2911 {SBIT(GSM_CSTATE_INITIATED),
2912 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2913 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2914 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2915 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2916 MNCC_REL_REQ, gsm48_cc_tx_release},
2917 /* special */
2918 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002919 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920};
2921
2922#define DOWNSLLEN \
2923 (sizeof(downstatelist) / sizeof(struct downstate))
2924
2925
Harald Welte76556372010-12-22 23:57:45 +01002926int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927{
Harald Welte1a6f7982009-08-09 18:52:33 +02002928 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002930 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002931 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 struct gsm_mncc *data = arg, rel;
2933
Harald Welte04dc88f2010-12-22 21:45:05 +01002934 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2935
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 /* handle special messages */
2937 switch(msg_type) {
2938 case MNCC_BRIDGE:
2939 return tch_bridge(net, arg);
2940 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002941 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002943 return tch_recv_mncc(net, data->callref, 1);
2944 case GSM_TCHF_FRAME:
2945 /* Find callref */
2946 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002947 if (!trans) {
2948 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002949 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002950 }
2951 if (!trans->conn) {
2952 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002953 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002954 }
2955 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2956 /* This should be LOGL_ERROR or NOTICE, but
2957 * unfortuantely it happens for a couple of frames at
2958 * the beginning of every RTP connection */
2959 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002960 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002961 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002962 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002963 switch (bts->type) {
2964 case GSM_BTS_TYPE_NANOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01002965 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002966 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002967 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002968 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002969 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002970 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002971 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002972 default:
2973 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2974 }
2975 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 }
2977
2978 memset(&rel, 0, sizeof(struct gsm_mncc));
2979 rel.callref = data->callref;
2980
2981 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002982 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983
2984 /* Callref unknown */
2985 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002986 struct gsm_subscriber *subscr;
2987
Harald Welte4a3464c2009-07-04 10:11:24 +02002988 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2990 "Received '%s' from MNCC with "
2991 "unknown callref %d\n", data->called.number,
2992 get_mncc_name(msg_type), data->callref);
2993 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002994 return mncc_release_ind(net, NULL, data->callref,
2995 GSM48_CAUSE_LOC_PRN_S_LU,
2996 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002998 if (!data->called.number[0] && !data->imsi[0]) {
2999 DEBUGP(DCC, "(bts - trx - ts - ti) "
3000 "Received '%s' from MNCC with "
3001 "no number or IMSI\n", get_mncc_name(msg_type));
3002 /* Invalid number */
3003 return mncc_release_ind(net, NULL, data->callref,
3004 GSM48_CAUSE_LOC_PRN_S_LU,
3005 GSM48_CC_CAUSE_INV_NR_FORMAT);
3006 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003008 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003009 subscr = subscr_get_by_extension(net,
3010 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003011 else
Harald Welte9176bd42009-07-23 18:46:00 +02003012 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003013 /* If subscriber is not found */
3014 if (!subscr) {
3015 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3016 "Received '%s' from MNCC with "
3017 "unknown subscriber %s\n", data->called.number,
3018 get_mncc_name(msg_type), data->called.number);
3019 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003020 return mncc_release_ind(net, NULL, data->callref,
3021 GSM48_CAUSE_LOC_PRN_S_LU,
3022 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 }
3024 /* If subscriber is not "attached" */
3025 if (!subscr->lac) {
3026 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3027 "Received '%s' from MNCC with "
3028 "detached subscriber %s\n", data->called.number,
3029 get_mncc_name(msg_type), data->called.number);
3030 subscr_put(subscr);
3031 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003032 return mncc_release_ind(net, NULL, data->callref,
3033 GSM48_CAUSE_LOC_PRN_S_LU,
3034 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 }
3036 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003037 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3038 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 DEBUGP(DCC, "No memory for trans.\n");
3040 subscr_put(subscr);
3041 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003042 mncc_release_ind(net, NULL, data->callref,
3043 GSM48_CAUSE_LOC_PRN_S_LU,
3044 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045 return -ENOMEM;
3046 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003048 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003049
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003051 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052 /* find transaction with this subscriber already paging */
3053 llist_for_each_entry(transt, &net->trans_list, entry) {
3054 /* Transaction of our lchan? */
3055 if (transt == trans ||
3056 transt->subscr != subscr)
3057 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003058 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 "Received '%s' from MNCC with "
3060 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003061 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003063 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003064 subscr_put(subscr);
3065 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 return 0;
3067 }
3068 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003069 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003070
3071 /* Get a channel */
3072 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
3073
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003074 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 return 0;
3076 }
3077 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003078 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003079 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003081
3082 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003083 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084
3085 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003086 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003088 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003089 (trans->subscr)?(trans->subscr->extension):"-",
3090 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003091 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3092 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003093 if (msg_type == MNCC_REL_REQ)
3094 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3095 else
3096 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3097 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003098 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 return rc;
3100 }
3101
3102 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3103 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003104 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003105 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003106 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003107 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003108 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003109
3110 /* Find function for current state and message */
3111 for (i = 0; i < DOWNSLLEN; i++)
3112 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003113 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114 break;
3115 if (i == DOWNSLLEN) {
3116 DEBUGP(DCC, "Message unhandled at this state.\n");
3117 return 0;
3118 }
3119
3120 rc = downstatelist[i].rout(trans, arg);
3121
3122 return rc;
3123}
3124
3125
3126static struct datastate {
3127 u_int32_t states;
3128 int type;
3129 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3130} datastatelist[] = {
3131 /* mobile originating call establishment */
3132 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3133 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3134 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3135 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3136 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3137 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3138 /* mobile terminating call establishment */
3139 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3140 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3141 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3142 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003143 {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 +08003144 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3145 /* signalling during call */
3146 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3147 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3148 {SBIT(GSM_CSTATE_ACTIVE),
3149 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3150 {ALL_STATES,
3151 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3152 {ALL_STATES,
3153 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3154 {ALL_STATES,
3155 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3156 {SBIT(GSM_CSTATE_ACTIVE),
3157 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3158 {SBIT(GSM_CSTATE_ACTIVE),
3159 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3160 {SBIT(GSM_CSTATE_ACTIVE),
3161 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3162 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3163 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3164 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3165 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3166 {SBIT(GSM_CSTATE_ACTIVE),
3167 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3168 /* clearing */
3169 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3170 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3171 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3172 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3173 {ALL_STATES, /* 5.4.3.4 */
3174 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3175};
3176
3177#define DATASLLEN \
3178 (sizeof(datastatelist) / sizeof(struct datastate))
3179
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003180static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003181{
3182 struct gsm48_hdr *gh = msgb_l3(msg);
3183 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003184 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003185 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003187
Harald Welte4bfdfe72009-06-10 23:11:52 +08003188 if (msg_type & 0x80) {
3189 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3190 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003191 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003192
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003194 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003195
Harald Welte6f5aee02009-07-23 21:21:14 +02003196 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003197 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003198 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003199 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003200 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3201 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003202
3203 /* Create transaction */
3204 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003205 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003206 "creating new trans.\n", transaction_id);
3207 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003208 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003209 transaction_id, new_callref++);
3210 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003211 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003212 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003213 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214 GSM48_MT_CC_RELEASE_COMPL);
3215 return -ENOMEM;
3216 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003217 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003218 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003219 }
3220
3221 /* find function for current state and message */
3222 for (i = 0; i < DATASLLEN; i++)
3223 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003224 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003225 break;
3226 if (i == DATASLLEN) {
3227 DEBUGP(DCC, "Message unhandled at this state.\n");
3228 return 0;
3229 }
3230
3231 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003232
3233 return rc;
3234}
3235
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003236/* Create a dummy to wait five seconds */
3237static void release_anchor(struct gsm_subscriber_connection *conn)
3238{
3239 if (!conn->anch_operation)
3240 return;
3241
3242 bsc_del_timer(&conn->anch_operation->timeout);
3243 talloc_free(conn->anch_operation);
3244 conn->anch_operation = NULL;
3245}
3246
3247static void anchor_timeout(void *_data)
3248{
3249 struct gsm_subscriber_connection *con = _data;
3250
3251 release_anchor(con);
3252 msc_release_connection(con);
3253}
3254
3255int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3256{
3257 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3258 if (!conn->anch_operation)
3259 return -1;
3260
3261 conn->anch_operation->timeout.data = conn;
3262 conn->anch_operation->timeout.cb = anchor_timeout;
3263 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3264 return 0;
3265}
3266
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003267/* here we get data from the BSC level... */
3268int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003269{
3270 struct gsm48_hdr *gh = msgb_l3(msg);
3271 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003272 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003273
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003274 if (silent_call_reroute(conn, msg))
3275 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003276
3277 switch (pdisc) {
3278 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003279 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003280 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003281 break;
3282 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003283 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003284 break;
3285 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003286 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003287 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003288 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003289 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003290 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003291 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003292 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003293 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003294 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3295 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003296 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003297 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003298 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003299 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003300 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003301 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003302 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3303 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003304 break;
3305 }
3306
3307 return rc;
3308}
Harald Welte8470bf22008-12-25 23:28:35 +00003309
Harald Welte805f6442009-07-28 18:25:29 +02003310/*
3311 * This will be ran by the linker when loading the DSO. We use it to
3312 * do system initialization, e.g. registration of signal handlers.
3313 */
3314static __attribute__((constructor)) void on_dso_load_0408(void)
3315{
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01003316 register_signal_handler(SS_HO, handle_ho_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003317 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3318}