blob: d3e48730750efb2a186582b5347479ed5e09f826 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/msgb.h>
35#include <osmocore/bitvec.h>
36#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010039#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010050#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010051#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000055
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020057
Holger Freytherd51524f2009-06-09 08:27:07 +000058int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000059static int gsm48_tx_simple(struct gsm_lchan *lchan,
60 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010061static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000062
Harald Welte52b1f982008-12-23 20:25:15 +000063struct gsm_lai {
64 u_int16_t mcc;
65 u_int16_t mnc;
66 u_int16_t lac;
67};
68
Harald Welte4bfdfe72009-06-10 23:11:52 +080069static u_int32_t new_callref = 0x80000001;
70
Holger Freyther73487a22008-12-31 18:53:57 +000071static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
72 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000073{
74 if (!subscriber)
75 return 0;
76
Holger Freyther73487a22008-12-31 18:53:57 +000077 /*
78 * Do not send accept yet as more information should arrive. Some
79 * phones will not send us the information and we will have to check
80 * what we want to do with that.
81 */
82 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
83 return 0;
84
Jan Luebbe06513f22009-08-12 12:48:00 +020085 switch (subscriber->net->auth_policy) {
86 case GSM_AUTH_POLICY_CLOSED:
87 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020088 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +020089 if (subscriber->authorized)
90 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020091 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +020092 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +000093 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +020094 default:
95 return 0;
96 }
Holger Freyther89824fc2008-12-30 16:18:18 +000097}
Holger Freyther07cc8d82008-12-29 06:23:46 +000098
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010099static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000100{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100101 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000102 return;
103
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100104 bsc_del_timer(&conn->loc_operation->updating_timer);
105 talloc_free(conn->loc_operation);
106 conn->loc_operation = 0;
107 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000108}
109
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100110static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000111{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100112 use_subscr_con(conn)
113 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000114
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100115 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200116 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000117}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000118
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100119static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000120{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100121 if (authorize_subscriber(conn->loc_operation, conn->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200122 int rc;
123
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100124 db_subscriber_alloc_tmsi(conn->subscr);
125 release_loc_updating_req(conn);
126 rc = gsm0408_loc_upd_acc(msg->lchan, conn->subscr->tmsi);
127 if (msg->lchan->ts->trx->bts->network->send_mm_info) {
Harald Welte648b6ce2009-12-14 09:00:24 +0100128 /* send MM INFO with network name */
129 rc = gsm48_tx_mm_info(msg->lchan);
130 }
Harald Welteb83d9382009-12-12 21:00:48 +0100131
Harald Welteee5ad162009-08-09 19:07:00 +0200132 /* call subscr_update after putting the loc_upd_acc
133 * in the transmit queue, since S_SUBSCR_ATTACHED might
134 * trigger further action like SMS delivery */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100135 subscr_update(conn->subscr, msg->trx->bts,
Harald Welteee5ad162009-08-09 19:07:00 +0200136 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100137
Harald Welteb83d9382009-12-12 21:00:48 +0100138 /* try to close channel ASAP */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100139 lchan_auto_release(conn->lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200140 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000141 }
142
143 return 0;
144}
145
Holger Freyther7c19f742009-06-06 13:54:35 +0000146static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
147 void *handler_data, void *signal_data)
148{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800149 struct gsm_trans *trans, *temp;
150
Holger Freyther7c19f742009-06-06 13:54:35 +0000151 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
152 return 0;
153
154 /*
155 * Cancel any outstanding location updating request
156 * operation taking place on the lchan.
157 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200158 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200159 if (!lchan)
160 return 0;
161
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100162 release_loc_updating_req(&lchan->conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000163
Harald Welte4bfdfe72009-06-10 23:11:52 +0800164 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200165 /* FIXME: this is not neccessarily the right thing to do, we should
166 * only set trans->lchan to NULL and wait for another lchan to be
167 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800168 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
169 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200170 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800171 }
172
Holger Freyther7c19f742009-06-06 13:54:35 +0000173 return 0;
174}
175
Holger Freyther429e7762008-12-30 13:28:30 +0000176/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000177int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000178{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100179 struct gsm_subscriber_connection *conn;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100180 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000181 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000182 struct gsm48_hdr *gh;
183
Harald Welte8470bf22008-12-25 23:28:35 +0000184 msg->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100185 conn = &lchan->conn;
Harald Welte52b1f982008-12-23 20:25:15 +0000186
187 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
188 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000189 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000190 gh->data[0] = cause;
191
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100192 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100193 "LAC=%u BTS=%u\n", conn->subscr ?
194 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100195 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100196
Harald Welteffa55a42009-12-22 19:07:32 +0100197 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000198
Harald Welte39e2ead2009-07-23 21:13:03 +0200199 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000200}
201
202/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000203int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000204{
Harald Welte8470bf22008-12-25 23:28:35 +0000205 struct gsm_bts *bts = lchan->ts->trx->bts;
206 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000207 struct gsm48_hdr *gh;
208 struct gsm48_loc_area_id *lai;
209 u_int8_t *mid;
210
Harald Welte8470bf22008-12-25 23:28:35 +0000211 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000212
213 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
214 gh->proto_discr = GSM48_PDISC_MM;
215 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
216
217 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100218 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000219 bts->network->network_code, bts->location_area_code);
220
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200221 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200222 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000223
224 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
225
Harald Welteffa55a42009-12-22 19:07:32 +0100226 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100227
Harald Welteb83d9382009-12-12 21:00:48 +0100228 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000229}
230
Harald Weltebf5e8df2009-02-03 12:59:45 +0000231/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000232static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
233{
234 struct msgb *msg = gsm48_msgb_alloc();
235 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000236
Harald Welte231ad4f2008-12-27 11:15:38 +0000237 msg->lchan = lchan;
238
239 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
240 gh->proto_discr = GSM48_PDISC_MM;
241 gh->msg_type = GSM48_MT_MM_ID_REQ;
242 gh->data[0] = id_type;
243
Harald Welte39e2ead2009-07-23 21:13:03 +0200244 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000245}
246
Harald Welte231ad4f2008-12-27 11:15:38 +0000247
Harald Weltebf5e8df2009-02-03 12:59:45 +0000248/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000249static int mm_rx_id_resp(struct msgb *msg)
250{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100251 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000252 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000253 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200254 struct gsm_bts *bts = lchan->ts->trx->bts;
255 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000256 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200257 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000258
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200259 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000260 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000261 mi_type, mi_string);
262
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100263 conn = &lchan->conn;
264
Harald Welte7659de12009-12-13 12:39:18 +0100265 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
266
Harald Welte75a983f2008-12-27 21:34:06 +0000267 switch (mi_type) {
268 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200269 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100270 if (!conn->subscr) {
271 conn->subscr = subscr_get_by_imsi(net, mi_string);
272 if (!conn->subscr)
273 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200274 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100275 if (conn->loc_operation)
276 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000277 break;
278 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000279 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000280 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100281 if (conn->subscr) {
282 db_subscriber_assoc_imei(conn->subscr, mi_string);
283 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200284 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100285 if (conn->loc_operation)
286 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000287 break;
288 }
Holger Freyther73487a22008-12-31 18:53:57 +0000289
290 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100291 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000292}
293
Harald Welte255539c2008-12-28 02:26:27 +0000294
295static void loc_upd_rej_cb(void *data)
296{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100297 struct gsm_subscriber_connection *conn = data;
298 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100299 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000300
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100301 release_loc_updating_req(conn);
Harald Welte1085c092009-11-18 20:33:19 +0100302 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000303 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000304}
305
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100306static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000307{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100308 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
309 conn->loc_operation->updating_timer.data = conn;
310 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000311}
312
Harald Welte2a139372009-02-22 21:14:55 +0000313static const char *lupd_name(u_int8_t type)
314{
315 switch (type) {
316 case GSM48_LUPD_NORMAL:
317 return "NORMAL";
318 case GSM48_LUPD_PERIODIC:
319 return "PEROIDOC";
320 case GSM48_LUPD_IMSI_ATT:
321 return "IMSI ATTACH";
322 default:
323 return "UNKNOWN";
324 }
325}
326
Harald Weltebf5e8df2009-02-03 12:59:45 +0000327/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000328static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000329{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100330 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000331 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000332 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800333 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000334 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200335 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000336 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200337 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000338 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000339
Harald Welte8470bf22008-12-25 23:28:35 +0000340 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100341 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000342
343 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000344
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200345 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000346
Harald Weltea0368542009-06-27 02:58:43 +0200347 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000348 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000349
Harald Welte7659de12009-12-13 12:39:18 +0100350 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
351
Harald Welte24ff6ee2009-12-22 00:41:05 +0100352 switch (lu->type) {
353 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100354 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100355 break;
356 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100357 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100358 break;
359 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100360 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100361 break;
362 }
363
Holger Freythereaf04692009-06-06 13:54:44 +0000364 /*
365 * Pseudo Spoof detection: Just drop a second/concurrent
366 * location updating request.
367 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100368 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200369 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100370 conn->loc_operation);
Holger Freythereaf04692009-06-06 13:54:44 +0000371 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
372 return 0;
373 }
374
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100375 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000376
Harald Welte52b1f982008-12-23 20:25:15 +0000377 switch (mi_type) {
378 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200379 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000380 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000381 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100382 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000383
Jan Luebbe370b41d2009-08-12 10:19:34 +0200384 /* look up subscriber based on IMSI, create if not found */
385 subscr = subscr_get_by_imsi(bts->network, mi_string);
386 if (!subscr) {
387 subscr = db_create_subscriber(bts->network, mi_string);
388 }
Harald Welte4b634542008-12-27 01:55:51 +0000389 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000390 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200391 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000392 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000393 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100394 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000395
Harald Welte52b1f982008-12-23 20:25:15 +0000396 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200397 subscr = subscr_get_by_tmsi(bts->network,
398 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000399 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000400 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000401 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100402 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000403 }
404 break;
405 case GSM_MI_TYPE_IMEI:
406 case GSM_MI_TYPE_IMEISV:
407 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200408 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000409 break;
410 default:
Harald Weltea0368542009-06-27 02:58:43 +0200411 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000412 break;
413 }
414
Harald Welte24516ea2009-07-04 10:18:00 +0200415 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100416 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200417
Harald Welte4bfdfe72009-06-10 23:11:52 +0800418 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200419 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420 /* FIXME: request id? close channel? */
421 return -EINVAL;
422 }
423
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100424 conn->subscr = subscr;
425 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000426
Harald Welte24516ea2009-07-04 10:18:00 +0200427 /* check if we can let the subscriber into our network immediately
428 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100429 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000430}
431
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432#if 0
433static u_int8_t to_bcd8(u_int8_t val)
434{
435 return ((val / 10) << 4) | (val % 10);
436}
437#endif
438
Harald Weltedb253af2008-12-30 17:56:55 +0000439/* Section 9.2.15a */
440int gsm48_tx_mm_info(struct gsm_lchan *lchan)
441{
442 struct msgb *msg = gsm48_msgb_alloc();
443 struct gsm48_hdr *gh;
444 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000445 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200446 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447#if 0
448 time_t cur_t;
449 struct tm* cur_time;
450 int tz15min;
451#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000452
453 msg->lchan = lchan;
454
455 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
456 gh->proto_discr = GSM48_PDISC_MM;
457 gh->msg_type = GSM48_MT_MM_INFO;
458
459 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200460#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000461 name_len = strlen(net->name_long);
462 /* 10.5.3.5a */
463 ptr8 = msgb_put(msg, 3);
464 ptr8[0] = GSM48_IE_NAME_LONG;
465 ptr8[1] = name_len*2 +1;
466 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
467
468 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
469 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000470 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000471
472 /* FIXME: Use Cell Broadcast, not UCS-2, since
473 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200474#endif
475 name_len = (strlen(net->name_long)*7)/8;
476 name_pad = (8 - strlen(net->name_long)*7)%8;
477 if (name_pad > 0)
478 name_len++;
479 /* 10.5.3.5a */
480 ptr8 = msgb_put(msg, 3);
481 ptr8[0] = GSM48_IE_NAME_LONG;
482 ptr8[1] = name_len +1;
483 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
484
485 ptr8 = msgb_put(msg, name_len);
486 gsm_7bit_encode(ptr8, net->name_long);
487
Harald Weltedb253af2008-12-30 17:56:55 +0000488 }
489
490 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200491#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000492 name_len = strlen(net->name_short);
493 /* 10.5.3.5a */
494 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200495 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000496 ptr8[1] = name_len*2 + 1;
497 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
498
Harald Weltee872cb12009-01-01 00:33:37 +0000499 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000500 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000501 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200502#endif
503 name_len = (strlen(net->name_short)*7)/8;
504 name_pad = (8 - strlen(net->name_short)*7)%8;
505 if (name_pad > 0)
506 name_len++;
507 /* 10.5.3.5a */
508 ptr8 = (u_int8_t *) msgb_put(msg, 3);
509 ptr8[0] = GSM48_IE_NAME_SHORT;
510 ptr8[1] = name_len +1;
511 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
512
513 ptr8 = msgb_put(msg, name_len);
514 gsm_7bit_encode(ptr8, net->name_short);
515
Harald Weltedb253af2008-12-30 17:56:55 +0000516 }
517
518#if 0
519 /* Section 10.5.3.9 */
520 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800521 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000522 ptr8 = msgb_put(msg, 8);
523 ptr8[0] = GSM48_IE_NET_TIME_TZ;
524 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
525 ptr8[2] = to_bcd8(cur_time->tm_mon);
526 ptr8[3] = to_bcd8(cur_time->tm_mday);
527 ptr8[4] = to_bcd8(cur_time->tm_hour);
528 ptr8[5] = to_bcd8(cur_time->tm_min);
529 ptr8[6] = to_bcd8(cur_time->tm_sec);
530 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
531 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800532 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000533 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800534 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000535#endif
536
Daniel Willmanneea93372009-08-13 03:42:07 +0200537 DEBUGP(DMM, "-> MM INFO\n");
538
Harald Welte39e2ead2009-07-23 21:13:03 +0200539 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000540}
541
Harald Welte7984d5c2009-08-12 22:56:50 +0200542/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100543int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200544{
545 struct msgb *msg = gsm48_msgb_alloc();
546 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200547 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200548
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100549 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200550
551 msg->lchan = lchan;
552 gh->proto_discr = GSM48_PDISC_MM;
553 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
554
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100555 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200556
Harald Welte7984d5c2009-08-12 22:56:50 +0200557 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200558 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200559 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200560
561 return gsm48_sendmsg(msg, NULL);
562}
563
564/* Section 9.2.1 */
565int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
566{
567 DEBUGP(DMM, "-> AUTH REJECT\n");
568 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
569}
570
Harald Welte4b634542008-12-27 01:55:51 +0000571static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
572{
Harald Welte4b634542008-12-27 01:55:51 +0000573 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000574 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000575}
Harald Welteba4cf162009-01-10 01:49:35 +0000576
577/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000579 enum gsm48_reject_value value)
580{
581 struct msgb *msg = gsm48_msgb_alloc();
582 struct gsm48_hdr *gh;
583
584 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
585
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100586 msg->lchan = conn->lchan;
587 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000588
589 gh->proto_discr = GSM48_PDISC_MM;
590 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
591 gh->data[0] = value;
592 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
593
Harald Welte39e2ead2009-07-23 21:13:03 +0200594 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000595}
596
Harald Welte4ed0e922009-01-10 03:17:30 +0000597/*
598 * Handle CM Service Requests
599 * a) Verify that the packet is long enough to contain the information
600 * we require otherwsie reject with INCORRECT_MESSAGE
601 * b) Try to parse the TMSI. If we do not have one reject
602 * c) Check that we know the subscriber with the TMSI otherwise reject
603 * with a HLR cause
604 * d) Set the subscriber on the gsm_lchan and accept
605 */
Harald Welte4b634542008-12-27 01:55:51 +0000606static int gsm48_rx_mm_serv_req(struct msgb *msg)
607{
Harald Welteba4cf162009-01-10 01:49:35 +0000608 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200609 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000610
Harald Welte9176bd42009-07-23 18:46:00 +0200611 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000612 struct gsm_subscriber *subscr;
613 struct gsm48_hdr *gh = msgb_l3(msg);
614 struct gsm48_service_request *req =
615 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000616 /* unfortunately in Phase1 the classmar2 length is variable */
617 u_int8_t classmark2_len = gh->data[1];
618 u_int8_t *classmark2 = gh->data+2;
619 u_int8_t mi_len = *(classmark2 + classmark2_len);
620 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000621
Harald Weltec9e02182009-05-01 19:07:53 +0000622 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000623 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000624 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100625 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000626 GSM48_REJECT_INCORRECT_MESSAGE);
627 }
628
629 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000630 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100631 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000632 GSM48_REJECT_INCORRECT_MESSAGE);
633 }
634
Harald Weltec9e02182009-05-01 19:07:53 +0000635 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000636 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000637 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100638 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000639 GSM48_REJECT_INCORRECT_MESSAGE);
640 }
641
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200642 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000643 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000644 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000645
Harald Welte7659de12009-12-13 12:39:18 +0100646 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
647
Harald Welte3ac7f102009-08-10 10:12:45 +0200648 if (is_siemens_bts(bts))
649 send_siemens_mrpci(msg->lchan, classmark2-1);
650
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200651 subscr = subscr_get_by_tmsi(bts->network,
652 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000653
Harald Welte2a139372009-02-22 21:14:55 +0000654 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000655 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100656 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000657 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
658
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100659 if (!msg->lchan->conn.subscr)
660 msg->lchan->conn.subscr = subscr;
661 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100662 subscr_put(subscr); /* lchan already has a ref, don't need another one */
663 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000664 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
665 subscr_put(subscr);
666 }
667
Harald Weltec2e302d2009-07-05 14:08:13 +0200668 subscr->equipment.classmark2_len = classmark2_len;
669 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
670 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000671
Harald Welte4b634542008-12-27 01:55:51 +0000672 return gsm48_tx_mm_serv_ack(msg->lchan);
673}
674
Harald Welte2a139372009-02-22 21:14:55 +0000675static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
676{
Harald Welte9176bd42009-07-23 18:46:00 +0200677 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000678 struct gsm48_hdr *gh = msgb_l3(msg);
679 struct gsm48_imsi_detach_ind *idi =
680 (struct gsm48_imsi_detach_ind *) gh->data;
681 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200682 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000684
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200685 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000686 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
687 mi_type, mi_string);
688
Harald Welteffa55a42009-12-22 19:07:32 +0100689 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100690
Harald Welte2a139372009-02-22 21:14:55 +0000691 switch (mi_type) {
692 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200693 subscr = subscr_get_by_tmsi(bts->network,
694 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000695 break;
696 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200697 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000698 break;
699 case GSM_MI_TYPE_IMEI:
700 case GSM_MI_TYPE_IMEISV:
701 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000702 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000703 break;
704 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000705 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000706 break;
707 }
708
Holger Freyther4a49e772009-04-12 05:37:29 +0000709 if (subscr) {
710 subscr_update(subscr, msg->trx->bts,
711 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100712 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200713
714 subscr->equipment.classmark1 = idi->classmark1;
715 db_sync_equipment(&subscr->equipment);
716
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000717 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000718 } else
Harald Welte2a139372009-02-22 21:14:55 +0000719 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
720
Harald Welte31981a02009-12-20 09:58:40 +0100721 /* FIXME: iterate over all transactions and release them,
722 * imagine an IMSI DETACH happening during an active call! */
723
Harald Welteb83d9382009-12-12 21:00:48 +0100724 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100725 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100726
Harald Welte2a139372009-02-22 21:14:55 +0000727 return 0;
728}
729
Harald Welted2a7f5a2009-06-05 20:08:20 +0000730static int gsm48_rx_mm_status(struct msgb *msg)
731{
732 struct gsm48_hdr *gh = msgb_l3(msg);
733
734 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
735
736 return 0;
737}
738
Harald Weltebf5e8df2009-02-03 12:59:45 +0000739/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000740static int gsm0408_rcv_mm(struct msgb *msg)
741{
742 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800743 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000744
745 switch (gh->msg_type & 0xbf) {
746 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200747 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000748 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000749 break;
750 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000751 rc = mm_rx_id_resp(msg);
752 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000753 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000754 rc = gsm48_rx_mm_serv_req(msg);
755 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000756 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000757 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000758 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000759 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000760 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100761 msg->lchan->conn.subscr ?
762 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000763 "unknown subscriber");
764 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000765 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000766 rc = gsm48_rx_mm_imsi_detach_ind(msg);
767 break;
768 case GSM48_MT_MM_CM_REEST_REQ:
769 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
770 break;
771 case GSM48_MT_MM_AUTH_RESP:
772 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000773 break;
774 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100775 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000776 gh->msg_type);
777 break;
778 }
779
780 return rc;
781}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000782
Harald Welte2d35ae62009-02-06 12:02:13 +0000783/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200784static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000785{
Harald Welte9176bd42009-07-23 18:46:00 +0200786 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000787 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000788 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200789 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200790 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800791 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000792 int rc = 0;
793
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200794 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000795 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
796 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200797
Harald Weltefe18d8f2009-02-22 21:14:24 +0000798 switch (mi_type) {
799 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200800 subscr = subscr_get_by_tmsi(bts->network,
801 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000802 break;
803 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200804 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000805 break;
806 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000807
808 if (!subscr) {
809 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000810 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000811 return -EINVAL;
812 }
813 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200814 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000815
Harald Weltec2e302d2009-07-05 14:08:13 +0200816 subscr->equipment.classmark2_len = *classmark2_lv;
817 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
818 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000819
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200820 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000821 return rc;
822}
823
Harald Weltef7c43522009-06-09 20:24:21 +0000824static int gsm48_rx_rr_classmark(struct msgb *msg)
825{
826 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100827 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +0000828 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
829 u_int8_t cm2_len, cm3_len = 0;
830 u_int8_t *cm2, *cm3 = NULL;
831
832 DEBUGP(DRR, "CLASSMARK CHANGE ");
833
834 /* classmark 2 */
835 cm2_len = gh->data[0];
836 cm2 = &gh->data[1];
837 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
838
839 if (payload_len > cm2_len + 1) {
840 /* we must have a classmark3 */
841 if (gh->data[cm2_len+1] != 0x20) {
842 DEBUGPC(DRR, "ERR CM3 TAG\n");
843 return -EINVAL;
844 }
845 if (cm2_len > 3) {
846 DEBUGPC(DRR, "CM2 too long!\n");
847 return -EINVAL;
848 }
849
850 cm3_len = gh->data[cm2_len+2];
851 cm3 = &gh->data[cm2_len+3];
852 if (cm3_len > 14) {
853 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
854 return -EINVAL;
855 }
856 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
857 }
858 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200859 subscr->equipment.classmark2_len = cm2_len;
860 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000861 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200862 subscr->equipment.classmark3_len = cm3_len;
863 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000864 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200865 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000866 }
867
Harald Weltef7c43522009-06-09 20:24:21 +0000868 return 0;
869}
870
Harald Weltecf5b3592009-05-01 18:28:42 +0000871static int gsm48_rx_rr_status(struct msgb *msg)
872{
873 struct gsm48_hdr *gh = msgb_l3(msg);
874
875 DEBUGP(DRR, "STATUS rr_cause = %s\n",
876 rr_cause_name(gh->data[0]));
877
878 return 0;
879}
880
Harald Weltef7c43522009-06-09 20:24:21 +0000881static int gsm48_rx_rr_meas_rep(struct msgb *msg)
882{
Harald Welted12b0fd2009-12-15 21:36:05 +0100883 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +0000884
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100885 /* This shouldn't actually end up here, as RSL treats
886 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
887 * directly into gsm48_parse_meas_rep */
888 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +0100889 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +0000890
891 return 0;
892}
893
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200894static int gsm48_rx_rr_app_info(struct msgb *msg)
895{
896 struct gsm48_hdr *gh = msgb_l3(msg);
897 u_int8_t apdu_id_flags;
898 u_int8_t apdu_len;
899 u_int8_t *apdu_data;
900
901 apdu_id_flags = gh->data[0];
902 apdu_len = gh->data[1];
903 apdu_data = gh->data+2;
904
905 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
906 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
907
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100908 return db_apdu_blob_store(msg->lchan->conn.subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200909}
910
Harald Welted011e8b2009-11-29 22:45:52 +0100911/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +0100912static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100913{
914 struct gsm48_hdr *gh = msgb_l3(msg);
915
916 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
917 rr_cause_name(gh->data[0]));
918
919 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
920 /* FIXME: release old channel */
921
922 return 0;
923}
924
925/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +0100926static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100927{
928 struct gsm48_hdr *gh = msgb_l3(msg);
929
930 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
931 rr_cause_name(gh->data[0]));
932
933 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
934 /* FIXME: release allocated new channel */
935
936 return 0;
937}
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200938
Harald Weltebf5e8df2009-02-03 12:59:45 +0000939/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000940static int gsm0408_rcv_rr(struct msgb *msg)
941{
942 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000943 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000944
945 switch (gh->msg_type) {
946 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +0000947 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000948 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000949 case GSM48_MT_RR_GPRS_SUSP_REQ:
950 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
951 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000952 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200953 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000954 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000955 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200956 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +0000957 break;
Harald Weltecf5b3592009-05-01 18:28:42 +0000958 case GSM48_MT_RR_STATUS:
959 rc = gsm48_rx_rr_status(msg);
960 break;
Harald Weltef7c43522009-06-09 20:24:21 +0000961 case GSM48_MT_RR_MEAS_REP:
962 rc = gsm48_rx_rr_meas_rep(msg);
963 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200964 case GSM48_MT_RR_APP_INFO:
965 rc = gsm48_rx_rr_app_info(msg);
966 break;
Harald Welte08d91a52009-08-30 15:37:11 +0900967 case GSM48_MT_RR_CIPH_M_COMPL:
968 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
969 /* FIXME: check for MI (if any) */
970 break;
Harald Welteccd5a882009-11-29 20:02:20 +0100971 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +0100972 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100973 break;
974 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +0100975 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100976 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000977 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100978 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
979 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000980 break;
981 }
982
Harald Welte2d35ae62009-02-06 12:02:13 +0000983 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000984}
985
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200986int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +0200987 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200988{
989 struct msgb *msg = gsm48_msgb_alloc();
990 struct gsm48_hdr *gh;
991
992 msg->lchan = lchan;
993
994 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
995 apdu_id, apdu_len);
996
997 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
998 gh->proto_discr = GSM48_PDISC_RR;
999 gh->msg_type = GSM48_MT_RR_APP_INFO;
1000 gh->data[0] = apdu_id;
1001 gh->data[1] = apdu_len;
1002 memcpy(gh->data+2, apdu, apdu_len);
1003
1004 return gsm48_sendmsg(msg, NULL);
1005}
1006
Harald Welte4bc90a12008-12-27 16:32:52 +00001007/* Call Control */
1008
Harald Welte7584aea2009-02-11 11:44:12 +00001009/* The entire call control code is written in accordance with Figure 7.10c
1010 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1011 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1012 * it for voice */
1013
Harald Welte4bfdfe72009-06-10 23:11:52 +08001014static void new_cc_state(struct gsm_trans *trans, int state)
1015{
1016 if (state > 31 || state < 0)
1017 return;
1018
1019 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001020 gsm48_cc_state_name(trans->cc.state),
1021 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001022
Harald Weltedcaf5652009-07-23 18:56:43 +02001023 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001024}
1025
1026static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001027{
1028 struct msgb *msg = gsm48_msgb_alloc();
1029 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1030 u_int8_t *cause, *call_state;
1031
Harald Welte4bc90a12008-12-27 16:32:52 +00001032 gh->msg_type = GSM48_MT_CC_STATUS;
1033
1034 cause = msgb_put(msg, 3);
1035 cause[0] = 2;
1036 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1037 cause[2] = 0x80 | 30; /* response to status inquiry */
1038
1039 call_state = msgb_put(msg, 1);
1040 call_state[0] = 0xc0 | 0x00;
1041
Harald Welte39e2ead2009-07-23 21:13:03 +02001042 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001043}
1044
Harald Welte6f4b7532008-12-29 00:39:37 +00001045static int gsm48_tx_simple(struct gsm_lchan *lchan,
1046 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001047{
1048 struct msgb *msg = gsm48_msgb_alloc();
1049 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1050
1051 msg->lchan = lchan;
1052
Harald Welte6f4b7532008-12-29 00:39:37 +00001053 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001054 gh->msg_type = msg_type;
1055
Harald Welte39e2ead2009-07-23 21:13:03 +02001056 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001057}
1058
Harald Welte4bfdfe72009-06-10 23:11:52 +08001059static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1060{
Harald Weltedcaf5652009-07-23 18:56:43 +02001061 if (bsc_timer_pending(&trans->cc.timer)) {
1062 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1063 bsc_del_timer(&trans->cc.timer);
1064 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001065 }
1066}
1067
1068static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1069 int msg_type, struct gsm_mncc *mncc)
1070{
1071 struct msgb *msg;
1072
1073 if (trans)
1074 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001075 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001076 "Sending '%s' to MNCC.\n",
1077 trans->lchan->ts->trx->bts->nr,
1078 trans->lchan->ts->trx->nr,
1079 trans->lchan->ts->nr, trans->transaction_id,
1080 (trans->subscr)?(trans->subscr->extension):"-",
1081 get_mncc_name(msg_type));
1082 else
1083 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1084 "Sending '%s' to MNCC.\n",
1085 (trans->subscr)?(trans->subscr->extension):"-",
1086 get_mncc_name(msg_type));
1087 else
1088 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1089 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1090
1091 mncc->msg_type = msg_type;
1092
Harald Welte966636f2009-06-26 19:39:35 +02001093 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001094 if (!msg)
1095 return -ENOMEM;
1096 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1097 msgb_enqueue(&net->upqueue, msg);
1098
1099 return 0;
1100}
1101
1102int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1103 u_int32_t callref, int location, int value)
1104{
1105 struct gsm_mncc rel;
1106
Harald Welte92f70c52009-06-12 01:54:08 +08001107 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001108 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001109 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001110 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1111}
1112
Harald Weltedcaf5652009-07-23 18:56:43 +02001113/* Call Control Specific transaction release.
1114 * gets called by trans_free, DO NOT CALL YOURSELF! */
1115void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001116{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001117 gsm48_stop_cc_timer(trans);
1118
1119 /* send release to L4, if callref still exists */
1120 if (trans->callref) {
1121 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001122 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001123 GSM48_CAUSE_LOC_PRN_S_LU,
1124 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001125 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001126 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001127 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001128 if (trans->lchan)
1129 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001130}
1131
1132static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1133
Harald Welte09e38af2009-02-16 22:52:23 +00001134/* call-back from paging the B-end of the connection */
1135static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001136 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001137{
Harald Welte7ccf7782009-02-17 01:43:01 +00001138 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001139 struct gsm_subscriber *subscr = param;
1140 struct gsm_trans *transt, *tmp;
1141 struct gsm_network *net;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001142 struct gsm_subscriber_connection *conn;
Harald Weltec05677b2009-06-26 20:17:06 +02001143
Harald Welte09e38af2009-02-16 22:52:23 +00001144 if (hooknum != GSM_HOOK_RR_PAGING)
1145 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001146
1147 if (!subscr)
1148 return -EINVAL;
1149 net = subscr->net;
1150 if (!net) {
1151 DEBUGP(DCC, "Error Network not set!\n");
1152 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001153 }
Harald Welte7584aea2009-02-11 11:44:12 +00001154
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001155 conn = &lchan->conn;
1156
Harald Welte4bfdfe72009-06-10 23:11:52 +08001157 /* check all tranactions (without lchan) for subscriber */
1158 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1159 if (transt->subscr != subscr || transt->lchan)
1160 continue;
1161 switch (event) {
1162 case GSM_PAGING_SUCCEEDED:
1163 if (!lchan) // paranoid
1164 break;
1165 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1166 subscr->extension);
1167 /* Assign lchan */
1168 if (!transt->lchan) {
1169 transt->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001170 use_subscr_con(conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001171 }
1172 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001173 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001174 break;
1175 case GSM_PAGING_EXPIRED:
1176 DEBUGP(DCC, "Paging subscr %s expired!\n",
1177 subscr->extension);
1178 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001179 mncc_release_ind(transt->subscr->net, transt,
1180 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001181 GSM48_CAUSE_LOC_PRN_S_LU,
1182 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001183 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001184 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001185 break;
1186 }
1187 }
Harald Welte09e38af2009-02-16 22:52:23 +00001188 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001189}
Harald Welte7584aea2009-02-11 11:44:12 +00001190
Harald Welteda7ab742009-12-19 22:23:05 +01001191static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1192
Harald Welte805f6442009-07-28 18:25:29 +02001193/* some other part of the code sends us a signal */
1194static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1195 void *handler_data, void *signal_data)
1196{
1197 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001198 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001199 struct gsm_network *net;
1200 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001201
1202 if (subsys != SS_ABISIP)
1203 return 0;
1204
1205 /* in case we use direct BTS-to-BTS RTP */
1206 if (ipacc_rtp_direct)
1207 return 0;
1208
Harald Welte805f6442009-07-28 18:25:29 +02001209 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001210 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001211 /* check if any transactions on this lchan still have
1212 * a tch_recv_mncc request pending */
1213 net = lchan->ts->trx->bts->network;
1214 llist_for_each_entry(trans, &net->trans_list, entry) {
1215 if (trans->lchan == lchan && trans->tch_recv) {
1216 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1217 tch_recv_mncc(net, trans->callref, 1);
1218 }
1219 }
Harald Welte805f6442009-07-28 18:25:29 +02001220 break;
Harald Welte805f6442009-07-28 18:25:29 +02001221 }
1222
1223 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001224}
1225
Harald Welte49f48b82009-02-17 15:29:33 +00001226/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001227static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001228{
Harald Welte11fa29c2009-02-19 17:24:39 +00001229 struct gsm_bts *bts = lchan->ts->trx->bts;
1230 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001231 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001232
Harald Welte11fa29c2009-02-19 17:24:39 +00001233 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1234 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1235 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1236
1237 if (bts->type != remote_bts->type) {
1238 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1239 return -EINVAL;
1240 }
Harald Welteda7ab742009-12-19 22:23:05 +01001241
1242 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001243 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001244 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001245 if (!ipacc_rtp_direct) {
1246 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001247 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001248 if (rc < 0)
1249 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001250 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001251#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001252
1253 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301254 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1255 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001256 } else {
1257 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301258 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1259 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301260 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001261 if (rc < 0)
1262 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301263 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1264 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301265 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001266 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001267 break;
1268 case GSM_BTS_TYPE_BS11:
1269 trau_mux_map_lchan(lchan, remote_lchan);
1270 break;
1271 default:
1272 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001273 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001274 }
Harald Welte49f48b82009-02-17 15:29:33 +00001275
1276 return 0;
1277}
1278
Harald Welte4bfdfe72009-06-10 23:11:52 +08001279/* bridge channels of two transactions */
1280static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001281{
Harald Weltedcaf5652009-07-23 18:56:43 +02001282 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1283 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001284
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001286 return -EIO;
1287
Harald Welte4bfdfe72009-06-10 23:11:52 +08001288 if (!trans1->lchan || !trans2->lchan)
1289 return -EIO;
1290
1291 /* through-connect channel */
1292 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001293}
1294
Harald Welteda7ab742009-12-19 22:23:05 +01001295/* enable receive of channels to MNCC upqueue */
1296static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297{
1298 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001299 struct gsm_lchan *lchan;
1300 struct gsm_bts *bts;
1301 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001302
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001304 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 if (!trans)
1306 return -EIO;
1307 if (!trans->lchan)
1308 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001309 lchan = trans->lchan;
1310 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311
Harald Welteda7ab742009-12-19 22:23:05 +01001312 switch (bts->type) {
1313 case GSM_BTS_TYPE_NANOBTS:
1314 if (ipacc_rtp_direct) {
1315 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1316 return -EINVAL;
1317 }
1318 /* in case, we don't have a RTP socket yet, we note this
1319 * in the transaction and try later */
1320 if (!lchan->abis_ip.rtp_socket) {
1321 trans->tch_recv = enable;
1322 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1323 return 0;
1324 }
1325 if (enable) {
1326 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001327 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001328 if (rc < 0)
1329 return rc;
1330 /* assign socket to application interface */
1331 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1332 net, callref);
1333 } else
1334 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1335 net, 0);
1336 break;
1337 case GSM_BTS_TYPE_BS11:
1338 if (enable)
1339 return trau_recv_lchan(lchan, callref);
1340 return trau_mux_unmap(NULL, callref);
1341 break;
1342 default:
1343 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1344 return -EINVAL;
1345 }
1346
1347 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348}
1349
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1351{
1352 DEBUGP(DCC, "-> STATUS ENQ\n");
1353 return gsm48_cc_tx_status(trans, msg);
1354}
1355
1356static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1357static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1358
1359static void gsm48_cc_timeout(void *arg)
1360{
1361 struct gsm_trans *trans = arg;
1362 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001363 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1364 int mo_location = GSM48_CAUSE_LOC_USER;
1365 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1366 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 struct gsm_mncc mo_rel, l4_rel;
1368
1369 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1370 mo_rel.callref = trans->callref;
1371 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1372 l4_rel.callref = trans->callref;
1373
Harald Weltedcaf5652009-07-23 18:56:43 +02001374 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 case 0x303:
1376 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001377 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 break;
1379 case 0x310:
1380 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001381 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 break;
1383 case 0x313:
1384 disconnect = 1;
1385 /* unknown, did not find it in the specs */
1386 break;
1387 case 0x301:
1388 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001389 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 break;
1391 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001392 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001394 gsm48_cc_tx_release(trans, &trans->cc.msg);
1395 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001396 break; /* stay in release state */
1397 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001398 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399 return;
1400// release = 1;
1401// l4_cause = 14;
1402// break;
1403 case 0x306:
1404 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001405 mo_cause = trans->cc.msg.cause.value;
1406 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407 break;
1408 case 0x323:
1409 disconnect = 1;
1410 break;
1411 default:
1412 release = 1;
1413 }
1414
1415 if (release && trans->callref) {
1416 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001417 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 l4_location, l4_cause);
1419 trans->callref = 0;
1420 }
1421
1422 if (disconnect && trans->callref) {
1423 /* process disconnect towards layer 4 */
1424 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001425 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 }
1427
1428 /* process disconnect towards mobile station */
1429 if (disconnect || release) {
1430 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001431 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1432 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1433 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001434 mo_rel.cause.diag_len = 3;
1435
1436 if (disconnect)
1437 gsm48_cc_tx_disconnect(trans, &mo_rel);
1438 if (release)
1439 gsm48_cc_tx_release(trans, &mo_rel);
1440 }
1441
1442}
1443
1444static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1445 int sec, int micro)
1446{
1447 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001448 trans->cc.timer.cb = gsm48_cc_timeout;
1449 trans->cc.timer.data = trans;
1450 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1451 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452}
1453
1454static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1455{
1456 struct gsm48_hdr *gh = msgb_l3(msg);
1457 u_int8_t msg_type = gh->msg_type & 0xbf;
1458 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1459 struct tlv_parsed tp;
1460 struct gsm_mncc setup;
1461
1462 memset(&setup, 0, sizeof(struct gsm_mncc));
1463 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001464 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001465 /* emergency setup is identified by msg_type */
1466 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1467 setup.emergency = 1;
1468
1469 /* use subscriber as calling party number */
1470 if (trans->subscr) {
1471 setup.fields |= MNCC_F_CALLING;
1472 strncpy(setup.calling.number, trans->subscr->extension,
1473 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001474 strncpy(setup.imsi, trans->subscr->imsi,
1475 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001476 }
1477 /* bearer capability */
1478 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1479 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001480 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001481 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1482 }
1483 /* facility */
1484 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1485 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001486 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001487 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1488 }
1489 /* called party bcd number */
1490 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1491 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001492 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001493 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1494 }
1495 /* user-user */
1496 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1497 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001498 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001499 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1500 }
1501 /* ss-version */
1502 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1503 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001504 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001505 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1506 }
1507 /* CLIR suppression */
1508 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1509 setup.clir.sup = 1;
1510 /* CLIR invocation */
1511 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1512 setup.clir.inv = 1;
1513 /* cc cap */
1514 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1515 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001516 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001517 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1518 }
1519
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520 new_cc_state(trans, GSM_CSTATE_INITIATED);
1521
Harald Welte (local)47df3992009-12-26 19:45:03 +01001522 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1523 subscr_name(trans->subscr), trans->subscr->extension,
1524 setup.called.number);
1525
Harald Welte4bfdfe72009-06-10 23:11:52 +08001526 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001527 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001528
Harald Welte13cac662009-07-29 12:10:35 +02001529 /* MNCC code will modify the channel asynchronously, we should
1530 * ipaccess-bind only after the modification has been made to the
1531 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001532 return 0;
1533}
1534
1535static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001536{
1537 struct msgb *msg = gsm48_msgb_alloc();
1538 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001539 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001540 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001541
Harald Welte7ccf7782009-02-17 01:43:01 +00001542 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001543
Harald Welte4bfdfe72009-06-10 23:11:52 +08001544 /* transaction id must not be assigned */
1545 if (trans->transaction_id != 0xff) { /* unasssigned */
1546 DEBUGP(DCC, "TX Setup with assigned transaction. "
1547 "This is not allowed!\n");
1548 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001549 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001550 GSM48_CAUSE_LOC_PRN_S_LU,
1551 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001553 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 return rc;
1555 }
1556
1557 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001558 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1559 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001560 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001561 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001562 GSM48_CAUSE_LOC_PRN_S_LU,
1563 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001564 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001565 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001566 return rc;
1567 }
Harald Welte78283ef2009-07-23 21:36:44 +02001568 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001569
Harald Welte65e74cc2008-12-29 01:55:35 +00001570 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001571
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001573
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 /* bearer capability */
1575 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001576 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 /* facility */
1578 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001579 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 /* progress */
1581 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001582 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 /* calling party BCD number */
1584 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001585 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 /* called party BCD number */
1587 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001588 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 /* user-user */
1590 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001591 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 /* redirecting party BCD number */
1593 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001594 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001595 /* signal */
1596 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001597 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598
1599 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001600
Harald Welte39e2ead2009-07-23 21:13:03 +02001601 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001602}
1603
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1605{
1606 struct gsm48_hdr *gh = msgb_l3(msg);
1607 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1608 struct tlv_parsed tp;
1609 struct gsm_mncc call_conf;
1610
1611 gsm48_stop_cc_timer(trans);
1612 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1613
1614 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1615 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001616 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617#if 0
1618 /* repeat */
1619 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1620 call_conf.repeat = 1;
1621 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1622 call_conf.repeat = 2;
1623#endif
1624 /* bearer capability */
1625 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1626 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001627 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001628 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1629 }
1630 /* cause */
1631 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1632 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001633 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1635 }
1636 /* cc cap */
1637 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1638 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001639 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1641 }
1642
1643 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1644
Harald Welte596fed42009-07-23 19:06:52 +02001645 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1646 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647}
1648
1649static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1650{
1651 struct gsm_mncc *proceeding = arg;
1652 struct msgb *msg = gsm48_msgb_alloc();
1653 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1654
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1656
1657 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1658
1659 /* bearer capability */
1660 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001661 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 /* facility */
1663 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001664 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 /* progress */
1666 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001667 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668
Harald Welte39e2ead2009-07-23 21:13:03 +02001669 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670}
1671
1672static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1673{
1674 struct gsm48_hdr *gh = msgb_l3(msg);
1675 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1676 struct tlv_parsed tp;
1677 struct gsm_mncc alerting;
1678
1679 gsm48_stop_cc_timer(trans);
1680 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1681
1682 memset(&alerting, 0, sizeof(struct gsm_mncc));
1683 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001684 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 /* facility */
1686 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1687 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001688 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1690 }
1691
1692 /* progress */
1693 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1694 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001695 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1697 }
1698 /* ss-version */
1699 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1700 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001701 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001702 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1703 }
1704
1705 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1706
Harald Welte596fed42009-07-23 19:06:52 +02001707 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1708 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709}
1710
1711static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1712{
1713 struct gsm_mncc *alerting = arg;
1714 struct msgb *msg = gsm48_msgb_alloc();
1715 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1716
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 gh->msg_type = GSM48_MT_CC_ALERTING;
1718
1719 /* facility */
1720 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001721 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 /* progress */
1723 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001724 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 /* user-user */
1726 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001727 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728
1729 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1730
Harald Welte39e2ead2009-07-23 21:13:03 +02001731 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001732}
1733
1734static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1735{
1736 struct gsm_mncc *progress = arg;
1737 struct msgb *msg = gsm48_msgb_alloc();
1738 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1739
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 gh->msg_type = GSM48_MT_CC_PROGRESS;
1741
1742 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001743 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 /* user-user */
1745 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001746 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747
Harald Welte39e2ead2009-07-23 21:13:03 +02001748 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749}
1750
1751static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1752{
1753 struct gsm_mncc *connect = arg;
1754 struct msgb *msg = gsm48_msgb_alloc();
1755 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1756
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 gh->msg_type = GSM48_MT_CC_CONNECT;
1758
1759 gsm48_stop_cc_timer(trans);
1760 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1761
1762 /* facility */
1763 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001764 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* progress */
1766 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001767 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 /* connected number */
1769 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001770 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* user-user */
1772 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774
1775 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1776
Harald Welte39e2ead2009-07-23 21:13:03 +02001777 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778}
1779
1780static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1781{
1782 struct gsm48_hdr *gh = msgb_l3(msg);
1783 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1784 struct tlv_parsed tp;
1785 struct gsm_mncc connect;
1786
1787 gsm48_stop_cc_timer(trans);
1788
1789 memset(&connect, 0, sizeof(struct gsm_mncc));
1790 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001791 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 /* use subscriber as connected party number */
1793 if (trans->subscr) {
1794 connect.fields |= MNCC_F_CONNECTED;
1795 strncpy(connect.connected.number, trans->subscr->extension,
1796 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001797 strncpy(connect.imsi, trans->subscr->imsi,
1798 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 }
1800 /* facility */
1801 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1802 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001803 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1805 }
1806 /* user-user */
1807 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1808 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1811 }
1812 /* ss-version */
1813 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1814 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001815 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1817 }
1818
1819 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1820
Harald Welte596fed42009-07-23 19:06:52 +02001821 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001822}
1823
1824
1825static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1826{
1827 struct gsm_mncc connect_ack;
1828
1829 gsm48_stop_cc_timer(trans);
1830
1831 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1832
1833 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1834 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001835 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 &connect_ack);
1837}
1838
1839static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1840{
1841 struct msgb *msg = gsm48_msgb_alloc();
1842 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1843
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1845
1846 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1847
Harald Welte39e2ead2009-07-23 21:13:03 +02001848 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849}
1850
1851static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1852{
1853 struct gsm48_hdr *gh = msgb_l3(msg);
1854 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1855 struct tlv_parsed tp;
1856 struct gsm_mncc disc;
1857
1858 gsm48_stop_cc_timer(trans);
1859
1860 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1861
1862 memset(&disc, 0, sizeof(struct gsm_mncc));
1863 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001864 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 /* cause */
1866 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1867 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1870 }
1871 /* facility */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1873 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1876 }
1877 /* user-user */
1878 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1879 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1882 }
1883 /* ss-version */
1884 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1885 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1888 }
1889
Harald Welte596fed42009-07-23 19:06:52 +02001890 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891
1892}
1893
Harald Weltec66b71c2009-06-11 14:23:20 +08001894static struct gsm_mncc_cause default_cause = {
1895 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1896 .coding = 0,
1897 .rec = 0,
1898 .rec_val = 0,
1899 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1900 .diag_len = 0,
1901 .diag = { 0 },
1902};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
1904static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1905{
1906 struct gsm_mncc *disc = arg;
1907 struct msgb *msg = gsm48_msgb_alloc();
1908 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1909
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1911
1912 gsm48_stop_cc_timer(trans);
1913 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1914
1915 /* cause */
1916 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001917 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918 else
Harald Welte55c8f352010-03-07 23:40:35 +01001919 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920
1921 /* facility */
1922 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 /* progress */
1925 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 /* user-user */
1928 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930
1931 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001932 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933
1934 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1935
Harald Welte39e2ead2009-07-23 21:13:03 +02001936 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937}
1938
1939static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1940{
1941 struct gsm48_hdr *gh = msgb_l3(msg);
1942 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1943 struct tlv_parsed tp;
1944 struct gsm_mncc rel;
1945 int rc;
1946
1947 gsm48_stop_cc_timer(trans);
1948
1949 memset(&rel, 0, sizeof(struct gsm_mncc));
1950 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001951 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 /* cause */
1953 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1954 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1957 }
1958 /* facility */
1959 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1960 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1963 }
1964 /* user-user */
1965 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1966 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1969 }
1970 /* ss-version */
1971 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1972 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1975 }
1976
Harald Weltedcaf5652009-07-23 18:56:43 +02001977 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02001979 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 } else {
Harald Welte596fed42009-07-23 19:06:52 +02001981 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02001982 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02001983 GSM48_MT_CC_RELEASE_COMPL);
1984 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985 }
1986
1987 new_cc_state(trans, GSM_CSTATE_NULL);
1988
1989 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001990 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991
1992 return rc;
1993}
1994
1995static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1996{
1997 struct gsm_mncc *rel = arg;
1998 struct msgb *msg = gsm48_msgb_alloc();
1999 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2000
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 gh->msg_type = GSM48_MT_CC_RELEASE;
2002
2003 trans->callref = 0;
2004
2005 gsm48_stop_cc_timer(trans);
2006 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2007
2008 /* cause */
2009 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002010 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 /* facility */
2012 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002013 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014 /* user-user */
2015 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002016 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017
Harald Weltedcaf5652009-07-23 18:56:43 +02002018 trans->cc.T308_second = 0;
2019 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020
Harald Weltedcaf5652009-07-23 18:56:43 +02002021 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2023
Harald Welte39e2ead2009-07-23 21:13:03 +02002024 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025}
2026
2027static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2028{
2029 struct gsm48_hdr *gh = msgb_l3(msg);
2030 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2031 struct tlv_parsed tp;
2032 struct gsm_mncc rel;
2033 int rc = 0;
2034
2035 gsm48_stop_cc_timer(trans);
2036
2037 memset(&rel, 0, sizeof(struct gsm_mncc));
2038 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002039 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 /* cause */
2041 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2042 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002043 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2045 }
2046 /* facility */
2047 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2048 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002049 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2051 }
2052 /* user-user */
2053 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2054 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2057 }
2058 /* ss-version */
2059 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2060 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002061 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2063 }
2064
2065 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002066 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002068 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 MNCC_REJ_IND, &rel);
2070 break;
2071 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002072 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002074 /* FIXME: in case of multiple calls, we can't simply
2075 * hang up here ! */
2076 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 break;
2078 default:
Harald Welte596fed42009-07-23 19:06:52 +02002079 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 MNCC_REL_IND, &rel);
2081 }
2082 }
2083
2084 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002085 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086
2087 return rc;
2088}
2089
2090static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2091{
2092 struct gsm_mncc *rel = arg;
2093 struct msgb *msg = gsm48_msgb_alloc();
2094 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2095
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2097
2098 trans->callref = 0;
2099
2100 gsm48_stop_cc_timer(trans);
2101
2102 /* cause */
2103 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 /* facility */
2106 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002107 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 /* user-user */
2109 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111
Harald Weltedcaf5652009-07-23 18:56:43 +02002112 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113
Harald Welte39e2ead2009-07-23 21:13:03 +02002114 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115}
2116
2117static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2118{
2119 struct gsm48_hdr *gh = msgb_l3(msg);
2120 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2121 struct tlv_parsed tp;
2122 struct gsm_mncc fac;
2123
2124 memset(&fac, 0, sizeof(struct gsm_mncc));
2125 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002126 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 /* facility */
2128 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2129 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002130 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2132 }
2133 /* ss-version */
2134 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2135 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002136 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2138 }
2139
Harald Welte596fed42009-07-23 19:06:52 +02002140 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141}
2142
2143static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2144{
2145 struct gsm_mncc *fac = arg;
2146 struct msgb *msg = gsm48_msgb_alloc();
2147 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2148
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 gh->msg_type = GSM48_MT_CC_FACILITY;
2150
2151 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153
Harald Welte39e2ead2009-07-23 21:13:03 +02002154 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155}
2156
2157static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2158{
2159 struct gsm_mncc hold;
2160
2161 memset(&hold, 0, sizeof(struct gsm_mncc));
2162 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002163 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164}
2165
2166static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2167{
2168 struct msgb *msg = gsm48_msgb_alloc();
2169 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2170
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2172
Harald Welte39e2ead2009-07-23 21:13:03 +02002173 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174}
2175
2176static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2177{
2178 struct gsm_mncc *hold_rej = arg;
2179 struct msgb *msg = gsm48_msgb_alloc();
2180 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2181
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2183
2184 /* cause */
2185 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 else
Harald Welte55c8f352010-03-07 23:40:35 +01002188 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189
Harald Welte39e2ead2009-07-23 21:13:03 +02002190 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191}
2192
2193static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2194{
2195 struct gsm_mncc retrieve;
2196
2197 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2198 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002199 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2200 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201}
2202
2203static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2204{
2205 struct msgb *msg = gsm48_msgb_alloc();
2206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2207
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2209
Harald Welte39e2ead2009-07-23 21:13:03 +02002210 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211}
2212
2213static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2214{
2215 struct gsm_mncc *retrieve_rej = arg;
2216 struct msgb *msg = gsm48_msgb_alloc();
2217 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2218
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2220
2221 /* cause */
2222 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002223 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224 else
Harald Welte55c8f352010-03-07 23:40:35 +01002225 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226
Harald Welte39e2ead2009-07-23 21:13:03 +02002227 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228}
2229
2230static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2231{
2232 struct gsm48_hdr *gh = msgb_l3(msg);
2233 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2234 struct tlv_parsed tp;
2235 struct gsm_mncc dtmf;
2236
2237 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2238 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002239 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 /* keypad facility */
2241 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2242 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002243 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2245 }
2246
Harald Welte596fed42009-07-23 19:06:52 +02002247 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248}
2249
2250static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2251{
2252 struct gsm_mncc *dtmf = arg;
2253 struct msgb *msg = gsm48_msgb_alloc();
2254 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2255
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2257
2258 /* keypad */
2259 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261
Harald Welte39e2ead2009-07-23 21:13:03 +02002262 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263}
2264
2265static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2266{
2267 struct gsm_mncc *dtmf = arg;
2268 struct msgb *msg = gsm48_msgb_alloc();
2269 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2270
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2272
2273 /* cause */
2274 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002275 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 else
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278
Harald Welte39e2ead2009-07-23 21:13:03 +02002279 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280}
2281
2282static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2283{
2284 struct msgb *msg = gsm48_msgb_alloc();
2285 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2286
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2288
Harald Welte39e2ead2009-07-23 21:13:03 +02002289 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290}
2291
2292static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2293{
2294 struct gsm_mncc dtmf;
2295
2296 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2297 dtmf.callref = trans->callref;
2298
Harald Welte596fed42009-07-23 19:06:52 +02002299 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300}
2301
2302static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2303{
2304 struct gsm48_hdr *gh = msgb_l3(msg);
2305 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2306 struct tlv_parsed tp;
2307 struct gsm_mncc modify;
2308
2309 memset(&modify, 0, sizeof(struct gsm_mncc));
2310 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002311 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 /* bearer capability */
2313 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2314 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2317 }
2318
2319 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2320
Harald Welte596fed42009-07-23 19:06:52 +02002321 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322}
2323
2324static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2325{
2326 struct gsm_mncc *modify = arg;
2327 struct msgb *msg = gsm48_msgb_alloc();
2328 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2329
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 gh->msg_type = GSM48_MT_CC_MODIFY;
2331
2332 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2333
2334 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002335 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336
2337 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2338
Harald Welte39e2ead2009-07-23 21:13:03 +02002339 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2343{
2344 struct gsm48_hdr *gh = msgb_l3(msg);
2345 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2346 struct tlv_parsed tp;
2347 struct gsm_mncc modify;
2348
2349 gsm48_stop_cc_timer(trans);
2350
2351 memset(&modify, 0, sizeof(struct gsm_mncc));
2352 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002353 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 /* bearer capability */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2356 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002357 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2359 }
2360
2361 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2362
Harald Welte596fed42009-07-23 19:06:52 +02002363 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364}
2365
2366static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2367{
2368 struct gsm_mncc *modify = arg;
2369 struct msgb *msg = gsm48_msgb_alloc();
2370 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2371
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2373
2374 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002375 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376
2377 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2378
Harald Welte39e2ead2009-07-23 21:13:03 +02002379 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380}
2381
2382static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2383{
2384 struct gsm48_hdr *gh = msgb_l3(msg);
2385 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2386 struct tlv_parsed tp;
2387 struct gsm_mncc modify;
2388
2389 gsm48_stop_cc_timer(trans);
2390
2391 memset(&modify, 0, sizeof(struct gsm_mncc));
2392 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 /* bearer capability */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2396 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2399 }
2400 /* cause */
2401 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2402 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002403 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2405 }
2406
2407 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2408
Harald Welte596fed42009-07-23 19:06:52 +02002409 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2413{
2414 struct gsm_mncc *modify = arg;
2415 struct msgb *msg = gsm48_msgb_alloc();
2416 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2417
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2419
2420 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002421 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002423 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424
2425 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2426
Harald Welte39e2ead2009-07-23 21:13:03 +02002427 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428}
2429
2430static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2431{
2432 struct gsm_mncc *notify = arg;
2433 struct msgb *msg = gsm48_msgb_alloc();
2434 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2435
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 gh->msg_type = GSM48_MT_CC_NOTIFY;
2437
2438 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440
Harald Welte39e2ead2009-07-23 21:13:03 +02002441 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2445{
2446 struct gsm48_hdr *gh = msgb_l3(msg);
2447 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2448// struct tlv_parsed tp;
2449 struct gsm_mncc notify;
2450
2451 memset(&notify, 0, sizeof(struct gsm_mncc));
2452 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002453// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002455 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456
Harald Welte596fed42009-07-23 19:06:52 +02002457 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458}
2459
2460static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2461{
2462 struct gsm_mncc *user = arg;
2463 struct msgb *msg = gsm48_msgb_alloc();
2464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2465
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 gh->msg_type = GSM48_MT_CC_USER_INFO;
2467
2468 /* user-user */
2469 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002470 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 /* more data */
2472 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002473 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474
Harald Welte39e2ead2009-07-23 21:13:03 +02002475 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476}
2477
2478static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2479{
2480 struct gsm48_hdr *gh = msgb_l3(msg);
2481 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2482 struct tlv_parsed tp;
2483 struct gsm_mncc user;
2484
2485 memset(&user, 0, sizeof(struct gsm_mncc));
2486 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002487 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 /* user-user */
2489 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2490 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002491 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2493 }
2494 /* more data */
2495 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2496 user.more = 1;
2497
Harald Welte596fed42009-07-23 19:06:52 +02002498 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002501static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502{
2503 struct gsm_mncc *mode = arg;
2504
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01002505 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506}
2507
2508static struct downstate {
2509 u_int32_t states;
2510 int type;
2511 int (*rout) (struct gsm_trans *trans, void *arg);
2512} downstatelist[] = {
2513 /* mobile originating call establishment */
2514 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2515 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2516 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2517 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2518 {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 */
2519 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2520 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2521 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2522 /* mobile terminating call establishment */
2523 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2524 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2525 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2526 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2527 /* signalling during call */
2528 {SBIT(GSM_CSTATE_ACTIVE),
2529 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2530 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2531 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2532 {ALL_STATES,
2533 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2534 {ALL_STATES,
2535 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2536 {ALL_STATES,
2537 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2538 {SBIT(GSM_CSTATE_ACTIVE),
2539 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2540 {SBIT(GSM_CSTATE_ACTIVE),
2541 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2542 {SBIT(GSM_CSTATE_ACTIVE),
2543 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2544 {SBIT(GSM_CSTATE_ACTIVE),
2545 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2546 {SBIT(GSM_CSTATE_ACTIVE),
2547 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2548 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2549 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2550 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2551 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2552 {SBIT(GSM_CSTATE_ACTIVE),
2553 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2554 /* clearing */
2555 {SBIT(GSM_CSTATE_INITIATED),
2556 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2557 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2558 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2559 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2560 MNCC_REL_REQ, gsm48_cc_tx_release},
2561 /* special */
2562 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002563 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564};
2565
2566#define DOWNSLLEN \
2567 (sizeof(downstatelist) / sizeof(struct downstate))
2568
2569
2570int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2571{
Harald Welte1a6f7982009-08-09 18:52:33 +02002572 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002574 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 struct gsm_mncc *data = arg, rel;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002577 struct gsm_subscriber_connection *conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578
2579 /* handle special messages */
2580 switch(msg_type) {
2581 case MNCC_BRIDGE:
2582 return tch_bridge(net, arg);
2583 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002584 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002586 return tch_recv_mncc(net, data->callref, 1);
2587 case GSM_TCHF_FRAME:
2588 /* Find callref */
2589 trans = trans_find_by_callref(net, data->callref);
2590 if (!trans)
2591 return -EIO;
2592 if (!trans->lchan)
2593 return 0;
2594 if (trans->lchan->type != GSM_LCHAN_TCH_F)
2595 return 0;
2596 bts = trans->lchan->ts->trx->bts;
2597 switch (bts->type) {
2598 case GSM_BTS_TYPE_NANOBTS:
2599 if (!trans->lchan->abis_ip.rtp_socket)
2600 return 0;
2601 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
2602 case GSM_BTS_TYPE_BS11:
2603 return trau_send_frame(trans->lchan, arg);
2604 default:
2605 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2606 }
2607 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 }
2609
2610 memset(&rel, 0, sizeof(struct gsm_mncc));
2611 rel.callref = data->callref;
2612
2613 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002614 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615
2616 /* Callref unknown */
2617 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002618 struct gsm_subscriber *subscr;
2619
Harald Welte4a3464c2009-07-04 10:11:24 +02002620 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2622 "Received '%s' from MNCC with "
2623 "unknown callref %d\n", data->called.number,
2624 get_mncc_name(msg_type), data->callref);
2625 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002626 return mncc_release_ind(net, NULL, data->callref,
2627 GSM48_CAUSE_LOC_PRN_S_LU,
2628 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002630 if (!data->called.number[0] && !data->imsi[0]) {
2631 DEBUGP(DCC, "(bts - trx - ts - ti) "
2632 "Received '%s' from MNCC with "
2633 "no number or IMSI\n", get_mncc_name(msg_type));
2634 /* Invalid number */
2635 return mncc_release_ind(net, NULL, data->callref,
2636 GSM48_CAUSE_LOC_PRN_S_LU,
2637 GSM48_CC_CAUSE_INV_NR_FORMAT);
2638 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002640 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002641 subscr = subscr_get_by_extension(net,
2642 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002643 else
Harald Welte9176bd42009-07-23 18:46:00 +02002644 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 /* If subscriber is not found */
2646 if (!subscr) {
2647 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2648 "Received '%s' from MNCC with "
2649 "unknown subscriber %s\n", data->called.number,
2650 get_mncc_name(msg_type), data->called.number);
2651 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002652 return mncc_release_ind(net, NULL, data->callref,
2653 GSM48_CAUSE_LOC_PRN_S_LU,
2654 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 }
2656 /* If subscriber is not "attached" */
2657 if (!subscr->lac) {
2658 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2659 "Received '%s' from MNCC with "
2660 "detached subscriber %s\n", data->called.number,
2661 get_mncc_name(msg_type), data->called.number);
2662 subscr_put(subscr);
2663 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002664 return mncc_release_ind(net, NULL, data->callref,
2665 GSM48_CAUSE_LOC_PRN_S_LU,
2666 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 }
2668 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002669 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2670 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 DEBUGP(DCC, "No memory for trans.\n");
2672 subscr_put(subscr);
2673 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002674 mncc_release_ind(net, NULL, data->callref,
2675 GSM48_CAUSE_LOC_PRN_S_LU,
2676 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 return -ENOMEM;
2678 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002680 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002681
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682 /* If subscriber has no lchan */
2683 if (!lchan) {
2684 /* find transaction with this subscriber already paging */
2685 llist_for_each_entry(transt, &net->trans_list, entry) {
2686 /* Transaction of our lchan? */
2687 if (transt == trans ||
2688 transt->subscr != subscr)
2689 continue;
2690 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2691 "Received '%s' from MNCC with "
2692 "unallocated channel, paging already "
2693 "started.\n", bts->nr,
2694 data->called.number,
2695 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002696 subscr_put(subscr);
2697 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 return 0;
2699 }
2700 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002701 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002702 /* Trigger paging */
2703 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2704 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002705 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 return 0;
2707 }
2708 /* Assign lchan */
2709 trans->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002710 conn = &lchan->conn;
2711 use_subscr_con(conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002712 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 }
2714 lchan = trans->lchan;
2715
2716 /* if paging did not respond yet */
2717 if (!lchan) {
2718 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2719 "Received '%s' from MNCC in paging state\n",
2720 (trans->subscr)?(trans->subscr->extension):"-",
2721 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002722 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2723 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 if (msg_type == MNCC_REL_REQ)
2725 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2726 else
2727 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2728 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002729 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 return rc;
2731 }
2732
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002733 conn = &lchan->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2735 "Received '%s' from MNCC in state %d (%s)\n",
2736 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2737 trans->transaction_id,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002738 (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002739 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08002740 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741
2742 /* Find function for current state and message */
2743 for (i = 0; i < DOWNSLLEN; i++)
2744 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002745 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746 break;
2747 if (i == DOWNSLLEN) {
2748 DEBUGP(DCC, "Message unhandled at this state.\n");
2749 return 0;
2750 }
2751
2752 rc = downstatelist[i].rout(trans, arg);
2753
2754 return rc;
2755}
2756
2757
2758static struct datastate {
2759 u_int32_t states;
2760 int type;
2761 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2762} datastatelist[] = {
2763 /* mobile originating call establishment */
2764 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2765 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2766 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2767 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2768 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2769 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2770 /* mobile terminating call establishment */
2771 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2772 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2773 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2774 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2775 {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 */
2776 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2777 /* signalling during call */
2778 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2779 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2780 {SBIT(GSM_CSTATE_ACTIVE),
2781 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2782 {ALL_STATES,
2783 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2784 {ALL_STATES,
2785 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2786 {ALL_STATES,
2787 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2788 {SBIT(GSM_CSTATE_ACTIVE),
2789 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2790 {SBIT(GSM_CSTATE_ACTIVE),
2791 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2792 {SBIT(GSM_CSTATE_ACTIVE),
2793 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2794 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2795 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2796 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2797 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2798 {SBIT(GSM_CSTATE_ACTIVE),
2799 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2800 /* clearing */
2801 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2802 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2803 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2804 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2805 {ALL_STATES, /* 5.4.3.4 */
2806 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2807};
2808
2809#define DATASLLEN \
2810 (sizeof(datastatelist) / sizeof(struct datastate))
2811
Harald Welte4bc90a12008-12-27 16:32:52 +00002812static int gsm0408_rcv_cc(struct msgb *msg)
2813{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002814 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002815 struct gsm48_hdr *gh = msgb_l3(msg);
2816 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002817 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002819 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002820 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002821
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 if (msg_type & 0x80) {
2823 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2824 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002825 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002826
2827 conn = &lchan->conn;
2828
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002830 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002831
Harald Welte6f5aee02009-07-23 21:21:14 +02002832 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 "Received '%s' from MS in state %d (%s)\n",
2834 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002835 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08002836 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2837 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838
2839 /* Create transaction */
2840 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002841 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 "creating new trans.\n", transaction_id);
2843 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002844 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002845 transaction_id, new_callref++);
2846 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 DEBUGP(DCC, "No memory for trans.\n");
2848 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002849 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 GSM48_MT_CC_RELEASE_COMPL);
2851 return -ENOMEM;
2852 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854 trans->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002855 use_subscr_con(conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856 }
2857
2858 /* find function for current state and message */
2859 for (i = 0; i < DATASLLEN; i++)
2860 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002861 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 break;
2863 if (i == DATASLLEN) {
2864 DEBUGP(DCC, "Message unhandled at this state.\n");
2865 return 0;
2866 }
2867
2868 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002869
2870 return rc;
2871}
2872
Harald Welte52b1f982008-12-23 20:25:15 +00002873/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02002874int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00002875{
2876 struct gsm48_hdr *gh = msgb_l3(msg);
2877 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002878 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002879
2880 if (silent_call_reroute(msg))
2881 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00002882
2883 switch (pdisc) {
2884 case GSM48_PDISC_CC:
2885 rc = gsm0408_rcv_cc(msg);
2886 break;
2887 case GSM48_PDISC_MM:
2888 rc = gsm0408_rcv_mm(msg);
2889 break;
2890 case GSM48_PDISC_RR:
2891 rc = gsm0408_rcv_rr(msg);
2892 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002893 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02002894 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00002895 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002896 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002897 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01002898 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2899 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002900 break;
Harald Welte6eafe912009-10-16 08:32:58 +02002901 case GSM48_PDISC_NC_SS:
2902 rc = handle_rcv_ussd(msg);
2903 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002904 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01002905 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2906 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002907 break;
2908 }
2909
2910 return rc;
2911}
Harald Welte8470bf22008-12-25 23:28:35 +00002912
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913/* dequeue messages to layer 4 */
2914int bsc_upqueue(struct gsm_network *net)
2915{
2916 struct gsm_mncc *mncc;
2917 struct msgb *msg;
2918 int work = 0;
2919
2920 if (net)
2921 while ((msg = msgb_dequeue(&net->upqueue))) {
2922 mncc = (struct gsm_mncc *)msg->data;
2923 if (net->mncc_recv)
2924 net->mncc_recv(net, mncc->msg_type, mncc);
2925 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02002926 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 }
2928
2929 return work;
2930}
Harald Weltedcaf5652009-07-23 18:56:43 +02002931
Harald Welte805f6442009-07-28 18:25:29 +02002932/*
2933 * This will be ran by the linker when loading the DSO. We use it to
2934 * do system initialization, e.g. registration of signal handlers.
2935 */
2936static __attribute__((constructor)) void on_dso_load_0408(void)
2937{
Harald Welte805f6442009-07-28 18:25:29 +02002938 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
2939 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
2940}