blob: eecf76ff09e72cd46f20015f9c2dfe9ea21a74a2 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
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) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100169 if (trans->conn && trans->conn->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;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800616 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000617 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
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200875 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +0000876 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 Weltee95daf12010-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}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001067
Harald Welte4bfdfe72009-06-10 23:11:52 +08001068static 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)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001074 if (trans->conn)
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",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001077 trans->conn->lchan->ts->trx->bts->nr,
1078 trans->conn->lchan->ts->trx->nr,
1079 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001080 (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);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001128 if (trans->conn)
1129 trau_mux_unmap(&trans->conn->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);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001133
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;
Harald Weltec05677b2009-06-26 20:17:06 +02001142
Harald Welte09e38af2009-02-16 22:52:23 +00001143 if (hooknum != GSM_HOOK_RR_PAGING)
1144 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001145
Harald Welte4bfdfe72009-06-10 23:11:52 +08001146 if (!subscr)
1147 return -EINVAL;
1148 net = subscr->net;
1149 if (!net) {
1150 DEBUGP(DCC, "Error Network not set!\n");
1151 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001152 }
Harald Welte7584aea2009-02-11 11:44:12 +00001153
Harald Welte4bfdfe72009-06-10 23:11:52 +08001154 /* check all tranactions (without lchan) for subscriber */
1155 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001156 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001157 continue;
1158 switch (event) {
1159 case GSM_PAGING_SUCCEEDED:
1160 if (!lchan) // paranoid
1161 break;
1162 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1163 subscr->extension);
1164 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001165 if (!transt->conn) {
1166 transt->conn = &lchan->conn;
1167 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001168 }
1169 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001170 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001171 break;
1172 case GSM_PAGING_EXPIRED:
1173 DEBUGP(DCC, "Paging subscr %s expired!\n",
1174 subscr->extension);
1175 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001176 mncc_release_ind(transt->subscr->net, transt,
1177 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001178 GSM48_CAUSE_LOC_PRN_S_LU,
1179 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001180 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001181 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001182 break;
1183 }
1184 }
Harald Welte09e38af2009-02-16 22:52:23 +00001185 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001186}
Harald Welte7584aea2009-02-11 11:44:12 +00001187
Harald Welteda7ab742009-12-19 22:23:05 +01001188static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1189
Harald Welte805f6442009-07-28 18:25:29 +02001190/* some other part of the code sends us a signal */
1191static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1192 void *handler_data, void *signal_data)
1193{
1194 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001195 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001196 struct gsm_network *net;
1197 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001198
1199 if (subsys != SS_ABISIP)
1200 return 0;
1201
1202 /* in case we use direct BTS-to-BTS RTP */
1203 if (ipacc_rtp_direct)
1204 return 0;
1205
Harald Welte805f6442009-07-28 18:25:29 +02001206 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001207 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001208 /* check if any transactions on this lchan still have
1209 * a tch_recv_mncc request pending */
1210 net = lchan->ts->trx->bts->network;
1211 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001212 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001213 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1214 tch_recv_mncc(net, trans->callref, 1);
1215 }
1216 }
Harald Welte805f6442009-07-28 18:25:29 +02001217 break;
Harald Welte805f6442009-07-28 18:25:29 +02001218 }
1219
1220 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001221}
1222
Harald Welte49f48b82009-02-17 15:29:33 +00001223/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001224static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001225{
Harald Welte11fa29c2009-02-19 17:24:39 +00001226 struct gsm_bts *bts = lchan->ts->trx->bts;
1227 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001228 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001229
Harald Welte11fa29c2009-02-19 17:24:39 +00001230 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1231 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1232 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1233
1234 if (bts->type != remote_bts->type) {
1235 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1236 return -EINVAL;
1237 }
Harald Welteda7ab742009-12-19 22:23:05 +01001238
1239 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001240 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001241 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001242 if (!ipacc_rtp_direct) {
1243 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001244 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001245 if (rc < 0)
1246 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001247 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001248#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001249
1250 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301251 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1252 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001253 } else {
1254 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301255 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1256 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301257 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001258 if (rc < 0)
1259 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301260 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1261 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301262 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001263 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001264 break;
1265 case GSM_BTS_TYPE_BS11:
1266 trau_mux_map_lchan(lchan, remote_lchan);
1267 break;
1268 default:
1269 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001270 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001271 }
Harald Welte49f48b82009-02-17 15:29:33 +00001272
1273 return 0;
1274}
1275
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276/* bridge channels of two transactions */
1277static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001278{
Harald Weltedcaf5652009-07-23 18:56:43 +02001279 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1280 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001281
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001283 return -EIO;
1284
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001285 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 return -EIO;
1287
1288 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001289 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001290}
1291
Harald Welteda7ab742009-12-19 22:23:05 +01001292/* enable receive of channels to MNCC upqueue */
1293static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294{
1295 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001296 struct gsm_lchan *lchan;
1297 struct gsm_bts *bts;
1298 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001299
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001301 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302 if (!trans)
1303 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001304 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001306 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001307 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308
Harald Welteda7ab742009-12-19 22:23:05 +01001309 switch (bts->type) {
1310 case GSM_BTS_TYPE_NANOBTS:
1311 if (ipacc_rtp_direct) {
1312 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1313 return -EINVAL;
1314 }
1315 /* in case, we don't have a RTP socket yet, we note this
1316 * in the transaction and try later */
1317 if (!lchan->abis_ip.rtp_socket) {
1318 trans->tch_recv = enable;
1319 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1320 return 0;
1321 }
1322 if (enable) {
1323 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001324 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001325 if (rc < 0)
1326 return rc;
1327 /* assign socket to application interface */
1328 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1329 net, callref);
1330 } else
1331 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1332 net, 0);
1333 break;
1334 case GSM_BTS_TYPE_BS11:
1335 if (enable)
1336 return trau_recv_lchan(lchan, callref);
1337 return trau_mux_unmap(NULL, callref);
1338 break;
1339 default:
1340 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1341 return -EINVAL;
1342 }
1343
1344 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345}
1346
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1348{
1349 DEBUGP(DCC, "-> STATUS ENQ\n");
1350 return gsm48_cc_tx_status(trans, msg);
1351}
1352
1353static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1354static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1355
1356static void gsm48_cc_timeout(void *arg)
1357{
1358 struct gsm_trans *trans = arg;
1359 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001360 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1361 int mo_location = GSM48_CAUSE_LOC_USER;
1362 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1363 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 struct gsm_mncc mo_rel, l4_rel;
1365
1366 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1367 mo_rel.callref = trans->callref;
1368 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1369 l4_rel.callref = trans->callref;
1370
Harald Weltedcaf5652009-07-23 18:56:43 +02001371 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 case 0x303:
1373 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001374 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 break;
1376 case 0x310:
1377 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001378 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379 break;
1380 case 0x313:
1381 disconnect = 1;
1382 /* unknown, did not find it in the specs */
1383 break;
1384 case 0x301:
1385 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001386 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 break;
1388 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001389 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 gsm48_cc_tx_release(trans, &trans->cc.msg);
1392 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 break; /* stay in release state */
1394 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001395 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001396 return;
1397// release = 1;
1398// l4_cause = 14;
1399// break;
1400 case 0x306:
1401 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001402 mo_cause = trans->cc.msg.cause.value;
1403 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 break;
1405 case 0x323:
1406 disconnect = 1;
1407 break;
1408 default:
1409 release = 1;
1410 }
1411
1412 if (release && trans->callref) {
1413 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001414 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415 l4_location, l4_cause);
1416 trans->callref = 0;
1417 }
1418
1419 if (disconnect && trans->callref) {
1420 /* process disconnect towards layer 4 */
1421 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001422 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 }
1424
1425 /* process disconnect towards mobile station */
1426 if (disconnect || release) {
1427 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001428 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1429 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1430 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431 mo_rel.cause.diag_len = 3;
1432
1433 if (disconnect)
1434 gsm48_cc_tx_disconnect(trans, &mo_rel);
1435 if (release)
1436 gsm48_cc_tx_release(trans, &mo_rel);
1437 }
1438
1439}
1440
1441static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1442 int sec, int micro)
1443{
1444 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001445 trans->cc.timer.cb = gsm48_cc_timeout;
1446 trans->cc.timer.data = trans;
1447 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1448 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001449}
1450
1451static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1452{
1453 struct gsm48_hdr *gh = msgb_l3(msg);
1454 u_int8_t msg_type = gh->msg_type & 0xbf;
1455 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1456 struct tlv_parsed tp;
1457 struct gsm_mncc setup;
1458
1459 memset(&setup, 0, sizeof(struct gsm_mncc));
1460 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001461 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 /* emergency setup is identified by msg_type */
1463 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1464 setup.emergency = 1;
1465
1466 /* use subscriber as calling party number */
1467 if (trans->subscr) {
1468 setup.fields |= MNCC_F_CALLING;
1469 strncpy(setup.calling.number, trans->subscr->extension,
1470 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001471 strncpy(setup.imsi, trans->subscr->imsi,
1472 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001473 }
1474 /* bearer capability */
1475 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1476 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001477 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1479 }
1480 /* facility */
1481 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1482 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001483 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001484 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1485 }
1486 /* called party bcd number */
1487 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1488 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001489 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001490 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1491 }
1492 /* user-user */
1493 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1494 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001495 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001496 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1497 }
1498 /* ss-version */
1499 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1500 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001501 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001502 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1503 }
1504 /* CLIR suppression */
1505 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1506 setup.clir.sup = 1;
1507 /* CLIR invocation */
1508 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1509 setup.clir.inv = 1;
1510 /* cc cap */
1511 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1512 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001513 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001514 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1515 }
1516
Harald Welte4bfdfe72009-06-10 23:11:52 +08001517 new_cc_state(trans, GSM_CSTATE_INITIATED);
1518
Harald Welte (local)47df3992009-12-26 19:45:03 +01001519 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1520 subscr_name(trans->subscr), trans->subscr->extension,
1521 setup.called.number);
1522
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001524 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001525
Harald Welte13cac662009-07-29 12:10:35 +02001526 /* MNCC code will modify the channel asynchronously, we should
1527 * ipaccess-bind only after the modification has been made to the
1528 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529 return 0;
1530}
1531
1532static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001533{
1534 struct msgb *msg = gsm48_msgb_alloc();
1535 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001537 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001538
Harald Welte7ccf7782009-02-17 01:43:01 +00001539 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001540
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541 /* transaction id must not be assigned */
1542 if (trans->transaction_id != 0xff) { /* unasssigned */
1543 DEBUGP(DCC, "TX Setup with assigned transaction. "
1544 "This is not allowed!\n");
1545 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001546 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001547 GSM48_CAUSE_LOC_PRN_S_LU,
1548 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001550 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 return rc;
1552 }
1553
1554 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001555 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1556 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001557 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001558 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001559 GSM48_CAUSE_LOC_PRN_S_LU,
1560 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001561 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001562 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 return rc;
1564 }
Harald Welte78283ef2009-07-23 21:36:44 +02001565 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001566
Harald Welte65e74cc2008-12-29 01:55:35 +00001567 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001568
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001570
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571 /* bearer capability */
1572 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001573 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 /* facility */
1575 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001576 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 /* progress */
1578 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001579 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 /* calling party BCD number */
1581 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001582 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 /* called party BCD number */
1584 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001585 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 /* user-user */
1587 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001588 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 /* redirecting party BCD number */
1590 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001591 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 /* signal */
1593 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001594 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001595
1596 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001597
Harald Welte39e2ead2009-07-23 21:13:03 +02001598 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001599}
1600
Harald Welte4bfdfe72009-06-10 23:11:52 +08001601static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1602{
1603 struct gsm48_hdr *gh = msgb_l3(msg);
1604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1605 struct tlv_parsed tp;
1606 struct gsm_mncc call_conf;
1607
1608 gsm48_stop_cc_timer(trans);
1609 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1610
1611 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1612 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001613 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001614#if 0
1615 /* repeat */
1616 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1617 call_conf.repeat = 1;
1618 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1619 call_conf.repeat = 2;
1620#endif
1621 /* bearer capability */
1622 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1623 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001624 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001625 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1626 }
1627 /* cause */
1628 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1629 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001630 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001631 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1632 }
1633 /* cc cap */
1634 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1635 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001636 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1638 }
1639
1640 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1641
Harald Welte596fed42009-07-23 19:06:52 +02001642 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1643 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644}
1645
1646static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1647{
1648 struct gsm_mncc *proceeding = arg;
1649 struct msgb *msg = gsm48_msgb_alloc();
1650 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1651
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1653
1654 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1655
1656 /* bearer capability */
1657 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001658 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 /* facility */
1660 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001661 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 /* progress */
1663 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001664 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665
Harald Welte39e2ead2009-07-23 21:13:03 +02001666 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667}
1668
1669static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1670{
1671 struct gsm48_hdr *gh = msgb_l3(msg);
1672 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1673 struct tlv_parsed tp;
1674 struct gsm_mncc alerting;
1675
1676 gsm48_stop_cc_timer(trans);
1677 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1678
1679 memset(&alerting, 0, sizeof(struct gsm_mncc));
1680 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001681 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 /* facility */
1683 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1684 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001685 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1687 }
1688
1689 /* progress */
1690 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1691 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001692 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1694 }
1695 /* ss-version */
1696 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1697 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001698 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1700 }
1701
1702 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1703
Harald Welte596fed42009-07-23 19:06:52 +02001704 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1705 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706}
1707
1708static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1709{
1710 struct gsm_mncc *alerting = arg;
1711 struct msgb *msg = gsm48_msgb_alloc();
1712 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1713
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 gh->msg_type = GSM48_MT_CC_ALERTING;
1715
1716 /* facility */
1717 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001718 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719 /* progress */
1720 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001721 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 /* user-user */
1723 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001724 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725
1726 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1727
Harald Welte39e2ead2009-07-23 21:13:03 +02001728 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729}
1730
1731static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1732{
1733 struct gsm_mncc *progress = arg;
1734 struct msgb *msg = gsm48_msgb_alloc();
1735 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1736
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 gh->msg_type = GSM48_MT_CC_PROGRESS;
1738
1739 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001740 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 /* user-user */
1742 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001743 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744
Harald Welte39e2ead2009-07-23 21:13:03 +02001745 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746}
1747
1748static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1749{
1750 struct gsm_mncc *connect = arg;
1751 struct msgb *msg = gsm48_msgb_alloc();
1752 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1753
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 gh->msg_type = GSM48_MT_CC_CONNECT;
1755
1756 gsm48_stop_cc_timer(trans);
1757 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1758
1759 /* facility */
1760 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001761 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 /* progress */
1763 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001764 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* connected number */
1766 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001767 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 /* user-user */
1769 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001770 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771
1772 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1773
Harald Welte39e2ead2009-07-23 21:13:03 +02001774 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775}
1776
1777static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1778{
1779 struct gsm48_hdr *gh = msgb_l3(msg);
1780 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1781 struct tlv_parsed tp;
1782 struct gsm_mncc connect;
1783
1784 gsm48_stop_cc_timer(trans);
1785
1786 memset(&connect, 0, sizeof(struct gsm_mncc));
1787 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001788 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 /* use subscriber as connected party number */
1790 if (trans->subscr) {
1791 connect.fields |= MNCC_F_CONNECTED;
1792 strncpy(connect.connected.number, trans->subscr->extension,
1793 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001794 strncpy(connect.imsi, trans->subscr->imsi,
1795 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 }
1797 /* facility */
1798 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1799 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1802 }
1803 /* user-user */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1805 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1808 }
1809 /* ss-version */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1811 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1814 }
1815
1816 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1817
Harald Welte596fed42009-07-23 19:06:52 +02001818 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819}
1820
1821
1822static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1823{
1824 struct gsm_mncc connect_ack;
1825
1826 gsm48_stop_cc_timer(trans);
1827
1828 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1829
1830 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1831 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001832 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833 &connect_ack);
1834}
1835
1836static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1837{
1838 struct msgb *msg = gsm48_msgb_alloc();
1839 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1840
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1842
1843 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1844
Harald Welte39e2ead2009-07-23 21:13:03 +02001845 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846}
1847
1848static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1849{
1850 struct gsm48_hdr *gh = msgb_l3(msg);
1851 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1852 struct tlv_parsed tp;
1853 struct gsm_mncc disc;
1854
1855 gsm48_stop_cc_timer(trans);
1856
1857 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1858
1859 memset(&disc, 0, sizeof(struct gsm_mncc));
1860 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001861 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862 /* cause */
1863 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1864 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1867 }
1868 /* facility */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1870 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1873 }
1874 /* user-user */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1876 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1879 }
1880 /* ss-version */
1881 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1882 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1885 }
1886
Harald Welte596fed42009-07-23 19:06:52 +02001887 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888
1889}
1890
Harald Weltec66b71c2009-06-11 14:23:20 +08001891static struct gsm_mncc_cause default_cause = {
1892 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1893 .coding = 0,
1894 .rec = 0,
1895 .rec_val = 0,
1896 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1897 .diag_len = 0,
1898 .diag = { 0 },
1899};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900
1901static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1902{
1903 struct gsm_mncc *disc = arg;
1904 struct msgb *msg = gsm48_msgb_alloc();
1905 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1906
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1908
1909 gsm48_stop_cc_timer(trans);
1910 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1911
1912 /* cause */
1913 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001914 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 else
Harald Welte55c8f352010-03-07 23:40:35 +01001916 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917
1918 /* facility */
1919 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 /* progress */
1922 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 /* user-user */
1925 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927
1928 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001929 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930
1931 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1932
Harald Welte39e2ead2009-07-23 21:13:03 +02001933 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934}
1935
1936static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1937{
1938 struct gsm48_hdr *gh = msgb_l3(msg);
1939 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1940 struct tlv_parsed tp;
1941 struct gsm_mncc rel;
1942 int rc;
1943
1944 gsm48_stop_cc_timer(trans);
1945
1946 memset(&rel, 0, sizeof(struct gsm_mncc));
1947 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001948 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 /* cause */
1950 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1951 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001952 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1954 }
1955 /* facility */
1956 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1957 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001958 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1960 }
1961 /* user-user */
1962 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1963 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1966 }
1967 /* ss-version */
1968 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1969 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1972 }
1973
Harald Weltedcaf5652009-07-23 18:56:43 +02001974 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02001976 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 } else {
Harald Welte596fed42009-07-23 19:06:52 +02001978 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02001979 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02001980 GSM48_MT_CC_RELEASE_COMPL);
1981 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 }
1983
1984 new_cc_state(trans, GSM_CSTATE_NULL);
1985
1986 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001987 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988
1989 return rc;
1990}
1991
1992static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1993{
1994 struct gsm_mncc *rel = arg;
1995 struct msgb *msg = gsm48_msgb_alloc();
1996 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 gh->msg_type = GSM48_MT_CC_RELEASE;
1999
2000 trans->callref = 0;
2001
2002 gsm48_stop_cc_timer(trans);
2003 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2004
2005 /* cause */
2006 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002007 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 /* facility */
2009 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002010 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 /* user-user */
2012 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002013 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014
Harald Weltedcaf5652009-07-23 18:56:43 +02002015 trans->cc.T308_second = 0;
2016 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017
Harald Weltedcaf5652009-07-23 18:56:43 +02002018 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2020
Harald Welte39e2ead2009-07-23 21:13:03 +02002021 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022}
2023
2024static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2025{
2026 struct gsm48_hdr *gh = msgb_l3(msg);
2027 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2028 struct tlv_parsed tp;
2029 struct gsm_mncc rel;
2030 int rc = 0;
2031
2032 gsm48_stop_cc_timer(trans);
2033
2034 memset(&rel, 0, sizeof(struct gsm_mncc));
2035 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002036 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 /* cause */
2038 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2039 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2042 }
2043 /* facility */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2045 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2048 }
2049 /* user-user */
2050 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2051 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002052 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2054 }
2055 /* ss-version */
2056 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2057 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002058 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2060 }
2061
2062 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002063 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002065 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 MNCC_REJ_IND, &rel);
2067 break;
2068 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002069 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002071 /* FIXME: in case of multiple calls, we can't simply
2072 * hang up here ! */
2073 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074 break;
2075 default:
Harald Welte596fed42009-07-23 19:06:52 +02002076 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 MNCC_REL_IND, &rel);
2078 }
2079 }
2080
2081 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002082 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083
2084 return rc;
2085}
2086
2087static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2088{
2089 struct gsm_mncc *rel = arg;
2090 struct msgb *msg = gsm48_msgb_alloc();
2091 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2092
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2094
2095 trans->callref = 0;
2096
2097 gsm48_stop_cc_timer(trans);
2098
2099 /* cause */
2100 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002101 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102 /* facility */
2103 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 /* user-user */
2106 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002107 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108
Harald Weltedcaf5652009-07-23 18:56:43 +02002109 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110
Harald Welte39e2ead2009-07-23 21:13:03 +02002111 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112}
2113
2114static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2115{
2116 struct gsm48_hdr *gh = msgb_l3(msg);
2117 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2118 struct tlv_parsed tp;
2119 struct gsm_mncc fac;
2120
2121 memset(&fac, 0, sizeof(struct gsm_mncc));
2122 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002123 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 /* facility */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2126 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2129 }
2130 /* ss-version */
2131 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2132 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2135 }
2136
Harald Welte596fed42009-07-23 19:06:52 +02002137 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138}
2139
2140static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2141{
2142 struct gsm_mncc *fac = arg;
2143 struct msgb *msg = gsm48_msgb_alloc();
2144 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2145
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 gh->msg_type = GSM48_MT_CC_FACILITY;
2147
2148 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002149 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150
Harald Welte39e2ead2009-07-23 21:13:03 +02002151 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152}
2153
2154static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2155{
2156 struct gsm_mncc hold;
2157
2158 memset(&hold, 0, sizeof(struct gsm_mncc));
2159 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002160 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161}
2162
2163static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2164{
2165 struct msgb *msg = gsm48_msgb_alloc();
2166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2167
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2169
Harald Welte39e2ead2009-07-23 21:13:03 +02002170 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171}
2172
2173static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2174{
2175 struct gsm_mncc *hold_rej = arg;
2176 struct msgb *msg = gsm48_msgb_alloc();
2177 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2178
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2180
2181 /* cause */
2182 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002183 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184 else
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186
Harald Welte39e2ead2009-07-23 21:13:03 +02002187 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188}
2189
2190static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2191{
2192 struct gsm_mncc retrieve;
2193
2194 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2195 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002196 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2197 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198}
2199
2200static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2201{
2202 struct msgb *msg = gsm48_msgb_alloc();
2203 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2204
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2206
Harald Welte39e2ead2009-07-23 21:13:03 +02002207 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208}
2209
2210static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2211{
2212 struct gsm_mncc *retrieve_rej = arg;
2213 struct msgb *msg = gsm48_msgb_alloc();
2214 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2215
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2217
2218 /* cause */
2219 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002220 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 else
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223
Harald Welte39e2ead2009-07-23 21:13:03 +02002224 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225}
2226
2227static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2228{
2229 struct gsm48_hdr *gh = msgb_l3(msg);
2230 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2231 struct tlv_parsed tp;
2232 struct gsm_mncc dtmf;
2233
2234 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2235 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002236 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* keypad facility */
2238 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2239 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002240 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2242 }
2243
Harald Welte596fed42009-07-23 19:06:52 +02002244 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245}
2246
2247static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2248{
2249 struct gsm_mncc *dtmf = arg;
2250 struct msgb *msg = gsm48_msgb_alloc();
2251 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2252
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2254
2255 /* keypad */
2256 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258
Harald Welte39e2ead2009-07-23 21:13:03 +02002259 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260}
2261
2262static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2263{
2264 struct gsm_mncc *dtmf = arg;
2265 struct msgb *msg = gsm48_msgb_alloc();
2266 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2267
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2269
2270 /* cause */
2271 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 else
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275
Harald Welte39e2ead2009-07-23 21:13:03 +02002276 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277}
2278
2279static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2280{
2281 struct msgb *msg = gsm48_msgb_alloc();
2282 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2283
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2285
Harald Welte39e2ead2009-07-23 21:13:03 +02002286 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287}
2288
2289static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2290{
2291 struct gsm_mncc dtmf;
2292
2293 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2294 dtmf.callref = trans->callref;
2295
Harald Welte596fed42009-07-23 19:06:52 +02002296 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297}
2298
2299static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2300{
2301 struct gsm48_hdr *gh = msgb_l3(msg);
2302 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2303 struct tlv_parsed tp;
2304 struct gsm_mncc modify;
2305
2306 memset(&modify, 0, sizeof(struct gsm_mncc));
2307 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002308 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 /* bearer capability */
2310 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2311 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002312 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2314 }
2315
2316 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2317
Harald Welte596fed42009-07-23 19:06:52 +02002318 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319}
2320
2321static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2322{
2323 struct gsm_mncc *modify = arg;
2324 struct msgb *msg = gsm48_msgb_alloc();
2325 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2326
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 gh->msg_type = GSM48_MT_CC_MODIFY;
2328
2329 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2330
2331 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002332 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333
2334 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2335
Harald Welte39e2ead2009-07-23 21:13:03 +02002336 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337}
2338
2339static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2340{
2341 struct gsm48_hdr *gh = msgb_l3(msg);
2342 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2343 struct tlv_parsed tp;
2344 struct gsm_mncc modify;
2345
2346 gsm48_stop_cc_timer(trans);
2347
2348 memset(&modify, 0, sizeof(struct gsm_mncc));
2349 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002350 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 /* bearer capability */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2353 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2356 }
2357
2358 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2359
Harald Welte596fed42009-07-23 19:06:52 +02002360 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361}
2362
2363static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2364{
2365 struct gsm_mncc *modify = arg;
2366 struct msgb *msg = gsm48_msgb_alloc();
2367 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2368
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2370
2371 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002372 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373
2374 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2375
Harald Welte39e2ead2009-07-23 21:13:03 +02002376 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377}
2378
2379static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2380{
2381 struct gsm48_hdr *gh = msgb_l3(msg);
2382 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2383 struct tlv_parsed tp;
2384 struct gsm_mncc modify;
2385
2386 gsm48_stop_cc_timer(trans);
2387
2388 memset(&modify, 0, sizeof(struct gsm_mncc));
2389 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002390 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391 /* bearer capability */
2392 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2393 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002394 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2396 }
2397 /* cause */
2398 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2399 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2402 }
2403
2404 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2405
Harald Welte596fed42009-07-23 19:06:52 +02002406 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2410{
2411 struct gsm_mncc *modify = arg;
2412 struct msgb *msg = gsm48_msgb_alloc();
2413 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2414
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2416
2417 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002418 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421
2422 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2423
Harald Welte39e2ead2009-07-23 21:13:03 +02002424 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *notify = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_NOTIFY;
2434
2435 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002436 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437
Harald Welte39e2ead2009-07-23 21:13:03 +02002438 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439}
2440
2441static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2442{
2443 struct gsm48_hdr *gh = msgb_l3(msg);
2444 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2445// struct tlv_parsed tp;
2446 struct gsm_mncc notify;
2447
2448 memset(&notify, 0, sizeof(struct gsm_mncc));
2449 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002450// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002452 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453
Harald Welte596fed42009-07-23 19:06:52 +02002454 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455}
2456
2457static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2458{
2459 struct gsm_mncc *user = arg;
2460 struct msgb *msg = gsm48_msgb_alloc();
2461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2462
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 gh->msg_type = GSM48_MT_CC_USER_INFO;
2464
2465 /* user-user */
2466 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002467 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 /* more data */
2469 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002470 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471
Harald Welte39e2ead2009-07-23 21:13:03 +02002472 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473}
2474
2475static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2476{
2477 struct gsm48_hdr *gh = msgb_l3(msg);
2478 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2479 struct tlv_parsed tp;
2480 struct gsm_mncc user;
2481
2482 memset(&user, 0, sizeof(struct gsm_mncc));
2483 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002484 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485 /* user-user */
2486 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2487 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002488 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2490 }
2491 /* more data */
2492 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2493 user.more = 1;
2494
Harald Welte596fed42009-07-23 19:06:52 +02002495 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496}
2497
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002498static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499{
2500 struct gsm_mncc *mode = arg;
2501
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002502 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503}
2504
2505static struct downstate {
2506 u_int32_t states;
2507 int type;
2508 int (*rout) (struct gsm_trans *trans, void *arg);
2509} downstatelist[] = {
2510 /* mobile originating call establishment */
2511 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2512 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2513 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2514 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2515 {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 */
2516 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2517 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2518 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2519 /* mobile terminating call establishment */
2520 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2521 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2522 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2523 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2524 /* signalling during call */
2525 {SBIT(GSM_CSTATE_ACTIVE),
2526 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2527 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2528 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2529 {ALL_STATES,
2530 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2531 {ALL_STATES,
2532 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2533 {ALL_STATES,
2534 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2535 {SBIT(GSM_CSTATE_ACTIVE),
2536 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2537 {SBIT(GSM_CSTATE_ACTIVE),
2538 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2539 {SBIT(GSM_CSTATE_ACTIVE),
2540 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2541 {SBIT(GSM_CSTATE_ACTIVE),
2542 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2543 {SBIT(GSM_CSTATE_ACTIVE),
2544 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2545 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2546 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2547 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2548 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2549 {SBIT(GSM_CSTATE_ACTIVE),
2550 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2551 /* clearing */
2552 {SBIT(GSM_CSTATE_INITIATED),
2553 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2554 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2555 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2556 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2557 MNCC_REL_REQ, gsm48_cc_tx_release},
2558 /* special */
2559 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002560 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561};
2562
2563#define DOWNSLLEN \
2564 (sizeof(downstatelist) / sizeof(struct downstate))
2565
2566
2567int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2568{
Harald Welte1a6f7982009-08-09 18:52:33 +02002569 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002571 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 struct gsm_mncc *data = arg, rel;
2574
2575 /* handle special messages */
2576 switch(msg_type) {
2577 case MNCC_BRIDGE:
2578 return tch_bridge(net, arg);
2579 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002580 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002582 return tch_recv_mncc(net, data->callref, 1);
2583 case GSM_TCHF_FRAME:
2584 /* Find callref */
2585 trans = trans_find_by_callref(net, data->callref);
2586 if (!trans)
2587 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002588 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002589 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002590 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002591 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002592 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002593 switch (bts->type) {
2594 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002595 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002596 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002597 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002598 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002599 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002600 default:
2601 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2602 }
2603 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604 }
2605
2606 memset(&rel, 0, sizeof(struct gsm_mncc));
2607 rel.callref = data->callref;
2608
2609 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002610 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611
2612 /* Callref unknown */
2613 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002614 struct gsm_subscriber *subscr;
2615
Harald Welte4a3464c2009-07-04 10:11:24 +02002616 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2618 "Received '%s' from MNCC with "
2619 "unknown callref %d\n", data->called.number,
2620 get_mncc_name(msg_type), data->callref);
2621 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002622 return mncc_release_ind(net, NULL, data->callref,
2623 GSM48_CAUSE_LOC_PRN_S_LU,
2624 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002626 if (!data->called.number[0] && !data->imsi[0]) {
2627 DEBUGP(DCC, "(bts - trx - ts - ti) "
2628 "Received '%s' from MNCC with "
2629 "no number or IMSI\n", get_mncc_name(msg_type));
2630 /* Invalid number */
2631 return mncc_release_ind(net, NULL, data->callref,
2632 GSM48_CAUSE_LOC_PRN_S_LU,
2633 GSM48_CC_CAUSE_INV_NR_FORMAT);
2634 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002636 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002637 subscr = subscr_get_by_extension(net,
2638 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002639 else
Harald Welte9176bd42009-07-23 18:46:00 +02002640 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 /* If subscriber is not found */
2642 if (!subscr) {
2643 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2644 "Received '%s' from MNCC with "
2645 "unknown subscriber %s\n", data->called.number,
2646 get_mncc_name(msg_type), data->called.number);
2647 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002648 return mncc_release_ind(net, NULL, data->callref,
2649 GSM48_CAUSE_LOC_PRN_S_LU,
2650 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 }
2652 /* If subscriber is not "attached" */
2653 if (!subscr->lac) {
2654 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2655 "Received '%s' from MNCC with "
2656 "detached subscriber %s\n", data->called.number,
2657 get_mncc_name(msg_type), data->called.number);
2658 subscr_put(subscr);
2659 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002660 return mncc_release_ind(net, NULL, data->callref,
2661 GSM48_CAUSE_LOC_PRN_S_LU,
2662 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 }
2664 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002665 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2666 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 DEBUGP(DCC, "No memory for trans.\n");
2668 subscr_put(subscr);
2669 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002670 mncc_release_ind(net, NULL, data->callref,
2671 GSM48_CAUSE_LOC_PRN_S_LU,
2672 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 return -ENOMEM;
2674 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002676 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002677
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 /* If subscriber has no lchan */
2679 if (!lchan) {
2680 /* find transaction with this subscriber already paging */
2681 llist_for_each_entry(transt, &net->trans_list, entry) {
2682 /* Transaction of our lchan? */
2683 if (transt == trans ||
2684 transt->subscr != subscr)
2685 continue;
2686 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2687 "Received '%s' from MNCC with "
2688 "unallocated channel, paging already "
2689 "started.\n", bts->nr,
2690 data->called.number,
2691 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002692 subscr_put(subscr);
2693 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694 return 0;
2695 }
2696 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002697 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002698 /* Trigger paging */
2699 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2700 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002701 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 return 0;
2703 }
2704 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002705 trans->conn = &lchan->conn;
2706 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002707 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002709
2710 if (trans->conn)
2711 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712
2713 /* if paging did not respond yet */
2714 if (!lchan) {
2715 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002716 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 (trans->subscr)?(trans->subscr->extension):"-",
2718 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002719 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2720 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 if (msg_type == MNCC_REL_REQ)
2722 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2723 else
2724 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2725 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002726 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 return rc;
2728 }
2729
2730 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2731 "Received '%s' from MNCC in state %d (%s)\n",
2732 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2733 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002734 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002735 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002736 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737
2738 /* Find function for current state and message */
2739 for (i = 0; i < DOWNSLLEN; i++)
2740 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002741 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 break;
2743 if (i == DOWNSLLEN) {
2744 DEBUGP(DCC, "Message unhandled at this state.\n");
2745 return 0;
2746 }
2747
2748 rc = downstatelist[i].rout(trans, arg);
2749
2750 return rc;
2751}
2752
2753
2754static struct datastate {
2755 u_int32_t states;
2756 int type;
2757 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2758} datastatelist[] = {
2759 /* mobile originating call establishment */
2760 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2761 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2762 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2763 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2764 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2765 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2766 /* mobile terminating call establishment */
2767 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2768 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2769 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2770 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002771 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2773 /* signalling during call */
2774 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2775 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2776 {SBIT(GSM_CSTATE_ACTIVE),
2777 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2778 {ALL_STATES,
2779 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2780 {ALL_STATES,
2781 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2782 {ALL_STATES,
2783 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2784 {SBIT(GSM_CSTATE_ACTIVE),
2785 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2786 {SBIT(GSM_CSTATE_ACTIVE),
2787 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2788 {SBIT(GSM_CSTATE_ACTIVE),
2789 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2790 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2791 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2792 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2793 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2794 {SBIT(GSM_CSTATE_ACTIVE),
2795 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2796 /* clearing */
2797 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2798 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2799 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2800 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2801 {ALL_STATES, /* 5.4.3.4 */
2802 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2803};
2804
2805#define DATASLLEN \
2806 (sizeof(datastatelist) / sizeof(struct datastate))
2807
Harald Welte4bc90a12008-12-27 16:32:52 +00002808static int gsm0408_rcv_cc(struct msgb *msg)
2809{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002810 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002811 struct gsm48_hdr *gh = msgb_l3(msg);
2812 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002813 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002815 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002817
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 if (msg_type & 0x80) {
2819 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2820 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002821 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002822
2823 conn = &lchan->conn;
2824
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002826 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002827
Harald Welte6f5aee02009-07-23 21:21:14 +02002828 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829 "Received '%s' from MS in state %d (%s)\n",
2830 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002831 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08002832 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2833 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834
2835 /* Create transaction */
2836 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002837 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 "creating new trans.\n", transaction_id);
2839 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002840 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002841 transaction_id, new_callref++);
2842 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843 DEBUGP(DCC, "No memory for trans.\n");
2844 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002845 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846 GSM48_MT_CC_RELEASE_COMPL);
2847 return -ENOMEM;
2848 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002850 trans->conn = &lchan->conn;
2851 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852 }
2853
2854 /* find function for current state and message */
2855 for (i = 0; i < DATASLLEN; i++)
2856 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002857 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 break;
2859 if (i == DATASLLEN) {
2860 DEBUGP(DCC, "Message unhandled at this state.\n");
2861 return 0;
2862 }
2863
2864 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002865
2866 return rc;
2867}
2868
Harald Welte52b1f982008-12-23 20:25:15 +00002869/* 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 +02002870int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00002871{
2872 struct gsm48_hdr *gh = msgb_l3(msg);
2873 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002874 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002875
2876 if (silent_call_reroute(msg))
2877 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00002878
2879 switch (pdisc) {
2880 case GSM48_PDISC_CC:
2881 rc = gsm0408_rcv_cc(msg);
2882 break;
2883 case GSM48_PDISC_MM:
2884 rc = gsm0408_rcv_mm(msg);
2885 break;
2886 case GSM48_PDISC_RR:
2887 rc = gsm0408_rcv_rr(msg);
2888 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002889 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02002890 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00002891 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002892 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002893 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01002894 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2895 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002896 break;
Harald Welte6eafe912009-10-16 08:32:58 +02002897 case GSM48_PDISC_NC_SS:
2898 rc = handle_rcv_ussd(msg);
2899 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002900 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01002901 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2902 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002903 break;
2904 }
2905
2906 return rc;
2907}
Harald Welte8470bf22008-12-25 23:28:35 +00002908
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909/* dequeue messages to layer 4 */
2910int bsc_upqueue(struct gsm_network *net)
2911{
2912 struct gsm_mncc *mncc;
2913 struct msgb *msg;
2914 int work = 0;
2915
2916 if (net)
2917 while ((msg = msgb_dequeue(&net->upqueue))) {
2918 mncc = (struct gsm_mncc *)msg->data;
2919 if (net->mncc_recv)
2920 net->mncc_recv(net, mncc->msg_type, mncc);
2921 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02002922 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 }
2924
2925 return work;
2926}
Harald Weltedcaf5652009-07-23 18:56:43 +02002927
Harald Welte805f6442009-07-28 18:25:29 +02002928/*
2929 * This will be ran by the linker when loading the DSO. We use it to
2930 * do system initialization, e.g. registration of signal handlers.
2931 */
2932static __attribute__((constructor)) void on_dso_load_0408(void)
2933{
Harald Welte805f6442009-07-28 18:25:29 +02002934 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
2935 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
2936}