blob: 42dd1b70e30557a2375fd696a45354dceeceed21 [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Sylvain Munaut30a15382009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000035#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000038#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080040#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000045#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000046#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020047#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020048#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020049#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010050#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080051#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080052#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080053#include <osmocore/bitvec.h>
54
55#include <osmocore/gsm48.h>
Holger Hans Peter Freyther12eb9b72010-09-30 18:53:47 +080056#include <osmocore/gsm0480.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057#include <osmocore/gsm_utils.h>
58#include <osmocore/msgb.h>
59#include <osmocore/talloc.h>
60#include <osmocore/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000061
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020062void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010063void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020064
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080065int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080066static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000067 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010068static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080069static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000070
Harald Welte52b1f982008-12-23 20:25:15 +000071struct gsm_lai {
72 u_int16_t mcc;
73 u_int16_t mnc;
74 u_int16_t lac;
75};
76
Harald Welte4bfdfe72009-06-10 23:11:52 +080077static u_int32_t new_callref = 0x80000001;
78
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080079static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
80 struct gsm_trans *trans)
81{
82 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
83
84 /* if we get passed a transaction reference, do some common
85 * work that the caller no longer has to do */
86 if (trans) {
87 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
88 msg->lchan = trans->conn->lchan;
89 }
90
91
92 if (msg->lchan) {
93 msg->trx = msg->lchan->ts->trx;
94 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
95 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
96 "Sending '%s' to MS.\n", msg->trx->bts->nr,
97 msg->trx->nr, msg->lchan->ts->nr,
98 gh->proto_discr & 0xf0,
99 gsm48_cc_msg_name(gh->msg_type));
100 else
101 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
102 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
103 msg->trx->nr, msg->lchan->ts->nr,
104 gh->proto_discr, gh->msg_type);
105 }
106
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100107 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800108}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100109
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800110int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
111{
112 struct gsm48_hdr *gh;
113 struct msgb *ss_notify;
114
115 ss_notify = gsm0480_create_notifySS(message);
116 if (!ss_notify)
117 return -1;
118
119 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
120 uint8_t *data = msgb_push(ss_notify, 1);
121 data[0] = ss_notify->len - 1;
122 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
123 gh->msg_type = GSM48_MT_CC_FACILITY;
124 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
125}
126
Sylvain Munaut30a15382009-12-24 00:27:26 +0100127static void release_security_operation(struct gsm_subscriber_connection *conn)
128{
129 if (!conn->sec_operation)
130 return;
131
132 talloc_free(conn->sec_operation);
133 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800134 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100135}
136
137static void allocate_security_operation(struct gsm_subscriber_connection *conn)
138{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100139 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
140 struct gsm_security_operation);
141}
142
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100143int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
144 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100145{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800146 struct gsm_network *net = conn->bts->network;
147 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100148 struct gsm_security_operation *op;
149 struct gsm_auth_tuple atuple;
150 int status = -1, rc;
151
152 /* Check if we _can_ enable encryption. Cases where we can't:
153 * - Encryption disabled in config
154 * - Channel already secured (nothing to do)
155 * - Subscriber equipment doesn't support configured encryption
156 */
157 if (!net->a5_encryption) {
158 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800159 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100160 DEBUGP(DMM, "Requesting to secure an already secure channel");
161 status = GSM_SECURITY_SUCCEEDED;
162 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
163 net->a5_encryption)) {
164 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
165 status = GSM_SECURITY_NOAVAIL;
166 }
167
168 /* If not done yet, try to get info for this user */
169 if (status < 0) {
170 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
171 if (rc <= 0)
172 status = GSM_SECURITY_NOAVAIL;
173 }
174
175 /* Are we done yet ? */
176 if (status >= 0)
177 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800178 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100179 0;
180
181 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800182 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183 return -EBUSY;
184
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800185 allocate_security_operation(conn);
186 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100187 op->cb = cb;
188 op->cb_data = cb_data;
189 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
190
191 /* FIXME: Should start a timer for completion ... */
192
193 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100194 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100195 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800196 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100197 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100199 return gsm0808_cipher_mode(conn, net->a5_encryption,
200 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100201 }
202
203 return -EINVAL; /* not reached */
204}
205
Holger Freyther73487a22008-12-31 18:53:57 +0000206static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
207 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000208{
209 if (!subscriber)
210 return 0;
211
Holger Freyther73487a22008-12-31 18:53:57 +0000212 /*
213 * Do not send accept yet as more information should arrive. Some
214 * phones will not send us the information and we will have to check
215 * what we want to do with that.
216 */
217 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
218 return 0;
219
Jan Luebbe06513f22009-08-12 12:48:00 +0200220 switch (subscriber->net->auth_policy) {
221 case GSM_AUTH_POLICY_CLOSED:
222 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200223 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200224 if (subscriber->authorized)
225 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200226 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200227 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000228 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200229 default:
230 return 0;
231 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000232}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000233
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100234static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000235{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100236 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000237 return;
238
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800239 /* No need to keep the connection up */
240 release_anchor(conn);
241
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100242 bsc_del_timer(&conn->loc_operation->updating_timer);
243 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100244 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800245 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000246}
247
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100248static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000249{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800250 if (conn->loc_operation)
251 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100252 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000253
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100254 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200255 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000256}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000257
Sylvain Munaut267fba02009-12-24 00:28:01 +0100258static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
259 struct msgb *msg, void *data, void *param)
260{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800261 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100262 int rc = 0;
263
264 switch (event) {
265 case GSM_SECURITY_AUTH_FAILED:
266 release_loc_updating_req(conn);
267 break;
268
269 case GSM_SECURITY_NOAVAIL:
270 case GSM_SECURITY_SUCCEEDED:
271 /* We're all good */
272 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800273 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800274 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100275 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800276 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100277 }
278
279 /* call subscr_update after putting the loc_upd_acc
280 * in the transmit queue, since S_SUBSCR_ATTACHED might
281 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800282 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100283 GSM_SUBSCRIBER_UPDATE_ATTACHED);
284
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100285 /*
286 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
287 * MS needs to respond with a TMSI REALLOCATION COMPLETE
288 * (even if the TMSI is the same).
289 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100290 break;
291
292 default:
293 rc = -EINVAL;
294 };
295
296 return rc;
297}
298
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100299static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000300{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100301 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800302 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100303 conn->loc_operation->key_seq,
304 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000305 return 0;
306}
307
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800308void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000309{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800310 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800311
312 /* avoid someone issuing a clear */
313 conn->in_release = 1;
314
Holger Freyther7c19f742009-06-06 13:54:35 +0000315 /*
316 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800317 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000318 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800319 release_loc_updating_req(conn);
320 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800321 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000322
Harald Welte4bfdfe72009-06-10 23:11:52 +0800323 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200324 /* FIXME: this is not neccessarily the right thing to do, we should
325 * only set trans->lchan to NULL and wait for another lchan to be
326 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800327 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
328 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200329 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800330 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000331}
332
Holger Freyther429e7762008-12-30 13:28:30 +0000333/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800334int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000335{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800336 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800337 struct msgb *msg;
338
339 counter_inc(bts->network->stats.loc_upd_resp.reject);
340
341 msg = gsm48_create_loc_upd_rej(cause);
342 if (!msg) {
343 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
344 return -1;
345 }
Harald Welte52b1f982008-12-23 20:25:15 +0000346
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800347 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000348
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100349 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100350 "LAC=%u BTS=%u\n", conn->subscr ?
351 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800352 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100353
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800354 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000355}
356
357/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800358int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000359{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800360 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000361 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000362 struct gsm48_hdr *gh;
363 struct gsm48_loc_area_id *lai;
364 u_int8_t *mid;
365
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800366 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000367
368 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
369 gh->proto_discr = GSM48_PDISC_MM;
370 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
371
372 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100373 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000374 bts->network->network_code, bts->location_area_code);
375
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200376 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200377 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000378
379 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
380
Harald Welteffa55a42009-12-22 19:07:32 +0100381 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100382
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800383 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000384}
385
Harald Weltebf5e8df2009-02-03 12:59:45 +0000386/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800387static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000388{
389 struct msgb *msg = gsm48_msgb_alloc();
390 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000391
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800392 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000393
394 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
395 gh->proto_discr = GSM48_PDISC_MM;
396 gh->msg_type = GSM48_MT_MM_ID_REQ;
397 gh->data[0] = id_type;
398
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800399 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000400}
401
Harald Welte231ad4f2008-12-27 11:15:38 +0000402
Harald Weltebf5e8df2009-02-03 12:59:45 +0000403/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800404static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000405{
406 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000407 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200408 struct gsm_bts *bts = lchan->ts->trx->bts;
409 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000410 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200411 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000412
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200413 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000414 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000415 mi_type, mi_string);
416
Harald Welte7659de12009-12-13 12:39:18 +0100417 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
418
Harald Welte75a983f2008-12-27 21:34:06 +0000419 switch (mi_type) {
420 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200421 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100422 if (!conn->subscr) {
423 conn->subscr = subscr_get_by_imsi(net, mi_string);
424 if (!conn->subscr)
425 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200426 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100427 if (conn->loc_operation)
428 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000429 break;
430 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000431 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000432 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100433 if (conn->subscr) {
434 db_subscriber_assoc_imei(conn->subscr, mi_string);
435 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200436 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100437 if (conn->loc_operation)
438 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000439 break;
440 }
Holger Freyther73487a22008-12-31 18:53:57 +0000441
442 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100443 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000444}
445
Harald Welte255539c2008-12-28 02:26:27 +0000446
447static void loc_upd_rej_cb(void *data)
448{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100449 struct gsm_subscriber_connection *conn = data;
450 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100451 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000452
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800453 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800454 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000455}
456
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100457static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000458{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100459 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
460 conn->loc_operation->updating_timer.data = conn;
461 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000462}
463
Harald Welte2a139372009-02-22 21:14:55 +0000464static const char *lupd_name(u_int8_t type)
465{
466 switch (type) {
467 case GSM48_LUPD_NORMAL:
468 return "NORMAL";
469 case GSM48_LUPD_PERIODIC:
470 return "PEROIDOC";
471 case GSM48_LUPD_IMSI_ATT:
472 return "IMSI ATTACH";
473 default:
474 return "UNKNOWN";
475 }
476}
477
Harald Weltebf5e8df2009-02-03 12:59:45 +0000478/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800479static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000480{
Harald Welte8470bf22008-12-25 23:28:35 +0000481 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000482 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800483 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800484 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000485 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200486 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000487 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000488
Harald Welte8470bf22008-12-25 23:28:35 +0000489 lu = (struct gsm48_loc_upd_req *) gh->data;
490
491 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000492
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200493 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000494
Harald Weltea0368542009-06-27 02:58:43 +0200495 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000496 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000497
Harald Welte7659de12009-12-13 12:39:18 +0100498 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
499
Harald Welte24ff6ee2009-12-22 00:41:05 +0100500 switch (lu->type) {
501 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100502 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100503 break;
504 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100505 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100506 break;
507 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100508 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100509 break;
510 }
511
Holger Freythereaf04692009-06-06 13:54:44 +0000512 /*
513 * Pseudo Spoof detection: Just drop a second/concurrent
514 * location updating request.
515 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100516 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200517 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100518 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800519 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000520 return 0;
521 }
522
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800523 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000524
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200525 conn->loc_operation->key_seq = lu->key_seq;
526
Harald Welte52b1f982008-12-23 20:25:15 +0000527 switch (mi_type) {
528 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200529 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000530 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800531 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100532 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000533
Jan Luebbe370b41d2009-08-12 10:19:34 +0200534 /* look up subscriber based on IMSI, create if not found */
535 subscr = subscr_get_by_imsi(bts->network, mi_string);
536 if (!subscr) {
537 subscr = db_create_subscriber(bts->network, mi_string);
538 }
Harald Welte4b634542008-12-27 01:55:51 +0000539 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000540 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200541 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000542 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200543 subscr = subscr_get_by_tmsi(bts->network,
544 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000545 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000546 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800547 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100548 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000549 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200550 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800551 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200552 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000553 break;
554 case GSM_MI_TYPE_IMEI:
555 case GSM_MI_TYPE_IMEISV:
556 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200557 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000558 break;
559 default:
Harald Weltea0368542009-06-27 02:58:43 +0200560 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000561 break;
562 }
563
Harald Welte24516ea2009-07-04 10:18:00 +0200564 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100565 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200566
Harald Welte4bfdfe72009-06-10 23:11:52 +0800567 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200568 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800569 /* FIXME: request id? close channel? */
570 return -EINVAL;
571 }
572
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100573 conn->subscr = subscr;
574 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000575
Harald Welte24516ea2009-07-04 10:18:00 +0200576 /* check if we can let the subscriber into our network immediately
577 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000579}
580
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581#if 0
582static u_int8_t to_bcd8(u_int8_t val)
583{
584 return ((val / 10) << 4) | (val % 10);
585}
586#endif
587
Harald Weltedb253af2008-12-30 17:56:55 +0000588/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800589int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000590{
591 struct msgb *msg = gsm48_msgb_alloc();
592 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800593 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000594 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200595 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800596#if 0
597 time_t cur_t;
598 struct tm* cur_time;
599 int tz15min;
600#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000601
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800602 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000603
604 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
605 gh->proto_discr = GSM48_PDISC_MM;
606 gh->msg_type = GSM48_MT_MM_INFO;
607
608 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200609#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000610 name_len = strlen(net->name_long);
611 /* 10.5.3.5a */
612 ptr8 = msgb_put(msg, 3);
613 ptr8[0] = GSM48_IE_NAME_LONG;
614 ptr8[1] = name_len*2 +1;
615 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
616
617 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
618 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000619 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000620
621 /* FIXME: Use Cell Broadcast, not UCS-2, since
622 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200623#endif
624 name_len = (strlen(net->name_long)*7)/8;
625 name_pad = (8 - strlen(net->name_long)*7)%8;
626 if (name_pad > 0)
627 name_len++;
628 /* 10.5.3.5a */
629 ptr8 = msgb_put(msg, 3);
630 ptr8[0] = GSM48_IE_NAME_LONG;
631 ptr8[1] = name_len +1;
632 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
633
634 ptr8 = msgb_put(msg, name_len);
635 gsm_7bit_encode(ptr8, net->name_long);
636
Harald Weltedb253af2008-12-30 17:56:55 +0000637 }
638
639 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200640#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000641 name_len = strlen(net->name_short);
642 /* 10.5.3.5a */
643 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200644 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000645 ptr8[1] = name_len*2 + 1;
646 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
647
Harald Weltee872cb12009-01-01 00:33:37 +0000648 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000649 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000650 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200651#endif
652 name_len = (strlen(net->name_short)*7)/8;
653 name_pad = (8 - strlen(net->name_short)*7)%8;
654 if (name_pad > 0)
655 name_len++;
656 /* 10.5.3.5a */
657 ptr8 = (u_int8_t *) msgb_put(msg, 3);
658 ptr8[0] = GSM48_IE_NAME_SHORT;
659 ptr8[1] = name_len +1;
660 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
661
662 ptr8 = msgb_put(msg, name_len);
663 gsm_7bit_encode(ptr8, net->name_short);
664
Harald Weltedb253af2008-12-30 17:56:55 +0000665 }
666
667#if 0
668 /* Section 10.5.3.9 */
669 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000671 ptr8 = msgb_put(msg, 8);
672 ptr8[0] = GSM48_IE_NET_TIME_TZ;
673 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
674 ptr8[2] = to_bcd8(cur_time->tm_mon);
675 ptr8[3] = to_bcd8(cur_time->tm_mday);
676 ptr8[4] = to_bcd8(cur_time->tm_hour);
677 ptr8[5] = to_bcd8(cur_time->tm_min);
678 ptr8[6] = to_bcd8(cur_time->tm_sec);
679 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
680 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800681 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000682 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000684#endif
685
Daniel Willmanneea93372009-08-13 03:42:07 +0200686 DEBUGP(DMM, "-> MM INFO\n");
687
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800688 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000689}
690
Harald Welte7984d5c2009-08-12 22:56:50 +0200691/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800692int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200693{
694 struct msgb *msg = gsm48_msgb_alloc();
695 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200696 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200697
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100698 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200699
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800700 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200701 gh->proto_discr = GSM48_PDISC_MM;
702 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
703
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100704 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200705
Harald Welte7984d5c2009-08-12 22:56:50 +0200706 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200707 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200708 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200709
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800710 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200711}
712
713/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800714int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200715{
716 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800717 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200718}
719
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800720static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000721{
Harald Welte4b634542008-12-27 01:55:51 +0000722 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800723 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000724}
Harald Welteba4cf162009-01-10 01:49:35 +0000725
726/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100727static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000728 enum gsm48_reject_value value)
729{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800730 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000731
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800732 msg = gsm48_create_mm_serv_rej(value);
733 if (!msg) {
734 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
735 return -1;
736 }
Harald Welteba4cf162009-01-10 01:49:35 +0000737
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800738 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100739 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800740 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000741}
742
Sylvain Munautba87f452009-12-24 00:28:46 +0100743static int _gsm48_rx_mm_serv_req_sec_cb(
744 unsigned int hooknum, unsigned int event,
745 struct msgb *msg, void *data, void *param)
746{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800747 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100748 int rc = 0;
749
750 switch (event) {
751 case GSM_SECURITY_AUTH_FAILED:
752 /* Nothing to do */
753 break;
754
755 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800756 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100757 break;
758
759 case GSM_SECURITY_SUCCEEDED:
760 /* nothing to do. CIPHER MODE COMMAND is
761 * implicit CM SERV ACK */
762 break;
763
764 default:
765 rc = -EINVAL;
766 };
767
768 return rc;
769}
770
Harald Welte4ed0e922009-01-10 03:17:30 +0000771/*
772 * Handle CM Service Requests
773 * a) Verify that the packet is long enough to contain the information
774 * we require otherwsie reject with INCORRECT_MESSAGE
775 * b) Try to parse the TMSI. If we do not have one reject
776 * c) Check that we know the subscriber with the TMSI otherwise reject
777 * with a HLR cause
778 * d) Set the subscriber on the gsm_lchan and accept
779 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800780static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000781{
Harald Welteba4cf162009-01-10 01:49:35 +0000782 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200783 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000784
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800785 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000786 struct gsm_subscriber *subscr;
787 struct gsm48_hdr *gh = msgb_l3(msg);
788 struct gsm48_service_request *req =
789 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800790 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000791 u_int8_t classmark2_len = gh->data[1];
792 u_int8_t *classmark2 = gh->data+2;
793 u_int8_t mi_len = *(classmark2 + classmark2_len);
794 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000795
Harald Weltec9e02182009-05-01 19:07:53 +0000796 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000797 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000798 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800799 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000800 GSM48_REJECT_INCORRECT_MESSAGE);
801 }
802
803 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000804 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800805 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000806 GSM48_REJECT_INCORRECT_MESSAGE);
807 }
808
Harald Weltec9e02182009-05-01 19:07:53 +0000809 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000810 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000811 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800812 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000813 GSM48_REJECT_INCORRECT_MESSAGE);
814 }
815
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200816 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000817 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000818 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000819
Harald Welte7659de12009-12-13 12:39:18 +0100820 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
821
Harald Welte3ac7f102009-08-10 10:12:45 +0200822 if (is_siemens_bts(bts))
823 send_siemens_mrpci(msg->lchan, classmark2-1);
824
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200825 subscr = subscr_get_by_tmsi(bts->network,
826 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000827
Harald Welte2a139372009-02-22 21:14:55 +0000828 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000829 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800830 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000831 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
832
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800833 if (!conn->subscr)
834 conn->subscr = subscr;
835 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100836 subscr_put(subscr); /* lchan already has a ref, don't need another one */
837 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000838 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
839 subscr_put(subscr);
840 }
841
Harald Weltec2e302d2009-07-05 14:08:13 +0200842 subscr->equipment.classmark2_len = classmark2_len;
843 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
844 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000845
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800846 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100847 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000848}
849
Harald Welte2a139372009-02-22 21:14:55 +0000850static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
851{
Harald Welte9176bd42009-07-23 18:46:00 +0200852 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000853 struct gsm48_hdr *gh = msgb_l3(msg);
854 struct gsm48_imsi_detach_ind *idi =
855 (struct gsm48_imsi_detach_ind *) gh->data;
856 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200857 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800858 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000859
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200860 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000861 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
862 mi_type, mi_string);
863
Harald Welteffa55a42009-12-22 19:07:32 +0100864 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100865
Harald Welte2a139372009-02-22 21:14:55 +0000866 switch (mi_type) {
867 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200868 subscr = subscr_get_by_tmsi(bts->network,
869 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000870 break;
871 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200872 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000873 break;
874 case GSM_MI_TYPE_IMEI:
875 case GSM_MI_TYPE_IMEISV:
876 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000877 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000878 break;
879 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000880 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000881 break;
882 }
883
Holger Freyther4a49e772009-04-12 05:37:29 +0000884 if (subscr) {
885 subscr_update(subscr, msg->trx->bts,
886 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100887 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200888
889 subscr->equipment.classmark1 = idi->classmark1;
890 db_sync_equipment(&subscr->equipment);
891
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000892 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000893 } else
Harald Welte2a139372009-02-22 21:14:55 +0000894 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
895
Harald Welte31981a02009-12-20 09:58:40 +0100896 /* FIXME: iterate over all transactions and release them,
897 * imagine an IMSI DETACH happening during an active call! */
898
Harald Welteb83d9382009-12-12 21:00:48 +0100899 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000900 return 0;
901}
902
Harald Welted2a7f5a2009-06-05 20:08:20 +0000903static int gsm48_rx_mm_status(struct msgb *msg)
904{
905 struct gsm48_hdr *gh = msgb_l3(msg);
906
907 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
908
909 return 0;
910}
911
Sylvain Munaut30a15382009-12-24 00:27:26 +0100912/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800913static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100914{
915 struct gsm48_hdr *gh = msgb_l3(msg);
916 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800917 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100918
Sylvain Munautc593cf12010-06-10 23:51:41 +0200919 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100920 hexdump(ar->sres, 4));
921
922 /* Safety check */
923 if (!conn->sec_operation) {
924 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
925 return -EIO;
926 }
927
928 /* Validate SRES */
929 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
930 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200931
932 DEBUGPC(DMM, "Invalid (expected %s)\n",
933 hexdump(conn->sec_operation->atuple.sres, 4));
934
Sylvain Munaut30a15382009-12-24 00:27:26 +0100935 if (cb)
936 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800937 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100938
939 release_security_operation(conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800940 return gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100941 }
942
Sylvain Munautc593cf12010-06-10 23:51:41 +0200943 DEBUGPC(DMM, "OK\n");
944
Sylvain Munaut30a15382009-12-24 00:27:26 +0100945 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100946 return gsm0808_cipher_mode(conn, net->a5_encryption,
947 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100948}
949
Harald Weltebf5e8df2009-02-03 12:59:45 +0000950/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800951static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000952{
953 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800954 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000955
956 switch (gh->msg_type & 0xbf) {
957 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200958 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800959 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000960 break;
961 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800962 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000963 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000964 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800965 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000966 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000967 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000968 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000969 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000970 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000971 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800972 conn->subscr ?
973 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000974 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100975 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +0000976 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000977 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000978 rc = gsm48_rx_mm_imsi_detach_ind(msg);
979 break;
980 case GSM48_MT_MM_CM_REEST_REQ:
981 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
982 break;
983 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800984 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000985 break;
986 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100987 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000988 gh->msg_type);
989 break;
990 }
991
992 return rc;
993}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000994
Harald Welte2d35ae62009-02-06 12:02:13 +0000995/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800996static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000997{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800998 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000999 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001000 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +00001001 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001002 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001003 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001004 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001005 int rc = 0;
1006
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001007 resp = (struct gsm48_pag_resp *) &gh->data[0];
1008 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1009 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001010 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1011 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001012
Harald Weltefe18d8f2009-02-22 21:14:24 +00001013 switch (mi_type) {
1014 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001015 subscr = subscr_get_by_tmsi(bts->network,
1016 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001017 break;
1018 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001019 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001020 break;
1021 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001022
1023 if (!subscr) {
1024 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001025 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001026 return -EINVAL;
1027 }
1028 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001029 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001030
Harald Weltec2e302d2009-07-05 14:08:13 +02001031 subscr->equipment.classmark2_len = *classmark2_lv;
1032 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1033 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001034
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001035 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001036 return rc;
1037}
1038
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001039static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001040{
1041 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001042 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001043 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1044 u_int8_t cm2_len, cm3_len = 0;
1045 u_int8_t *cm2, *cm3 = NULL;
1046
1047 DEBUGP(DRR, "CLASSMARK CHANGE ");
1048
1049 /* classmark 2 */
1050 cm2_len = gh->data[0];
1051 cm2 = &gh->data[1];
1052 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1053
1054 if (payload_len > cm2_len + 1) {
1055 /* we must have a classmark3 */
1056 if (gh->data[cm2_len+1] != 0x20) {
1057 DEBUGPC(DRR, "ERR CM3 TAG\n");
1058 return -EINVAL;
1059 }
1060 if (cm2_len > 3) {
1061 DEBUGPC(DRR, "CM2 too long!\n");
1062 return -EINVAL;
1063 }
1064
1065 cm3_len = gh->data[cm2_len+2];
1066 cm3 = &gh->data[cm2_len+3];
1067 if (cm3_len > 14) {
1068 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1069 return -EINVAL;
1070 }
1071 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1072 }
1073 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001074 subscr->equipment.classmark2_len = cm2_len;
1075 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001076 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001077 subscr->equipment.classmark3_len = cm3_len;
1078 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001079 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001080 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001081 }
1082
Harald Weltef7c43522009-06-09 20:24:21 +00001083 return 0;
1084}
1085
Harald Weltecf5b3592009-05-01 18:28:42 +00001086static int gsm48_rx_rr_status(struct msgb *msg)
1087{
1088 struct gsm48_hdr *gh = msgb_l3(msg);
1089
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001090 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001091 rr_cause_name(gh->data[0]));
1092
1093 return 0;
1094}
1095
Harald Weltef7c43522009-06-09 20:24:21 +00001096static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1097{
Harald Welted12b0fd2009-12-15 21:36:05 +01001098 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001099
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001100 /* This shouldn't actually end up here, as RSL treats
1101 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1102 * directly into gsm48_parse_meas_rep */
1103 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001104 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001105
1106 return 0;
1107}
1108
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001109static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001110{
1111 struct gsm48_hdr *gh = msgb_l3(msg);
1112 u_int8_t apdu_id_flags;
1113 u_int8_t apdu_len;
1114 u_int8_t *apdu_data;
1115
1116 apdu_id_flags = gh->data[0];
1117 apdu_len = gh->data[1];
1118 apdu_data = gh->data+2;
1119
1120 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1121 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1122
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001123 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001124}
1125
Sylvain Munaut30a15382009-12-24 00:27:26 +01001126/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001127static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001129 gsm_cbfn *cb;
1130 int rc = 0;
1131
1132 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1133
1134 /* Safety check */
1135 if (!conn->sec_operation) {
1136 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1137 return -EIO;
1138 }
1139
1140 /* FIXME: check for MI (if any) */
1141
1142 /* Call back whatever was in progress (if anything) ... */
1143 cb = conn->sec_operation->cb;
1144 if (cb) {
1145 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001146 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001147 }
1148
1149 /* Complete the operation */
1150 release_security_operation(conn);
1151
1152 return rc;
1153}
1154
Harald Welted011e8b2009-11-29 22:45:52 +01001155/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001156static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001157{
1158 struct gsm48_hdr *gh = msgb_l3(msg);
1159
1160 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1161 rr_cause_name(gh->data[0]));
1162
1163 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1164 /* FIXME: release old channel */
1165
1166 return 0;
1167}
1168
1169/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001170static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001171{
1172 struct gsm48_hdr *gh = msgb_l3(msg);
1173
1174 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1175 rr_cause_name(gh->data[0]));
1176
1177 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1178 /* FIXME: release allocated new channel */
1179
1180 return 0;
1181}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001182
Harald Weltebf5e8df2009-02-03 12:59:45 +00001183/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001184static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001185{
1186 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001187 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001188
1189 switch (gh->msg_type) {
1190 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001191 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001192 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001193 case GSM48_MT_RR_GPRS_SUSP_REQ:
1194 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1195 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001196 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001197 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001198 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001199 case GSM48_MT_RR_STATUS:
1200 rc = gsm48_rx_rr_status(msg);
1201 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001202 case GSM48_MT_RR_MEAS_REP:
1203 rc = gsm48_rx_rr_meas_rep(msg);
1204 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001205 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001206 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001207 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001208 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001209 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001210 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001211 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001212 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001213 break;
1214 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001215 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001216 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001217 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001218 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1219 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001220 break;
1221 }
1222
Harald Welte2d35ae62009-02-06 12:02:13 +00001223 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001224}
1225
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001226int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001227 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001228{
1229 struct msgb *msg = gsm48_msgb_alloc();
1230 struct gsm48_hdr *gh;
1231
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001232 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001233
1234 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1235 apdu_id, apdu_len);
1236
1237 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1238 gh->proto_discr = GSM48_PDISC_RR;
1239 gh->msg_type = GSM48_MT_RR_APP_INFO;
1240 gh->data[0] = apdu_id;
1241 gh->data[1] = apdu_len;
1242 memcpy(gh->data+2, apdu, apdu_len);
1243
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001244 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245}
1246
Harald Welte4bc90a12008-12-27 16:32:52 +00001247/* Call Control */
1248
Harald Welte7584aea2009-02-11 11:44:12 +00001249/* The entire call control code is written in accordance with Figure 7.10c
1250 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1251 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1252 * it for voice */
1253
Harald Welte4bfdfe72009-06-10 23:11:52 +08001254static void new_cc_state(struct gsm_trans *trans, int state)
1255{
1256 if (state > 31 || state < 0)
1257 return;
1258
1259 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001260 gsm48_cc_state_name(trans->cc.state),
1261 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001262
Harald Weltedcaf5652009-07-23 18:56:43 +02001263 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264}
1265
1266static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001267{
1268 struct msgb *msg = gsm48_msgb_alloc();
1269 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1270 u_int8_t *cause, *call_state;
1271
Harald Welte4bc90a12008-12-27 16:32:52 +00001272 gh->msg_type = GSM48_MT_CC_STATUS;
1273
1274 cause = msgb_put(msg, 3);
1275 cause[0] = 2;
1276 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1277 cause[2] = 0x80 | 30; /* response to status inquiry */
1278
1279 call_state = msgb_put(msg, 1);
1280 call_state[0] = 0xc0 | 0x00;
1281
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001282 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001283}
1284
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001285static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001286 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001287{
1288 struct msgb *msg = gsm48_msgb_alloc();
1289 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1290
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001291 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001292
Harald Welte6f4b7532008-12-29 00:39:37 +00001293 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001294 gh->msg_type = msg_type;
1295
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001296 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001297}
1298
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1300{
Harald Weltedcaf5652009-07-23 18:56:43 +02001301 if (bsc_timer_pending(&trans->cc.timer)) {
1302 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1303 bsc_del_timer(&trans->cc.timer);
1304 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 }
1306}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001307
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1309 int msg_type, struct gsm_mncc *mncc)
1310{
1311 struct msgb *msg;
1312
1313 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001314 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001315 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001316 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001317 trans->conn->lchan->ts->trx->bts->nr,
1318 trans->conn->lchan->ts->trx->nr,
1319 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001320 (trans->subscr)?(trans->subscr->extension):"-",
1321 get_mncc_name(msg_type));
1322 else
1323 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1324 "Sending '%s' to MNCC.\n",
1325 (trans->subscr)?(trans->subscr->extension):"-",
1326 get_mncc_name(msg_type));
1327 else
1328 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1329 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1330
1331 mncc->msg_type = msg_type;
1332
Harald Welte966636f2009-06-26 19:39:35 +02001333 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334 if (!msg)
1335 return -ENOMEM;
1336 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
Harald Welte434bb3f2010-12-22 19:02:27 +01001337 mncc_upq_enqueue(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338
1339 return 0;
1340}
1341
1342int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1343 u_int32_t callref, int location, int value)
1344{
1345 struct gsm_mncc rel;
1346
Harald Welte92f70c52009-06-12 01:54:08 +08001347 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001349 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1351}
1352
Harald Weltedcaf5652009-07-23 18:56:43 +02001353/* Call Control Specific transaction release.
1354 * gets called by trans_free, DO NOT CALL YOURSELF! */
1355void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 gsm48_stop_cc_timer(trans);
1358
1359 /* send release to L4, if callref still exists */
1360 if (trans->callref) {
1361 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001362 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001363 GSM48_CAUSE_LOC_PRN_S_LU,
1364 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001366 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001368 if (trans->conn)
1369 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370}
1371
1372static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001373
Harald Welte09e38af2009-02-16 22:52:23 +00001374/* call-back from paging the B-end of the connection */
1375static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001376 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001377{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001378 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379 struct gsm_subscriber *subscr = param;
1380 struct gsm_trans *transt, *tmp;
1381 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001382
Harald Welte09e38af2009-02-16 22:52:23 +00001383 if (hooknum != GSM_HOOK_RR_PAGING)
1384 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001385
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 if (!subscr)
1387 return -EINVAL;
1388 net = subscr->net;
1389 if (!net) {
1390 DEBUGP(DCC, "Error Network not set!\n");
1391 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001392 }
Harald Welte7584aea2009-02-11 11:44:12 +00001393
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 /* check all tranactions (without lchan) for subscriber */
1395 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001396 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397 continue;
1398 switch (event) {
1399 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001400 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401 break;
1402 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1403 subscr->extension);
1404 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001405 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001406 transt->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407 }
1408 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001409 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 break;
1411 case GSM_PAGING_EXPIRED:
1412 DEBUGP(DCC, "Paging subscr %s expired!\n",
1413 subscr->extension);
1414 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001415 mncc_release_ind(transt->subscr->net, transt,
1416 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001417 GSM48_CAUSE_LOC_PRN_S_LU,
1418 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001420 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 break;
1422 }
1423 }
Harald Welte09e38af2009-02-16 22:52:23 +00001424 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001425}
Harald Welte7584aea2009-02-11 11:44:12 +00001426
Harald Welteda7ab742009-12-19 22:23:05 +01001427static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1428
Harald Welte805f6442009-07-28 18:25:29 +02001429/* some other part of the code sends us a signal */
1430static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1431 void *handler_data, void *signal_data)
1432{
1433 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001434 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001435 struct gsm_network *net;
1436 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001437
1438 if (subsys != SS_ABISIP)
1439 return 0;
1440
1441 /* in case we use direct BTS-to-BTS RTP */
1442 if (ipacc_rtp_direct)
1443 return 0;
1444
Harald Welte805f6442009-07-28 18:25:29 +02001445 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001446 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001447 /* check if any transactions on this lchan still have
1448 * a tch_recv_mncc request pending */
1449 net = lchan->ts->trx->bts->network;
1450 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001451 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001452 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1453 tch_recv_mncc(net, trans->callref, 1);
1454 }
1455 }
Harald Welte805f6442009-07-28 18:25:29 +02001456 break;
Harald Welte805f6442009-07-28 18:25:29 +02001457 }
1458
1459 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001460}
1461
Harald Welte49f48b82009-02-17 15:29:33 +00001462/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001463static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001464{
Harald Welte11fa29c2009-02-19 17:24:39 +00001465 struct gsm_bts *bts = lchan->ts->trx->bts;
1466 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001467 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001468
Harald Welte11fa29c2009-02-19 17:24:39 +00001469 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1470 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1471 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1472
1473 if (bts->type != remote_bts->type) {
1474 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1475 return -EINVAL;
1476 }
Harald Welteda7ab742009-12-19 22:23:05 +01001477
1478 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001479 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001480 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001481 if (!ipacc_rtp_direct) {
1482 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001483 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001484 if (rc < 0)
1485 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001486 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001487#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001488
1489 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301490 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1491 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001492 } else {
1493 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301494 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1495 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301496 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001497 if (rc < 0)
1498 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301499 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1500 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301501 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001502 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001503 break;
1504 case GSM_BTS_TYPE_BS11:
1505 trau_mux_map_lchan(lchan, remote_lchan);
1506 break;
1507 default:
1508 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001509 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001510 }
Harald Welte49f48b82009-02-17 15:29:33 +00001511
1512 return 0;
1513}
1514
Harald Welte4bfdfe72009-06-10 23:11:52 +08001515/* bridge channels of two transactions */
1516static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001517{
Harald Weltedcaf5652009-07-23 18:56:43 +02001518 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1519 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001520
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001522 return -EIO;
1523
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001524 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001525 return -EIO;
1526
1527 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001528 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001529}
1530
Harald Welteda7ab742009-12-19 22:23:05 +01001531/* enable receive of channels to MNCC upqueue */
1532static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001533{
1534 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001535 struct gsm_lchan *lchan;
1536 struct gsm_bts *bts;
1537 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001538
Harald Welte4bfdfe72009-06-10 23:11:52 +08001539 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001540 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541 if (!trans)
1542 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001543 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001544 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001545 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001546 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547
Harald Welteda7ab742009-12-19 22:23:05 +01001548 switch (bts->type) {
1549 case GSM_BTS_TYPE_NANOBTS:
1550 if (ipacc_rtp_direct) {
1551 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1552 return -EINVAL;
1553 }
1554 /* in case, we don't have a RTP socket yet, we note this
1555 * in the transaction and try later */
1556 if (!lchan->abis_ip.rtp_socket) {
1557 trans->tch_recv = enable;
1558 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1559 return 0;
1560 }
1561 if (enable) {
1562 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001563 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001564 if (rc < 0)
1565 return rc;
1566 /* assign socket to application interface */
1567 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1568 net, callref);
1569 } else
1570 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1571 net, 0);
1572 break;
1573 case GSM_BTS_TYPE_BS11:
1574 if (enable)
1575 return trau_recv_lchan(lchan, callref);
1576 return trau_mux_unmap(NULL, callref);
1577 break;
1578 default:
1579 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1580 return -EINVAL;
1581 }
1582
1583 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001584}
1585
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1587{
1588 DEBUGP(DCC, "-> STATUS ENQ\n");
1589 return gsm48_cc_tx_status(trans, msg);
1590}
1591
1592static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1593static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1594
1595static void gsm48_cc_timeout(void *arg)
1596{
1597 struct gsm_trans *trans = arg;
1598 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001599 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1600 int mo_location = GSM48_CAUSE_LOC_USER;
1601 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1602 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603 struct gsm_mncc mo_rel, l4_rel;
1604
1605 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1606 mo_rel.callref = trans->callref;
1607 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1608 l4_rel.callref = trans->callref;
1609
Harald Weltedcaf5652009-07-23 18:56:43 +02001610 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611 case 0x303:
1612 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001613 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001614 break;
1615 case 0x310:
1616 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001617 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001618 break;
1619 case 0x313:
1620 disconnect = 1;
1621 /* unknown, did not find it in the specs */
1622 break;
1623 case 0x301:
1624 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001625 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626 break;
1627 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001628 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001630 gsm48_cc_tx_release(trans, &trans->cc.msg);
1631 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001632 break; /* stay in release state */
1633 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001634 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001635 return;
1636// release = 1;
1637// l4_cause = 14;
1638// break;
1639 case 0x306:
1640 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001641 mo_cause = trans->cc.msg.cause.value;
1642 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 break;
1644 case 0x323:
1645 disconnect = 1;
1646 break;
1647 default:
1648 release = 1;
1649 }
1650
1651 if (release && trans->callref) {
1652 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001653 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 l4_location, l4_cause);
1655 trans->callref = 0;
1656 }
1657
1658 if (disconnect && trans->callref) {
1659 /* process disconnect towards layer 4 */
1660 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001661 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 }
1663
1664 /* process disconnect towards mobile station */
1665 if (disconnect || release) {
1666 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001667 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1668 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1669 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 mo_rel.cause.diag_len = 3;
1671
1672 if (disconnect)
1673 gsm48_cc_tx_disconnect(trans, &mo_rel);
1674 if (release)
1675 gsm48_cc_tx_release(trans, &mo_rel);
1676 }
1677
1678}
1679
1680static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1681 int sec, int micro)
1682{
1683 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001684 trans->cc.timer.cb = gsm48_cc_timeout;
1685 trans->cc.timer.data = trans;
1686 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1687 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688}
1689
1690static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1691{
1692 struct gsm48_hdr *gh = msgb_l3(msg);
1693 u_int8_t msg_type = gh->msg_type & 0xbf;
1694 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1695 struct tlv_parsed tp;
1696 struct gsm_mncc setup;
1697
1698 memset(&setup, 0, sizeof(struct gsm_mncc));
1699 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001700 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 /* emergency setup is identified by msg_type */
1702 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1703 setup.emergency = 1;
1704
1705 /* use subscriber as calling party number */
1706 if (trans->subscr) {
1707 setup.fields |= MNCC_F_CALLING;
1708 strncpy(setup.calling.number, trans->subscr->extension,
1709 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001710 strncpy(setup.imsi, trans->subscr->imsi,
1711 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 }
1713 /* bearer capability */
1714 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1715 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001716 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1718 }
1719 /* facility */
1720 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1721 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001722 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1724 }
1725 /* called party bcd number */
1726 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1727 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001728 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1730 }
1731 /* user-user */
1732 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1733 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001734 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1736 }
1737 /* ss-version */
1738 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1739 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001740 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1742 }
1743 /* CLIR suppression */
1744 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1745 setup.clir.sup = 1;
1746 /* CLIR invocation */
1747 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1748 setup.clir.inv = 1;
1749 /* cc cap */
1750 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1751 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001752 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1754 }
1755
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 new_cc_state(trans, GSM_CSTATE_INITIATED);
1757
Harald Welte (local)47df3992009-12-26 19:45:03 +01001758 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1759 subscr_name(trans->subscr), trans->subscr->extension,
1760 setup.called.number);
1761
Harald Weltea29e43a2010-12-24 16:06:33 +01001762 counter_inc(trans->subscr->net->stats.call.mo_setup);
1763
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001765 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766
Harald Welte13cac662009-07-29 12:10:35 +02001767 /* MNCC code will modify the channel asynchronously, we should
1768 * ipaccess-bind only after the modification has been made to the
1769 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 return 0;
1771}
1772
1773static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001774{
1775 struct msgb *msg = gsm48_msgb_alloc();
1776 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001778 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001779
Harald Welte7ccf7782009-02-17 01:43:01 +00001780 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001781
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* transaction id must not be assigned */
1783 if (trans->transaction_id != 0xff) { /* unasssigned */
1784 DEBUGP(DCC, "TX Setup with assigned transaction. "
1785 "This is not allowed!\n");
1786 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001787 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001788 GSM48_CAUSE_LOC_PRN_S_LU,
1789 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001791 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 return rc;
1793 }
1794
1795 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001796 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1797 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001799 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001800 GSM48_CAUSE_LOC_PRN_S_LU,
1801 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001803 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 return rc;
1805 }
Harald Welte78283ef2009-07-23 21:36:44 +02001806 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001807
Harald Welte65e74cc2008-12-29 01:55:35 +00001808 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001809
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001811
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 /* bearer capability */
1813 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001814 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815 /* facility */
1816 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001817 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 /* progress */
1819 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001820 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 /* calling party BCD number */
1822 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001823 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 /* called party BCD number */
1825 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001826 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 /* user-user */
1828 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001829 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 /* redirecting party BCD number */
1831 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001832 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833 /* signal */
1834 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001835 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836
1837 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001838
Harald Weltea29e43a2010-12-24 16:06:33 +01001839 counter_inc(trans->subscr->net->stats.call.mt_setup);
1840
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001841 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001842}
1843
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1845{
1846 struct gsm48_hdr *gh = msgb_l3(msg);
1847 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1848 struct tlv_parsed tp;
1849 struct gsm_mncc call_conf;
1850
1851 gsm48_stop_cc_timer(trans);
1852 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1853
1854 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1855 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001856 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857#if 0
1858 /* repeat */
1859 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1860 call_conf.repeat = 1;
1861 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1862 call_conf.repeat = 2;
1863#endif
1864 /* bearer capability */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1866 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1869 }
1870 /* cause */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1872 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1875 }
1876 /* cc cap */
1877 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1878 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001879 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1881 }
1882
1883 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1884
Harald Welte596fed42009-07-23 19:06:52 +02001885 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1886 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887}
1888
1889static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1890{
1891 struct gsm_mncc *proceeding = arg;
1892 struct msgb *msg = gsm48_msgb_alloc();
1893 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1894
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1896
1897 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1898
1899 /* bearer capability */
1900 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 /* facility */
1903 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 /* progress */
1906 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001909 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910}
1911
1912static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1913{
1914 struct gsm48_hdr *gh = msgb_l3(msg);
1915 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1916 struct tlv_parsed tp;
1917 struct gsm_mncc alerting;
1918
1919 gsm48_stop_cc_timer(trans);
1920 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1921
1922 memset(&alerting, 0, sizeof(struct gsm_mncc));
1923 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001924 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 /* facility */
1926 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1927 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001928 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1930 }
1931
1932 /* progress */
1933 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1934 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001935 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1937 }
1938 /* ss-version */
1939 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1940 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001941 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1943 }
1944
1945 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1946
Harald Welte596fed42009-07-23 19:06:52 +02001947 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1948 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949}
1950
1951static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1952{
1953 struct gsm_mncc *alerting = arg;
1954 struct msgb *msg = gsm48_msgb_alloc();
1955 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 gh->msg_type = GSM48_MT_CC_ALERTING;
1958
1959 /* facility */
1960 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 /* progress */
1963 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* user-user */
1966 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968
1969 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1970
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001971 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972}
1973
1974static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1975{
1976 struct gsm_mncc *progress = arg;
1977 struct msgb *msg = gsm48_msgb_alloc();
1978 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1979
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 gh->msg_type = GSM48_MT_CC_PROGRESS;
1981
1982 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001983 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 /* user-user */
1985 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001986 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001988 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989}
1990
1991static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1992{
1993 struct gsm_mncc *connect = arg;
1994 struct msgb *msg = gsm48_msgb_alloc();
1995 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1996
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997 gh->msg_type = GSM48_MT_CC_CONNECT;
1998
1999 gsm48_stop_cc_timer(trans);
2000 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2001
2002 /* facility */
2003 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 /* progress */
2006 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002007 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 /* connected number */
2009 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002010 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 /* user-user */
2012 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002013 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014
2015 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2016
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002017 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018}
2019
2020static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2021{
2022 struct gsm48_hdr *gh = msgb_l3(msg);
2023 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2024 struct tlv_parsed tp;
2025 struct gsm_mncc connect;
2026
2027 gsm48_stop_cc_timer(trans);
2028
2029 memset(&connect, 0, sizeof(struct gsm_mncc));
2030 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002031 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 /* use subscriber as connected party number */
2033 if (trans->subscr) {
2034 connect.fields |= MNCC_F_CONNECTED;
2035 strncpy(connect.connected.number, trans->subscr->extension,
2036 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002037 strncpy(connect.imsi, trans->subscr->imsi,
2038 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 }
2040 /* facility */
2041 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2042 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002043 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2045 }
2046 /* user-user */
2047 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2048 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002049 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2051 }
2052 /* ss-version */
2053 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2054 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2057 }
2058
2059 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltea29e43a2010-12-24 16:06:33 +01002060 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061
Harald Welte596fed42009-07-23 19:06:52 +02002062 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063}
2064
2065
2066static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2067{
2068 struct gsm_mncc connect_ack;
2069
2070 gsm48_stop_cc_timer(trans);
2071
2072 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltea29e43a2010-12-24 16:06:33 +01002073 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074
2075 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2076 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002077
Harald Welte596fed42009-07-23 19:06:52 +02002078 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 &connect_ack);
2080}
2081
2082static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2083{
2084 struct msgb *msg = gsm48_msgb_alloc();
2085 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2086
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2088
2089 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2090
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002091 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092}
2093
2094static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2095{
2096 struct gsm48_hdr *gh = msgb_l3(msg);
2097 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2098 struct tlv_parsed tp;
2099 struct gsm_mncc disc;
2100
2101 gsm48_stop_cc_timer(trans);
2102
2103 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2104
2105 memset(&disc, 0, sizeof(struct gsm_mncc));
2106 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002107 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 /* cause */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2110 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2113 }
2114 /* facility */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2116 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2119 }
2120 /* user-user */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2122 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2125 }
2126 /* ss-version */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2128 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2131 }
2132
Harald Welte596fed42009-07-23 19:06:52 +02002133 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134
2135}
2136
Harald Weltec66b71c2009-06-11 14:23:20 +08002137static struct gsm_mncc_cause default_cause = {
2138 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2139 .coding = 0,
2140 .rec = 0,
2141 .rec_val = 0,
2142 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2143 .diag_len = 0,
2144 .diag = { 0 },
2145};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146
2147static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2148{
2149 struct gsm_mncc *disc = arg;
2150 struct msgb *msg = gsm48_msgb_alloc();
2151 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2152
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2154
2155 gsm48_stop_cc_timer(trans);
2156 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2157
2158 /* cause */
2159 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002160 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161 else
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163
2164 /* facility */
2165 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 /* progress */
2168 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002169 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 /* user-user */
2171 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173
2174 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002175 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
2177 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2178
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002179 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180}
2181
2182static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2183{
2184 struct gsm48_hdr *gh = msgb_l3(msg);
2185 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2186 struct tlv_parsed tp;
2187 struct gsm_mncc rel;
2188 int rc;
2189
2190 gsm48_stop_cc_timer(trans);
2191
2192 memset(&rel, 0, sizeof(struct gsm_mncc));
2193 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002194 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 /* cause */
2196 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2197 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002198 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2200 }
2201 /* facility */
2202 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2203 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002204 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2206 }
2207 /* user-user */
2208 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2209 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2212 }
2213 /* ss-version */
2214 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2215 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2218 }
2219
Harald Weltedcaf5652009-07-23 18:56:43 +02002220 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002222 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002224 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002225 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002226 GSM48_MT_CC_RELEASE_COMPL);
2227 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 }
2229
2230 new_cc_state(trans, GSM_CSTATE_NULL);
2231
2232 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002233 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234
2235 return rc;
2236}
2237
2238static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2239{
2240 struct gsm_mncc *rel = arg;
2241 struct msgb *msg = gsm48_msgb_alloc();
2242 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2243
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 gh->msg_type = GSM48_MT_CC_RELEASE;
2245
2246 trans->callref = 0;
2247
2248 gsm48_stop_cc_timer(trans);
2249 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2250
2251 /* cause */
2252 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002253 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 /* facility */
2255 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 /* user-user */
2258 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260
Harald Weltedcaf5652009-07-23 18:56:43 +02002261 trans->cc.T308_second = 0;
2262 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263
Harald Weltedcaf5652009-07-23 18:56:43 +02002264 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2266
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002267 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268}
2269
2270static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2271{
2272 struct gsm48_hdr *gh = msgb_l3(msg);
2273 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2274 struct tlv_parsed tp;
2275 struct gsm_mncc rel;
2276 int rc = 0;
2277
2278 gsm48_stop_cc_timer(trans);
2279
2280 memset(&rel, 0, sizeof(struct gsm_mncc));
2281 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002282 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 /* cause */
2284 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2285 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002286 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2288 }
2289 /* facility */
2290 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2291 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002292 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2294 }
2295 /* user-user */
2296 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2297 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002298 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2300 }
2301 /* ss-version */
2302 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2303 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002304 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2306 }
2307
2308 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002309 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002311 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 MNCC_REJ_IND, &rel);
2313 break;
2314 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002315 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 MNCC_REL_CNF, &rel);
2317 break;
2318 default:
Harald Welte596fed42009-07-23 19:06:52 +02002319 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 MNCC_REL_IND, &rel);
2321 }
2322 }
2323
2324 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002325 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326
2327 return rc;
2328}
2329
2330static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2331{
2332 struct gsm_mncc *rel = arg;
2333 struct msgb *msg = gsm48_msgb_alloc();
2334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002335 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2338
2339 trans->callref = 0;
2340
2341 gsm48_stop_cc_timer(trans);
2342
2343 /* cause */
2344 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 /* facility */
2347 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 /* user-user */
2350 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002351 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352
Harald Weltec7782de2010-12-21 19:31:41 +01002353 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2354
Harald Weltedcaf5652009-07-23 18:56:43 +02002355 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356
Harald Weltec7782de2010-12-21 19:31:41 +01002357 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358}
2359
2360static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2361{
2362 struct gsm48_hdr *gh = msgb_l3(msg);
2363 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2364 struct tlv_parsed tp;
2365 struct gsm_mncc fac;
2366
2367 memset(&fac, 0, sizeof(struct gsm_mncc));
2368 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002369 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 /* facility */
2371 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2372 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002373 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2375 }
2376 /* ss-version */
2377 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2378 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002379 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2381 }
2382
Harald Welte596fed42009-07-23 19:06:52 +02002383 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384}
2385
2386static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2387{
2388 struct gsm_mncc *fac = arg;
2389 struct msgb *msg = gsm48_msgb_alloc();
2390 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2391
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 gh->msg_type = GSM48_MT_CC_FACILITY;
2393
2394 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002395 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002397 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398}
2399
2400static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2401{
2402 struct gsm_mncc hold;
2403
2404 memset(&hold, 0, sizeof(struct gsm_mncc));
2405 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002406 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2410{
2411 struct msgb *msg = gsm48_msgb_alloc();
2412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2413
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2415
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002416 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417}
2418
2419static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2420{
2421 struct gsm_mncc *hold_rej = arg;
2422 struct msgb *msg = gsm48_msgb_alloc();
2423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2424
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2426
2427 /* cause */
2428 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002429 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 else
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002433 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434}
2435
2436static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2437{
2438 struct gsm_mncc retrieve;
2439
2440 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2441 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002442 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2443 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2447{
2448 struct msgb *msg = gsm48_msgb_alloc();
2449 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2450
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2452
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002453 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2457{
2458 struct gsm_mncc *retrieve_rej = arg;
2459 struct msgb *msg = gsm48_msgb_alloc();
2460 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2461
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2463
2464 /* cause */
2465 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002466 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467 else
Harald Welte55c8f352010-03-07 23:40:35 +01002468 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002470 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471}
2472
2473static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2474{
2475 struct gsm48_hdr *gh = msgb_l3(msg);
2476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2477 struct tlv_parsed tp;
2478 struct gsm_mncc dtmf;
2479
2480 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2481 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002482 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483 /* keypad facility */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2485 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2488 }
2489
Harald Welte596fed42009-07-23 19:06:52 +02002490 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491}
2492
2493static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2494{
2495 struct gsm_mncc *dtmf = arg;
2496 struct msgb *msg = gsm48_msgb_alloc();
2497 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2498
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2500
2501 /* keypad */
2502 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002503 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002505 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506}
2507
2508static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2509{
2510 struct gsm_mncc *dtmf = arg;
2511 struct msgb *msg = gsm48_msgb_alloc();
2512 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2513
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2515
2516 /* cause */
2517 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002518 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 else
Harald Welte55c8f352010-03-07 23:40:35 +01002520 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002522 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523}
2524
2525static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2526{
2527 struct msgb *msg = gsm48_msgb_alloc();
2528 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2529
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2531
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002532 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533}
2534
2535static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2536{
2537 struct gsm_mncc dtmf;
2538
2539 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2540 dtmf.callref = trans->callref;
2541
Harald Welte596fed42009-07-23 19:06:52 +02002542 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2546{
2547 struct gsm48_hdr *gh = msgb_l3(msg);
2548 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2549 struct tlv_parsed tp;
2550 struct gsm_mncc modify;
2551
2552 memset(&modify, 0, sizeof(struct gsm_mncc));
2553 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002554 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 /* bearer capability */
2556 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2557 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002558 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2560 }
2561
2562 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2563
Harald Welte596fed42009-07-23 19:06:52 +02002564 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565}
2566
2567static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2568{
2569 struct gsm_mncc *modify = arg;
2570 struct msgb *msg = gsm48_msgb_alloc();
2571 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2572
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 gh->msg_type = GSM48_MT_CC_MODIFY;
2574
2575 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2576
2577 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579
2580 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2581
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002582 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
2585static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2586{
2587 struct gsm48_hdr *gh = msgb_l3(msg);
2588 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2589 struct tlv_parsed tp;
2590 struct gsm_mncc modify;
2591
2592 gsm48_stop_cc_timer(trans);
2593
2594 memset(&modify, 0, sizeof(struct gsm_mncc));
2595 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002596 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 /* bearer capability */
2598 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2599 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2602 }
2603
2604 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2605
Harald Welte596fed42009-07-23 19:06:52 +02002606 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2610{
2611 struct gsm_mncc *modify = arg;
2612 struct msgb *msg = gsm48_msgb_alloc();
2613 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2614
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2616
2617 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619
2620 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2621
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002622 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623}
2624
2625static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2626{
2627 struct gsm48_hdr *gh = msgb_l3(msg);
2628 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2629 struct tlv_parsed tp;
2630 struct gsm_mncc modify;
2631
2632 gsm48_stop_cc_timer(trans);
2633
2634 memset(&modify, 0, sizeof(struct gsm_mncc));
2635 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002636 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 /* bearer capability */
2638 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2639 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002640 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2642 }
2643 /* cause */
2644 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2645 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002646 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2648 }
2649
2650 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2651
Harald Welte596fed42009-07-23 19:06:52 +02002652 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653}
2654
2655static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2656{
2657 struct gsm_mncc *modify = arg;
2658 struct msgb *msg = gsm48_msgb_alloc();
2659 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2660
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2662
2663 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002664 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002666 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667
2668 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2669
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002670 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671}
2672
2673static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2674{
2675 struct gsm_mncc *notify = arg;
2676 struct msgb *msg = gsm48_msgb_alloc();
2677 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2678
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 gh->msg_type = GSM48_MT_CC_NOTIFY;
2680
2681 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002682 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002684 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685}
2686
2687static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2688{
2689 struct gsm48_hdr *gh = msgb_l3(msg);
2690 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2691// struct tlv_parsed tp;
2692 struct gsm_mncc notify;
2693
2694 memset(&notify, 0, sizeof(struct gsm_mncc));
2695 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002696// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002698 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002699
Harald Welte596fed42009-07-23 19:06:52 +02002700 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701}
2702
2703static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2704{
2705 struct gsm_mncc *user = arg;
2706 struct msgb *msg = gsm48_msgb_alloc();
2707 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2708
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 gh->msg_type = GSM48_MT_CC_USER_INFO;
2710
2711 /* user-user */
2712 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002713 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 /* more data */
2715 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002716 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002718 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719}
2720
2721static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2722{
2723 struct gsm48_hdr *gh = msgb_l3(msg);
2724 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2725 struct tlv_parsed tp;
2726 struct gsm_mncc user;
2727
2728 memset(&user, 0, sizeof(struct gsm_mncc));
2729 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002730 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731 /* user-user */
2732 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2733 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002734 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2736 }
2737 /* more data */
2738 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2739 user.more = 1;
2740
Harald Welte596fed42009-07-23 19:06:52 +02002741 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742}
2743
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002744static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745{
2746 struct gsm_mncc *mode = arg;
2747
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002748 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749}
2750
2751static struct downstate {
2752 u_int32_t states;
2753 int type;
2754 int (*rout) (struct gsm_trans *trans, void *arg);
2755} downstatelist[] = {
2756 /* mobile originating call establishment */
2757 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2758 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2759 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2760 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2761 {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 */
2762 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2763 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2764 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2765 /* mobile terminating call establishment */
2766 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2767 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2768 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2769 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2770 /* signalling during call */
2771 {SBIT(GSM_CSTATE_ACTIVE),
2772 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2773 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2774 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2775 {ALL_STATES,
2776 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2777 {ALL_STATES,
2778 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2779 {ALL_STATES,
2780 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2781 {SBIT(GSM_CSTATE_ACTIVE),
2782 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2783 {SBIT(GSM_CSTATE_ACTIVE),
2784 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2785 {SBIT(GSM_CSTATE_ACTIVE),
2786 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2787 {SBIT(GSM_CSTATE_ACTIVE),
2788 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2789 {SBIT(GSM_CSTATE_ACTIVE),
2790 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2791 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2792 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2793 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2794 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2795 {SBIT(GSM_CSTATE_ACTIVE),
2796 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2797 /* clearing */
2798 {SBIT(GSM_CSTATE_INITIATED),
2799 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2800 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2801 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2802 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2803 MNCC_REL_REQ, gsm48_cc_tx_release},
2804 /* special */
2805 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002806 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807};
2808
2809#define DOWNSLLEN \
2810 (sizeof(downstatelist) / sizeof(struct downstate))
2811
2812
2813int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2814{
Harald Welte1a6f7982009-08-09 18:52:33 +02002815 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002817 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819 struct gsm_mncc *data = arg, rel;
2820
2821 /* handle special messages */
2822 switch(msg_type) {
2823 case MNCC_BRIDGE:
2824 return tch_bridge(net, arg);
2825 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002826 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002828 return tch_recv_mncc(net, data->callref, 1);
2829 case GSM_TCHF_FRAME:
2830 /* Find callref */
2831 trans = trans_find_by_callref(net, data->callref);
2832 if (!trans)
2833 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002834 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002835 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002836 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002837 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002838 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002839 switch (bts->type) {
2840 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002841 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002842 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002843 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002844 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002845 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002846 default:
2847 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2848 }
2849 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 }
2851
2852 memset(&rel, 0, sizeof(struct gsm_mncc));
2853 rel.callref = data->callref;
2854
2855 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002856 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857
2858 /* Callref unknown */
2859 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002860 struct gsm_subscriber *subscr;
2861
Harald Welte4a3464c2009-07-04 10:11:24 +02002862 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2864 "Received '%s' from MNCC with "
2865 "unknown callref %d\n", data->called.number,
2866 get_mncc_name(msg_type), data->callref);
2867 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002868 return mncc_release_ind(net, NULL, data->callref,
2869 GSM48_CAUSE_LOC_PRN_S_LU,
2870 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002872 if (!data->called.number[0] && !data->imsi[0]) {
2873 DEBUGP(DCC, "(bts - trx - ts - ti) "
2874 "Received '%s' from MNCC with "
2875 "no number or IMSI\n", get_mncc_name(msg_type));
2876 /* Invalid number */
2877 return mncc_release_ind(net, NULL, data->callref,
2878 GSM48_CAUSE_LOC_PRN_S_LU,
2879 GSM48_CC_CAUSE_INV_NR_FORMAT);
2880 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002882 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002883 subscr = subscr_get_by_extension(net,
2884 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002885 else
Harald Welte9176bd42009-07-23 18:46:00 +02002886 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 /* If subscriber is not found */
2888 if (!subscr) {
2889 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2890 "Received '%s' from MNCC with "
2891 "unknown subscriber %s\n", data->called.number,
2892 get_mncc_name(msg_type), data->called.number);
2893 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002894 return mncc_release_ind(net, NULL, data->callref,
2895 GSM48_CAUSE_LOC_PRN_S_LU,
2896 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 }
2898 /* If subscriber is not "attached" */
2899 if (!subscr->lac) {
2900 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2901 "Received '%s' from MNCC with "
2902 "detached subscriber %s\n", data->called.number,
2903 get_mncc_name(msg_type), data->called.number);
2904 subscr_put(subscr);
2905 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002906 return mncc_release_ind(net, NULL, data->callref,
2907 GSM48_CAUSE_LOC_PRN_S_LU,
2908 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 }
2910 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002911 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2912 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 DEBUGP(DCC, "No memory for trans.\n");
2914 subscr_put(subscr);
2915 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002916 mncc_release_ind(net, NULL, data->callref,
2917 GSM48_CAUSE_LOC_PRN_S_LU,
2918 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919 return -ENOMEM;
2920 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002922 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002923
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002925 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926 /* find transaction with this subscriber already paging */
2927 llist_for_each_entry(transt, &net->trans_list, entry) {
2928 /* Transaction of our lchan? */
2929 if (transt == trans ||
2930 transt->subscr != subscr)
2931 continue;
2932 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2933 "Received '%s' from MNCC with "
2934 "unallocated channel, paging already "
2935 "started.\n", bts->nr,
2936 data->called.number,
2937 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002938 subscr_put(subscr);
2939 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 return 0;
2941 }
2942 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002943 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002944
2945 /* Get a channel */
2946 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2947
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002948 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 return 0;
2950 }
2951 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002952 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002953 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002955
2956 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002957 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958
2959 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002960 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002962 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 (trans->subscr)?(trans->subscr->extension):"-",
2964 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002965 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2966 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 if (msg_type == MNCC_REL_REQ)
2968 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2969 else
2970 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2971 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002972 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973 return rc;
2974 }
2975
2976 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2977 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002978 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002980 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002981 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08002982 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983
2984 /* Find function for current state and message */
2985 for (i = 0; i < DOWNSLLEN; i++)
2986 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002987 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 break;
2989 if (i == DOWNSLLEN) {
2990 DEBUGP(DCC, "Message unhandled at this state.\n");
2991 return 0;
2992 }
2993
2994 rc = downstatelist[i].rout(trans, arg);
2995
2996 return rc;
2997}
2998
2999
3000static struct datastate {
3001 u_int32_t states;
3002 int type;
3003 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3004} datastatelist[] = {
3005 /* mobile originating call establishment */
3006 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3007 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3008 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3009 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3010 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3011 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3012 /* mobile terminating call establishment */
3013 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3014 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3015 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3016 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003017 {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 +08003018 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3019 /* signalling during call */
3020 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3021 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3022 {SBIT(GSM_CSTATE_ACTIVE),
3023 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3024 {ALL_STATES,
3025 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3026 {ALL_STATES,
3027 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3028 {ALL_STATES,
3029 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3030 {SBIT(GSM_CSTATE_ACTIVE),
3031 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3032 {SBIT(GSM_CSTATE_ACTIVE),
3033 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3034 {SBIT(GSM_CSTATE_ACTIVE),
3035 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3036 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3037 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3038 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3039 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3040 {SBIT(GSM_CSTATE_ACTIVE),
3041 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3042 /* clearing */
3043 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3044 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3045 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3046 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3047 {ALL_STATES, /* 5.4.3.4 */
3048 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3049};
3050
3051#define DATASLLEN \
3052 (sizeof(datastatelist) / sizeof(struct datastate))
3053
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003054static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003055{
3056 struct gsm48_hdr *gh = msgb_l3(msg);
3057 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003058 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003059 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003061
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062 if (msg_type & 0x80) {
3063 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3064 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003065 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003066
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003068 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003069
Harald Welte6f5aee02009-07-23 21:21:14 +02003070 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003072 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003073 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003074 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3075 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003076
3077 /* Create transaction */
3078 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003079 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 "creating new trans.\n", transaction_id);
3081 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003082 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003083 transaction_id, new_callref++);
3084 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003086 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003087 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 GSM48_MT_CC_RELEASE_COMPL);
3089 return -ENOMEM;
3090 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003091 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003092 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003093 }
3094
3095 /* find function for current state and message */
3096 for (i = 0; i < DATASLLEN; i++)
3097 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003098 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 break;
3100 if (i == DATASLLEN) {
3101 DEBUGP(DCC, "Message unhandled at this state.\n");
3102 return 0;
3103 }
3104
3105 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003106
3107 return rc;
3108}
3109
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003110/* Create a dummy to wait five seconds */
3111static void release_anchor(struct gsm_subscriber_connection *conn)
3112{
3113 if (!conn->anch_operation)
3114 return;
3115
3116 bsc_del_timer(&conn->anch_operation->timeout);
3117 talloc_free(conn->anch_operation);
3118 conn->anch_operation = NULL;
3119}
3120
3121static void anchor_timeout(void *_data)
3122{
3123 struct gsm_subscriber_connection *con = _data;
3124
3125 release_anchor(con);
3126 msc_release_connection(con);
3127}
3128
3129int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3130{
3131 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3132 if (!conn->anch_operation)
3133 return -1;
3134
3135 conn->anch_operation->timeout.data = conn;
3136 conn->anch_operation->timeout.cb = anchor_timeout;
3137 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3138 return 0;
3139}
3140
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003141/* here we get data from the BSC level... */
3142int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003143{
3144 struct gsm48_hdr *gh = msgb_l3(msg);
3145 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003146 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003147
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003148 if (silent_call_reroute(conn, msg))
3149 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003150
3151 switch (pdisc) {
3152 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003153 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003154 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003155 break;
3156 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003157 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003158 break;
3159 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003160 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003161 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003162 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003163 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003164 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003165 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003166 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003167 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003168 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3169 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003170 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003171 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003172 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003173 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003174 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003175 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003176 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3177 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003178 break;
3179 }
3180
3181 return rc;
3182}
Harald Welte8470bf22008-12-25 23:28:35 +00003183
Harald Welte805f6442009-07-28 18:25:29 +02003184/*
3185 * This will be ran by the linker when loading the DSO. We use it to
3186 * do system initialization, e.g. registration of signal handlers.
3187 */
3188static __attribute__((constructor)) void on_dso_load_0408(void)
3189{
Harald Welte805f6442009-07-28 18:25:29 +02003190 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3191}