blob: fb90ae3def97b1ef6603af715926743f6878da78 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/msgb.h>
35#include <osmocore/bitvec.h>
36#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010039#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010050#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010051#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000055
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020057
Holger Freytherd51524f2009-06-09 08:27:07 +000058int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000059static int gsm48_tx_simple(struct gsm_lchan *lchan,
60 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000061static void schedule_reject(struct gsm_lchan *lchan);
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 Freyther73487a22008-12-31 18:53:57 +000099static void release_loc_updating_req(struct gsm_lchan *lchan)
100{
Harald Welte179f0642008-12-31 23:59:18 +0000101 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000102 return;
103
Harald Welteff117a82009-05-23 05:22:08 +0000104 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200105 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000106 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000107 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000108}
109
110static void allocate_loc_updating_req(struct gsm_lchan *lchan)
111{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000112 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000113 release_loc_updating_req(lchan);
114
Harald Welte470ec292009-06-26 20:25:23 +0200115 lchan->loc_operation = talloc_zero(tall_locop_ctx,
116 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000117}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000118
Holger Freytherd51524f2009-06-09 08:27:07 +0000119static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
120{
Holger Freytherd51524f2009-06-09 08:27:07 +0000121 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200122 int rc;
123
Holger Freytherd51524f2009-06-09 08:27:07 +0000124 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000125 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200126 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100127 if (lchan->ts->trx->bts->network->send_mm_info) {
128 /* 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 */
135 subscr_update(lchan->subscr, msg->trx->bts,
136 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100137 /* try to close channel ASAP */
138 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200139 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000140 }
141
142 return 0;
143}
144
Holger Freyther7c19f742009-06-06 13:54:35 +0000145static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
146 void *handler_data, void *signal_data)
147{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800148 struct gsm_trans *trans, *temp;
149
Holger Freyther7c19f742009-06-06 13:54:35 +0000150 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
151 return 0;
152
153 /*
154 * Cancel any outstanding location updating request
155 * operation taking place on the lchan.
156 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200157 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200158 if (!lchan)
159 return 0;
160
Holger Freyther7c19f742009-06-06 13:54:35 +0000161 release_loc_updating_req(lchan);
162
Harald Welte4bfdfe72009-06-10 23:11:52 +0800163 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200164 /* FIXME: this is not neccessarily the right thing to do, we should
165 * only set trans->lchan to NULL and wait for another lchan to be
166 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800167 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
168 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200169 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800170 }
171
Holger Freyther7c19f742009-06-06 13:54:35 +0000172 return 0;
173}
174
Holger Freyther429e7762008-12-30 13:28:30 +0000175/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000176int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000177{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100178 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000179 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000180 struct gsm48_hdr *gh;
181
Harald Welte8470bf22008-12-25 23:28:35 +0000182 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000183
184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
185 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000186 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000187 gh->data[0] = cause;
188
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100189 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
190 "LAC=%u BTS=%u\n", lchan->subscr ?
191 subscr_name(lchan->subscr) : "unknown",
192 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100193
Harald Welteffa55a42009-12-22 19:07:32 +0100194 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000195
Harald Welte39e2ead2009-07-23 21:13:03 +0200196 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
198
199/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000200int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000201{
Harald Welte8470bf22008-12-25 23:28:35 +0000202 struct gsm_bts *bts = lchan->ts->trx->bts;
203 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000204 struct gsm48_hdr *gh;
205 struct gsm48_loc_area_id *lai;
206 u_int8_t *mid;
207
Harald Welte8470bf22008-12-25 23:28:35 +0000208 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000209
210 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
211 gh->proto_discr = GSM48_PDISC_MM;
212 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
213
214 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100215 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000216 bts->network->network_code, bts->location_area_code);
217
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200218 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200219 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000220
221 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
222
Harald Welteffa55a42009-12-22 19:07:32 +0100223 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100224
Harald Welteb83d9382009-12-12 21:00:48 +0100225 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000226}
227
Harald Weltebf5e8df2009-02-03 12:59:45 +0000228/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000229static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
230{
231 struct msgb *msg = gsm48_msgb_alloc();
232 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000233
Harald Welte231ad4f2008-12-27 11:15:38 +0000234 msg->lchan = lchan;
235
236 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
237 gh->proto_discr = GSM48_PDISC_MM;
238 gh->msg_type = GSM48_MT_MM_ID_REQ;
239 gh->data[0] = id_type;
240
Harald Welte39e2ead2009-07-23 21:13:03 +0200241 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000242}
243
Harald Welte231ad4f2008-12-27 11:15:38 +0000244
Harald Weltebf5e8df2009-02-03 12:59:45 +0000245/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000246static int mm_rx_id_resp(struct msgb *msg)
247{
248 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000249 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200250 struct gsm_bts *bts = lchan->ts->trx->bts;
251 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000252 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200253 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000254
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200255 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000256 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000257 mi_type, mi_string);
258
Harald Welte7659de12009-12-13 12:39:18 +0100259 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
260
Harald Welte75a983f2008-12-27 21:34:06 +0000261 switch (mi_type) {
262 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200263 /* look up subscriber based on IMSI, create if not found */
264 if (!lchan->subscr) {
265 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200266 if (!lchan->subscr)
267 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200268 }
Holger Freyther73487a22008-12-31 18:53:57 +0000269 if (lchan->loc_operation)
270 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000271 break;
272 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000273 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000274 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200275 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000276 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200277 db_sync_equipment(&lchan->subscr->equipment);
278 }
Holger Freyther73487a22008-12-31 18:53:57 +0000279 if (lchan->loc_operation)
280 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000281 break;
282 }
Holger Freyther73487a22008-12-31 18:53:57 +0000283
284 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000285 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000286}
287
Harald Welte255539c2008-12-28 02:26:27 +0000288
289static void loc_upd_rej_cb(void *data)
290{
291 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100292 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000293
Holger Freyther73487a22008-12-31 18:53:57 +0000294 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100295 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000296 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000297}
298
Holger Freytherb7193e42008-12-29 17:44:08 +0000299static void schedule_reject(struct gsm_lchan *lchan)
300{
Holger Freyther73487a22008-12-31 18:53:57 +0000301 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
302 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000303 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000304}
305
Harald Welte2a139372009-02-22 21:14:55 +0000306static const char *lupd_name(u_int8_t type)
307{
308 switch (type) {
309 case GSM48_LUPD_NORMAL:
310 return "NORMAL";
311 case GSM48_LUPD_PERIODIC:
312 return "PEROIDOC";
313 case GSM48_LUPD_IMSI_ATT:
314 return "IMSI ATTACH";
315 default:
316 return "UNKNOWN";
317 }
318}
319
Harald Weltebf5e8df2009-02-03 12:59:45 +0000320/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000321static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000322{
Harald Welte8470bf22008-12-25 23:28:35 +0000323 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000324 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800325 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000326 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200327 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000328 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200329 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000330 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000331
Harald Welte8470bf22008-12-25 23:28:35 +0000332 lu = (struct gsm48_loc_upd_req *) gh->data;
333
334 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000335
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200336 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000337
Harald Weltea0368542009-06-27 02:58:43 +0200338 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000339 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000340
Harald Welte7659de12009-12-13 12:39:18 +0100341 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
342
Harald Welte24ff6ee2009-12-22 00:41:05 +0100343 switch (lu->type) {
344 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100345 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100346 break;
347 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100348 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100349 break;
350 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100351 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100352 break;
353 }
354
Holger Freythereaf04692009-06-06 13:54:44 +0000355 /*
356 * Pseudo Spoof detection: Just drop a second/concurrent
357 * location updating request.
358 */
359 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200360 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +0000361 lchan->loc_operation);
362 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
363 return 0;
364 }
365
Holger Freyther73487a22008-12-31 18:53:57 +0000366 allocate_loc_updating_req(lchan);
367
Harald Welte52b1f982008-12-23 20:25:15 +0000368 switch (mi_type) {
369 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200370 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000371 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000372 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000373 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000374
Jan Luebbe370b41d2009-08-12 10:19:34 +0200375 /* look up subscriber based on IMSI, create if not found */
376 subscr = subscr_get_by_imsi(bts->network, mi_string);
377 if (!subscr) {
378 subscr = db_create_subscriber(bts->network, mi_string);
379 }
Harald Welte4b634542008-12-27 01:55:51 +0000380 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000381 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200382 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000383 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000384 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000385 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000386
Harald Welte52b1f982008-12-23 20:25:15 +0000387 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200388 subscr = subscr_get_by_tmsi(bts->network,
389 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000390 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000391 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000392 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000393 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000394 }
395 break;
396 case GSM_MI_TYPE_IMEI:
397 case GSM_MI_TYPE_IMEISV:
398 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200399 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000400 break;
401 default:
Harald Weltea0368542009-06-27 02:58:43 +0200402 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000403 break;
404 }
405
Harald Welte24516ea2009-07-04 10:18:00 +0200406 /* schedule the reject timer */
407 schedule_reject(lchan);
408
Harald Welte4bfdfe72009-06-10 23:11:52 +0800409 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200410 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800411 /* FIXME: request id? close channel? */
412 return -EINVAL;
413 }
414
Harald Welte255539c2008-12-28 02:26:27 +0000415 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200416 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000417
Harald Welte24516ea2009-07-04 10:18:00 +0200418 /* check if we can let the subscriber into our network immediately
419 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +0000420 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000421}
422
Harald Welte4bfdfe72009-06-10 23:11:52 +0800423#if 0
424static u_int8_t to_bcd8(u_int8_t val)
425{
426 return ((val / 10) << 4) | (val % 10);
427}
428#endif
429
Harald Weltedb253af2008-12-30 17:56:55 +0000430/* Section 9.2.15a */
431int gsm48_tx_mm_info(struct gsm_lchan *lchan)
432{
433 struct msgb *msg = gsm48_msgb_alloc();
434 struct gsm48_hdr *gh;
435 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000436 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200437 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800438#if 0
439 time_t cur_t;
440 struct tm* cur_time;
441 int tz15min;
442#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000443
444 msg->lchan = lchan;
445
446 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
447 gh->proto_discr = GSM48_PDISC_MM;
448 gh->msg_type = GSM48_MT_MM_INFO;
449
450 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200451#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000452 name_len = strlen(net->name_long);
453 /* 10.5.3.5a */
454 ptr8 = msgb_put(msg, 3);
455 ptr8[0] = GSM48_IE_NAME_LONG;
456 ptr8[1] = name_len*2 +1;
457 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
458
459 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
460 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000461 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000462
463 /* FIXME: Use Cell Broadcast, not UCS-2, since
464 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200465#endif
466 name_len = (strlen(net->name_long)*7)/8;
467 name_pad = (8 - strlen(net->name_long)*7)%8;
468 if (name_pad > 0)
469 name_len++;
470 /* 10.5.3.5a */
471 ptr8 = msgb_put(msg, 3);
472 ptr8[0] = GSM48_IE_NAME_LONG;
473 ptr8[1] = name_len +1;
474 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
475
476 ptr8 = msgb_put(msg, name_len);
477 gsm_7bit_encode(ptr8, net->name_long);
478
Harald Weltedb253af2008-12-30 17:56:55 +0000479 }
480
481 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200482#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000483 name_len = strlen(net->name_short);
484 /* 10.5.3.5a */
485 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200486 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000487 ptr8[1] = name_len*2 + 1;
488 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
489
Harald Weltee872cb12009-01-01 00:33:37 +0000490 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000491 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000492 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200493#endif
494 name_len = (strlen(net->name_short)*7)/8;
495 name_pad = (8 - strlen(net->name_short)*7)%8;
496 if (name_pad > 0)
497 name_len++;
498 /* 10.5.3.5a */
499 ptr8 = (u_int8_t *) msgb_put(msg, 3);
500 ptr8[0] = GSM48_IE_NAME_SHORT;
501 ptr8[1] = name_len +1;
502 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
503
504 ptr8 = msgb_put(msg, name_len);
505 gsm_7bit_encode(ptr8, net->name_short);
506
Harald Weltedb253af2008-12-30 17:56:55 +0000507 }
508
509#if 0
510 /* Section 10.5.3.9 */
511 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800512 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000513 ptr8 = msgb_put(msg, 8);
514 ptr8[0] = GSM48_IE_NET_TIME_TZ;
515 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
516 ptr8[2] = to_bcd8(cur_time->tm_mon);
517 ptr8[3] = to_bcd8(cur_time->tm_mday);
518 ptr8[4] = to_bcd8(cur_time->tm_hour);
519 ptr8[5] = to_bcd8(cur_time->tm_min);
520 ptr8[6] = to_bcd8(cur_time->tm_sec);
521 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
522 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800523 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000524 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800525 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000526#endif
527
Daniel Willmanneea93372009-08-13 03:42:07 +0200528 DEBUGP(DMM, "-> MM INFO\n");
529
Harald Welte39e2ead2009-07-23 21:13:03 +0200530 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000531}
532
Harald Welte7984d5c2009-08-12 22:56:50 +0200533/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100534int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200535{
536 struct msgb *msg = gsm48_msgb_alloc();
537 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200538 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200539
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100540 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200541
542 msg->lchan = lchan;
543 gh->proto_discr = GSM48_PDISC_MM;
544 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
545
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100546 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200547
Harald Welte7984d5c2009-08-12 22:56:50 +0200548 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200549 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200550 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200551
552 return gsm48_sendmsg(msg, NULL);
553}
554
555/* Section 9.2.1 */
556int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
557{
558 DEBUGP(DMM, "-> AUTH REJECT\n");
559 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
560}
561
Harald Welte4b634542008-12-27 01:55:51 +0000562static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
563{
Harald Welte4b634542008-12-27 01:55:51 +0000564 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000565 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000566}
Harald Welteba4cf162009-01-10 01:49:35 +0000567
568/* 9.2.6 CM service reject */
569static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
570 enum gsm48_reject_value value)
571{
572 struct msgb *msg = gsm48_msgb_alloc();
573 struct gsm48_hdr *gh;
574
575 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
576
577 msg->lchan = lchan;
578 use_lchan(lchan);
579
580 gh->proto_discr = GSM48_PDISC_MM;
581 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
582 gh->data[0] = value;
583 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
584
Harald Welte39e2ead2009-07-23 21:13:03 +0200585 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000586}
587
Harald Welte4ed0e922009-01-10 03:17:30 +0000588/*
589 * Handle CM Service Requests
590 * a) Verify that the packet is long enough to contain the information
591 * we require otherwsie reject with INCORRECT_MESSAGE
592 * b) Try to parse the TMSI. If we do not have one reject
593 * c) Check that we know the subscriber with the TMSI otherwise reject
594 * with a HLR cause
595 * d) Set the subscriber on the gsm_lchan and accept
596 */
Harald Welte4b634542008-12-27 01:55:51 +0000597static int gsm48_rx_mm_serv_req(struct msgb *msg)
598{
Harald Welteba4cf162009-01-10 01:49:35 +0000599 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200600 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000601
Harald Welte9176bd42009-07-23 18:46:00 +0200602 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000603 struct gsm_subscriber *subscr;
604 struct gsm48_hdr *gh = msgb_l3(msg);
605 struct gsm48_service_request *req =
606 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000607 /* unfortunately in Phase1 the classmar2 length is variable */
608 u_int8_t classmark2_len = gh->data[1];
609 u_int8_t *classmark2 = gh->data+2;
610 u_int8_t mi_len = *(classmark2 + classmark2_len);
611 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000612
Harald Weltec9e02182009-05-01 19:07:53 +0000613 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000614 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000615 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000616 return gsm48_tx_mm_serv_rej(msg->lchan,
617 GSM48_REJECT_INCORRECT_MESSAGE);
618 }
619
620 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000621 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000622 return gsm48_tx_mm_serv_rej(msg->lchan,
623 GSM48_REJECT_INCORRECT_MESSAGE);
624 }
625
Harald Weltec9e02182009-05-01 19:07:53 +0000626 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000627 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000628 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000629 return gsm48_tx_mm_serv_rej(msg->lchan,
630 GSM48_REJECT_INCORRECT_MESSAGE);
631 }
632
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200633 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000634 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000635 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000636
Harald Welte7659de12009-12-13 12:39:18 +0100637 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
638
Harald Welte3ac7f102009-08-10 10:12:45 +0200639 if (is_siemens_bts(bts))
640 send_siemens_mrpci(msg->lchan, classmark2-1);
641
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200642 subscr = subscr_get_by_tmsi(bts->network,
643 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000644
Harald Welte2a139372009-02-22 21:14:55 +0000645 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000646 if (!subscr)
647 return gsm48_tx_mm_serv_rej(msg->lchan,
648 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
649
650 if (!msg->lchan->subscr)
651 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +0100652 else if (msg->lchan->subscr == subscr)
653 subscr_put(subscr); /* lchan already has a ref, don't need another one */
654 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000655 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
656 subscr_put(subscr);
657 }
658
Harald Weltec2e302d2009-07-05 14:08:13 +0200659 subscr->equipment.classmark2_len = classmark2_len;
660 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
661 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000662
Harald Welte4b634542008-12-27 01:55:51 +0000663 return gsm48_tx_mm_serv_ack(msg->lchan);
664}
665
Harald Welte2a139372009-02-22 21:14:55 +0000666static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
667{
Harald Welte9176bd42009-07-23 18:46:00 +0200668 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000669 struct gsm48_hdr *gh = msgb_l3(msg);
670 struct gsm48_imsi_detach_ind *idi =
671 (struct gsm48_imsi_detach_ind *) gh->data;
672 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200673 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000675
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200676 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000677 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
678 mi_type, mi_string);
679
Harald Welteffa55a42009-12-22 19:07:32 +0100680 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100681
Harald Welte2a139372009-02-22 21:14:55 +0000682 switch (mi_type) {
683 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200684 subscr = subscr_get_by_tmsi(bts->network,
685 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000686 break;
687 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200688 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000689 break;
690 case GSM_MI_TYPE_IMEI:
691 case GSM_MI_TYPE_IMEISV:
692 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000693 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000694 break;
695 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000696 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000697 break;
698 }
699
Holger Freyther4a49e772009-04-12 05:37:29 +0000700 if (subscr) {
701 subscr_update(subscr, msg->trx->bts,
702 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100703 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200704
705 subscr->equipment.classmark1 = idi->classmark1;
706 db_sync_equipment(&subscr->equipment);
707
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000708 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000709 } else
Harald Welte2a139372009-02-22 21:14:55 +0000710 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
711
Harald Welte31981a02009-12-20 09:58:40 +0100712 /* FIXME: iterate over all transactions and release them,
713 * imagine an IMSI DETACH happening during an active call! */
714
Harald Welteb83d9382009-12-12 21:00:48 +0100715 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100716 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100717
Harald Welte2a139372009-02-22 21:14:55 +0000718 return 0;
719}
720
Harald Welted2a7f5a2009-06-05 20:08:20 +0000721static int gsm48_rx_mm_status(struct msgb *msg)
722{
723 struct gsm48_hdr *gh = msgb_l3(msg);
724
725 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
726
727 return 0;
728}
729
Harald Weltebf5e8df2009-02-03 12:59:45 +0000730/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000731static int gsm0408_rcv_mm(struct msgb *msg)
732{
733 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800734 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000735
736 switch (gh->msg_type & 0xbf) {
737 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200738 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000739 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000740 break;
741 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000742 rc = mm_rx_id_resp(msg);
743 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000744 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000745 rc = gsm48_rx_mm_serv_req(msg);
746 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000747 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000748 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000749 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000750 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000751 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
752 msg->lchan->subscr ?
Harald Welte2e6d4682009-12-24 14:50:24 +0100753 subscr_name(msg->lchan->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000754 "unknown subscriber");
755 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000756 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000757 rc = gsm48_rx_mm_imsi_detach_ind(msg);
758 break;
759 case GSM48_MT_MM_CM_REEST_REQ:
760 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
761 break;
762 case GSM48_MT_MM_AUTH_RESP:
763 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000764 break;
765 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100766 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000767 gh->msg_type);
768 break;
769 }
770
771 return rc;
772}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000773
Harald Welte2d35ae62009-02-06 12:02:13 +0000774/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200775static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000776{
Harald Welte9176bd42009-07-23 18:46:00 +0200777 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000778 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000779 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200780 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200781 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800782 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000783 int rc = 0;
784
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200785 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000786 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
787 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200788
Harald Weltefe18d8f2009-02-22 21:14:24 +0000789 switch (mi_type) {
790 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200791 subscr = subscr_get_by_tmsi(bts->network,
792 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000793 break;
794 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200795 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000796 break;
797 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000798
799 if (!subscr) {
800 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000801 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000802 return -EINVAL;
803 }
804 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200805 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000806
Harald Weltec2e302d2009-07-05 14:08:13 +0200807 subscr->equipment.classmark2_len = *classmark2_lv;
808 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
809 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000810
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200811 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000812 return rc;
813}
814
Harald Weltef7c43522009-06-09 20:24:21 +0000815static int gsm48_rx_rr_classmark(struct msgb *msg)
816{
817 struct gsm48_hdr *gh = msgb_l3(msg);
818 struct gsm_subscriber *subscr = msg->lchan->subscr;
819 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
820 u_int8_t cm2_len, cm3_len = 0;
821 u_int8_t *cm2, *cm3 = NULL;
822
823 DEBUGP(DRR, "CLASSMARK CHANGE ");
824
825 /* classmark 2 */
826 cm2_len = gh->data[0];
827 cm2 = &gh->data[1];
828 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
829
830 if (payload_len > cm2_len + 1) {
831 /* we must have a classmark3 */
832 if (gh->data[cm2_len+1] != 0x20) {
833 DEBUGPC(DRR, "ERR CM3 TAG\n");
834 return -EINVAL;
835 }
836 if (cm2_len > 3) {
837 DEBUGPC(DRR, "CM2 too long!\n");
838 return -EINVAL;
839 }
840
841 cm3_len = gh->data[cm2_len+2];
842 cm3 = &gh->data[cm2_len+3];
843 if (cm3_len > 14) {
844 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
845 return -EINVAL;
846 }
847 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
848 }
849 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200850 subscr->equipment.classmark2_len = cm2_len;
851 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000852 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200853 subscr->equipment.classmark3_len = cm3_len;
854 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000855 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200856 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000857 }
858
Harald Weltef7c43522009-06-09 20:24:21 +0000859 return 0;
860}
861
Harald Weltecf5b3592009-05-01 18:28:42 +0000862static int gsm48_rx_rr_status(struct msgb *msg)
863{
864 struct gsm48_hdr *gh = msgb_l3(msg);
865
866 DEBUGP(DRR, "STATUS rr_cause = %s\n",
867 rr_cause_name(gh->data[0]));
868
869 return 0;
870}
871
Harald Weltef7c43522009-06-09 20:24:21 +0000872static int gsm48_rx_rr_meas_rep(struct msgb *msg)
873{
Harald Welted12b0fd2009-12-15 21:36:05 +0100874 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +0000875
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100876 /* This shouldn't actually end up here, as RSL treats
877 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
878 * directly into gsm48_parse_meas_rep */
879 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +0100880 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +0000881
882 return 0;
883}
884
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200885static int gsm48_rx_rr_app_info(struct msgb *msg)
886{
887 struct gsm48_hdr *gh = msgb_l3(msg);
888 u_int8_t apdu_id_flags;
889 u_int8_t apdu_len;
890 u_int8_t *apdu_data;
891
892 apdu_id_flags = gh->data[0];
893 apdu_len = gh->data[1];
894 apdu_data = gh->data+2;
895
896 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
897 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
898
Harald Welte (local)026531e2009-08-16 10:40:10 +0200899 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200900}
901
Harald Welted011e8b2009-11-29 22:45:52 +0100902/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +0100903static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100904{
905 struct gsm48_hdr *gh = msgb_l3(msg);
906
907 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
908 rr_cause_name(gh->data[0]));
909
910 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
911 /* FIXME: release old channel */
912
913 return 0;
914}
915
916/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +0100917static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +0100918{
919 struct gsm48_hdr *gh = msgb_l3(msg);
920
921 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
922 rr_cause_name(gh->data[0]));
923
924 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
925 /* FIXME: release allocated new channel */
926
927 return 0;
928}
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200929
Harald Weltebf5e8df2009-02-03 12:59:45 +0000930/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000931static int gsm0408_rcv_rr(struct msgb *msg)
932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000934 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000935
936 switch (gh->msg_type) {
937 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +0000938 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000939 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000940 case GSM48_MT_RR_GPRS_SUSP_REQ:
941 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
942 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000943 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200944 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000945 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000946 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200947 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +0000948 break;
Harald Weltecf5b3592009-05-01 18:28:42 +0000949 case GSM48_MT_RR_STATUS:
950 rc = gsm48_rx_rr_status(msg);
951 break;
Harald Weltef7c43522009-06-09 20:24:21 +0000952 case GSM48_MT_RR_MEAS_REP:
953 rc = gsm48_rx_rr_meas_rep(msg);
954 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200955 case GSM48_MT_RR_APP_INFO:
956 rc = gsm48_rx_rr_app_info(msg);
957 break;
Harald Welte08d91a52009-08-30 15:37:11 +0900958 case GSM48_MT_RR_CIPH_M_COMPL:
959 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
960 /* FIXME: check for MI (if any) */
961 break;
Harald Welteccd5a882009-11-29 20:02:20 +0100962 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +0100963 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100964 break;
965 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +0100966 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100967 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000968 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100969 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
970 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +0000971 break;
972 }
973
Harald Welte2d35ae62009-02-06 12:02:13 +0000974 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000975}
976
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200977int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +0200978 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200979{
980 struct msgb *msg = gsm48_msgb_alloc();
981 struct gsm48_hdr *gh;
982
983 msg->lchan = lchan;
984
985 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
986 apdu_id, apdu_len);
987
988 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
989 gh->proto_discr = GSM48_PDISC_RR;
990 gh->msg_type = GSM48_MT_RR_APP_INFO;
991 gh->data[0] = apdu_id;
992 gh->data[1] = apdu_len;
993 memcpy(gh->data+2, apdu, apdu_len);
994
995 return gsm48_sendmsg(msg, NULL);
996}
997
Harald Welte4bc90a12008-12-27 16:32:52 +0000998/* Call Control */
999
Harald Welte7584aea2009-02-11 11:44:12 +00001000/* The entire call control code is written in accordance with Figure 7.10c
1001 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1002 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1003 * it for voice */
1004
Harald Welte4bfdfe72009-06-10 23:11:52 +08001005static void new_cc_state(struct gsm_trans *trans, int state)
1006{
1007 if (state > 31 || state < 0)
1008 return;
1009
1010 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001011 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001012
Harald Weltedcaf5652009-07-23 18:56:43 +02001013 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001014}
1015
1016static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001017{
1018 struct msgb *msg = gsm48_msgb_alloc();
1019 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1020 u_int8_t *cause, *call_state;
1021
Harald Welte4bc90a12008-12-27 16:32:52 +00001022 gh->msg_type = GSM48_MT_CC_STATUS;
1023
1024 cause = msgb_put(msg, 3);
1025 cause[0] = 2;
1026 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1027 cause[2] = 0x80 | 30; /* response to status inquiry */
1028
1029 call_state = msgb_put(msg, 1);
1030 call_state[0] = 0xc0 | 0x00;
1031
Harald Welte39e2ead2009-07-23 21:13:03 +02001032 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001033}
1034
Harald Welte6f4b7532008-12-29 00:39:37 +00001035static int gsm48_tx_simple(struct gsm_lchan *lchan,
1036 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001037{
1038 struct msgb *msg = gsm48_msgb_alloc();
1039 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1040
1041 msg->lchan = lchan;
1042
Harald Welte6f4b7532008-12-29 00:39:37 +00001043 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001044 gh->msg_type = msg_type;
1045
Harald Welte39e2ead2009-07-23 21:13:03 +02001046 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001047}
1048
Harald Welte4bfdfe72009-06-10 23:11:52 +08001049static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1050{
Harald Weltedcaf5652009-07-23 18:56:43 +02001051 if (bsc_timer_pending(&trans->cc.timer)) {
1052 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1053 bsc_del_timer(&trans->cc.timer);
1054 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001055 }
1056}
1057
1058static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1059 int msg_type, struct gsm_mncc *mncc)
1060{
1061 struct msgb *msg;
1062
1063 if (trans)
1064 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001065 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001066 "Sending '%s' to MNCC.\n",
1067 trans->lchan->ts->trx->bts->nr,
1068 trans->lchan->ts->trx->nr,
1069 trans->lchan->ts->nr, trans->transaction_id,
1070 (trans->subscr)?(trans->subscr->extension):"-",
1071 get_mncc_name(msg_type));
1072 else
1073 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1074 "Sending '%s' to MNCC.\n",
1075 (trans->subscr)?(trans->subscr->extension):"-",
1076 get_mncc_name(msg_type));
1077 else
1078 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1079 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1080
1081 mncc->msg_type = msg_type;
1082
Harald Welte966636f2009-06-26 19:39:35 +02001083 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001084 if (!msg)
1085 return -ENOMEM;
1086 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1087 msgb_enqueue(&net->upqueue, msg);
1088
1089 return 0;
1090}
1091
1092int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1093 u_int32_t callref, int location, int value)
1094{
1095 struct gsm_mncc rel;
1096
Harald Welte92f70c52009-06-12 01:54:08 +08001097 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001098 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001099 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001100 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1101}
1102
Harald Weltedcaf5652009-07-23 18:56:43 +02001103/* Call Control Specific transaction release.
1104 * gets called by trans_free, DO NOT CALL YOURSELF! */
1105void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001106{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001107 gsm48_stop_cc_timer(trans);
1108
1109 /* send release to L4, if callref still exists */
1110 if (trans->callref) {
1111 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001112 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001113 GSM48_CAUSE_LOC_PRN_S_LU,
1114 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001115 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001116 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001117 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001118 if (trans->lchan)
1119 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001120}
1121
1122static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1123
Harald Welte09e38af2009-02-16 22:52:23 +00001124/* call-back from paging the B-end of the connection */
1125static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001126 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001127{
Harald Welte7ccf7782009-02-17 01:43:01 +00001128 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001129 struct gsm_subscriber *subscr = param;
1130 struct gsm_trans *transt, *tmp;
1131 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001132
Harald Welte09e38af2009-02-16 22:52:23 +00001133 if (hooknum != GSM_HOOK_RR_PAGING)
1134 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001135
1136 if (!subscr)
1137 return -EINVAL;
1138 net = subscr->net;
1139 if (!net) {
1140 DEBUGP(DCC, "Error Network not set!\n");
1141 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001142 }
Harald Welte7584aea2009-02-11 11:44:12 +00001143
Harald Welte4bfdfe72009-06-10 23:11:52 +08001144 /* check all tranactions (without lchan) for subscriber */
1145 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1146 if (transt->subscr != subscr || transt->lchan)
1147 continue;
1148 switch (event) {
1149 case GSM_PAGING_SUCCEEDED:
1150 if (!lchan) // paranoid
1151 break;
1152 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1153 subscr->extension);
1154 /* Assign lchan */
1155 if (!transt->lchan) {
1156 transt->lchan = lchan;
1157 use_lchan(lchan);
1158 }
1159 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001160 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001161 break;
1162 case GSM_PAGING_EXPIRED:
1163 DEBUGP(DCC, "Paging subscr %s expired!\n",
1164 subscr->extension);
1165 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001166 mncc_release_ind(transt->subscr->net, transt,
1167 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001168 GSM48_CAUSE_LOC_PRN_S_LU,
1169 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001170 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001171 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001172 break;
1173 }
1174 }
Harald Welte09e38af2009-02-16 22:52:23 +00001175 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001176}
Harald Welte7584aea2009-02-11 11:44:12 +00001177
Harald Welteda7ab742009-12-19 22:23:05 +01001178static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1179
Harald Welte805f6442009-07-28 18:25:29 +02001180/* some other part of the code sends us a signal */
1181static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1182 void *handler_data, void *signal_data)
1183{
1184 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001185 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001186 struct gsm_network *net;
1187 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001188
1189 if (subsys != SS_ABISIP)
1190 return 0;
1191
1192 /* in case we use direct BTS-to-BTS RTP */
1193 if (ipacc_rtp_direct)
1194 return 0;
1195
Harald Welte805f6442009-07-28 18:25:29 +02001196 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001197 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001198 /* check if any transactions on this lchan still have
1199 * a tch_recv_mncc request pending */
1200 net = lchan->ts->trx->bts->network;
1201 llist_for_each_entry(trans, &net->trans_list, entry) {
1202 if (trans->lchan == lchan && trans->tch_recv) {
1203 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1204 tch_recv_mncc(net, trans->callref, 1);
1205 }
1206 }
Harald Welte805f6442009-07-28 18:25:29 +02001207 break;
Harald Welte805f6442009-07-28 18:25:29 +02001208 }
1209
1210 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001211}
1212
Harald Welte49f48b82009-02-17 15:29:33 +00001213/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001214static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001215{
Harald Welte11fa29c2009-02-19 17:24:39 +00001216 struct gsm_bts *bts = lchan->ts->trx->bts;
1217 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001218 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001219
Harald Welte11fa29c2009-02-19 17:24:39 +00001220 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1221 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1222 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1223
1224 if (bts->type != remote_bts->type) {
1225 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1226 return -EINVAL;
1227 }
Harald Welteda7ab742009-12-19 22:23:05 +01001228
1229 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001230 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001231 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001232 if (!ipacc_rtp_direct) {
1233 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001234 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001235 if (rc < 0)
1236 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001237 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001238#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001239
1240 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301241 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1242 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001243 } else {
1244 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301245 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1246 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301247 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001248 if (rc < 0)
1249 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301250 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1251 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301252 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001253 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001254 break;
1255 case GSM_BTS_TYPE_BS11:
1256 trau_mux_map_lchan(lchan, remote_lchan);
1257 break;
1258 default:
1259 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001260 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001261 }
Harald Welte49f48b82009-02-17 15:29:33 +00001262
1263 return 0;
1264}
1265
Harald Welte4bfdfe72009-06-10 23:11:52 +08001266/* bridge channels of two transactions */
1267static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001268{
Harald Weltedcaf5652009-07-23 18:56:43 +02001269 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1270 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001271
Harald Welte4bfdfe72009-06-10 23:11:52 +08001272 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001273 return -EIO;
1274
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275 if (!trans1->lchan || !trans2->lchan)
1276 return -EIO;
1277
1278 /* through-connect channel */
1279 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001280}
1281
Harald Welteda7ab742009-12-19 22:23:05 +01001282/* enable receive of channels to MNCC upqueue */
1283static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001284{
1285 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001286 struct gsm_lchan *lchan;
1287 struct gsm_bts *bts;
1288 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001289
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001291 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292 if (!trans)
1293 return -EIO;
1294 if (!trans->lchan)
1295 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001296 lchan = trans->lchan;
1297 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298
Harald Welteda7ab742009-12-19 22:23:05 +01001299 switch (bts->type) {
1300 case GSM_BTS_TYPE_NANOBTS:
1301 if (ipacc_rtp_direct) {
1302 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1303 return -EINVAL;
1304 }
1305 /* in case, we don't have a RTP socket yet, we note this
1306 * in the transaction and try later */
1307 if (!lchan->abis_ip.rtp_socket) {
1308 trans->tch_recv = enable;
1309 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1310 return 0;
1311 }
1312 if (enable) {
1313 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001314 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001315 if (rc < 0)
1316 return rc;
1317 /* assign socket to application interface */
1318 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1319 net, callref);
1320 } else
1321 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1322 net, 0);
1323 break;
1324 case GSM_BTS_TYPE_BS11:
1325 if (enable)
1326 return trau_recv_lchan(lchan, callref);
1327 return trau_mux_unmap(NULL, callref);
1328 break;
1329 default:
1330 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1331 return -EINVAL;
1332 }
1333
1334 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001335}
1336
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1338{
1339 DEBUGP(DCC, "-> STATUS ENQ\n");
1340 return gsm48_cc_tx_status(trans, msg);
1341}
1342
1343static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1344static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1345
1346static void gsm48_cc_timeout(void *arg)
1347{
1348 struct gsm_trans *trans = arg;
1349 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001350 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1351 int mo_location = GSM48_CAUSE_LOC_USER;
1352 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1353 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354 struct gsm_mncc mo_rel, l4_rel;
1355
1356 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1357 mo_rel.callref = trans->callref;
1358 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1359 l4_rel.callref = trans->callref;
1360
Harald Weltedcaf5652009-07-23 18:56:43 +02001361 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 case 0x303:
1363 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001364 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 break;
1366 case 0x310:
1367 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001368 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 break;
1370 case 0x313:
1371 disconnect = 1;
1372 /* unknown, did not find it in the specs */
1373 break;
1374 case 0x301:
1375 disconnect = 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 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001379 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001381 gsm48_cc_tx_release(trans, &trans->cc.msg);
1382 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 break; /* stay in release state */
1384 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001385 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 return;
1387// release = 1;
1388// l4_cause = 14;
1389// break;
1390 case 0x306:
1391 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001392 mo_cause = trans->cc.msg.cause.value;
1393 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 break;
1395 case 0x323:
1396 disconnect = 1;
1397 break;
1398 default:
1399 release = 1;
1400 }
1401
1402 if (release && trans->callref) {
1403 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001404 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 l4_location, l4_cause);
1406 trans->callref = 0;
1407 }
1408
1409 if (disconnect && trans->callref) {
1410 /* process disconnect towards layer 4 */
1411 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001412 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001413 }
1414
1415 /* process disconnect towards mobile station */
1416 if (disconnect || release) {
1417 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001418 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1419 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1420 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 mo_rel.cause.diag_len = 3;
1422
1423 if (disconnect)
1424 gsm48_cc_tx_disconnect(trans, &mo_rel);
1425 if (release)
1426 gsm48_cc_tx_release(trans, &mo_rel);
1427 }
1428
1429}
1430
1431static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1432 int sec, int micro)
1433{
1434 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001435 trans->cc.timer.cb = gsm48_cc_timeout;
1436 trans->cc.timer.data = trans;
1437 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1438 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439}
1440
1441static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1442{
1443 struct gsm48_hdr *gh = msgb_l3(msg);
1444 u_int8_t msg_type = gh->msg_type & 0xbf;
1445 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1446 struct tlv_parsed tp;
1447 struct gsm_mncc setup;
1448
1449 memset(&setup, 0, sizeof(struct gsm_mncc));
1450 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001451 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 /* emergency setup is identified by msg_type */
1453 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1454 setup.emergency = 1;
1455
1456 /* use subscriber as calling party number */
1457 if (trans->subscr) {
1458 setup.fields |= MNCC_F_CALLING;
1459 strncpy(setup.calling.number, trans->subscr->extension,
1460 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001461 strncpy(setup.imsi, trans->subscr->imsi,
1462 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001463 }
1464 /* bearer capability */
1465 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1466 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001467 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001468 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1469 }
1470 /* facility */
1471 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1472 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001473 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1475 }
1476 /* called party bcd number */
1477 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1478 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001479 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001480 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1481 }
1482 /* user-user */
1483 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1484 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001485 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1487 }
1488 /* ss-version */
1489 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1490 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001491 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1493 }
1494 /* CLIR suppression */
1495 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1496 setup.clir.sup = 1;
1497 /* CLIR invocation */
1498 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1499 setup.clir.inv = 1;
1500 /* cc cap */
1501 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1502 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001503 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1505 }
1506
Harald Welte4bfdfe72009-06-10 23:11:52 +08001507 new_cc_state(trans, GSM_CSTATE_INITIATED);
1508
Harald Welte (local)47df3992009-12-26 19:45:03 +01001509 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1510 subscr_name(trans->subscr), trans->subscr->extension,
1511 setup.called.number);
1512
Harald Welte4bfdfe72009-06-10 23:11:52 +08001513 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001514 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001515
Harald Welte13cac662009-07-29 12:10:35 +02001516 /* MNCC code will modify the channel asynchronously, we should
1517 * ipaccess-bind only after the modification has been made to the
1518 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001519 return 0;
1520}
1521
1522static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001523{
1524 struct msgb *msg = gsm48_msgb_alloc();
1525 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001526 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001527 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001528
Harald Welte7ccf7782009-02-17 01:43:01 +00001529 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001530
Harald Welte4bfdfe72009-06-10 23:11:52 +08001531 /* transaction id must not be assigned */
1532 if (trans->transaction_id != 0xff) { /* unasssigned */
1533 DEBUGP(DCC, "TX Setup with assigned transaction. "
1534 "This is not allowed!\n");
1535 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001536 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001537 GSM48_CAUSE_LOC_PRN_S_LU,
1538 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001539 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001540 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541 return rc;
1542 }
1543
1544 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001545 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1546 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 /* no free transaction ID */
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 }
Harald Welte78283ef2009-07-23 21:36:44 +02001555 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001556
Harald Welte65e74cc2008-12-29 01:55:35 +00001557 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001558
Harald Welte4bfdfe72009-06-10 23:11:52 +08001559 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001560
Harald Welte4bfdfe72009-06-10 23:11:52 +08001561 /* bearer capability */
1562 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001563 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001564 /* facility */
1565 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001566 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567 /* progress */
1568 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001569 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001570 /* calling party BCD number */
1571 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001572 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 /* called party BCD number */
1574 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001575 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001576 /* user-user */
1577 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001578 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001579 /* redirecting party BCD number */
1580 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001581 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582 /* signal */
1583 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001584 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585
1586 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001587
Harald Welte39e2ead2009-07-23 21:13:03 +02001588 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001589}
1590
Harald Welte4bfdfe72009-06-10 23:11:52 +08001591static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1592{
1593 struct gsm48_hdr *gh = msgb_l3(msg);
1594 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1595 struct tlv_parsed tp;
1596 struct gsm_mncc call_conf;
1597
1598 gsm48_stop_cc_timer(trans);
1599 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1600
1601 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1602 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001603 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604#if 0
1605 /* repeat */
1606 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1607 call_conf.repeat = 1;
1608 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1609 call_conf.repeat = 2;
1610#endif
1611 /* bearer capability */
1612 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1613 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001614 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001615 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1616 }
1617 /* cause */
1618 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1619 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001620 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1622 }
1623 /* cc cap */
1624 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1625 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001626 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1628 }
1629
1630 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1631
Harald Welte596fed42009-07-23 19:06:52 +02001632 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1633 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634}
1635
1636static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1637{
1638 struct gsm_mncc *proceeding = arg;
1639 struct msgb *msg = gsm48_msgb_alloc();
1640 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1643
1644 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1645
1646 /* bearer capability */
1647 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001648 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649 /* facility */
1650 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001651 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 /* progress */
1653 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001654 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655
Harald Welte39e2ead2009-07-23 21:13:03 +02001656 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657}
1658
1659static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1660{
1661 struct gsm48_hdr *gh = msgb_l3(msg);
1662 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1663 struct tlv_parsed tp;
1664 struct gsm_mncc alerting;
1665
1666 gsm48_stop_cc_timer(trans);
1667 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1668
1669 memset(&alerting, 0, sizeof(struct gsm_mncc));
1670 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001671 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672 /* facility */
1673 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1674 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001675 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1677 }
1678
1679 /* progress */
1680 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1681 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001682 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1684 }
1685 /* ss-version */
1686 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1687 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001688 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1690 }
1691
1692 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1693
Harald Welte596fed42009-07-23 19:06:52 +02001694 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1695 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696}
1697
1698static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1699{
1700 struct gsm_mncc *alerting = arg;
1701 struct msgb *msg = gsm48_msgb_alloc();
1702 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1703
Harald Welte4bfdfe72009-06-10 23:11:52 +08001704 gh->msg_type = GSM48_MT_CC_ALERTING;
1705
1706 /* facility */
1707 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001708 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 /* progress */
1710 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 /* user-user */
1713 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001714 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715
1716 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1717
Harald Welte39e2ead2009-07-23 21:13:03 +02001718 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719}
1720
1721static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1722{
1723 struct gsm_mncc *progress = arg;
1724 struct msgb *msg = gsm48_msgb_alloc();
1725 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1726
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 gh->msg_type = GSM48_MT_CC_PROGRESS;
1728
1729 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001730 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 /* user-user */
1732 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001733 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734
Harald Welte39e2ead2009-07-23 21:13:03 +02001735 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736}
1737
1738static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1739{
1740 struct gsm_mncc *connect = arg;
1741 struct msgb *msg = gsm48_msgb_alloc();
1742 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1743
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 gh->msg_type = GSM48_MT_CC_CONNECT;
1745
1746 gsm48_stop_cc_timer(trans);
1747 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1748
1749 /* facility */
1750 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001751 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 /* progress */
1753 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001754 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 /* connected number */
1756 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001757 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 /* user-user */
1759 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001760 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761
1762 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1763
Harald Welte39e2ead2009-07-23 21:13:03 +02001764 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765}
1766
1767static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1768{
1769 struct gsm48_hdr *gh = msgb_l3(msg);
1770 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1771 struct tlv_parsed tp;
1772 struct gsm_mncc connect;
1773
1774 gsm48_stop_cc_timer(trans);
1775
1776 memset(&connect, 0, sizeof(struct gsm_mncc));
1777 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001778 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* use subscriber as connected party number */
1780 if (trans->subscr) {
1781 connect.fields |= MNCC_F_CONNECTED;
1782 strncpy(connect.connected.number, trans->subscr->extension,
1783 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001784 strncpy(connect.imsi, trans->subscr->imsi,
1785 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 }
1787 /* facility */
1788 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1789 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001790 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1792 }
1793 /* user-user */
1794 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1795 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001796 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1798 }
1799 /* ss-version */
1800 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1801 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001802 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1804 }
1805
1806 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1807
Harald Welte596fed42009-07-23 19:06:52 +02001808 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809}
1810
1811
1812static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1813{
1814 struct gsm_mncc connect_ack;
1815
1816 gsm48_stop_cc_timer(trans);
1817
1818 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1819
1820 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1821 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001822 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 &connect_ack);
1824}
1825
1826static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1827{
1828 struct msgb *msg = gsm48_msgb_alloc();
1829 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1830
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1832
1833 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1834
Harald Welte39e2ead2009-07-23 21:13:03 +02001835 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836}
1837
1838static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1839{
1840 struct gsm48_hdr *gh = msgb_l3(msg);
1841 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1842 struct tlv_parsed tp;
1843 struct gsm_mncc disc;
1844
1845 gsm48_stop_cc_timer(trans);
1846
1847 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1848
1849 memset(&disc, 0, sizeof(struct gsm_mncc));
1850 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001851 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 /* cause */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1854 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1857 }
1858 /* facility */
1859 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1860 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001861 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1863 }
1864 /* user-user */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1866 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1869 }
1870 /* ss-version */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1872 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1875 }
1876
Harald Welte596fed42009-07-23 19:06:52 +02001877 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878
1879}
1880
Harald Weltec66b71c2009-06-11 14:23:20 +08001881static struct gsm_mncc_cause default_cause = {
1882 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1883 .coding = 0,
1884 .rec = 0,
1885 .rec_val = 0,
1886 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1887 .diag_len = 0,
1888 .diag = { 0 },
1889};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890
1891static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1892{
1893 struct gsm_mncc *disc = arg;
1894 struct msgb *msg = gsm48_msgb_alloc();
1895 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1896
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1898
1899 gsm48_stop_cc_timer(trans);
1900 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1901
1902 /* cause */
1903 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 else
Harald Welte55c8f352010-03-07 23:40:35 +01001906 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907
1908 /* facility */
1909 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001910 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 /* progress */
1912 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001913 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 /* user-user */
1915 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001916 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917
1918 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001919 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920
1921 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1922
Harald Welte39e2ead2009-07-23 21:13:03 +02001923 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924}
1925
1926static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1927{
1928 struct gsm48_hdr *gh = msgb_l3(msg);
1929 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1930 struct tlv_parsed tp;
1931 struct gsm_mncc rel;
1932 int rc;
1933
1934 gsm48_stop_cc_timer(trans);
1935
1936 memset(&rel, 0, sizeof(struct gsm_mncc));
1937 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001938 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 /* cause */
1940 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1941 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1944 }
1945 /* facility */
1946 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1947 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001948 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1950 }
1951 /* user-user */
1952 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1953 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1956 }
1957 /* ss-version */
1958 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1959 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1962 }
1963
Harald Weltedcaf5652009-07-23 18:56:43 +02001964 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02001966 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 } else {
Harald Welte596fed42009-07-23 19:06:52 +02001968 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02001969 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02001970 GSM48_MT_CC_RELEASE_COMPL);
1971 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 }
1973
1974 new_cc_state(trans, GSM_CSTATE_NULL);
1975
1976 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001977 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978
1979 return rc;
1980}
1981
1982static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1983{
1984 struct gsm_mncc *rel = arg;
1985 struct msgb *msg = gsm48_msgb_alloc();
1986 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1987
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 gh->msg_type = GSM48_MT_CC_RELEASE;
1989
1990 trans->callref = 0;
1991
1992 gsm48_stop_cc_timer(trans);
1993 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1994
1995 /* cause */
1996 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001997 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 /* facility */
1999 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 /* user-user */
2002 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004
Harald Weltedcaf5652009-07-23 18:56:43 +02002005 trans->cc.T308_second = 0;
2006 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007
Harald Weltedcaf5652009-07-23 18:56:43 +02002008 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2010
Harald Welte39e2ead2009-07-23 21:13:03 +02002011 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012}
2013
2014static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2015{
2016 struct gsm48_hdr *gh = msgb_l3(msg);
2017 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2018 struct tlv_parsed tp;
2019 struct gsm_mncc rel;
2020 int rc = 0;
2021
2022 gsm48_stop_cc_timer(trans);
2023
2024 memset(&rel, 0, sizeof(struct gsm_mncc));
2025 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002026 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* cause */
2028 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2029 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2032 }
2033 /* facility */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2035 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2038 }
2039 /* user-user */
2040 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2041 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2044 }
2045 /* ss-version */
2046 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2047 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2050 }
2051
2052 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002053 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002055 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 MNCC_REJ_IND, &rel);
2057 break;
2058 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002059 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002061 /* FIXME: in case of multiple calls, we can't simply
2062 * hang up here ! */
2063 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 break;
2065 default:
Harald Welte596fed42009-07-23 19:06:52 +02002066 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 MNCC_REL_IND, &rel);
2068 }
2069 }
2070
2071 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002072 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073
2074 return rc;
2075}
2076
2077static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2078{
2079 struct gsm_mncc *rel = arg;
2080 struct msgb *msg = gsm48_msgb_alloc();
2081 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2082
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2084
2085 trans->callref = 0;
2086
2087 gsm48_stop_cc_timer(trans);
2088
2089 /* cause */
2090 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002091 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 /* facility */
2093 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* user-user */
2096 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098
Harald Weltedcaf5652009-07-23 18:56:43 +02002099 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
Harald Welte39e2ead2009-07-23 21:13:03 +02002101 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102}
2103
2104static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2105{
2106 struct gsm48_hdr *gh = msgb_l3(msg);
2107 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2108 struct tlv_parsed tp;
2109 struct gsm_mncc fac;
2110
2111 memset(&fac, 0, sizeof(struct gsm_mncc));
2112 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002113 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 /* facility */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2116 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2119 }
2120 /* ss-version */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2122 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2125 }
2126
Harald Welte596fed42009-07-23 19:06:52 +02002127 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128}
2129
2130static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2131{
2132 struct gsm_mncc *fac = arg;
2133 struct msgb *msg = gsm48_msgb_alloc();
2134 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2135
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 gh->msg_type = GSM48_MT_CC_FACILITY;
2137
2138 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140
Harald Welte39e2ead2009-07-23 21:13:03 +02002141 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142}
2143
2144static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2145{
2146 struct gsm_mncc hold;
2147
2148 memset(&hold, 0, sizeof(struct gsm_mncc));
2149 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002150 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151}
2152
2153static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2154{
2155 struct msgb *msg = gsm48_msgb_alloc();
2156 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2157
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2159
Harald Welte39e2ead2009-07-23 21:13:03 +02002160 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161}
2162
2163static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2164{
2165 struct gsm_mncc *hold_rej = arg;
2166 struct msgb *msg = gsm48_msgb_alloc();
2167 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2168
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2170
2171 /* cause */
2172 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174 else
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
Harald Welte39e2ead2009-07-23 21:13:03 +02002177 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178}
2179
2180static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2181{
2182 struct gsm_mncc retrieve;
2183
2184 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2185 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002186 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2187 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188}
2189
2190static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2191{
2192 struct msgb *msg = gsm48_msgb_alloc();
2193 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2194
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2196
Harald Welte39e2ead2009-07-23 21:13:03 +02002197 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198}
2199
2200static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2201{
2202 struct gsm_mncc *retrieve_rej = arg;
2203 struct msgb *msg = gsm48_msgb_alloc();
2204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2205
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2207
2208 /* cause */
2209 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 else
Harald Welte55c8f352010-03-07 23:40:35 +01002212 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213
Harald Welte39e2ead2009-07-23 21:13:03 +02002214 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215}
2216
2217static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2218{
2219 struct gsm48_hdr *gh = msgb_l3(msg);
2220 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2221 struct tlv_parsed tp;
2222 struct gsm_mncc dtmf;
2223
2224 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2225 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002226 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 /* keypad facility */
2228 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2229 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2232 }
2233
Harald Welte596fed42009-07-23 19:06:52 +02002234 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235}
2236
2237static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2238{
2239 struct gsm_mncc *dtmf = arg;
2240 struct msgb *msg = gsm48_msgb_alloc();
2241 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2242
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2244
2245 /* keypad */
2246 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002247 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248
Harald Welte39e2ead2009-07-23 21:13:03 +02002249 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250}
2251
2252static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2253{
2254 struct gsm_mncc *dtmf = arg;
2255 struct msgb *msg = gsm48_msgb_alloc();
2256 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2257
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2259
2260 /* cause */
2261 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 else
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265
Harald Welte39e2ead2009-07-23 21:13:03 +02002266 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267}
2268
2269static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2270{
2271 struct msgb *msg = gsm48_msgb_alloc();
2272 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2273
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2275
Harald Welte39e2ead2009-07-23 21:13:03 +02002276 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277}
2278
2279static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2280{
2281 struct gsm_mncc dtmf;
2282
2283 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2284 dtmf.callref = trans->callref;
2285
Harald Welte596fed42009-07-23 19:06:52 +02002286 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287}
2288
2289static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2290{
2291 struct gsm48_hdr *gh = msgb_l3(msg);
2292 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2293 struct tlv_parsed tp;
2294 struct gsm_mncc modify;
2295
2296 memset(&modify, 0, sizeof(struct gsm_mncc));
2297 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002298 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 /* bearer capability */
2300 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2301 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002302 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2304 }
2305
2306 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2307
Harald Welte596fed42009-07-23 19:06:52 +02002308 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309}
2310
2311static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2312{
2313 struct gsm_mncc *modify = arg;
2314 struct msgb *msg = gsm48_msgb_alloc();
2315 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2316
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317 gh->msg_type = GSM48_MT_CC_MODIFY;
2318
2319 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2320
2321 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002322 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323
2324 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2325
Harald Welte39e2ead2009-07-23 21:13:03 +02002326 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327}
2328
2329static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2330{
2331 struct gsm48_hdr *gh = msgb_l3(msg);
2332 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2333 struct tlv_parsed tp;
2334 struct gsm_mncc modify;
2335
2336 gsm48_stop_cc_timer(trans);
2337
2338 memset(&modify, 0, sizeof(struct gsm_mncc));
2339 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002340 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 /* bearer capability */
2342 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2343 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002344 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2346 }
2347
2348 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2349
Harald Welte596fed42009-07-23 19:06:52 +02002350 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351}
2352
2353static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2354{
2355 struct gsm_mncc *modify = arg;
2356 struct msgb *msg = gsm48_msgb_alloc();
2357 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2358
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2360
2361 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002362 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363
2364 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2365
Harald Welte39e2ead2009-07-23 21:13:03 +02002366 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367}
2368
2369static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2370{
2371 struct gsm48_hdr *gh = msgb_l3(msg);
2372 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2373 struct tlv_parsed tp;
2374 struct gsm_mncc modify;
2375
2376 gsm48_stop_cc_timer(trans);
2377
2378 memset(&modify, 0, sizeof(struct gsm_mncc));
2379 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002380 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 /* bearer capability */
2382 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2383 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002384 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2386 }
2387 /* cause */
2388 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2389 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002390 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2392 }
2393
2394 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2395
Harald Welte596fed42009-07-23 19:06:52 +02002396 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397}
2398
2399static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2400{
2401 struct gsm_mncc *modify = arg;
2402 struct msgb *msg = gsm48_msgb_alloc();
2403 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2404
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2406
2407 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002408 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002410 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411
2412 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2413
Harald Welte39e2ead2009-07-23 21:13:03 +02002414 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2418{
2419 struct gsm_mncc *notify = arg;
2420 struct msgb *msg = gsm48_msgb_alloc();
2421 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2422
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 gh->msg_type = GSM48_MT_CC_NOTIFY;
2424
2425 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002426 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427
Harald Welte39e2ead2009-07-23 21:13:03 +02002428 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429}
2430
2431static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2432{
2433 struct gsm48_hdr *gh = msgb_l3(msg);
2434 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2435// struct tlv_parsed tp;
2436 struct gsm_mncc notify;
2437
2438 memset(&notify, 0, sizeof(struct gsm_mncc));
2439 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002440// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443
Harald Welte596fed42009-07-23 19:06:52 +02002444 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445}
2446
2447static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2448{
2449 struct gsm_mncc *user = arg;
2450 struct msgb *msg = gsm48_msgb_alloc();
2451 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2452
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 gh->msg_type = GSM48_MT_CC_USER_INFO;
2454
2455 /* user-user */
2456 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 /* more data */
2459 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002460 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461
Harald Welte39e2ead2009-07-23 21:13:03 +02002462 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm48_hdr *gh = msgb_l3(msg);
2468 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2469 struct tlv_parsed tp;
2470 struct gsm_mncc user;
2471
2472 memset(&user, 0, sizeof(struct gsm_mncc));
2473 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002474 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 /* user-user */
2476 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2477 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2480 }
2481 /* more data */
2482 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2483 user.more = 1;
2484
Harald Welte596fed42009-07-23 19:06:52 +02002485 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486}
2487
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002488static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489{
2490 struct gsm_mncc *mode = arg;
2491
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01002492 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static struct downstate {
2496 u_int32_t states;
2497 int type;
2498 int (*rout) (struct gsm_trans *trans, void *arg);
2499} downstatelist[] = {
2500 /* mobile originating call establishment */
2501 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2502 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2503 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2504 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2505 {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 */
2506 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2507 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2508 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2509 /* mobile terminating call establishment */
2510 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2511 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2512 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2513 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2514 /* signalling during call */
2515 {SBIT(GSM_CSTATE_ACTIVE),
2516 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2517 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2518 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2519 {ALL_STATES,
2520 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2521 {ALL_STATES,
2522 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2523 {ALL_STATES,
2524 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2525 {SBIT(GSM_CSTATE_ACTIVE),
2526 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2527 {SBIT(GSM_CSTATE_ACTIVE),
2528 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2529 {SBIT(GSM_CSTATE_ACTIVE),
2530 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2531 {SBIT(GSM_CSTATE_ACTIVE),
2532 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2533 {SBIT(GSM_CSTATE_ACTIVE),
2534 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2535 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2536 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2537 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2538 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2539 {SBIT(GSM_CSTATE_ACTIVE),
2540 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2541 /* clearing */
2542 {SBIT(GSM_CSTATE_INITIATED),
2543 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2544 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2545 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2546 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2547 MNCC_REL_REQ, gsm48_cc_tx_release},
2548 /* special */
2549 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002550 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551};
2552
2553#define DOWNSLLEN \
2554 (sizeof(downstatelist) / sizeof(struct downstate))
2555
2556
2557int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2558{
Harald Welte1a6f7982009-08-09 18:52:33 +02002559 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002561 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 struct gsm_mncc *data = arg, rel;
2564
2565 /* handle special messages */
2566 switch(msg_type) {
2567 case MNCC_BRIDGE:
2568 return tch_bridge(net, arg);
2569 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002570 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002572 return tch_recv_mncc(net, data->callref, 1);
2573 case GSM_TCHF_FRAME:
2574 /* Find callref */
2575 trans = trans_find_by_callref(net, data->callref);
2576 if (!trans)
2577 return -EIO;
2578 if (!trans->lchan)
2579 return 0;
2580 if (trans->lchan->type != GSM_LCHAN_TCH_F)
2581 return 0;
2582 bts = trans->lchan->ts->trx->bts;
2583 switch (bts->type) {
2584 case GSM_BTS_TYPE_NANOBTS:
2585 if (!trans->lchan->abis_ip.rtp_socket)
2586 return 0;
2587 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
2588 case GSM_BTS_TYPE_BS11:
2589 return trau_send_frame(trans->lchan, arg);
2590 default:
2591 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2592 }
2593 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 }
2595
2596 memset(&rel, 0, sizeof(struct gsm_mncc));
2597 rel.callref = data->callref;
2598
2599 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002600 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601
2602 /* Callref unknown */
2603 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002604 struct gsm_subscriber *subscr;
2605
Harald Welte4a3464c2009-07-04 10:11:24 +02002606 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2608 "Received '%s' from MNCC with "
2609 "unknown callref %d\n", data->called.number,
2610 get_mncc_name(msg_type), data->callref);
2611 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002612 return mncc_release_ind(net, NULL, data->callref,
2613 GSM48_CAUSE_LOC_PRN_S_LU,
2614 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002616 if (!data->called.number[0] && !data->imsi[0]) {
2617 DEBUGP(DCC, "(bts - trx - ts - ti) "
2618 "Received '%s' from MNCC with "
2619 "no number or IMSI\n", get_mncc_name(msg_type));
2620 /* Invalid number */
2621 return mncc_release_ind(net, NULL, data->callref,
2622 GSM48_CAUSE_LOC_PRN_S_LU,
2623 GSM48_CC_CAUSE_INV_NR_FORMAT);
2624 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002626 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002627 subscr = subscr_get_by_extension(net,
2628 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002629 else
Harald Welte9176bd42009-07-23 18:46:00 +02002630 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631 /* If subscriber is not found */
2632 if (!subscr) {
2633 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2634 "Received '%s' from MNCC with "
2635 "unknown subscriber %s\n", data->called.number,
2636 get_mncc_name(msg_type), data->called.number);
2637 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002638 return mncc_release_ind(net, NULL, data->callref,
2639 GSM48_CAUSE_LOC_PRN_S_LU,
2640 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 }
2642 /* If subscriber is not "attached" */
2643 if (!subscr->lac) {
2644 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2645 "Received '%s' from MNCC with "
2646 "detached subscriber %s\n", data->called.number,
2647 get_mncc_name(msg_type), data->called.number);
2648 subscr_put(subscr);
2649 /* Temporarily out of order */
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_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 }
2654 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002655 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2656 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 DEBUGP(DCC, "No memory for trans.\n");
2658 subscr_put(subscr);
2659 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002660 mncc_release_ind(net, NULL, data->callref,
2661 GSM48_CAUSE_LOC_PRN_S_LU,
2662 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 return -ENOMEM;
2664 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002666 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 /* If subscriber has no lchan */
2668 if (!lchan) {
2669 /* find transaction with this subscriber already paging */
2670 llist_for_each_entry(transt, &net->trans_list, entry) {
2671 /* Transaction of our lchan? */
2672 if (transt == trans ||
2673 transt->subscr != subscr)
2674 continue;
2675 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2676 "Received '%s' from MNCC with "
2677 "unallocated channel, paging already "
2678 "started.\n", bts->nr,
2679 data->called.number,
2680 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002681 subscr_put(subscr);
2682 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 return 0;
2684 }
2685 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002686 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002687 /* Trigger paging */
2688 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2689 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002690 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 return 0;
2692 }
2693 /* Assign lchan */
2694 trans->lchan = lchan;
2695 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002696 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 }
2698 lchan = trans->lchan;
2699
2700 /* if paging did not respond yet */
2701 if (!lchan) {
2702 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2703 "Received '%s' from MNCC in paging state\n",
2704 (trans->subscr)?(trans->subscr->extension):"-",
2705 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002706 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2707 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 if (msg_type == MNCC_REL_REQ)
2709 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2710 else
2711 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2712 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002713 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 return rc;
2715 }
2716
2717 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2718 "Received '%s' from MNCC in state %d (%s)\n",
2719 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2720 trans->transaction_id,
2721 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002722 get_mncc_name(msg_type), trans->cc.state,
2723 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724
2725 /* Find function for current state and message */
2726 for (i = 0; i < DOWNSLLEN; i++)
2727 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002728 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 break;
2730 if (i == DOWNSLLEN) {
2731 DEBUGP(DCC, "Message unhandled at this state.\n");
2732 return 0;
2733 }
2734
2735 rc = downstatelist[i].rout(trans, arg);
2736
2737 return rc;
2738}
2739
2740
2741static struct datastate {
2742 u_int32_t states;
2743 int type;
2744 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2745} datastatelist[] = {
2746 /* mobile originating call establishment */
2747 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2748 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2749 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2750 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2751 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2752 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2753 /* mobile terminating call establishment */
2754 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2755 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2756 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2757 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2758 {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 */
2759 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2760 /* signalling during call */
2761 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2762 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2763 {SBIT(GSM_CSTATE_ACTIVE),
2764 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2765 {ALL_STATES,
2766 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2767 {ALL_STATES,
2768 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2769 {ALL_STATES,
2770 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2771 {SBIT(GSM_CSTATE_ACTIVE),
2772 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2773 {SBIT(GSM_CSTATE_ACTIVE),
2774 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2775 {SBIT(GSM_CSTATE_ACTIVE),
2776 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2777 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2778 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2779 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2780 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2781 {SBIT(GSM_CSTATE_ACTIVE),
2782 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2783 /* clearing */
2784 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2785 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2786 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2787 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2788 {ALL_STATES, /* 5.4.3.4 */
2789 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2790};
2791
2792#define DATASLLEN \
2793 (sizeof(datastatelist) / sizeof(struct datastate))
2794
Harald Welte4bc90a12008-12-27 16:32:52 +00002795static int gsm0408_rcv_cc(struct msgb *msg)
2796{
2797 struct gsm48_hdr *gh = msgb_l3(msg);
2798 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002799 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002801 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002803
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 if (msg_type & 0x80) {
2805 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2806 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002807 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808
2809 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02002810 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002811
Harald Welte6f5aee02009-07-23 21:21:14 +02002812 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813 "Received '%s' from MS in state %d (%s)\n",
2814 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2815 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Welteafedeab2010-03-04 10:55:40 +01002816 gsm48_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02002817 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818
2819 /* Create transaction */
2820 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002821 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 "creating new trans.\n", transaction_id);
2823 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002824 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
2825 transaction_id, new_callref++);
2826 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 DEBUGP(DCC, "No memory for trans.\n");
2828 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002829 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830 GSM48_MT_CC_RELEASE_COMPL);
2831 return -ENOMEM;
2832 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 trans->lchan = lchan;
2835 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002836 }
2837
2838 /* find function for current state and message */
2839 for (i = 0; i < DATASLLEN; i++)
2840 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002841 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 break;
2843 if (i == DATASLLEN) {
2844 DEBUGP(DCC, "Message unhandled at this state.\n");
2845 return 0;
2846 }
2847
2848 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002849
2850 return rc;
2851}
2852
Harald Welte52b1f982008-12-23 20:25:15 +00002853/* 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 +02002854int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00002855{
2856 struct gsm48_hdr *gh = msgb_l3(msg);
2857 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002858 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002859
2860 if (silent_call_reroute(msg))
2861 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00002862
2863 switch (pdisc) {
2864 case GSM48_PDISC_CC:
2865 rc = gsm0408_rcv_cc(msg);
2866 break;
2867 case GSM48_PDISC_MM:
2868 rc = gsm0408_rcv_mm(msg);
2869 break;
2870 case GSM48_PDISC_RR:
2871 rc = gsm0408_rcv_rr(msg);
2872 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00002873 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02002874 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00002875 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002876 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002877 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01002878 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2879 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002880 break;
Harald Welte6eafe912009-10-16 08:32:58 +02002881 case GSM48_PDISC_NC_SS:
2882 rc = handle_rcv_ussd(msg);
2883 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002884 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01002885 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2886 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002887 break;
2888 }
2889
2890 return rc;
2891}
Harald Welte8470bf22008-12-25 23:28:35 +00002892
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893/* dequeue messages to layer 4 */
2894int bsc_upqueue(struct gsm_network *net)
2895{
2896 struct gsm_mncc *mncc;
2897 struct msgb *msg;
2898 int work = 0;
2899
2900 if (net)
2901 while ((msg = msgb_dequeue(&net->upqueue))) {
2902 mncc = (struct gsm_mncc *)msg->data;
2903 if (net->mncc_recv)
2904 net->mncc_recv(net, mncc->msg_type, mncc);
2905 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02002906 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002907 }
2908
2909 return work;
2910}
Harald Weltedcaf5652009-07-23 18:56:43 +02002911
Harald Welte805f6442009-07-28 18:25:29 +02002912/*
2913 * This will be ran by the linker when loading the DSO. We use it to
2914 * do system initialization, e.g. registration of signal handlers.
2915 */
2916static __attribute__((constructor)) void on_dso_load_0408(void)
2917{
Harald Welte805f6442009-07-28 18:25:29 +02002918 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
2919 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
2920}