blob: 42652e22e6ddc437b45e0034b2a08e7d3cd1b28f [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
Harald Welte31c00f72011-03-03 23:29:05 +010078void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
79{
80 net->mncc_recv(net, msg);
81}
82
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080083static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
84 struct gsm_trans *trans)
85{
86 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
87
88 /* if we get passed a transaction reference, do some common
89 * work that the caller no longer has to do */
90 if (trans) {
91 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
92 msg->lchan = trans->conn->lchan;
93 }
94
95
96 if (msg->lchan) {
97 msg->trx = msg->lchan->ts->trx;
98 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
99 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
100 "Sending '%s' to MS.\n", msg->trx->bts->nr,
101 msg->trx->nr, msg->lchan->ts->nr,
102 gh->proto_discr & 0xf0,
103 gsm48_cc_msg_name(gh->msg_type));
104 else
105 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
106 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
107 msg->trx->nr, msg->lchan->ts->nr,
108 gh->proto_discr, gh->msg_type);
109 }
110
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100111 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100113
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800114int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
115{
116 struct gsm48_hdr *gh;
117 struct msgb *ss_notify;
118
119 ss_notify = gsm0480_create_notifySS(message);
120 if (!ss_notify)
121 return -1;
122
123 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
124 uint8_t *data = msgb_push(ss_notify, 1);
125 data[0] = ss_notify->len - 1;
126 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
127 gh->msg_type = GSM48_MT_CC_FACILITY;
128 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
129}
130
Sylvain Munaut30a15382009-12-24 00:27:26 +0100131static void release_security_operation(struct gsm_subscriber_connection *conn)
132{
133 if (!conn->sec_operation)
134 return;
135
136 talloc_free(conn->sec_operation);
137 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800138 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100139}
140
141static void allocate_security_operation(struct gsm_subscriber_connection *conn)
142{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100143 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
144 struct gsm_security_operation);
145}
146
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100147int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
148 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100149{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800150 struct gsm_network *net = conn->bts->network;
151 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100152 struct gsm_security_operation *op;
153 struct gsm_auth_tuple atuple;
154 int status = -1, rc;
155
156 /* Check if we _can_ enable encryption. Cases where we can't:
157 * - Encryption disabled in config
158 * - Channel already secured (nothing to do)
159 * - Subscriber equipment doesn't support configured encryption
160 */
161 if (!net->a5_encryption) {
162 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800163 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100164 DEBUGP(DMM, "Requesting to secure an already secure channel");
165 status = GSM_SECURITY_SUCCEEDED;
166 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
167 net->a5_encryption)) {
168 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
169 status = GSM_SECURITY_NOAVAIL;
170 }
171
172 /* If not done yet, try to get info for this user */
173 if (status < 0) {
174 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
175 if (rc <= 0)
176 status = GSM_SECURITY_NOAVAIL;
177 }
178
179 /* Are we done yet ? */
180 if (status >= 0)
181 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800182 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183 0;
184
185 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800186 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100187 return -EBUSY;
188
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800189 allocate_security_operation(conn);
190 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100191 op->cb = cb;
192 op->cb_data = cb_data;
193 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
194
195 /* FIXME: Should start a timer for completion ... */
196
197 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100198 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100199 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800200 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100201 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100202 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100203 return gsm0808_cipher_mode(conn, net->a5_encryption,
204 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100205 }
206
207 return -EINVAL; /* not reached */
208}
209
Holger Freyther73487a22008-12-31 18:53:57 +0000210static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
211 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000212{
213 if (!subscriber)
214 return 0;
215
Holger Freyther73487a22008-12-31 18:53:57 +0000216 /*
217 * Do not send accept yet as more information should arrive. Some
218 * phones will not send us the information and we will have to check
219 * what we want to do with that.
220 */
221 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
222 return 0;
223
Jan Luebbe06513f22009-08-12 12:48:00 +0200224 switch (subscriber->net->auth_policy) {
225 case GSM_AUTH_POLICY_CLOSED:
226 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200227 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200228 if (subscriber->authorized)
229 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200230 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200231 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000232 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200233 default:
234 return 0;
235 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000236}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000237
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100238static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000239{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100240 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000241 return;
242
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800243 /* No need to keep the connection up */
244 release_anchor(conn);
245
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100246 bsc_del_timer(&conn->loc_operation->updating_timer);
247 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100248 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800249 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000250}
251
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100252static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000253{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800254 if (conn->loc_operation)
255 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100256 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000257
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100258 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200259 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000260}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000261
Sylvain Munaut267fba02009-12-24 00:28:01 +0100262static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
263 struct msgb *msg, void *data, void *param)
264{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800265 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100266 int rc = 0;
267
268 switch (event) {
269 case GSM_SECURITY_AUTH_FAILED:
270 release_loc_updating_req(conn);
271 break;
272
273 case GSM_SECURITY_NOAVAIL:
274 case GSM_SECURITY_SUCCEEDED:
275 /* We're all good */
276 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800277 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800278 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100279 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800280 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100281 }
282
283 /* call subscr_update after putting the loc_upd_acc
284 * in the transmit queue, since S_SUBSCR_ATTACHED might
285 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800286 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100287 GSM_SUBSCRIBER_UPDATE_ATTACHED);
288
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100289 /*
290 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
291 * MS needs to respond with a TMSI REALLOCATION COMPLETE
292 * (even if the TMSI is the same).
293 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100294 break;
295
296 default:
297 rc = -EINVAL;
298 };
299
300 return rc;
301}
302
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100303static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000304{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100305 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800306 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100307 conn->loc_operation->key_seq,
308 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000309 return 0;
310}
311
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800312void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000313{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800314 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800315
316 /* avoid someone issuing a clear */
317 conn->in_release = 1;
318
Holger Freyther7c19f742009-06-06 13:54:35 +0000319 /*
320 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800321 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000322 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800323 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100324
325 /* We might need to cancel the paging response or such. */
326 if (conn->sec_operation && conn->sec_operation->cb) {
327 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
328 NULL, conn, conn->sec_operation->cb_data);
329 }
330
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800331 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800332 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000333
Harald Welte4bfdfe72009-06-10 23:11:52 +0800334 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200335 /* FIXME: this is not neccessarily the right thing to do, we should
336 * only set trans->lchan to NULL and wait for another lchan to be
337 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800338 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
339 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200340 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800341 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000342}
343
Harald Welte371efe52010-12-22 23:17:50 +0100344void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
345{
346 struct gsm_trans *trans, *temp;
347
348 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
349
350 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100351 if (trans->protocol == protocol) {
352 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100353 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100354 }
Harald Welte371efe52010-12-22 23:17:50 +0100355 }
356}
357
Holger Freyther429e7762008-12-30 13:28:30 +0000358/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800359int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000360{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800361 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800362 struct msgb *msg;
363
364 counter_inc(bts->network->stats.loc_upd_resp.reject);
365
366 msg = gsm48_create_loc_upd_rej(cause);
367 if (!msg) {
368 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
369 return -1;
370 }
Harald Welte52b1f982008-12-23 20:25:15 +0000371
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800372 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000373
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100374 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100375 "LAC=%u BTS=%u\n", conn->subscr ?
376 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800377 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100378
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800379 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000380}
381
382/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800383int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000384{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800385 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000386 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000387 struct gsm48_hdr *gh;
388 struct gsm48_loc_area_id *lai;
389 u_int8_t *mid;
390
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800391 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000392
393 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
394 gh->proto_discr = GSM48_PDISC_MM;
395 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
396
397 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100398 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000399 bts->network->network_code, bts->location_area_code);
400
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200401 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200402 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000403
404 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
405
Harald Welteffa55a42009-12-22 19:07:32 +0100406 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100407
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800408 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000409}
410
Harald Weltebf5e8df2009-02-03 12:59:45 +0000411/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800412static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000413{
414 struct msgb *msg = gsm48_msgb_alloc();
415 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000416
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800417 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000418
419 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
420 gh->proto_discr = GSM48_PDISC_MM;
421 gh->msg_type = GSM48_MT_MM_ID_REQ;
422 gh->data[0] = id_type;
423
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800424 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000425}
426
Harald Welte231ad4f2008-12-27 11:15:38 +0000427
Harald Weltebf5e8df2009-02-03 12:59:45 +0000428/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800429static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000430{
431 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000432 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200433 struct gsm_bts *bts = lchan->ts->trx->bts;
434 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000435 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200436 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000437
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200438 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000439 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000440 mi_type, mi_string);
441
Harald Welte7659de12009-12-13 12:39:18 +0100442 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
443
Harald Welte75a983f2008-12-27 21:34:06 +0000444 switch (mi_type) {
445 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200446 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100447 if (!conn->subscr) {
448 conn->subscr = subscr_get_by_imsi(net, mi_string);
449 if (!conn->subscr)
450 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200451 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100452 if (conn->loc_operation)
453 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000454 break;
455 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000456 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000457 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100458 if (conn->subscr) {
459 db_subscriber_assoc_imei(conn->subscr, mi_string);
460 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200461 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100462 if (conn->loc_operation)
463 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000464 break;
465 }
Holger Freyther73487a22008-12-31 18:53:57 +0000466
467 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100468 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000469}
470
Harald Welte255539c2008-12-28 02:26:27 +0000471
472static void loc_upd_rej_cb(void *data)
473{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100474 struct gsm_subscriber_connection *conn = data;
475 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100476 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000477
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800478 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800479 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000480}
481
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100482static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000483{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100484 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
485 conn->loc_operation->updating_timer.data = conn;
486 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000487}
488
Harald Welte2a139372009-02-22 21:14:55 +0000489static const char *lupd_name(u_int8_t type)
490{
491 switch (type) {
492 case GSM48_LUPD_NORMAL:
493 return "NORMAL";
494 case GSM48_LUPD_PERIODIC:
495 return "PEROIDOC";
496 case GSM48_LUPD_IMSI_ATT:
497 return "IMSI ATTACH";
498 default:
499 return "UNKNOWN";
500 }
501}
502
Harald Weltebf5e8df2009-02-03 12:59:45 +0000503/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800504static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000505{
Harald Welte8470bf22008-12-25 23:28:35 +0000506 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000507 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800508 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800509 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000510 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200511 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000512 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000513
Harald Welte8470bf22008-12-25 23:28:35 +0000514 lu = (struct gsm48_loc_upd_req *) gh->data;
515
516 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000517
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200518 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000519
Harald Weltea0368542009-06-27 02:58:43 +0200520 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000521 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000522
Harald Welte7659de12009-12-13 12:39:18 +0100523 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
524
Harald Welte24ff6ee2009-12-22 00:41:05 +0100525 switch (lu->type) {
526 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100527 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100528 break;
529 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100530 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100531 break;
532 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100533 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100534 break;
535 }
536
Holger Freythereaf04692009-06-06 13:54:44 +0000537 /*
538 * Pseudo Spoof detection: Just drop a second/concurrent
539 * location updating request.
540 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100541 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200542 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100543 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800544 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000545 return 0;
546 }
547
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800548 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000549
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200550 conn->loc_operation->key_seq = lu->key_seq;
551
Harald Welte52b1f982008-12-23 20:25:15 +0000552 switch (mi_type) {
553 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200554 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000555 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800556 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100557 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000558
Jan Luebbe370b41d2009-08-12 10:19:34 +0200559 /* look up subscriber based on IMSI, create if not found */
560 subscr = subscr_get_by_imsi(bts->network, mi_string);
561 if (!subscr) {
562 subscr = db_create_subscriber(bts->network, mi_string);
563 }
Harald Welte4b634542008-12-27 01:55:51 +0000564 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000565 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200566 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000567 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200568 subscr = subscr_get_by_tmsi(bts->network,
569 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000570 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000571 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800572 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100573 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000574 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200575 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800576 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200577 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000578 break;
579 case GSM_MI_TYPE_IMEI:
580 case GSM_MI_TYPE_IMEISV:
581 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200582 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000583 break;
584 default:
Harald Weltea0368542009-06-27 02:58:43 +0200585 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000586 break;
587 }
588
Harald Welte24516ea2009-07-04 10:18:00 +0200589 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100590 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200591
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200593 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800594 /* FIXME: request id? close channel? */
595 return -EINVAL;
596 }
597
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100598 conn->subscr = subscr;
599 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000600
Harald Welte24516ea2009-07-04 10:18:00 +0200601 /* check if we can let the subscriber into our network immediately
602 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000604}
605
Harald Welte4bfdfe72009-06-10 23:11:52 +0800606#if 0
607static u_int8_t to_bcd8(u_int8_t val)
608{
609 return ((val / 10) << 4) | (val % 10);
610}
611#endif
612
Harald Weltedb253af2008-12-30 17:56:55 +0000613/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800614int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000615{
616 struct msgb *msg = gsm48_msgb_alloc();
617 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800618 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000619 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200620 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621#if 0
622 time_t cur_t;
623 struct tm* cur_time;
624 int tz15min;
625#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000626
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800627 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000628
629 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
630 gh->proto_discr = GSM48_PDISC_MM;
631 gh->msg_type = GSM48_MT_MM_INFO;
632
633 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200634#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000635 name_len = strlen(net->name_long);
636 /* 10.5.3.5a */
637 ptr8 = msgb_put(msg, 3);
638 ptr8[0] = GSM48_IE_NAME_LONG;
639 ptr8[1] = name_len*2 +1;
640 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
641
642 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
643 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000644 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000645
646 /* FIXME: Use Cell Broadcast, not UCS-2, since
647 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200648#endif
649 name_len = (strlen(net->name_long)*7)/8;
650 name_pad = (8 - strlen(net->name_long)*7)%8;
651 if (name_pad > 0)
652 name_len++;
653 /* 10.5.3.5a */
654 ptr8 = msgb_put(msg, 3);
655 ptr8[0] = GSM48_IE_NAME_LONG;
656 ptr8[1] = name_len +1;
657 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
658
659 ptr8 = msgb_put(msg, name_len);
660 gsm_7bit_encode(ptr8, net->name_long);
661
Harald Weltedb253af2008-12-30 17:56:55 +0000662 }
663
664 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200665#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000666 name_len = strlen(net->name_short);
667 /* 10.5.3.5a */
668 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200669 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000670 ptr8[1] = name_len*2 + 1;
671 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
672
Harald Weltee872cb12009-01-01 00:33:37 +0000673 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000674 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000675 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200676#endif
677 name_len = (strlen(net->name_short)*7)/8;
678 name_pad = (8 - strlen(net->name_short)*7)%8;
679 if (name_pad > 0)
680 name_len++;
681 /* 10.5.3.5a */
682 ptr8 = (u_int8_t *) msgb_put(msg, 3);
683 ptr8[0] = GSM48_IE_NAME_SHORT;
684 ptr8[1] = name_len +1;
685 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
686
687 ptr8 = msgb_put(msg, name_len);
688 gsm_7bit_encode(ptr8, net->name_short);
689
Harald Weltedb253af2008-12-30 17:56:55 +0000690 }
691
692#if 0
693 /* Section 10.5.3.9 */
694 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000696 ptr8 = msgb_put(msg, 8);
697 ptr8[0] = GSM48_IE_NET_TIME_TZ;
698 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
699 ptr8[2] = to_bcd8(cur_time->tm_mon);
700 ptr8[3] = to_bcd8(cur_time->tm_mday);
701 ptr8[4] = to_bcd8(cur_time->tm_hour);
702 ptr8[5] = to_bcd8(cur_time->tm_min);
703 ptr8[6] = to_bcd8(cur_time->tm_sec);
704 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
705 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000707 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000709#endif
710
Daniel Willmanneea93372009-08-13 03:42:07 +0200711 DEBUGP(DMM, "-> MM INFO\n");
712
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800713 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000714}
715
Harald Welte7984d5c2009-08-12 22:56:50 +0200716/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800717int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200718{
719 struct msgb *msg = gsm48_msgb_alloc();
720 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200721 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200722
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100723 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200724
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800725 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200726 gh->proto_discr = GSM48_PDISC_MM;
727 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
728
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100729 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200730
Harald Welte7984d5c2009-08-12 22:56:50 +0200731 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200732 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200733 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200734
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800735 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200736}
737
738/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800739int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200740{
741 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800742 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200743}
744
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800745static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000746{
Harald Welte4b634542008-12-27 01:55:51 +0000747 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800748 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000749}
Harald Welteba4cf162009-01-10 01:49:35 +0000750
751/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100752static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000753 enum gsm48_reject_value value)
754{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800755 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000756
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800757 msg = gsm48_create_mm_serv_rej(value);
758 if (!msg) {
759 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
760 return -1;
761 }
Harald Welteba4cf162009-01-10 01:49:35 +0000762
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800763 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100764 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800765 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000766}
767
Sylvain Munautba87f452009-12-24 00:28:46 +0100768static int _gsm48_rx_mm_serv_req_sec_cb(
769 unsigned int hooknum, unsigned int event,
770 struct msgb *msg, void *data, void *param)
771{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800772 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100773 int rc = 0;
774
775 switch (event) {
776 case GSM_SECURITY_AUTH_FAILED:
777 /* Nothing to do */
778 break;
779
780 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800781 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100782 break;
783
784 case GSM_SECURITY_SUCCEEDED:
785 /* nothing to do. CIPHER MODE COMMAND is
786 * implicit CM SERV ACK */
787 break;
788
789 default:
790 rc = -EINVAL;
791 };
792
793 return rc;
794}
795
Harald Welte4ed0e922009-01-10 03:17:30 +0000796/*
797 * Handle CM Service Requests
798 * a) Verify that the packet is long enough to contain the information
799 * we require otherwsie reject with INCORRECT_MESSAGE
800 * b) Try to parse the TMSI. If we do not have one reject
801 * c) Check that we know the subscriber with the TMSI otherwise reject
802 * with a HLR cause
803 * d) Set the subscriber on the gsm_lchan and accept
804 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800805static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000806{
Harald Welteba4cf162009-01-10 01:49:35 +0000807 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200808 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000809
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800810 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000811 struct gsm_subscriber *subscr;
812 struct gsm48_hdr *gh = msgb_l3(msg);
813 struct gsm48_service_request *req =
814 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800815 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000816 u_int8_t classmark2_len = gh->data[1];
817 u_int8_t *classmark2 = gh->data+2;
818 u_int8_t mi_len = *(classmark2 + classmark2_len);
819 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000820
Harald Weltec9e02182009-05-01 19:07:53 +0000821 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000822 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000823 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800824 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000825 GSM48_REJECT_INCORRECT_MESSAGE);
826 }
827
828 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000829 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800830 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000831 GSM48_REJECT_INCORRECT_MESSAGE);
832 }
833
Harald Weltec9e02182009-05-01 19:07:53 +0000834 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000835 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000836 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800837 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000838 GSM48_REJECT_INCORRECT_MESSAGE);
839 }
840
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200841 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000842 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000843 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000844
Harald Welte7659de12009-12-13 12:39:18 +0100845 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
846
Harald Welte3ac7f102009-08-10 10:12:45 +0200847 if (is_siemens_bts(bts))
848 send_siemens_mrpci(msg->lchan, classmark2-1);
849
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200850 subscr = subscr_get_by_tmsi(bts->network,
851 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000852
Harald Welte2a139372009-02-22 21:14:55 +0000853 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000854 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800855 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000856 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
857
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800858 if (!conn->subscr)
859 conn->subscr = subscr;
860 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100861 subscr_put(subscr); /* lchan already has a ref, don't need another one */
862 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000863 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
864 subscr_put(subscr);
865 }
866
Harald Weltec2e302d2009-07-05 14:08:13 +0200867 subscr->equipment.classmark2_len = classmark2_len;
868 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
869 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000870
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800871 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100872 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000873}
874
Harald Welte2a139372009-02-22 21:14:55 +0000875static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
876{
Harald Welte9176bd42009-07-23 18:46:00 +0200877 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000878 struct gsm48_hdr *gh = msgb_l3(msg);
879 struct gsm48_imsi_detach_ind *idi =
880 (struct gsm48_imsi_detach_ind *) gh->data;
881 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200882 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800883 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000884
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200885 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000886 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
887 mi_type, mi_string);
888
Harald Welteffa55a42009-12-22 19:07:32 +0100889 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100890
Harald Welte2a139372009-02-22 21:14:55 +0000891 switch (mi_type) {
892 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200893 subscr = subscr_get_by_tmsi(bts->network,
894 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000895 break;
896 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200897 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000898 break;
899 case GSM_MI_TYPE_IMEI:
900 case GSM_MI_TYPE_IMEISV:
901 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000902 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000903 break;
904 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000905 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000906 break;
907 }
908
Holger Freyther4a49e772009-04-12 05:37:29 +0000909 if (subscr) {
910 subscr_update(subscr, msg->trx->bts,
911 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100912 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200913
914 subscr->equipment.classmark1 = idi->classmark1;
915 db_sync_equipment(&subscr->equipment);
916
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000917 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000918 } else
Harald Welte2a139372009-02-22 21:14:55 +0000919 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
920
Harald Welte31981a02009-12-20 09:58:40 +0100921 /* FIXME: iterate over all transactions and release them,
922 * imagine an IMSI DETACH happening during an active call! */
923
Harald Welteb83d9382009-12-12 21:00:48 +0100924 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000925 return 0;
926}
927
Harald Welted2a7f5a2009-06-05 20:08:20 +0000928static int gsm48_rx_mm_status(struct msgb *msg)
929{
930 struct gsm48_hdr *gh = msgb_l3(msg);
931
932 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
933
934 return 0;
935}
936
Sylvain Munaut30a15382009-12-24 00:27:26 +0100937/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800938static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100939{
940 struct gsm48_hdr *gh = msgb_l3(msg);
941 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800942 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100943
Sylvain Munautc593cf12010-06-10 23:51:41 +0200944 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100945 hexdump(ar->sres, 4));
946
947 /* Safety check */
948 if (!conn->sec_operation) {
949 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
950 return -EIO;
951 }
952
953 /* Validate SRES */
954 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100955 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100956 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200957
958 DEBUGPC(DMM, "Invalid (expected %s)\n",
959 hexdump(conn->sec_operation->atuple.sres, 4));
960
Sylvain Munaut30a15382009-12-24 00:27:26 +0100961 if (cb)
962 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800963 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100964
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100965 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100966 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100967 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100968 }
969
Sylvain Munautc593cf12010-06-10 23:51:41 +0200970 DEBUGPC(DMM, "OK\n");
971
Sylvain Munaut30a15382009-12-24 00:27:26 +0100972 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100973 return gsm0808_cipher_mode(conn, net->a5_encryption,
974 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100975}
976
Harald Weltebf5e8df2009-02-03 12:59:45 +0000977/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800978static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000979{
980 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800981 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000982
983 switch (gh->msg_type & 0xbf) {
984 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200985 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800986 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000987 break;
988 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800989 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000990 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000991 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800992 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000993 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000994 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000995 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000996 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000997 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000998 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800999 conn->subscr ?
1000 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001001 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001002 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001003 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001004 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001005 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1006 break;
1007 case GSM48_MT_MM_CM_REEST_REQ:
1008 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1009 break;
1010 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001011 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001012 break;
1013 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001014 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001015 gh->msg_type);
1016 break;
1017 }
1018
1019 return rc;
1020}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001021
Harald Welte2d35ae62009-02-06 12:02:13 +00001022/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001023static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001024{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001025 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001026 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001027 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +00001028 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001029 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001030 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001031 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001032 int rc = 0;
1033
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001034 resp = (struct gsm48_pag_resp *) &gh->data[0];
1035 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1036 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001037 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1038 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001039
Harald Weltefe18d8f2009-02-22 21:14:24 +00001040 switch (mi_type) {
1041 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001042 subscr = subscr_get_by_tmsi(bts->network,
1043 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001044 break;
1045 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001046 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001047 break;
1048 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001049
1050 if (!subscr) {
1051 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001052 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001053 return -EINVAL;
1054 }
1055 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001056 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001057
Harald Weltec2e302d2009-07-05 14:08:13 +02001058 subscr->equipment.classmark2_len = *classmark2_lv;
1059 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1060 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001061
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001062 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001063 return rc;
1064}
1065
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001066static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001067{
1068 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001069 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001070 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1071 u_int8_t cm2_len, cm3_len = 0;
1072 u_int8_t *cm2, *cm3 = NULL;
1073
1074 DEBUGP(DRR, "CLASSMARK CHANGE ");
1075
1076 /* classmark 2 */
1077 cm2_len = gh->data[0];
1078 cm2 = &gh->data[1];
1079 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1080
1081 if (payload_len > cm2_len + 1) {
1082 /* we must have a classmark3 */
1083 if (gh->data[cm2_len+1] != 0x20) {
1084 DEBUGPC(DRR, "ERR CM3 TAG\n");
1085 return -EINVAL;
1086 }
1087 if (cm2_len > 3) {
1088 DEBUGPC(DRR, "CM2 too long!\n");
1089 return -EINVAL;
1090 }
1091
1092 cm3_len = gh->data[cm2_len+2];
1093 cm3 = &gh->data[cm2_len+3];
1094 if (cm3_len > 14) {
1095 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1096 return -EINVAL;
1097 }
1098 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1099 }
1100 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001101 subscr->equipment.classmark2_len = cm2_len;
1102 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001103 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001104 subscr->equipment.classmark3_len = cm3_len;
1105 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001106 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001107 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001108 }
1109
Harald Weltef7c43522009-06-09 20:24:21 +00001110 return 0;
1111}
1112
Harald Weltecf5b3592009-05-01 18:28:42 +00001113static int gsm48_rx_rr_status(struct msgb *msg)
1114{
1115 struct gsm48_hdr *gh = msgb_l3(msg);
1116
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001117 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001118 rr_cause_name(gh->data[0]));
1119
1120 return 0;
1121}
1122
Harald Weltef7c43522009-06-09 20:24:21 +00001123static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1124{
Harald Welted12b0fd2009-12-15 21:36:05 +01001125 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001126
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001127 /* This shouldn't actually end up here, as RSL treats
1128 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1129 * directly into gsm48_parse_meas_rep */
1130 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001131 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001132
1133 return 0;
1134}
1135
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001136static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001137{
1138 struct gsm48_hdr *gh = msgb_l3(msg);
1139 u_int8_t apdu_id_flags;
1140 u_int8_t apdu_len;
1141 u_int8_t *apdu_data;
1142
1143 apdu_id_flags = gh->data[0];
1144 apdu_len = gh->data[1];
1145 apdu_data = gh->data+2;
1146
1147 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1148 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1149
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001150 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001151}
1152
Sylvain Munaut30a15382009-12-24 00:27:26 +01001153/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001154static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001155{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001156 gsm_cbfn *cb;
1157 int rc = 0;
1158
1159 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1160
1161 /* Safety check */
1162 if (!conn->sec_operation) {
1163 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1164 return -EIO;
1165 }
1166
1167 /* FIXME: check for MI (if any) */
1168
1169 /* Call back whatever was in progress (if anything) ... */
1170 cb = conn->sec_operation->cb;
1171 if (cb) {
1172 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001173 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001174 }
1175
1176 /* Complete the operation */
1177 release_security_operation(conn);
1178
1179 return rc;
1180}
1181
Harald Welted011e8b2009-11-29 22:45:52 +01001182/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001183static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001184{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001185 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001186 struct gsm48_hdr *gh = msgb_l3(msg);
1187
1188 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1189 rr_cause_name(gh->data[0]));
1190
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001191 sig.lchan = msg->lchan;
1192 sig.mr = NULL;
1193 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001194 /* FIXME: release old channel */
1195
1196 return 0;
1197}
1198
1199/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001200static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001201{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001202 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001203 struct gsm48_hdr *gh = msgb_l3(msg);
1204
1205 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1206 rr_cause_name(gh->data[0]));
1207
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001208 sig.lchan = msg->lchan;
1209 sig.mr = NULL;
1210 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001211 /* FIXME: release allocated new channel */
1212
1213 return 0;
1214}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001215
Harald Weltebf5e8df2009-02-03 12:59:45 +00001216/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001217static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001218{
1219 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001220 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001221
1222 switch (gh->msg_type) {
1223 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001224 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001225 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001226 case GSM48_MT_RR_GPRS_SUSP_REQ:
1227 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1228 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001229 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001230 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001231 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001232 case GSM48_MT_RR_STATUS:
1233 rc = gsm48_rx_rr_status(msg);
1234 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001235 case GSM48_MT_RR_MEAS_REP:
1236 rc = gsm48_rx_rr_meas_rep(msg);
1237 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001238 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001239 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001240 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001241 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001242 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001243 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001244 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001245 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001246 break;
1247 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001248 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001249 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001250 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001251 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1252 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001253 break;
1254 }
1255
Harald Welte2d35ae62009-02-06 12:02:13 +00001256 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001257}
1258
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001259int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001260 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261{
1262 struct msgb *msg = gsm48_msgb_alloc();
1263 struct gsm48_hdr *gh;
1264
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001265 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266
1267 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1268 apdu_id, apdu_len);
1269
1270 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1271 gh->proto_discr = GSM48_PDISC_RR;
1272 gh->msg_type = GSM48_MT_RR_APP_INFO;
1273 gh->data[0] = apdu_id;
1274 gh->data[1] = apdu_len;
1275 memcpy(gh->data+2, apdu, apdu_len);
1276
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001277 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001278}
1279
Harald Welte4bc90a12008-12-27 16:32:52 +00001280/* Call Control */
1281
Harald Welte7584aea2009-02-11 11:44:12 +00001282/* The entire call control code is written in accordance with Figure 7.10c
1283 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1284 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1285 * it for voice */
1286
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287static void new_cc_state(struct gsm_trans *trans, int state)
1288{
1289 if (state > 31 || state < 0)
1290 return;
1291
1292 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001293 gsm48_cc_state_name(trans->cc.state),
1294 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295
Harald Weltedcaf5652009-07-23 18:56:43 +02001296 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297}
1298
1299static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001300{
1301 struct msgb *msg = gsm48_msgb_alloc();
1302 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1303 u_int8_t *cause, *call_state;
1304
Harald Welte4bc90a12008-12-27 16:32:52 +00001305 gh->msg_type = GSM48_MT_CC_STATUS;
1306
1307 cause = msgb_put(msg, 3);
1308 cause[0] = 2;
1309 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1310 cause[2] = 0x80 | 30; /* response to status inquiry */
1311
1312 call_state = msgb_put(msg, 1);
1313 call_state[0] = 0xc0 | 0x00;
1314
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001315 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001316}
1317
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001318static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001319 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001320{
1321 struct msgb *msg = gsm48_msgb_alloc();
1322 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1323
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001324 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001325
Harald Welte6f4b7532008-12-29 00:39:37 +00001326 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001327 gh->msg_type = msg_type;
1328
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001329 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001330}
1331
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1333{
Harald Weltedcaf5652009-07-23 18:56:43 +02001334 if (bsc_timer_pending(&trans->cc.timer)) {
1335 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1336 bsc_del_timer(&trans->cc.timer);
1337 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 }
1339}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001340
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1342 int msg_type, struct gsm_mncc *mncc)
1343{
1344 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001345 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346
1347 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001348 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001349 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001351 trans->conn->lchan->ts->trx->bts->nr,
1352 trans->conn->lchan->ts->trx->nr,
1353 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354 (trans->subscr)?(trans->subscr->extension):"-",
1355 get_mncc_name(msg_type));
1356 else
1357 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1358 "Sending '%s' to MNCC.\n",
1359 (trans->subscr)?(trans->subscr->extension):"-",
1360 get_mncc_name(msg_type));
1361 else
1362 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1363 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1364
1365 mncc->msg_type = msg_type;
1366
Harald Welte966636f2009-06-26 19:39:35 +02001367 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368 if (!msg)
1369 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001370
1371 data = msgb_put(msg, sizeof(struct gsm_mncc));
1372 memcpy(data, mncc, sizeof(struct gsm_mncc));
1373
Harald Welte54209c22010-12-22 23:43:29 +01001374 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375
1376 return 0;
1377}
1378
1379int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1380 u_int32_t callref, int location, int value)
1381{
1382 struct gsm_mncc rel;
1383
Harald Welte92f70c52009-06-12 01:54:08 +08001384 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001386 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1388}
1389
Harald Weltedcaf5652009-07-23 18:56:43 +02001390/* Call Control Specific transaction release.
1391 * gets called by trans_free, DO NOT CALL YOURSELF! */
1392void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 gsm48_stop_cc_timer(trans);
1395
1396 /* send release to L4, if callref still exists */
1397 if (trans->callref) {
1398 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001399 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001400 GSM48_CAUSE_LOC_PRN_S_LU,
1401 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001403 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001405 if (trans->conn)
1406 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407}
1408
1409static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001410
Harald Welte09e38af2009-02-16 22:52:23 +00001411/* call-back from paging the B-end of the connection */
1412static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001413 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001414{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001415 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001416 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001417 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001419
Harald Welte09e38af2009-02-16 22:52:23 +00001420 if (hooknum != GSM_HOOK_RR_PAGING)
1421 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001422
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001423 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 if (!net) {
1425 DEBUGP(DCC, "Error Network not set!\n");
1426 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001427 }
Harald Welte7584aea2009-02-11 11:44:12 +00001428
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429 /* check all tranactions (without lchan) for subscriber */
1430 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001431 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001432 continue;
1433 switch (event) {
1434 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001435 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001436 break;
1437 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001438 transt->subscr->extension);
1439 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001440 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001441 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001442 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001443 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001444 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 }
1446 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001447 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 break;
1449 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001450 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001451 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001452 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001454 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001455 mncc_release_ind(transt->subscr->net, transt,
1456 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001457 GSM48_CAUSE_LOC_PRN_S_LU,
1458 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001459 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001460 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001461 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 break;
1463 }
1464 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001465
1466 talloc_free(paging_request);
1467
1468 /*
1469 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1470 * failure and then the subscr will be poke again. This needs a lot of fixing
1471 * in the subscriber queue code.
1472 */
1473 if (!found && conn)
1474 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001475 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001476}
Harald Welte7584aea2009-02-11 11:44:12 +00001477
Harald Welteda7ab742009-12-19 22:23:05 +01001478static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1479
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001480/* handle audio path for handover */
1481static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1482 void *handler_data, void *signal_data)
1483{
1484 struct rtp_socket *old_rs, *new_rs, *other_rs;
1485 struct ho_signal_data *sig = signal_data;
1486
1487 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1488 return 0;
1489
1490 if (ipacc_rtp_direct) {
1491 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1492 return 0;
1493 }
1494
1495 /* RTP Proxy mode */
1496 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1497 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1498
1499 if (!new_rs) {
1500 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1501 return -EIO;
1502 }
1503
1504 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1505
1506 if (!old_rs) {
1507 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1508 return -EIO;
1509 }
1510
1511 /* copy rx_action and reference to other sock */
1512 new_rs->rx_action = old_rs->rx_action;
1513 new_rs->tx_action = old_rs->tx_action;
1514 new_rs->transmit = old_rs->transmit;
1515
1516 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1517 case RTP_PROXY:
1518 other_rs = old_rs->proxy.other_sock;
1519 rtp_socket_proxy(new_rs, other_rs);
1520 /* delete reference to other end socket to prevent
1521 * rtp_socket_free() from removing the inverse reference */
1522 old_rs->proxy.other_sock = NULL;
1523 break;
1524 case RTP_RECV_UPSTREAM:
1525 new_rs->receive = old_rs->receive;
1526 break;
1527 case RTP_NONE:
1528 break;
1529 }
1530
1531 return 0;
1532}
1533
Harald Welte805f6442009-07-28 18:25:29 +02001534/* some other part of the code sends us a signal */
1535static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1536 void *handler_data, void *signal_data)
1537{
1538 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001539 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001540 struct gsm_network *net;
1541 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001542
1543 if (subsys != SS_ABISIP)
1544 return 0;
1545
1546 /* in case we use direct BTS-to-BTS RTP */
1547 if (ipacc_rtp_direct)
1548 return 0;
1549
Harald Welte805f6442009-07-28 18:25:29 +02001550 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001551 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001552 /* in case we don't use direct BTS-to-BTS RTP */
1553 /* the BTS has successfully bound a TCH to a local ip/port,
1554 * which means we can connect our UDP socket to it */
1555 if (lchan->abis_ip.rtp_socket) {
1556 rtp_socket_free(lchan->abis_ip.rtp_socket);
1557 lchan->abis_ip.rtp_socket = NULL;
1558 }
1559
1560 lchan->abis_ip.rtp_socket = rtp_socket_create();
1561 if (!lchan->abis_ip.rtp_socket)
1562 return -EIO;
1563
1564 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1565 lchan->abis_ip.bound_ip,
1566 lchan->abis_ip.bound_port);
1567 if (rc < 0)
1568 return -EIO;
1569
Harald Welteda7ab742009-12-19 22:23:05 +01001570 /* check if any transactions on this lchan still have
1571 * a tch_recv_mncc request pending */
1572 net = lchan->ts->trx->bts->network;
1573 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001574 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001575 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1576 tch_recv_mncc(net, trans->callref, 1);
1577 }
1578 }
Harald Welte805f6442009-07-28 18:25:29 +02001579 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001580 case S_ABISIP_DLCX_IND:
1581 /* the BTS tells us a RTP stream has been disconnected */
1582 if (lchan->abis_ip.rtp_socket) {
1583 rtp_socket_free(lchan->abis_ip.rtp_socket);
1584 lchan->abis_ip.rtp_socket = NULL;
1585 }
1586
1587 break;
Harald Welte805f6442009-07-28 18:25:29 +02001588 }
1589
1590 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001591}
1592
Harald Welte49f48b82009-02-17 15:29:33 +00001593/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001594static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001595{
Harald Welte11fa29c2009-02-19 17:24:39 +00001596 struct gsm_bts *bts = lchan->ts->trx->bts;
1597 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001598 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001599
Harald Welte11fa29c2009-02-19 17:24:39 +00001600 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1601 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1602 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1603
1604 if (bts->type != remote_bts->type) {
1605 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1606 return -EINVAL;
1607 }
Harald Welteda7ab742009-12-19 22:23:05 +01001608
1609 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001610 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001611 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001612 if (!ipacc_rtp_direct) {
1613 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001614 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001615 if (rc < 0)
1616 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001617 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001618 if (rc < 0)
1619 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001620 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301621 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1622 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001623 } else {
1624 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301625 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1626 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301627 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001628 if (rc < 0)
1629 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301630 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1631 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301632 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001633 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001634 break;
1635 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001636 case GSM_BTS_TYPE_RBS2000:
Harald Welte11fa29c2009-02-19 17:24:39 +00001637 trau_mux_map_lchan(lchan, remote_lchan);
1638 break;
1639 default:
1640 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001641 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001642 }
Harald Welte49f48b82009-02-17 15:29:33 +00001643
1644 return 0;
1645}
1646
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647/* bridge channels of two transactions */
1648static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001649{
Harald Weltedcaf5652009-07-23 18:56:43 +02001650 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1651 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001652
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001654 return -EIO;
1655
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001656 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 return -EIO;
1658
1659 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001660 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001661}
1662
Harald Welteda7ab742009-12-19 22:23:05 +01001663/* enable receive of channels to MNCC upqueue */
1664static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665{
1666 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001667 struct gsm_lchan *lchan;
1668 struct gsm_bts *bts;
1669 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001670
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001672 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 if (!trans)
1674 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001675 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001677 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001678 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679
Harald Welteda7ab742009-12-19 22:23:05 +01001680 switch (bts->type) {
1681 case GSM_BTS_TYPE_NANOBTS:
1682 if (ipacc_rtp_direct) {
1683 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1684 return -EINVAL;
1685 }
1686 /* in case, we don't have a RTP socket yet, we note this
1687 * in the transaction and try later */
1688 if (!lchan->abis_ip.rtp_socket) {
1689 trans->tch_recv = enable;
1690 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1691 return 0;
1692 }
1693 if (enable) {
1694 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001695 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001696 if (rc < 0)
1697 return rc;
1698 /* assign socket to application interface */
1699 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1700 net, callref);
1701 } else
1702 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1703 net, 0);
1704 break;
1705 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001706 case GSM_BTS_TYPE_RBS2000:
Harald Welteda7ab742009-12-19 22:23:05 +01001707 if (enable)
1708 return trau_recv_lchan(lchan, callref);
1709 return trau_mux_unmap(NULL, callref);
1710 break;
1711 default:
1712 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1713 return -EINVAL;
1714 }
1715
1716 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717}
1718
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1720{
1721 DEBUGP(DCC, "-> STATUS ENQ\n");
1722 return gsm48_cc_tx_status(trans, msg);
1723}
1724
1725static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1726static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1727
1728static void gsm48_cc_timeout(void *arg)
1729{
1730 struct gsm_trans *trans = arg;
1731 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001732 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1733 int mo_location = GSM48_CAUSE_LOC_USER;
1734 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1735 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 struct gsm_mncc mo_rel, l4_rel;
1737
1738 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1739 mo_rel.callref = trans->callref;
1740 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1741 l4_rel.callref = trans->callref;
1742
Harald Weltedcaf5652009-07-23 18:56:43 +02001743 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 case 0x303:
1745 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001746 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 break;
1748 case 0x310:
1749 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001750 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 break;
1752 case 0x313:
1753 disconnect = 1;
1754 /* unknown, did not find it in the specs */
1755 break;
1756 case 0x301:
1757 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001758 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 break;
1760 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001761 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001763 gsm48_cc_tx_release(trans, &trans->cc.msg);
1764 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 break; /* stay in release state */
1766 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001767 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 return;
1769// release = 1;
1770// l4_cause = 14;
1771// break;
1772 case 0x306:
1773 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001774 mo_cause = trans->cc.msg.cause.value;
1775 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 break;
1777 case 0x323:
1778 disconnect = 1;
1779 break;
1780 default:
1781 release = 1;
1782 }
1783
1784 if (release && trans->callref) {
1785 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001786 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 l4_location, l4_cause);
1788 trans->callref = 0;
1789 }
1790
1791 if (disconnect && trans->callref) {
1792 /* process disconnect towards layer 4 */
1793 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001794 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 }
1796
1797 /* process disconnect towards mobile station */
1798 if (disconnect || release) {
1799 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001800 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1801 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1802 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 mo_rel.cause.diag_len = 3;
1804
1805 if (disconnect)
1806 gsm48_cc_tx_disconnect(trans, &mo_rel);
1807 if (release)
1808 gsm48_cc_tx_release(trans, &mo_rel);
1809 }
1810
1811}
1812
1813static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1814 int sec, int micro)
1815{
1816 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001817 trans->cc.timer.cb = gsm48_cc_timeout;
1818 trans->cc.timer.data = trans;
1819 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1820 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821}
1822
1823static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1824{
1825 struct gsm48_hdr *gh = msgb_l3(msg);
1826 u_int8_t msg_type = gh->msg_type & 0xbf;
1827 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1828 struct tlv_parsed tp;
1829 struct gsm_mncc setup;
1830
1831 memset(&setup, 0, sizeof(struct gsm_mncc));
1832 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001833 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 /* emergency setup is identified by msg_type */
1835 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1836 setup.emergency = 1;
1837
1838 /* use subscriber as calling party number */
1839 if (trans->subscr) {
1840 setup.fields |= MNCC_F_CALLING;
1841 strncpy(setup.calling.number, trans->subscr->extension,
1842 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001843 strncpy(setup.imsi, trans->subscr->imsi,
1844 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 }
1846 /* bearer capability */
1847 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1848 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1851 }
1852 /* facility */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1854 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1857 }
1858 /* called party bcd number */
1859 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1860 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001861 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1863 }
1864 /* user-user */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1866 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1869 }
1870 /* ss-version */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1872 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1875 }
1876 /* CLIR suppression */
1877 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1878 setup.clir.sup = 1;
1879 /* CLIR invocation */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1881 setup.clir.inv = 1;
1882 /* cc cap */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1884 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1887 }
1888
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 new_cc_state(trans, GSM_CSTATE_INITIATED);
1890
Harald Welte (local)47df3992009-12-26 19:45:03 +01001891 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1892 subscr_name(trans->subscr), trans->subscr->extension,
1893 setup.called.number);
1894
Harald Weltea29e43a2010-12-24 16:06:33 +01001895 counter_inc(trans->subscr->net->stats.call.mo_setup);
1896
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001898 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899
Harald Welte13cac662009-07-29 12:10:35 +02001900 /* MNCC code will modify the channel asynchronously, we should
1901 * ipaccess-bind only after the modification has been made to the
1902 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903 return 0;
1904}
1905
1906static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001907{
1908 struct msgb *msg = gsm48_msgb_alloc();
1909 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001911 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001912
Harald Welte7ccf7782009-02-17 01:43:01 +00001913 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001914
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 /* transaction id must not be assigned */
1916 if (trans->transaction_id != 0xff) { /* unasssigned */
1917 DEBUGP(DCC, "TX Setup with assigned transaction. "
1918 "This is not allowed!\n");
1919 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001920 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001921 GSM48_CAUSE_LOC_PRN_S_LU,
1922 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001924 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 return rc;
1926 }
1927
1928 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001929 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1930 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001932 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001933 GSM48_CAUSE_LOC_PRN_S_LU,
1934 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001936 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 return rc;
1938 }
Harald Welte78283ef2009-07-23 21:36:44 +02001939 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001940
Harald Welte65e74cc2008-12-29 01:55:35 +00001941 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001942
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001944
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 /* bearer capability */
1946 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001947 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 /* facility */
1949 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001950 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* progress */
1952 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001953 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 /* calling party BCD number */
1955 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* called party BCD number */
1958 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* user-user */
1961 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 /* redirecting party BCD number */
1964 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 /* signal */
1967 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001968 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969
1970 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001971
Harald Weltea29e43a2010-12-24 16:06:33 +01001972 counter_inc(trans->subscr->net->stats.call.mt_setup);
1973
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001974 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001975}
1976
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1978{
1979 struct gsm48_hdr *gh = msgb_l3(msg);
1980 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1981 struct tlv_parsed tp;
1982 struct gsm_mncc call_conf;
1983
1984 gsm48_stop_cc_timer(trans);
1985 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1986
1987 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1988 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001989 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990#if 0
1991 /* repeat */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1993 call_conf.repeat = 1;
1994 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1995 call_conf.repeat = 2;
1996#endif
1997 /* bearer capability */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1999 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2002 }
2003 /* cause */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2005 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2008 }
2009 /* cc cap */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2011 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2014 }
2015
2016 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2017
Harald Welte596fed42009-07-23 19:06:52 +02002018 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2019 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020}
2021
2022static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2023{
2024 struct gsm_mncc *proceeding = arg;
2025 struct msgb *msg = gsm48_msgb_alloc();
2026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2027
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2029
2030 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2031
2032 /* bearer capability */
2033 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 /* facility */
2036 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002037 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 /* progress */
2039 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002042 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043}
2044
2045static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2046{
2047 struct gsm48_hdr *gh = msgb_l3(msg);
2048 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2049 struct tlv_parsed tp;
2050 struct gsm_mncc alerting;
2051
2052 gsm48_stop_cc_timer(trans);
2053 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2054
2055 memset(&alerting, 0, sizeof(struct gsm_mncc));
2056 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002057 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 /* facility */
2059 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2060 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002061 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2063 }
2064
2065 /* progress */
2066 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2067 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2070 }
2071 /* ss-version */
2072 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2073 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002074 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2076 }
2077
2078 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2079
Harald Welte596fed42009-07-23 19:06:52 +02002080 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2081 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082}
2083
2084static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2085{
2086 struct gsm_mncc *alerting = arg;
2087 struct msgb *msg = gsm48_msgb_alloc();
2088 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2089
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 gh->msg_type = GSM48_MT_CC_ALERTING;
2091
2092 /* facility */
2093 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* progress */
2096 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 /* user-user */
2099 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002100 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101
2102 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2103
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002104 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105}
2106
2107static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2108{
2109 struct gsm_mncc *progress = arg;
2110 struct msgb *msg = gsm48_msgb_alloc();
2111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2112
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 gh->msg_type = GSM48_MT_CC_PROGRESS;
2114
2115 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 /* user-user */
2118 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002121 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122}
2123
2124static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2125{
2126 struct gsm_mncc *connect = arg;
2127 struct msgb *msg = gsm48_msgb_alloc();
2128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2129
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 gh->msg_type = GSM48_MT_CC_CONNECT;
2131
2132 gsm48_stop_cc_timer(trans);
2133 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2134
2135 /* facility */
2136 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* progress */
2139 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 /* connected number */
2142 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 /* user-user */
2145 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002146 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
2148 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2149
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002150 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151}
2152
2153static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2154{
2155 struct gsm48_hdr *gh = msgb_l3(msg);
2156 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2157 struct tlv_parsed tp;
2158 struct gsm_mncc connect;
2159
2160 gsm48_stop_cc_timer(trans);
2161
2162 memset(&connect, 0, sizeof(struct gsm_mncc));
2163 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002164 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 /* use subscriber as connected party number */
2166 if (trans->subscr) {
2167 connect.fields |= MNCC_F_CONNECTED;
2168 strncpy(connect.connected.number, trans->subscr->extension,
2169 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002170 strncpy(connect.imsi, trans->subscr->imsi,
2171 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 }
2173 /* facility */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2175 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002176 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2178 }
2179 /* user-user */
2180 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2181 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002182 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2184 }
2185 /* ss-version */
2186 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2187 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002188 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2190 }
2191
2192 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltea29e43a2010-12-24 16:06:33 +01002193 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194
Harald Welte596fed42009-07-23 19:06:52 +02002195 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196}
2197
2198
2199static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2200{
2201 struct gsm_mncc connect_ack;
2202
2203 gsm48_stop_cc_timer(trans);
2204
2205 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltea29e43a2010-12-24 16:06:33 +01002206 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207
2208 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2209 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002210
Harald Welte596fed42009-07-23 19:06:52 +02002211 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 &connect_ack);
2213}
2214
2215static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2216{
2217 struct msgb *msg = gsm48_msgb_alloc();
2218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2219
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2221
2222 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2223
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002224 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225}
2226
2227static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2228{
2229 struct gsm48_hdr *gh = msgb_l3(msg);
2230 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2231 struct tlv_parsed tp;
2232 struct gsm_mncc disc;
2233
2234 gsm48_stop_cc_timer(trans);
2235
2236 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2237
2238 memset(&disc, 0, sizeof(struct gsm_mncc));
2239 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002240 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 /* cause */
2242 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2243 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002244 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2246 }
2247 /* facility */
2248 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2249 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002250 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2252 }
2253 /* user-user */
2254 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2255 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2258 }
2259 /* ss-version */
2260 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2261 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2264 }
2265
Harald Welte596fed42009-07-23 19:06:52 +02002266 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267
2268}
2269
Harald Weltec66b71c2009-06-11 14:23:20 +08002270static struct gsm_mncc_cause default_cause = {
2271 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2272 .coding = 0,
2273 .rec = 0,
2274 .rec_val = 0,
2275 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2276 .diag_len = 0,
2277 .diag = { 0 },
2278};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279
2280static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2281{
2282 struct gsm_mncc *disc = arg;
2283 struct msgb *msg = gsm48_msgb_alloc();
2284 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2285
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2287
2288 gsm48_stop_cc_timer(trans);
2289 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2290
2291 /* cause */
2292 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002293 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 else
Harald Welte55c8f352010-03-07 23:40:35 +01002295 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296
2297 /* facility */
2298 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002299 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300 /* progress */
2301 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002302 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 /* user-user */
2304 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002305 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306
2307 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002308 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309
2310 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2311
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002312 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313}
2314
2315static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2316{
2317 struct gsm48_hdr *gh = msgb_l3(msg);
2318 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2319 struct tlv_parsed tp;
2320 struct gsm_mncc rel;
2321 int rc;
2322
2323 gsm48_stop_cc_timer(trans);
2324
2325 memset(&rel, 0, sizeof(struct gsm_mncc));
2326 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002327 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 /* cause */
2329 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2330 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002331 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2333 }
2334 /* facility */
2335 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2336 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002337 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2339 }
2340 /* user-user */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2342 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2345 }
2346 /* ss-version */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2348 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002349 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2351 }
2352
Harald Weltedcaf5652009-07-23 18:56:43 +02002353 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002355 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002357 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002358 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002359 GSM48_MT_CC_RELEASE_COMPL);
2360 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 }
2362
2363 new_cc_state(trans, GSM_CSTATE_NULL);
2364
2365 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002366 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367
2368 return rc;
2369}
2370
2371static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2372{
2373 struct gsm_mncc *rel = arg;
2374 struct msgb *msg = gsm48_msgb_alloc();
2375 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2376
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377 gh->msg_type = GSM48_MT_CC_RELEASE;
2378
2379 trans->callref = 0;
2380
2381 gsm48_stop_cc_timer(trans);
2382 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2383
2384 /* cause */
2385 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002386 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 /* facility */
2388 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 /* user-user */
2391 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002392 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393
Harald Weltedcaf5652009-07-23 18:56:43 +02002394 trans->cc.T308_second = 0;
2395 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396
Harald Weltedcaf5652009-07-23 18:56:43 +02002397 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2399
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002400 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401}
2402
2403static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2404{
2405 struct gsm48_hdr *gh = msgb_l3(msg);
2406 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2407 struct tlv_parsed tp;
2408 struct gsm_mncc rel;
2409 int rc = 0;
2410
2411 gsm48_stop_cc_timer(trans);
2412
2413 memset(&rel, 0, sizeof(struct gsm_mncc));
2414 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002415 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416 /* cause */
2417 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2418 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2421 }
2422 /* facility */
2423 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2424 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002425 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2427 }
2428 /* user-user */
2429 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2430 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2433 }
2434 /* ss-version */
2435 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2436 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002437 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2439 }
2440
2441 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002442 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002444 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445 MNCC_REJ_IND, &rel);
2446 break;
2447 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002448 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449 MNCC_REL_CNF, &rel);
2450 break;
2451 default:
Harald Welte596fed42009-07-23 19:06:52 +02002452 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 MNCC_REL_IND, &rel);
2454 }
2455 }
2456
2457 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002458 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459
2460 return rc;
2461}
2462
2463static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2464{
2465 struct gsm_mncc *rel = arg;
2466 struct msgb *msg = gsm48_msgb_alloc();
2467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002468 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2471
2472 trans->callref = 0;
2473
2474 gsm48_stop_cc_timer(trans);
2475
2476 /* cause */
2477 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 /* facility */
2480 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002481 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482 /* user-user */
2483 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002484 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485
Harald Weltec7782de2010-12-21 19:31:41 +01002486 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2487
Harald Weltedcaf5652009-07-23 18:56:43 +02002488 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489
Harald Weltec7782de2010-12-21 19:31:41 +01002490 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491}
2492
2493static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2494{
2495 struct gsm48_hdr *gh = msgb_l3(msg);
2496 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2497 struct tlv_parsed tp;
2498 struct gsm_mncc fac;
2499
2500 memset(&fac, 0, sizeof(struct gsm_mncc));
2501 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002502 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 /* facility */
2504 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2505 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2508 }
2509 /* ss-version */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2511 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002512 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2514 }
2515
Harald Welte596fed42009-07-23 19:06:52 +02002516 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2520{
2521 struct gsm_mncc *fac = arg;
2522 struct msgb *msg = gsm48_msgb_alloc();
2523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 gh->msg_type = GSM48_MT_CC_FACILITY;
2526
2527 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002530 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531}
2532
2533static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2534{
2535 struct gsm_mncc hold;
2536
2537 memset(&hold, 0, sizeof(struct gsm_mncc));
2538 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002539 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540}
2541
2542static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2543{
2544 struct msgb *msg = gsm48_msgb_alloc();
2545 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2546
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2548
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002549 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550}
2551
2552static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2553{
2554 struct gsm_mncc *hold_rej = arg;
2555 struct msgb *msg = gsm48_msgb_alloc();
2556 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2557
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2559
2560 /* cause */
2561 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002562 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 else
Harald Welte55c8f352010-03-07 23:40:35 +01002564 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565
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_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2570{
2571 struct gsm_mncc retrieve;
2572
2573 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2574 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002575 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2576 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577}
2578
2579static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2580{
2581 struct msgb *msg = gsm48_msgb_alloc();
2582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2583
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2585
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002586 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587}
2588
2589static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2590{
2591 struct gsm_mncc *retrieve_rej = arg;
2592 struct msgb *msg = gsm48_msgb_alloc();
2593 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2594
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2596
2597 /* cause */
2598 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002599 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600 else
Harald Welte55c8f352010-03-07 23:40:35 +01002601 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002602
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002603 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604}
2605
2606static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2607{
2608 struct gsm48_hdr *gh = msgb_l3(msg);
2609 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2610 struct tlv_parsed tp;
2611 struct gsm_mncc dtmf;
2612
2613 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2614 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002615 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616 /* keypad facility */
2617 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2618 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002619 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2621 }
2622
Harald Welte596fed42009-07-23 19:06:52 +02002623 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624}
2625
2626static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2627{
2628 struct gsm_mncc *dtmf = arg;
2629 struct msgb *msg = gsm48_msgb_alloc();
2630 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2631
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2633
2634 /* keypad */
2635 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002636 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002638 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639}
2640
2641static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2642{
2643 struct gsm_mncc *dtmf = arg;
2644 struct msgb *msg = gsm48_msgb_alloc();
2645 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2646
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2648
2649 /* cause */
2650 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002651 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 else
Harald Welte55c8f352010-03-07 23:40:35 +01002653 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002655 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2659{
2660 struct msgb *msg = gsm48_msgb_alloc();
2661 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2662
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2664
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002665 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666}
2667
2668static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2669{
2670 struct gsm_mncc dtmf;
2671
2672 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2673 dtmf.callref = trans->callref;
2674
Harald Welte596fed42009-07-23 19:06:52 +02002675 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676}
2677
2678static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2679{
2680 struct gsm48_hdr *gh = msgb_l3(msg);
2681 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2682 struct tlv_parsed tp;
2683 struct gsm_mncc modify;
2684
2685 memset(&modify, 0, sizeof(struct gsm_mncc));
2686 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002687 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 /* bearer capability */
2689 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2690 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002691 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2693 }
2694
2695 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2696
Harald Welte596fed42009-07-23 19:06:52 +02002697 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698}
2699
2700static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2701{
2702 struct gsm_mncc *modify = arg;
2703 struct msgb *msg = gsm48_msgb_alloc();
2704 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2705
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 gh->msg_type = GSM48_MT_CC_MODIFY;
2707
2708 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2709
2710 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002711 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712
2713 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2714
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002715 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716}
2717
2718static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2719{
2720 struct gsm48_hdr *gh = msgb_l3(msg);
2721 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2722 struct tlv_parsed tp;
2723 struct gsm_mncc modify;
2724
2725 gsm48_stop_cc_timer(trans);
2726
2727 memset(&modify, 0, sizeof(struct gsm_mncc));
2728 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002729 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 /* bearer capability */
2731 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2732 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002733 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2735 }
2736
2737 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2738
Harald Welte596fed42009-07-23 19:06:52 +02002739 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740}
2741
2742static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2743{
2744 struct gsm_mncc *modify = arg;
2745 struct msgb *msg = gsm48_msgb_alloc();
2746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2747
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2749
2750 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002751 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752
2753 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2754
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002755 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756}
2757
2758static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2759{
2760 struct gsm48_hdr *gh = msgb_l3(msg);
2761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2762 struct tlv_parsed tp;
2763 struct gsm_mncc modify;
2764
2765 gsm48_stop_cc_timer(trans);
2766
2767 memset(&modify, 0, sizeof(struct gsm_mncc));
2768 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002769 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 /* bearer capability */
2771 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2772 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002773 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2775 }
2776 /* cause */
2777 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2778 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002779 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2781 }
2782
2783 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2784
Harald Welte596fed42009-07-23 19:06:52 +02002785 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786}
2787
2788static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2789{
2790 struct gsm_mncc *modify = arg;
2791 struct msgb *msg = gsm48_msgb_alloc();
2792 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2793
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2795
2796 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002797 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002799 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800
2801 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2802
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002803 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804}
2805
2806static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2807{
2808 struct gsm_mncc *notify = arg;
2809 struct msgb *msg = gsm48_msgb_alloc();
2810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2811
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812 gh->msg_type = GSM48_MT_CC_NOTIFY;
2813
2814 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002815 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818}
2819
2820static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2821{
2822 struct gsm48_hdr *gh = msgb_l3(msg);
2823 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2824// struct tlv_parsed tp;
2825 struct gsm_mncc notify;
2826
2827 memset(&notify, 0, sizeof(struct gsm_mncc));
2828 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002829// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002831 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832
Harald Welte596fed42009-07-23 19:06:52 +02002833 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834}
2835
2836static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2837{
2838 struct gsm_mncc *user = arg;
2839 struct msgb *msg = gsm48_msgb_alloc();
2840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2841
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 gh->msg_type = GSM48_MT_CC_USER_INFO;
2843
2844 /* user-user */
2845 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002846 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* more data */
2848 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002849 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002851 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852}
2853
2854static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2855{
2856 struct gsm48_hdr *gh = msgb_l3(msg);
2857 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2858 struct tlv_parsed tp;
2859 struct gsm_mncc user;
2860
2861 memset(&user, 0, sizeof(struct gsm_mncc));
2862 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002863 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 /* user-user */
2865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2866 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002867 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2869 }
2870 /* more data */
2871 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2872 user.more = 1;
2873
Harald Welte596fed42009-07-23 19:06:52 +02002874 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875}
2876
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002877static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878{
2879 struct gsm_mncc *mode = arg;
2880
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002881 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002882}
2883
2884static struct downstate {
2885 u_int32_t states;
2886 int type;
2887 int (*rout) (struct gsm_trans *trans, void *arg);
2888} downstatelist[] = {
2889 /* mobile originating call establishment */
2890 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2891 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2892 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2893 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2894 {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 */
2895 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2896 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2897 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2898 /* mobile terminating call establishment */
2899 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2900 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2901 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2902 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2903 /* signalling during call */
2904 {SBIT(GSM_CSTATE_ACTIVE),
2905 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2906 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2907 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2908 {ALL_STATES,
2909 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2910 {ALL_STATES,
2911 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2912 {ALL_STATES,
2913 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2914 {SBIT(GSM_CSTATE_ACTIVE),
2915 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2916 {SBIT(GSM_CSTATE_ACTIVE),
2917 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2918 {SBIT(GSM_CSTATE_ACTIVE),
2919 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2920 {SBIT(GSM_CSTATE_ACTIVE),
2921 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2922 {SBIT(GSM_CSTATE_ACTIVE),
2923 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2924 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2925 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2926 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2927 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2930 /* clearing */
2931 {SBIT(GSM_CSTATE_INITIATED),
2932 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2933 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2934 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2935 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2936 MNCC_REL_REQ, gsm48_cc_tx_release},
2937 /* special */
2938 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002939 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940};
2941
2942#define DOWNSLLEN \
2943 (sizeof(downstatelist) / sizeof(struct downstate))
2944
2945
Harald Welte76556372010-12-22 23:57:45 +01002946int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002947{
Harald Welte1a6f7982009-08-09 18:52:33 +02002948 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002950 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002951 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 struct gsm_mncc *data = arg, rel;
2953
Harald Welte04dc88f2010-12-22 21:45:05 +01002954 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2955
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 /* handle special messages */
2957 switch(msg_type) {
2958 case MNCC_BRIDGE:
2959 return tch_bridge(net, arg);
2960 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002961 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002963 return tch_recv_mncc(net, data->callref, 1);
2964 case GSM_TCHF_FRAME:
2965 /* Find callref */
2966 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002967 if (!trans) {
2968 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002969 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002970 }
2971 if (!trans->conn) {
2972 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002973 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002974 }
2975 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2976 /* This should be LOGL_ERROR or NOTICE, but
2977 * unfortuantely it happens for a couple of frames at
2978 * the beginning of every RTP connection */
2979 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002980 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002981 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002982 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002983 switch (bts->type) {
2984 case GSM_BTS_TYPE_NANOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01002985 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002986 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002987 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002988 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002989 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002990 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002991 case GSM_BTS_TYPE_RBS2000:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002992 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002993 default:
2994 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2995 }
2996 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 }
2998
2999 memset(&rel, 0, sizeof(struct gsm_mncc));
3000 rel.callref = data->callref;
3001
3002 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003003 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004
3005 /* Callref unknown */
3006 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003007 struct gsm_subscriber *subscr;
3008
Harald Welte4a3464c2009-07-04 10:11:24 +02003009 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3011 "Received '%s' from MNCC with "
3012 "unknown callref %d\n", data->called.number,
3013 get_mncc_name(msg_type), data->callref);
3014 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003015 return mncc_release_ind(net, NULL, data->callref,
3016 GSM48_CAUSE_LOC_PRN_S_LU,
3017 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003019 if (!data->called.number[0] && !data->imsi[0]) {
3020 DEBUGP(DCC, "(bts - trx - ts - ti) "
3021 "Received '%s' from MNCC with "
3022 "no number or IMSI\n", get_mncc_name(msg_type));
3023 /* Invalid number */
3024 return mncc_release_ind(net, NULL, data->callref,
3025 GSM48_CAUSE_LOC_PRN_S_LU,
3026 GSM48_CC_CAUSE_INV_NR_FORMAT);
3027 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003029 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003030 subscr = subscr_get_by_extension(net,
3031 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003032 else
Harald Welte9176bd42009-07-23 18:46:00 +02003033 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034 /* If subscriber is not found */
3035 if (!subscr) {
3036 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3037 "Received '%s' from MNCC with "
3038 "unknown subscriber %s\n", data->called.number,
3039 get_mncc_name(msg_type), data->called.number);
3040 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003041 return mncc_release_ind(net, NULL, data->callref,
3042 GSM48_CAUSE_LOC_PRN_S_LU,
3043 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 }
3045 /* If subscriber is not "attached" */
3046 if (!subscr->lac) {
3047 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3048 "Received '%s' from MNCC with "
3049 "detached subscriber %s\n", data->called.number,
3050 get_mncc_name(msg_type), data->called.number);
3051 subscr_put(subscr);
3052 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003053 return mncc_release_ind(net, NULL, data->callref,
3054 GSM48_CAUSE_LOC_PRN_S_LU,
3055 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 }
3057 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003058 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3059 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 DEBUGP(DCC, "No memory for trans.\n");
3061 subscr_put(subscr);
3062 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003063 mncc_release_ind(net, NULL, data->callref,
3064 GSM48_CAUSE_LOC_PRN_S_LU,
3065 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 return -ENOMEM;
3067 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003069 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003070
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003072 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073 /* find transaction with this subscriber already paging */
3074 llist_for_each_entry(transt, &net->trans_list, entry) {
3075 /* Transaction of our lchan? */
3076 if (transt == trans ||
3077 transt->subscr != subscr)
3078 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003079 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 "Received '%s' from MNCC with "
3081 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003082 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003084 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003085 subscr_put(subscr);
3086 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 return 0;
3088 }
3089 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003090 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003091
3092 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003093 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3094 if (!trans->paging_request) {
3095 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3096 subscr_put(subscr);
3097 trans_free(trans);
3098 return 0;
3099 }
3100
3101 *trans->paging_request = subscr->net;
3102 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003103
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003104 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003105 return 0;
3106 }
3107 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003108 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003109 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003111
3112 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003113 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114
3115 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003116 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003117 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003118 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 (trans->subscr)?(trans->subscr->extension):"-",
3120 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003121 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3122 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123 if (msg_type == MNCC_REL_REQ)
3124 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3125 else
3126 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3127 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003128 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 return rc;
3130 }
3131
3132 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3133 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003134 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003135 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003136 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003137 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003138 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139
3140 /* Find function for current state and message */
3141 for (i = 0; i < DOWNSLLEN; i++)
3142 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003143 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 break;
3145 if (i == DOWNSLLEN) {
3146 DEBUGP(DCC, "Message unhandled at this state.\n");
3147 return 0;
3148 }
3149
3150 rc = downstatelist[i].rout(trans, arg);
3151
3152 return rc;
3153}
3154
3155
3156static struct datastate {
3157 u_int32_t states;
3158 int type;
3159 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3160} datastatelist[] = {
3161 /* mobile originating call establishment */
3162 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3163 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3164 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3165 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3166 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3167 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3168 /* mobile terminating call establishment */
3169 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3170 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3171 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3172 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003173 {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 +08003174 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3175 /* signalling during call */
3176 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3177 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3178 {SBIT(GSM_CSTATE_ACTIVE),
3179 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3180 {ALL_STATES,
3181 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3182 {ALL_STATES,
3183 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3184 {ALL_STATES,
3185 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3186 {SBIT(GSM_CSTATE_ACTIVE),
3187 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3188 {SBIT(GSM_CSTATE_ACTIVE),
3189 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3190 {SBIT(GSM_CSTATE_ACTIVE),
3191 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3192 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3193 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3194 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3195 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3198 /* clearing */
3199 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3200 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3201 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3202 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3203 {ALL_STATES, /* 5.4.3.4 */
3204 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3205};
3206
3207#define DATASLLEN \
3208 (sizeof(datastatelist) / sizeof(struct datastate))
3209
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003210static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003211{
3212 struct gsm48_hdr *gh = msgb_l3(msg);
3213 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003214 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003215 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003216 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003217
Harald Welte4bfdfe72009-06-10 23:11:52 +08003218 if (msg_type & 0x80) {
3219 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3220 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003221 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003222
Harald Welte4bfdfe72009-06-10 23:11:52 +08003223 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003224 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003225
Harald Welte6f5aee02009-07-23 21:21:14 +02003226 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003228 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003229 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003230 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3231 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003232
3233 /* Create transaction */
3234 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003235 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003236 "creating new trans.\n", transaction_id);
3237 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003238 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003239 transaction_id, new_callref++);
3240 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003241 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003242 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003243 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244 GSM48_MT_CC_RELEASE_COMPL);
3245 return -ENOMEM;
3246 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003248 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249 }
3250
3251 /* find function for current state and message */
3252 for (i = 0; i < DATASLLEN; i++)
3253 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003254 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 break;
3256 if (i == DATASLLEN) {
3257 DEBUGP(DCC, "Message unhandled at this state.\n");
3258 return 0;
3259 }
3260
3261 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003262
3263 return rc;
3264}
3265
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003266/* Create a dummy to wait five seconds */
3267static void release_anchor(struct gsm_subscriber_connection *conn)
3268{
3269 if (!conn->anch_operation)
3270 return;
3271
3272 bsc_del_timer(&conn->anch_operation->timeout);
3273 talloc_free(conn->anch_operation);
3274 conn->anch_operation = NULL;
3275}
3276
3277static void anchor_timeout(void *_data)
3278{
3279 struct gsm_subscriber_connection *con = _data;
3280
3281 release_anchor(con);
3282 msc_release_connection(con);
3283}
3284
3285int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3286{
3287 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3288 if (!conn->anch_operation)
3289 return -1;
3290
3291 conn->anch_operation->timeout.data = conn;
3292 conn->anch_operation->timeout.cb = anchor_timeout;
3293 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3294 return 0;
3295}
3296
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003297/* here we get data from the BSC level... */
3298int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003299{
3300 struct gsm48_hdr *gh = msgb_l3(msg);
3301 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003302 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003303
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003304 if (silent_call_reroute(conn, msg))
3305 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003306
3307 switch (pdisc) {
3308 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003309 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003310 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003311 break;
3312 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003313 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003314 break;
3315 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003316 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003317 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003318 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003319 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003320 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003321 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003322 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003323 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003324 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3325 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003326 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003327 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003328 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003329 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003330 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003331 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003332 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3333 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003334 break;
3335 }
3336
3337 return rc;
3338}
Harald Welte8470bf22008-12-25 23:28:35 +00003339
Harald Welte805f6442009-07-28 18:25:29 +02003340/*
3341 * This will be ran by the linker when loading the DSO. We use it to
3342 * do system initialization, e.g. registration of signal handlers.
3343 */
3344static __attribute__((constructor)) void on_dso_load_0408(void)
3345{
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01003346 register_signal_handler(SS_HO, handle_ho_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003347 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3348}