blob: ed2b4e5b086fac132da71ffe0a9f60bc5ff1f03b [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 Welte52b1f982008-12-23 20:25:15 +0000392 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200393 subscr = subscr_get_by_tmsi(bts->network,
394 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000395 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000396 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000397 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100398 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000399 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200400 /* we always want the IMEI, too */
401 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
402 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000403 break;
404 case GSM_MI_TYPE_IMEI:
405 case GSM_MI_TYPE_IMEISV:
406 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200407 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000408 break;
409 default:
Harald Weltea0368542009-06-27 02:58:43 +0200410 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000411 break;
412 }
413
Harald Welte24516ea2009-07-04 10:18:00 +0200414 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100415 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200416
Harald Welte4bfdfe72009-06-10 23:11:52 +0800417 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200418 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800419 /* FIXME: request id? close channel? */
420 return -EINVAL;
421 }
422
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100423 conn->subscr = subscr;
424 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000425
Harald Welte24516ea2009-07-04 10:18:00 +0200426 /* check if we can let the subscriber into our network immediately
427 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100428 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000429}
430
Harald Welte4bfdfe72009-06-10 23:11:52 +0800431#if 0
432static u_int8_t to_bcd8(u_int8_t val)
433{
434 return ((val / 10) << 4) | (val % 10);
435}
436#endif
437
Harald Weltedb253af2008-12-30 17:56:55 +0000438/* Section 9.2.15a */
439int gsm48_tx_mm_info(struct gsm_lchan *lchan)
440{
441 struct msgb *msg = gsm48_msgb_alloc();
442 struct gsm48_hdr *gh;
443 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000444 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200445 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800446#if 0
447 time_t cur_t;
448 struct tm* cur_time;
449 int tz15min;
450#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000451
452 msg->lchan = lchan;
453
454 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
455 gh->proto_discr = GSM48_PDISC_MM;
456 gh->msg_type = GSM48_MT_MM_INFO;
457
458 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200459#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000460 name_len = strlen(net->name_long);
461 /* 10.5.3.5a */
462 ptr8 = msgb_put(msg, 3);
463 ptr8[0] = GSM48_IE_NAME_LONG;
464 ptr8[1] = name_len*2 +1;
465 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
466
467 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
468 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000469 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000470
471 /* FIXME: Use Cell Broadcast, not UCS-2, since
472 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200473#endif
474 name_len = (strlen(net->name_long)*7)/8;
475 name_pad = (8 - strlen(net->name_long)*7)%8;
476 if (name_pad > 0)
477 name_len++;
478 /* 10.5.3.5a */
479 ptr8 = msgb_put(msg, 3);
480 ptr8[0] = GSM48_IE_NAME_LONG;
481 ptr8[1] = name_len +1;
482 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
483
484 ptr8 = msgb_put(msg, name_len);
485 gsm_7bit_encode(ptr8, net->name_long);
486
Harald Weltedb253af2008-12-30 17:56:55 +0000487 }
488
489 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200490#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000491 name_len = strlen(net->name_short);
492 /* 10.5.3.5a */
493 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200494 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000495 ptr8[1] = name_len*2 + 1;
496 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
497
Harald Weltee872cb12009-01-01 00:33:37 +0000498 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000499 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000500 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200501#endif
502 name_len = (strlen(net->name_short)*7)/8;
503 name_pad = (8 - strlen(net->name_short)*7)%8;
504 if (name_pad > 0)
505 name_len++;
506 /* 10.5.3.5a */
507 ptr8 = (u_int8_t *) msgb_put(msg, 3);
508 ptr8[0] = GSM48_IE_NAME_SHORT;
509 ptr8[1] = name_len +1;
510 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
511
512 ptr8 = msgb_put(msg, name_len);
513 gsm_7bit_encode(ptr8, net->name_short);
514
Harald Weltedb253af2008-12-30 17:56:55 +0000515 }
516
517#if 0
518 /* Section 10.5.3.9 */
519 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800520 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000521 ptr8 = msgb_put(msg, 8);
522 ptr8[0] = GSM48_IE_NET_TIME_TZ;
523 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
524 ptr8[2] = to_bcd8(cur_time->tm_mon);
525 ptr8[3] = to_bcd8(cur_time->tm_mday);
526 ptr8[4] = to_bcd8(cur_time->tm_hour);
527 ptr8[5] = to_bcd8(cur_time->tm_min);
528 ptr8[6] = to_bcd8(cur_time->tm_sec);
529 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
530 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800531 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000532 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800533 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000534#endif
535
Daniel Willmanneea93372009-08-13 03:42:07 +0200536 DEBUGP(DMM, "-> MM INFO\n");
537
Harald Welte39e2ead2009-07-23 21:13:03 +0200538 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000539}
540
Harald Welte7984d5c2009-08-12 22:56:50 +0200541/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100542int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200543{
544 struct msgb *msg = gsm48_msgb_alloc();
545 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200546 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200547
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100548 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200549
550 msg->lchan = lchan;
551 gh->proto_discr = GSM48_PDISC_MM;
552 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
553
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100554 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200555
Harald Welte7984d5c2009-08-12 22:56:50 +0200556 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200557 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200558 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200559
560 return gsm48_sendmsg(msg, NULL);
561}
562
563/* Section 9.2.1 */
564int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
565{
566 DEBUGP(DMM, "-> AUTH REJECT\n");
567 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
568}
569
Harald Welte4b634542008-12-27 01:55:51 +0000570static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
571{
Harald Welte4b634542008-12-27 01:55:51 +0000572 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000573 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000574}
Harald Welteba4cf162009-01-10 01:49:35 +0000575
576/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100577static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000578 enum gsm48_reject_value value)
579{
580 struct msgb *msg = gsm48_msgb_alloc();
581 struct gsm48_hdr *gh;
582
583 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
584
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100585 msg->lchan = conn->lchan;
586 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000587
588 gh->proto_discr = GSM48_PDISC_MM;
589 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
590 gh->data[0] = value;
591 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
592
Harald Welte39e2ead2009-07-23 21:13:03 +0200593 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000594}
595
Harald Welte4ed0e922009-01-10 03:17:30 +0000596/*
597 * Handle CM Service Requests
598 * a) Verify that the packet is long enough to contain the information
599 * we require otherwsie reject with INCORRECT_MESSAGE
600 * b) Try to parse the TMSI. If we do not have one reject
601 * c) Check that we know the subscriber with the TMSI otherwise reject
602 * with a HLR cause
603 * d) Set the subscriber on the gsm_lchan and accept
604 */
Harald Welte4b634542008-12-27 01:55:51 +0000605static int gsm48_rx_mm_serv_req(struct msgb *msg)
606{
Harald Welteba4cf162009-01-10 01:49:35 +0000607 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200608 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000609
Harald Welte9176bd42009-07-23 18:46:00 +0200610 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000611 struct gsm_subscriber *subscr;
612 struct gsm48_hdr *gh = msgb_l3(msg);
613 struct gsm48_service_request *req =
614 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800615 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000616 u_int8_t classmark2_len = gh->data[1];
617 u_int8_t *classmark2 = gh->data+2;
618 u_int8_t mi_len = *(classmark2 + classmark2_len);
619 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000620
Harald Weltec9e02182009-05-01 19:07:53 +0000621 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000622 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000623 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100624 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000625 GSM48_REJECT_INCORRECT_MESSAGE);
626 }
627
628 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000629 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100630 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000631 GSM48_REJECT_INCORRECT_MESSAGE);
632 }
633
Harald Weltec9e02182009-05-01 19:07:53 +0000634 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000635 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000636 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100637 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000638 GSM48_REJECT_INCORRECT_MESSAGE);
639 }
640
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200641 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000642 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000643 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000644
Harald Welte7659de12009-12-13 12:39:18 +0100645 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
646
Harald Welte3ac7f102009-08-10 10:12:45 +0200647 if (is_siemens_bts(bts))
648 send_siemens_mrpci(msg->lchan, classmark2-1);
649
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200650 subscr = subscr_get_by_tmsi(bts->network,
651 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000652
Harald Welte2a139372009-02-22 21:14:55 +0000653 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000654 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100655 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000656 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
657
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100658 if (!msg->lchan->conn.subscr)
659 msg->lchan->conn.subscr = subscr;
660 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100661 subscr_put(subscr); /* lchan already has a ref, don't need another one */
662 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000663 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
664 subscr_put(subscr);
665 }
666
Harald Weltec2e302d2009-07-05 14:08:13 +0200667 subscr->equipment.classmark2_len = classmark2_len;
668 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
669 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000670
Harald Welte4b634542008-12-27 01:55:51 +0000671 return gsm48_tx_mm_serv_ack(msg->lchan);
672}
673
Harald Welte2a139372009-02-22 21:14:55 +0000674static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
675{
Harald Welte9176bd42009-07-23 18:46:00 +0200676 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000677 struct gsm48_hdr *gh = msgb_l3(msg);
678 struct gsm48_imsi_detach_ind *idi =
679 (struct gsm48_imsi_detach_ind *) gh->data;
680 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200681 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000683
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200684 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000685 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
686 mi_type, mi_string);
687
Harald Welteffa55a42009-12-22 19:07:32 +0100688 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100689
Harald Welte2a139372009-02-22 21:14:55 +0000690 switch (mi_type) {
691 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200692 subscr = subscr_get_by_tmsi(bts->network,
693 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000694 break;
695 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200696 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000697 break;
698 case GSM_MI_TYPE_IMEI:
699 case GSM_MI_TYPE_IMEISV:
700 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000701 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000702 break;
703 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000704 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000705 break;
706 }
707
Holger Freyther4a49e772009-04-12 05:37:29 +0000708 if (subscr) {
709 subscr_update(subscr, msg->trx->bts,
710 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100711 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200712
713 subscr->equipment.classmark1 = idi->classmark1;
714 db_sync_equipment(&subscr->equipment);
715
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000716 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000717 } else
Harald Welte2a139372009-02-22 21:14:55 +0000718 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
719
Harald Welte31981a02009-12-20 09:58:40 +0100720 /* FIXME: iterate over all transactions and release them,
721 * imagine an IMSI DETACH happening during an active call! */
722
Harald Welteb83d9382009-12-12 21:00:48 +0100723 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100724 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100725
Harald Welte2a139372009-02-22 21:14:55 +0000726 return 0;
727}
728
Harald Welted2a7f5a2009-06-05 20:08:20 +0000729static int gsm48_rx_mm_status(struct msgb *msg)
730{
731 struct gsm48_hdr *gh = msgb_l3(msg);
732
733 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
734
735 return 0;
736}
737
Harald Weltebf5e8df2009-02-03 12:59:45 +0000738/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000739static int gsm0408_rcv_mm(struct msgb *msg)
740{
741 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800742 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000743
744 switch (gh->msg_type & 0xbf) {
745 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200746 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000747 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000748 break;
749 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000750 rc = mm_rx_id_resp(msg);
751 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000752 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000753 rc = gsm48_rx_mm_serv_req(msg);
754 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000755 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000756 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000757 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000758 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000759 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100760 msg->lchan->conn.subscr ?
761 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000762 "unknown subscriber");
763 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000764 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000765 rc = gsm48_rx_mm_imsi_detach_ind(msg);
766 break;
767 case GSM48_MT_MM_CM_REEST_REQ:
768 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
769 break;
770 case GSM48_MT_MM_AUTH_RESP:
771 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000772 break;
773 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100774 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000775 gh->msg_type);
776 break;
777 }
778
779 return rc;
780}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000781
Harald Welte2d35ae62009-02-06 12:02:13 +0000782/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200783static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000784{
Harald Welte9176bd42009-07-23 18:46:00 +0200785 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000786 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800787 struct gsm48_pag_resp *resp;
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 Freytherf6903de2010-05-16 01:51:14 +0800794 resp = (struct gsm48_pag_resp *) &gh->data[0];
795 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
796 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000797 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
798 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200799
Harald Weltefe18d8f2009-02-22 21:14:24 +0000800 switch (mi_type) {
801 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200802 subscr = subscr_get_by_tmsi(bts->network,
803 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000804 break;
805 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200806 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000807 break;
808 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000809
810 if (!subscr) {
811 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000812 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000813 return -EINVAL;
814 }
815 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200816 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000817
Harald Weltec2e302d2009-07-05 14:08:13 +0200818 subscr->equipment.classmark2_len = *classmark2_lv;
819 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
820 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000821
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200822 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000823 return rc;
824}
825
Harald Weltef7c43522009-06-09 20:24:21 +0000826static int gsm48_rx_rr_classmark(struct msgb *msg)
827{
828 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100829 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +0000830 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
831 u_int8_t cm2_len, cm3_len = 0;
832 u_int8_t *cm2, *cm3 = NULL;
833
834 DEBUGP(DRR, "CLASSMARK CHANGE ");
835
836 /* classmark 2 */
837 cm2_len = gh->data[0];
838 cm2 = &gh->data[1];
839 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
840
841 if (payload_len > cm2_len + 1) {
842 /* we must have a classmark3 */
843 if (gh->data[cm2_len+1] != 0x20) {
844 DEBUGPC(DRR, "ERR CM3 TAG\n");
845 return -EINVAL;
846 }
847 if (cm2_len > 3) {
848 DEBUGPC(DRR, "CM2 too long!\n");
849 return -EINVAL;
850 }
851
852 cm3_len = gh->data[cm2_len+2];
853 cm3 = &gh->data[cm2_len+3];
854 if (cm3_len > 14) {
855 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
856 return -EINVAL;
857 }
858 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
859 }
860 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200861 subscr->equipment.classmark2_len = cm2_len;
862 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000863 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200864 subscr->equipment.classmark3_len = cm3_len;
865 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000866 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200867 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000868 }
869
Harald Weltef7c43522009-06-09 20:24:21 +0000870 return 0;
871}
872
Harald Weltecf5b3592009-05-01 18:28:42 +0000873static int gsm48_rx_rr_status(struct msgb *msg)
874{
875 struct gsm48_hdr *gh = msgb_l3(msg);
876
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200877 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +0000878 rr_cause_name(gh->data[0]));
879
880 return 0;
881}
882
Harald Weltef7c43522009-06-09 20:24:21 +0000883static int gsm48_rx_rr_meas_rep(struct msgb *msg)
884{
Harald Welted12b0fd2009-12-15 21:36:05 +0100885 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +0000886
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100887 /* This shouldn't actually end up here, as RSL treats
888 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
889 * directly into gsm48_parse_meas_rep */
890 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +0100891 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +0000892
893 return 0;
894}
895
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200896static int gsm48_rx_rr_app_info(struct msgb *msg)
897{
898 struct gsm48_hdr *gh = msgb_l3(msg);
899 u_int8_t apdu_id_flags;
900 u_int8_t apdu_len;
901 u_int8_t *apdu_data;
902
903 apdu_id_flags = gh->data[0];
904 apdu_len = gh->data[1];
905 apdu_data = gh->data+2;
906
907 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
908 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
909
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100910 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 +0200911}
912
Harald Welted011e8b2009-11-29 22:45:52 +0100913/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +0100914static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100915{
916 struct gsm48_hdr *gh = msgb_l3(msg);
917
918 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
919 rr_cause_name(gh->data[0]));
920
921 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
922 /* FIXME: release old channel */
923
924 return 0;
925}
926
927/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +0100928static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100929{
930 struct gsm48_hdr *gh = msgb_l3(msg);
931
932 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
933 rr_cause_name(gh->data[0]));
934
935 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
936 /* FIXME: release allocated new channel */
937
938 return 0;
939}
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200940
Harald Weltebf5e8df2009-02-03 12:59:45 +0000941/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000942static int gsm0408_rcv_rr(struct msgb *msg)
943{
944 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000945 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000946
947 switch (gh->msg_type) {
948 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +0000949 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000950 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000951 case GSM48_MT_RR_GPRS_SUSP_REQ:
952 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
953 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000954 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200955 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000956 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000957 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200958 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +0000959 break;
Harald Weltecf5b3592009-05-01 18:28:42 +0000960 case GSM48_MT_RR_STATUS:
961 rc = gsm48_rx_rr_status(msg);
962 break;
Harald Weltef7c43522009-06-09 20:24:21 +0000963 case GSM48_MT_RR_MEAS_REP:
964 rc = gsm48_rx_rr_meas_rep(msg);
965 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200966 case GSM48_MT_RR_APP_INFO:
967 rc = gsm48_rx_rr_app_info(msg);
968 break;
Harald Welte08d91a52009-08-30 15:37:11 +0900969 case GSM48_MT_RR_CIPH_M_COMPL:
970 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
971 /* FIXME: check for MI (if any) */
972 break;
Harald Welteccd5a882009-11-29 20:02:20 +0100973 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +0100974 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100975 break;
976 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +0100977 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100978 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000979 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100980 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
981 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000982 break;
983 }
984
Harald Welte2d35ae62009-02-06 12:02:13 +0000985 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000986}
987
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200988int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +0200989 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200990{
991 struct msgb *msg = gsm48_msgb_alloc();
992 struct gsm48_hdr *gh;
993
994 msg->lchan = lchan;
995
996 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
997 apdu_id, apdu_len);
998
999 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1000 gh->proto_discr = GSM48_PDISC_RR;
1001 gh->msg_type = GSM48_MT_RR_APP_INFO;
1002 gh->data[0] = apdu_id;
1003 gh->data[1] = apdu_len;
1004 memcpy(gh->data+2, apdu, apdu_len);
1005
1006 return gsm48_sendmsg(msg, NULL);
1007}
1008
Harald Welte4bc90a12008-12-27 16:32:52 +00001009/* Call Control */
1010
Harald Welte7584aea2009-02-11 11:44:12 +00001011/* The entire call control code is written in accordance with Figure 7.10c
1012 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1013 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1014 * it for voice */
1015
Harald Welte4bfdfe72009-06-10 23:11:52 +08001016static void new_cc_state(struct gsm_trans *trans, int state)
1017{
1018 if (state > 31 || state < 0)
1019 return;
1020
1021 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001022 gsm48_cc_state_name(trans->cc.state),
1023 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001024
Harald Weltedcaf5652009-07-23 18:56:43 +02001025 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001026}
1027
1028static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001029{
1030 struct msgb *msg = gsm48_msgb_alloc();
1031 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1032 u_int8_t *cause, *call_state;
1033
Harald Welte4bc90a12008-12-27 16:32:52 +00001034 gh->msg_type = GSM48_MT_CC_STATUS;
1035
1036 cause = msgb_put(msg, 3);
1037 cause[0] = 2;
1038 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1039 cause[2] = 0x80 | 30; /* response to status inquiry */
1040
1041 call_state = msgb_put(msg, 1);
1042 call_state[0] = 0xc0 | 0x00;
1043
Harald Welte39e2ead2009-07-23 21:13:03 +02001044 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001045}
1046
Harald Welte6f4b7532008-12-29 00:39:37 +00001047static int gsm48_tx_simple(struct gsm_lchan *lchan,
1048 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001049{
1050 struct msgb *msg = gsm48_msgb_alloc();
1051 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1052
1053 msg->lchan = lchan;
1054
Harald Welte6f4b7532008-12-29 00:39:37 +00001055 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001056 gh->msg_type = msg_type;
1057
Harald Welte39e2ead2009-07-23 21:13:03 +02001058 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001059}
1060
Harald Welte4bfdfe72009-06-10 23:11:52 +08001061static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1062{
Harald Weltedcaf5652009-07-23 18:56:43 +02001063 if (bsc_timer_pending(&trans->cc.timer)) {
1064 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1065 bsc_del_timer(&trans->cc.timer);
1066 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001067 }
1068}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001069
Harald Welte4bfdfe72009-06-10 23:11:52 +08001070static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1071 int msg_type, struct gsm_mncc *mncc)
1072{
1073 struct msgb *msg;
1074
1075 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001076 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001077 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001078 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001079 trans->conn->lchan->ts->trx->bts->nr,
1080 trans->conn->lchan->ts->trx->nr,
1081 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001082 (trans->subscr)?(trans->subscr->extension):"-",
1083 get_mncc_name(msg_type));
1084 else
1085 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1086 "Sending '%s' to MNCC.\n",
1087 (trans->subscr)?(trans->subscr->extension):"-",
1088 get_mncc_name(msg_type));
1089 else
1090 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1091 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1092
1093 mncc->msg_type = msg_type;
1094
Harald Welte966636f2009-06-26 19:39:35 +02001095 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001096 if (!msg)
1097 return -ENOMEM;
1098 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1099 msgb_enqueue(&net->upqueue, msg);
1100
1101 return 0;
1102}
1103
1104int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1105 u_int32_t callref, int location, int value)
1106{
1107 struct gsm_mncc rel;
1108
Harald Welte92f70c52009-06-12 01:54:08 +08001109 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001110 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001111 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001112 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1113}
1114
Harald Weltedcaf5652009-07-23 18:56:43 +02001115/* Call Control Specific transaction release.
1116 * gets called by trans_free, DO NOT CALL YOURSELF! */
1117void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001118{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001119 gsm48_stop_cc_timer(trans);
1120
1121 /* send release to L4, if callref still exists */
1122 if (trans->callref) {
1123 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001124 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001125 GSM48_CAUSE_LOC_PRN_S_LU,
1126 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001127 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001128 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001129 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001130 if (trans->conn)
1131 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001132}
1133
1134static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001135
Harald Welte09e38af2009-02-16 22:52:23 +00001136/* call-back from paging the B-end of the connection */
1137static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001138 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001139{
Harald Welte7ccf7782009-02-17 01:43:01 +00001140 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001141 struct gsm_subscriber *subscr = param;
1142 struct gsm_trans *transt, *tmp;
1143 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001144
Harald Welte09e38af2009-02-16 22:52:23 +00001145 if (hooknum != GSM_HOOK_RR_PAGING)
1146 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001147
Harald Welte4bfdfe72009-06-10 23:11:52 +08001148 if (!subscr)
1149 return -EINVAL;
1150 net = subscr->net;
1151 if (!net) {
1152 DEBUGP(DCC, "Error Network not set!\n");
1153 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001154 }
Harald Welte7584aea2009-02-11 11:44:12 +00001155
Harald Welte4bfdfe72009-06-10 23:11:52 +08001156 /* check all tranactions (without lchan) for subscriber */
1157 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001158 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001159 continue;
1160 switch (event) {
1161 case GSM_PAGING_SUCCEEDED:
1162 if (!lchan) // paranoid
1163 break;
1164 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1165 subscr->extension);
1166 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001167 if (!transt->conn) {
1168 transt->conn = &lchan->conn;
1169 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001170 }
1171 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001172 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001173 break;
1174 case GSM_PAGING_EXPIRED:
1175 DEBUGP(DCC, "Paging subscr %s expired!\n",
1176 subscr->extension);
1177 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001178 mncc_release_ind(transt->subscr->net, transt,
1179 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001180 GSM48_CAUSE_LOC_PRN_S_LU,
1181 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001182 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001183 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001184 break;
1185 }
1186 }
Harald Welte09e38af2009-02-16 22:52:23 +00001187 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001188}
Harald Welte7584aea2009-02-11 11:44:12 +00001189
Harald Welteda7ab742009-12-19 22:23:05 +01001190static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1191
Harald Welte805f6442009-07-28 18:25:29 +02001192/* some other part of the code sends us a signal */
1193static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1194 void *handler_data, void *signal_data)
1195{
1196 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001197 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001198 struct gsm_network *net;
1199 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001200
1201 if (subsys != SS_ABISIP)
1202 return 0;
1203
1204 /* in case we use direct BTS-to-BTS RTP */
1205 if (ipacc_rtp_direct)
1206 return 0;
1207
Harald Welte805f6442009-07-28 18:25:29 +02001208 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001209 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001210 /* check if any transactions on this lchan still have
1211 * a tch_recv_mncc request pending */
1212 net = lchan->ts->trx->bts->network;
1213 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001214 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001215 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1216 tch_recv_mncc(net, trans->callref, 1);
1217 }
1218 }
Harald Welte805f6442009-07-28 18:25:29 +02001219 break;
Harald Welte805f6442009-07-28 18:25:29 +02001220 }
1221
1222 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001223}
1224
Harald Welte49f48b82009-02-17 15:29:33 +00001225/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001226static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001227{
Harald Welte11fa29c2009-02-19 17:24:39 +00001228 struct gsm_bts *bts = lchan->ts->trx->bts;
1229 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001230 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001231
Harald Welte11fa29c2009-02-19 17:24:39 +00001232 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1233 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1234 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1235
1236 if (bts->type != remote_bts->type) {
1237 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1238 return -EINVAL;
1239 }
Harald Welteda7ab742009-12-19 22:23:05 +01001240
1241 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001242 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001243 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001244 if (!ipacc_rtp_direct) {
1245 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001246 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001247 if (rc < 0)
1248 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001249 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001250#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001251
1252 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301253 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1254 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001255 } else {
1256 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301257 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1258 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301259 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001260 if (rc < 0)
1261 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301262 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1263 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301264 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001265 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001266 break;
1267 case GSM_BTS_TYPE_BS11:
1268 trau_mux_map_lchan(lchan, remote_lchan);
1269 break;
1270 default:
1271 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001272 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001273 }
Harald Welte49f48b82009-02-17 15:29:33 +00001274
1275 return 0;
1276}
1277
Harald Welte4bfdfe72009-06-10 23:11:52 +08001278/* bridge channels of two transactions */
1279static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001280{
Harald Weltedcaf5652009-07-23 18:56:43 +02001281 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1282 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001283
Harald Welte4bfdfe72009-06-10 23:11:52 +08001284 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001285 return -EIO;
1286
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001287 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001288 return -EIO;
1289
1290 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001291 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001292}
1293
Harald Welteda7ab742009-12-19 22:23:05 +01001294/* enable receive of channels to MNCC upqueue */
1295static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296{
1297 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001298 struct gsm_lchan *lchan;
1299 struct gsm_bts *bts;
1300 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001301
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001303 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304 if (!trans)
1305 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001306 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001308 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001309 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310
Harald Welteda7ab742009-12-19 22:23:05 +01001311 switch (bts->type) {
1312 case GSM_BTS_TYPE_NANOBTS:
1313 if (ipacc_rtp_direct) {
1314 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1315 return -EINVAL;
1316 }
1317 /* in case, we don't have a RTP socket yet, we note this
1318 * in the transaction and try later */
1319 if (!lchan->abis_ip.rtp_socket) {
1320 trans->tch_recv = enable;
1321 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1322 return 0;
1323 }
1324 if (enable) {
1325 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001326 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001327 if (rc < 0)
1328 return rc;
1329 /* assign socket to application interface */
1330 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1331 net, callref);
1332 } else
1333 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1334 net, 0);
1335 break;
1336 case GSM_BTS_TYPE_BS11:
1337 if (enable)
1338 return trau_recv_lchan(lchan, callref);
1339 return trau_mux_unmap(NULL, callref);
1340 break;
1341 default:
1342 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1343 return -EINVAL;
1344 }
1345
1346 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347}
1348
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1350{
1351 DEBUGP(DCC, "-> STATUS ENQ\n");
1352 return gsm48_cc_tx_status(trans, msg);
1353}
1354
1355static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1356static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1357
1358static void gsm48_cc_timeout(void *arg)
1359{
1360 struct gsm_trans *trans = arg;
1361 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001362 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1363 int mo_location = GSM48_CAUSE_LOC_USER;
1364 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1365 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 struct gsm_mncc mo_rel, l4_rel;
1367
1368 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1369 mo_rel.callref = trans->callref;
1370 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1371 l4_rel.callref = trans->callref;
1372
Harald Weltedcaf5652009-07-23 18:56:43 +02001373 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 case 0x303:
1375 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001376 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 break;
1378 case 0x310:
1379 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001380 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 break;
1382 case 0x313:
1383 disconnect = 1;
1384 /* unknown, did not find it in the specs */
1385 break;
1386 case 0x301:
1387 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001388 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 break;
1390 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001393 gsm48_cc_tx_release(trans, &trans->cc.msg);
1394 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395 break; /* stay in release state */
1396 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001397 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 return;
1399// release = 1;
1400// l4_cause = 14;
1401// break;
1402 case 0x306:
1403 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001404 mo_cause = trans->cc.msg.cause.value;
1405 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 break;
1407 case 0x323:
1408 disconnect = 1;
1409 break;
1410 default:
1411 release = 1;
1412 }
1413
1414 if (release && trans->callref) {
1415 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001416 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417 l4_location, l4_cause);
1418 trans->callref = 0;
1419 }
1420
1421 if (disconnect && trans->callref) {
1422 /* process disconnect towards layer 4 */
1423 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001424 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 }
1426
1427 /* process disconnect towards mobile station */
1428 if (disconnect || release) {
1429 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001430 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1431 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1432 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433 mo_rel.cause.diag_len = 3;
1434
1435 if (disconnect)
1436 gsm48_cc_tx_disconnect(trans, &mo_rel);
1437 if (release)
1438 gsm48_cc_tx_release(trans, &mo_rel);
1439 }
1440
1441}
1442
1443static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1444 int sec, int micro)
1445{
1446 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001447 trans->cc.timer.cb = gsm48_cc_timeout;
1448 trans->cc.timer.data = trans;
1449 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1450 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001451}
1452
1453static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1454{
1455 struct gsm48_hdr *gh = msgb_l3(msg);
1456 u_int8_t msg_type = gh->msg_type & 0xbf;
1457 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1458 struct tlv_parsed tp;
1459 struct gsm_mncc setup;
1460
1461 memset(&setup, 0, sizeof(struct gsm_mncc));
1462 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001463 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001464 /* emergency setup is identified by msg_type */
1465 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1466 setup.emergency = 1;
1467
1468 /* use subscriber as calling party number */
1469 if (trans->subscr) {
1470 setup.fields |= MNCC_F_CALLING;
1471 strncpy(setup.calling.number, trans->subscr->extension,
1472 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001473 strncpy(setup.imsi, trans->subscr->imsi,
1474 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001475 }
1476 /* bearer capability */
1477 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1478 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001479 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001480 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1481 }
1482 /* facility */
1483 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1484 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001485 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1487 }
1488 /* called party bcd number */
1489 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1490 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001491 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1493 }
1494 /* user-user */
1495 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1496 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001497 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001498 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1499 }
1500 /* ss-version */
1501 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1502 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001503 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1505 }
1506 /* CLIR suppression */
1507 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1508 setup.clir.sup = 1;
1509 /* CLIR invocation */
1510 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1511 setup.clir.inv = 1;
1512 /* cc cap */
1513 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1514 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001515 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001516 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1517 }
1518
Harald Welte4bfdfe72009-06-10 23:11:52 +08001519 new_cc_state(trans, GSM_CSTATE_INITIATED);
1520
Harald Welte (local)47df3992009-12-26 19:45:03 +01001521 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1522 subscr_name(trans->subscr), trans->subscr->extension,
1523 setup.called.number);
1524
Harald Welte4bfdfe72009-06-10 23:11:52 +08001525 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001526 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527
Harald Welte13cac662009-07-29 12:10:35 +02001528 /* MNCC code will modify the channel asynchronously, we should
1529 * ipaccess-bind only after the modification has been made to the
1530 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001531 return 0;
1532}
1533
1534static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001535{
1536 struct msgb *msg = gsm48_msgb_alloc();
1537 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001538 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001539 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001540
Harald Welte7ccf7782009-02-17 01:43:01 +00001541 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001542
Harald Welte4bfdfe72009-06-10 23:11:52 +08001543 /* transaction id must not be assigned */
1544 if (trans->transaction_id != 0xff) { /* unasssigned */
1545 DEBUGP(DCC, "TX Setup with assigned transaction. "
1546 "This is not allowed!\n");
1547 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001548 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001549 GSM48_CAUSE_LOC_PRN_S_LU,
1550 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001552 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553 return rc;
1554 }
1555
1556 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001557 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1558 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001559 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001560 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001561 GSM48_CAUSE_LOC_PRN_S_LU,
1562 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001564 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001565 return rc;
1566 }
Harald Welte78283ef2009-07-23 21:36:44 +02001567 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001568
Harald Welte65e74cc2008-12-29 01:55:35 +00001569 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001570
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001572
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 /* bearer capability */
1574 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001575 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001576 /* facility */
1577 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001578 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001579 /* progress */
1580 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001581 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582 /* calling party BCD number */
1583 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001584 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 /* called party BCD number */
1586 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001587 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 /* user-user */
1589 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001590 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001591 /* redirecting party BCD number */
1592 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001593 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001594 /* signal */
1595 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001596 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597
1598 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001599
Harald Welte39e2ead2009-07-23 21:13:03 +02001600 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001601}
1602
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1604{
1605 struct gsm48_hdr *gh = msgb_l3(msg);
1606 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1607 struct tlv_parsed tp;
1608 struct gsm_mncc call_conf;
1609
1610 gsm48_stop_cc_timer(trans);
1611 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1612
1613 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1614 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001615 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001616#if 0
1617 /* repeat */
1618 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1619 call_conf.repeat = 1;
1620 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1621 call_conf.repeat = 2;
1622#endif
1623 /* bearer capability */
1624 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1625 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001626 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1628 }
1629 /* cause */
1630 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1631 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001632 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001633 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1634 }
1635 /* cc cap */
1636 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1637 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001638 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1640 }
1641
1642 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1643
Harald Welte596fed42009-07-23 19:06:52 +02001644 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1645 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646}
1647
1648static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1649{
1650 struct gsm_mncc *proceeding = arg;
1651 struct msgb *msg = gsm48_msgb_alloc();
1652 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1653
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1655
1656 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1657
1658 /* bearer capability */
1659 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001660 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 /* facility */
1662 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001663 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001664 /* progress */
1665 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001666 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667
Harald Welte39e2ead2009-07-23 21:13:03 +02001668 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669}
1670
1671static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1672{
1673 struct gsm48_hdr *gh = msgb_l3(msg);
1674 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1675 struct tlv_parsed tp;
1676 struct gsm_mncc alerting;
1677
1678 gsm48_stop_cc_timer(trans);
1679 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1680
1681 memset(&alerting, 0, sizeof(struct gsm_mncc));
1682 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001683 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 /* facility */
1685 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1686 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001687 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1689 }
1690
1691 /* progress */
1692 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1693 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001694 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1696 }
1697 /* ss-version */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1699 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001700 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1702 }
1703
1704 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1705
Harald Welte596fed42009-07-23 19:06:52 +02001706 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1707 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708}
1709
1710static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1711{
1712 struct gsm_mncc *alerting = arg;
1713 struct msgb *msg = gsm48_msgb_alloc();
1714 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1715
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 gh->msg_type = GSM48_MT_CC_ALERTING;
1717
1718 /* facility */
1719 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001720 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 /* progress */
1722 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001723 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 /* user-user */
1725 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001726 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727
1728 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1729
Harald Welte39e2ead2009-07-23 21:13:03 +02001730 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731}
1732
1733static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1734{
1735 struct gsm_mncc *progress = arg;
1736 struct msgb *msg = gsm48_msgb_alloc();
1737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1738
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 gh->msg_type = GSM48_MT_CC_PROGRESS;
1740
1741 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001742 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 /* user-user */
1744 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001745 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746
Harald Welte39e2ead2009-07-23 21:13:03 +02001747 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748}
1749
1750static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1751{
1752 struct gsm_mncc *connect = arg;
1753 struct msgb *msg = gsm48_msgb_alloc();
1754 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1755
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 gh->msg_type = GSM48_MT_CC_CONNECT;
1757
1758 gsm48_stop_cc_timer(trans);
1759 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1760
1761 /* facility */
1762 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001763 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 /* progress */
1765 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001766 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 /* connected number */
1768 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001769 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* user-user */
1771 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001772 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773
1774 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1775
Harald Welte39e2ead2009-07-23 21:13:03 +02001776 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777}
1778
1779static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1780{
1781 struct gsm48_hdr *gh = msgb_l3(msg);
1782 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1783 struct tlv_parsed tp;
1784 struct gsm_mncc connect;
1785
1786 gsm48_stop_cc_timer(trans);
1787
1788 memset(&connect, 0, sizeof(struct gsm_mncc));
1789 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001790 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 /* use subscriber as connected party number */
1792 if (trans->subscr) {
1793 connect.fields |= MNCC_F_CONNECTED;
1794 strncpy(connect.connected.number, trans->subscr->extension,
1795 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001796 strncpy(connect.imsi, trans->subscr->imsi,
1797 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 }
1799 /* facility */
1800 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1801 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001802 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1804 }
1805 /* user-user */
1806 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1807 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001808 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1810 }
1811 /* ss-version */
1812 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1813 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001814 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1816 }
1817
1818 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1819
Harald Welte596fed42009-07-23 19:06:52 +02001820 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821}
1822
1823
1824static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1825{
1826 struct gsm_mncc connect_ack;
1827
1828 gsm48_stop_cc_timer(trans);
1829
1830 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1831
1832 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1833 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001834 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 &connect_ack);
1836}
1837
1838static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1839{
1840 struct msgb *msg = gsm48_msgb_alloc();
1841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1842
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1844
1845 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1846
Harald Welte39e2ead2009-07-23 21:13:03 +02001847 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848}
1849
1850static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1851{
1852 struct gsm48_hdr *gh = msgb_l3(msg);
1853 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1854 struct tlv_parsed tp;
1855 struct gsm_mncc disc;
1856
1857 gsm48_stop_cc_timer(trans);
1858
1859 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1860
1861 memset(&disc, 0, sizeof(struct gsm_mncc));
1862 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001863 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 /* cause */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1866 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1869 }
1870 /* facility */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1872 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1875 }
1876 /* user-user */
1877 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1878 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001879 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1881 }
1882 /* ss-version */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1884 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1887 }
1888
Harald Welte596fed42009-07-23 19:06:52 +02001889 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890
1891}
1892
Harald Weltec66b71c2009-06-11 14:23:20 +08001893static struct gsm_mncc_cause default_cause = {
1894 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1895 .coding = 0,
1896 .rec = 0,
1897 .rec_val = 0,
1898 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1899 .diag_len = 0,
1900 .diag = { 0 },
1901};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902
1903static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1904{
1905 struct gsm_mncc *disc = arg;
1906 struct msgb *msg = gsm48_msgb_alloc();
1907 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1908
Harald Welte4bfdfe72009-06-10 23:11:52 +08001909 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1910
1911 gsm48_stop_cc_timer(trans);
1912 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1913
1914 /* cause */
1915 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001916 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 else
Harald Welte55c8f352010-03-07 23:40:35 +01001918 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919
1920 /* facility */
1921 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001922 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 /* progress */
1924 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001925 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926 /* user-user */
1927 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001928 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929
1930 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001931 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932
1933 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1934
Harald Welte39e2ead2009-07-23 21:13:03 +02001935 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936}
1937
1938static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1939{
1940 struct gsm48_hdr *gh = msgb_l3(msg);
1941 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1942 struct tlv_parsed tp;
1943 struct gsm_mncc rel;
1944 int rc;
1945
1946 gsm48_stop_cc_timer(trans);
1947
1948 memset(&rel, 0, sizeof(struct gsm_mncc));
1949 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001950 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* cause */
1952 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1953 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1956 }
1957 /* facility */
1958 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1959 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1962 }
1963 /* user-user */
1964 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1965 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001966 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1968 }
1969 /* ss-version */
1970 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1971 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1974 }
1975
Harald Weltedcaf5652009-07-23 18:56:43 +02001976 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02001978 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 } else {
Harald Welte596fed42009-07-23 19:06:52 +02001980 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02001981 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02001982 GSM48_MT_CC_RELEASE_COMPL);
1983 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 }
1985
1986 new_cc_state(trans, GSM_CSTATE_NULL);
1987
1988 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001989 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990
1991 return rc;
1992}
1993
1994static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1995{
1996 struct gsm_mncc *rel = arg;
1997 struct msgb *msg = gsm48_msgb_alloc();
1998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1999
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 gh->msg_type = GSM48_MT_CC_RELEASE;
2001
2002 trans->callref = 0;
2003
2004 gsm48_stop_cc_timer(trans);
2005 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2006
2007 /* cause */
2008 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 /* facility */
2011 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 /* user-user */
2014 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002015 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016
Harald Weltedcaf5652009-07-23 18:56:43 +02002017 trans->cc.T308_second = 0;
2018 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019
Harald Weltedcaf5652009-07-23 18:56:43 +02002020 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2022
Harald Welte39e2ead2009-07-23 21:13:03 +02002023 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024}
2025
2026static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2027{
2028 struct gsm48_hdr *gh = msgb_l3(msg);
2029 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2030 struct tlv_parsed tp;
2031 struct gsm_mncc rel;
2032 int rc = 0;
2033
2034 gsm48_stop_cc_timer(trans);
2035
2036 memset(&rel, 0, sizeof(struct gsm_mncc));
2037 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002038 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 /* cause */
2040 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2041 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2044 }
2045 /* facility */
2046 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2047 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2050 }
2051 /* user-user */
2052 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2053 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2056 }
2057 /* ss-version */
2058 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2059 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002060 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2062 }
2063
2064 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002065 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002067 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 MNCC_REJ_IND, &rel);
2069 break;
2070 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002071 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002073 /* FIXME: in case of multiple calls, we can't simply
2074 * hang up here ! */
2075 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 break;
2077 default:
Harald Welte596fed42009-07-23 19:06:52 +02002078 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 MNCC_REL_IND, &rel);
2080 }
2081 }
2082
2083 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002084 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085
2086 return rc;
2087}
2088
2089static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2090{
2091 struct gsm_mncc *rel = arg;
2092 struct msgb *msg = gsm48_msgb_alloc();
2093 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2094
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2096
2097 trans->callref = 0;
2098
2099 gsm48_stop_cc_timer(trans);
2100
2101 /* cause */
2102 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002103 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 /* facility */
2105 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002106 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 /* user-user */
2108 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002109 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110
Harald Weltedcaf5652009-07-23 18:56:43 +02002111 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112
Harald Welte39e2ead2009-07-23 21:13:03 +02002113 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114}
2115
2116static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2117{
2118 struct gsm48_hdr *gh = msgb_l3(msg);
2119 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2120 struct tlv_parsed tp;
2121 struct gsm_mncc fac;
2122
2123 memset(&fac, 0, sizeof(struct gsm_mncc));
2124 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002125 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 /* facility */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2128 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2131 }
2132 /* ss-version */
2133 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2134 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002135 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2137 }
2138
Harald Welte596fed42009-07-23 19:06:52 +02002139 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140}
2141
2142static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2143{
2144 struct gsm_mncc *fac = arg;
2145 struct msgb *msg = gsm48_msgb_alloc();
2146 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2147
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148 gh->msg_type = GSM48_MT_CC_FACILITY;
2149
2150 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002151 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152
Harald Welte39e2ead2009-07-23 21:13:03 +02002153 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154}
2155
2156static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2157{
2158 struct gsm_mncc hold;
2159
2160 memset(&hold, 0, sizeof(struct gsm_mncc));
2161 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002162 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163}
2164
2165static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2166{
2167 struct msgb *msg = gsm48_msgb_alloc();
2168 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2169
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2171
Harald Welte39e2ead2009-07-23 21:13:03 +02002172 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173}
2174
2175static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2176{
2177 struct gsm_mncc *hold_rej = arg;
2178 struct msgb *msg = gsm48_msgb_alloc();
2179 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2180
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2182
2183 /* cause */
2184 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 else
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188
Harald Welte39e2ead2009-07-23 21:13:03 +02002189 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190}
2191
2192static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2193{
2194 struct gsm_mncc retrieve;
2195
2196 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2197 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002198 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2199 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200}
2201
2202static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2203{
2204 struct msgb *msg = gsm48_msgb_alloc();
2205 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2206
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2208
Harald Welte39e2ead2009-07-23 21:13:03 +02002209 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210}
2211
2212static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2213{
2214 struct gsm_mncc *retrieve_rej = arg;
2215 struct msgb *msg = gsm48_msgb_alloc();
2216 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2217
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2219
2220 /* cause */
2221 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 else
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225
Harald Welte39e2ead2009-07-23 21:13:03 +02002226 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227}
2228
2229static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2230{
2231 struct gsm48_hdr *gh = msgb_l3(msg);
2232 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2233 struct tlv_parsed tp;
2234 struct gsm_mncc dtmf;
2235
2236 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2237 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002238 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 /* keypad facility */
2240 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2241 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2244 }
2245
Harald Welte596fed42009-07-23 19:06:52 +02002246 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2250{
2251 struct gsm_mncc *dtmf = arg;
2252 struct msgb *msg = gsm48_msgb_alloc();
2253 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2254
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2256
2257 /* keypad */
2258 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260
Harald Welte39e2ead2009-07-23 21:13:03 +02002261 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262}
2263
2264static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2265{
2266 struct gsm_mncc *dtmf = arg;
2267 struct msgb *msg = gsm48_msgb_alloc();
2268 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2269
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2271
2272 /* cause */
2273 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 else
Harald Welte55c8f352010-03-07 23:40:35 +01002276 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277
Harald Welte39e2ead2009-07-23 21:13:03 +02002278 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279}
2280
2281static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2282{
2283 struct msgb *msg = gsm48_msgb_alloc();
2284 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2285
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2287
Harald Welte39e2ead2009-07-23 21:13:03 +02002288 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289}
2290
2291static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2292{
2293 struct gsm_mncc dtmf;
2294
2295 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2296 dtmf.callref = trans->callref;
2297
Harald Welte596fed42009-07-23 19:06:52 +02002298 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299}
2300
2301static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2302{
2303 struct gsm48_hdr *gh = msgb_l3(msg);
2304 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2305 struct tlv_parsed tp;
2306 struct gsm_mncc modify;
2307
2308 memset(&modify, 0, sizeof(struct gsm_mncc));
2309 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002310 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 /* bearer capability */
2312 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2313 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002314 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2316 }
2317
2318 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2319
Harald Welte596fed42009-07-23 19:06:52 +02002320 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321}
2322
2323static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2324{
2325 struct gsm_mncc *modify = arg;
2326 struct msgb *msg = gsm48_msgb_alloc();
2327 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2328
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329 gh->msg_type = GSM48_MT_CC_MODIFY;
2330
2331 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2332
2333 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002334 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335
2336 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2337
Harald Welte39e2ead2009-07-23 21:13:03 +02002338 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339}
2340
2341static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2342{
2343 struct gsm48_hdr *gh = msgb_l3(msg);
2344 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2345 struct tlv_parsed tp;
2346 struct gsm_mncc modify;
2347
2348 gsm48_stop_cc_timer(trans);
2349
2350 memset(&modify, 0, sizeof(struct gsm_mncc));
2351 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002352 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 /* bearer capability */
2354 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2355 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002356 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2358 }
2359
2360 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2361
Harald Welte596fed42009-07-23 19:06:52 +02002362 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363}
2364
2365static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2366{
2367 struct gsm_mncc *modify = arg;
2368 struct msgb *msg = gsm48_msgb_alloc();
2369 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2370
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2372
2373 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002374 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375
2376 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2377
Harald Welte39e2ead2009-07-23 21:13:03 +02002378 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379}
2380
2381static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2382{
2383 struct gsm48_hdr *gh = msgb_l3(msg);
2384 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2385 struct tlv_parsed tp;
2386 struct gsm_mncc modify;
2387
2388 gsm48_stop_cc_timer(trans);
2389
2390 memset(&modify, 0, sizeof(struct gsm_mncc));
2391 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002392 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393 /* bearer capability */
2394 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2395 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002396 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2398 }
2399 /* cause */
2400 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2401 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002402 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2404 }
2405
2406 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2407
Harald Welte596fed42009-07-23 19:06:52 +02002408 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2412{
2413 struct gsm_mncc *modify = arg;
2414 struct msgb *msg = gsm48_msgb_alloc();
2415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2416
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2418
2419 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002422 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423
2424 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2425
Harald Welte39e2ead2009-07-23 21:13:03 +02002426 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2430{
2431 struct gsm_mncc *notify = arg;
2432 struct msgb *msg = gsm48_msgb_alloc();
2433 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2434
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 gh->msg_type = GSM48_MT_CC_NOTIFY;
2436
2437 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
Harald Welte39e2ead2009-07-23 21:13:03 +02002440 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441}
2442
2443static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2444{
2445 struct gsm48_hdr *gh = msgb_l3(msg);
2446 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2447// struct tlv_parsed tp;
2448 struct gsm_mncc notify;
2449
2450 memset(&notify, 0, sizeof(struct gsm_mncc));
2451 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002452// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002454 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455
Harald Welte596fed42009-07-23 19:06:52 +02002456 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457}
2458
2459static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2460{
2461 struct gsm_mncc *user = arg;
2462 struct msgb *msg = gsm48_msgb_alloc();
2463 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2464
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465 gh->msg_type = GSM48_MT_CC_USER_INFO;
2466
2467 /* user-user */
2468 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002469 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 /* more data */
2471 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002472 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473
Harald Welte39e2ead2009-07-23 21:13:03 +02002474 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475}
2476
2477static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2478{
2479 struct gsm48_hdr *gh = msgb_l3(msg);
2480 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2481 struct tlv_parsed tp;
2482 struct gsm_mncc user;
2483
2484 memset(&user, 0, sizeof(struct gsm_mncc));
2485 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002486 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 /* user-user */
2488 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2489 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002490 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2492 }
2493 /* more data */
2494 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2495 user.more = 1;
2496
Harald Welte596fed42009-07-23 19:06:52 +02002497 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002500static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501{
2502 struct gsm_mncc *mode = arg;
2503
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002504 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505}
2506
2507static struct downstate {
2508 u_int32_t states;
2509 int type;
2510 int (*rout) (struct gsm_trans *trans, void *arg);
2511} downstatelist[] = {
2512 /* mobile originating call establishment */
2513 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2514 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2515 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2516 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2517 {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 */
2518 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2519 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2520 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2521 /* mobile terminating call establishment */
2522 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2523 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2524 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2525 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2526 /* signalling during call */
2527 {SBIT(GSM_CSTATE_ACTIVE),
2528 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2529 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2530 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2531 {ALL_STATES,
2532 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2533 {ALL_STATES,
2534 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2535 {ALL_STATES,
2536 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2537 {SBIT(GSM_CSTATE_ACTIVE),
2538 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2539 {SBIT(GSM_CSTATE_ACTIVE),
2540 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2541 {SBIT(GSM_CSTATE_ACTIVE),
2542 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2543 {SBIT(GSM_CSTATE_ACTIVE),
2544 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2545 {SBIT(GSM_CSTATE_ACTIVE),
2546 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2547 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2548 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2549 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2550 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2551 {SBIT(GSM_CSTATE_ACTIVE),
2552 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2553 /* clearing */
2554 {SBIT(GSM_CSTATE_INITIATED),
2555 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2556 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2557 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2558 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2559 MNCC_REL_REQ, gsm48_cc_tx_release},
2560 /* special */
2561 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002562 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563};
2564
2565#define DOWNSLLEN \
2566 (sizeof(downstatelist) / sizeof(struct downstate))
2567
2568
2569int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2570{
Harald Welte1a6f7982009-08-09 18:52:33 +02002571 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002573 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 struct gsm_mncc *data = arg, rel;
2576
2577 /* handle special messages */
2578 switch(msg_type) {
2579 case MNCC_BRIDGE:
2580 return tch_bridge(net, arg);
2581 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002582 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002584 return tch_recv_mncc(net, data->callref, 1);
2585 case GSM_TCHF_FRAME:
2586 /* Find callref */
2587 trans = trans_find_by_callref(net, data->callref);
2588 if (!trans)
2589 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002590 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002591 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002592 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002593 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002594 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002595 switch (bts->type) {
2596 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002597 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002598 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002599 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002600 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002601 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002602 default:
2603 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2604 }
2605 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606 }
2607
2608 memset(&rel, 0, sizeof(struct gsm_mncc));
2609 rel.callref = data->callref;
2610
2611 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002612 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613
2614 /* Callref unknown */
2615 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002616 struct gsm_subscriber *subscr;
2617
Harald Welte4a3464c2009-07-04 10:11:24 +02002618 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2620 "Received '%s' from MNCC with "
2621 "unknown callref %d\n", data->called.number,
2622 get_mncc_name(msg_type), data->callref);
2623 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002624 return mncc_release_ind(net, NULL, data->callref,
2625 GSM48_CAUSE_LOC_PRN_S_LU,
2626 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002628 if (!data->called.number[0] && !data->imsi[0]) {
2629 DEBUGP(DCC, "(bts - trx - ts - ti) "
2630 "Received '%s' from MNCC with "
2631 "no number or IMSI\n", get_mncc_name(msg_type));
2632 /* Invalid number */
2633 return mncc_release_ind(net, NULL, data->callref,
2634 GSM48_CAUSE_LOC_PRN_S_LU,
2635 GSM48_CC_CAUSE_INV_NR_FORMAT);
2636 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002638 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002639 subscr = subscr_get_by_extension(net,
2640 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002641 else
Harald Welte9176bd42009-07-23 18:46:00 +02002642 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643 /* If subscriber is not found */
2644 if (!subscr) {
2645 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2646 "Received '%s' from MNCC with "
2647 "unknown subscriber %s\n", data->called.number,
2648 get_mncc_name(msg_type), data->called.number);
2649 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002650 return mncc_release_ind(net, NULL, data->callref,
2651 GSM48_CAUSE_LOC_PRN_S_LU,
2652 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 }
2654 /* If subscriber is not "attached" */
2655 if (!subscr->lac) {
2656 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2657 "Received '%s' from MNCC with "
2658 "detached subscriber %s\n", data->called.number,
2659 get_mncc_name(msg_type), data->called.number);
2660 subscr_put(subscr);
2661 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002662 return mncc_release_ind(net, NULL, data->callref,
2663 GSM48_CAUSE_LOC_PRN_S_LU,
2664 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 }
2666 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002667 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2668 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 DEBUGP(DCC, "No memory for trans.\n");
2670 subscr_put(subscr);
2671 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002672 mncc_release_ind(net, NULL, data->callref,
2673 GSM48_CAUSE_LOC_PRN_S_LU,
2674 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 return -ENOMEM;
2676 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002678 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002679
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680 /* If subscriber has no lchan */
2681 if (!lchan) {
2682 /* find transaction with this subscriber already paging */
2683 llist_for_each_entry(transt, &net->trans_list, entry) {
2684 /* Transaction of our lchan? */
2685 if (transt == trans ||
2686 transt->subscr != subscr)
2687 continue;
2688 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2689 "Received '%s' from MNCC with "
2690 "unallocated channel, paging already "
2691 "started.\n", bts->nr,
2692 data->called.number,
2693 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002694 subscr_put(subscr);
2695 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696 return 0;
2697 }
2698 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002699 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002700 /* Trigger paging */
2701 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2702 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002703 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002704 return 0;
2705 }
2706 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002707 trans->conn = &lchan->conn;
2708 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002709 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002711
2712 if (trans->conn)
2713 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714
2715 /* if paging did not respond yet */
2716 if (!lchan) {
2717 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002718 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 (trans->subscr)?(trans->subscr->extension):"-",
2720 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002721 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2722 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 if (msg_type == MNCC_REL_REQ)
2724 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2725 else
2726 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2727 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002728 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 return rc;
2730 }
2731
2732 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2733 "Received '%s' from MNCC in state %d (%s)\n",
2734 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2735 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002736 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002737 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08002738 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739
2740 /* Find function for current state and message */
2741 for (i = 0; i < DOWNSLLEN; i++)
2742 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002743 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 break;
2745 if (i == DOWNSLLEN) {
2746 DEBUGP(DCC, "Message unhandled at this state.\n");
2747 return 0;
2748 }
2749
2750 rc = downstatelist[i].rout(trans, arg);
2751
2752 return rc;
2753}
2754
2755
2756static struct datastate {
2757 u_int32_t states;
2758 int type;
2759 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2760} datastatelist[] = {
2761 /* mobile originating call establishment */
2762 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2763 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2764 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2765 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2766 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2767 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2768 /* mobile terminating call establishment */
2769 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2770 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2771 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2772 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002773 {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 +08002774 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2775 /* signalling during call */
2776 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2777 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2778 {SBIT(GSM_CSTATE_ACTIVE),
2779 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2780 {ALL_STATES,
2781 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2782 {ALL_STATES,
2783 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2784 {ALL_STATES,
2785 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2786 {SBIT(GSM_CSTATE_ACTIVE),
2787 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2788 {SBIT(GSM_CSTATE_ACTIVE),
2789 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2790 {SBIT(GSM_CSTATE_ACTIVE),
2791 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2792 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2793 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2794 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2795 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2796 {SBIT(GSM_CSTATE_ACTIVE),
2797 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2798 /* clearing */
2799 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2800 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2801 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2802 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2803 {ALL_STATES, /* 5.4.3.4 */
2804 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2805};
2806
2807#define DATASLLEN \
2808 (sizeof(datastatelist) / sizeof(struct datastate))
2809
Harald Welte4bc90a12008-12-27 16:32:52 +00002810static int gsm0408_rcv_cc(struct msgb *msg)
2811{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002812 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002813 struct gsm48_hdr *gh = msgb_l3(msg);
2814 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002815 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002817 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002819
Harald Welte4bfdfe72009-06-10 23:11:52 +08002820 if (msg_type & 0x80) {
2821 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2822 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002823 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002824
2825 conn = &lchan->conn;
2826
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002828 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002829
Harald Welte6f5aee02009-07-23 21:21:14 +02002830 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002831 "Received '%s' from MS in state %d (%s)\n",
2832 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002833 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08002834 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2835 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002836
2837 /* Create transaction */
2838 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002839 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840 "creating new trans.\n", transaction_id);
2841 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002842 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002843 transaction_id, new_callref++);
2844 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845 DEBUGP(DCC, "No memory for trans.\n");
2846 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002847 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848 GSM48_MT_CC_RELEASE_COMPL);
2849 return -ENOMEM;
2850 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002852 trans->conn = &lchan->conn;
2853 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854 }
2855
2856 /* find function for current state and message */
2857 for (i = 0; i < DATASLLEN; i++)
2858 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002859 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860 break;
2861 if (i == DATASLLEN) {
2862 DEBUGP(DCC, "Message unhandled at this state.\n");
2863 return 0;
2864 }
2865
2866 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002867
2868 return rc;
2869}
2870
Harald Welte52b1f982008-12-23 20:25:15 +00002871/* 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 +02002872int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00002873{
2874 struct gsm48_hdr *gh = msgb_l3(msg);
2875 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002876 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002877
2878 if (silent_call_reroute(msg))
2879 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00002880
2881 switch (pdisc) {
2882 case GSM48_PDISC_CC:
2883 rc = gsm0408_rcv_cc(msg);
2884 break;
2885 case GSM48_PDISC_MM:
2886 rc = gsm0408_rcv_mm(msg);
2887 break;
2888 case GSM48_PDISC_RR:
2889 rc = gsm0408_rcv_rr(msg);
2890 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002891 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02002892 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00002893 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002894 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002895 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01002896 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2897 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002898 break;
Harald Welte6eafe912009-10-16 08:32:58 +02002899 case GSM48_PDISC_NC_SS:
2900 rc = handle_rcv_ussd(msg);
2901 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002902 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01002903 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2904 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002905 break;
2906 }
2907
2908 return rc;
2909}
Harald Welte8470bf22008-12-25 23:28:35 +00002910
Harald Welte805f6442009-07-28 18:25:29 +02002911/*
2912 * This will be ran by the linker when loading the DSO. We use it to
2913 * do system initialization, e.g. registration of signal handlers.
2914 */
2915static __attribute__((constructor)) void on_dso_load_0408(void)
2916{
Harald Welte805f6442009-07-28 18:25:29 +02002917 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
2918 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
2919}