blob: 88b1b6900eb6d48e6c066b4040a427cddd86734a [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 Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
Daniel Willmanneea93372009-08-13 03:42:07 +020038#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000040#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_04_08.h>
42#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000046#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020049#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020050#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020051#include <openbsc/ussd.h>
Harald Welte52b1f982008-12-23 20:25:15 +000052
Harald Welte0c389302009-06-10 12:08:54 +080053#define GSM_MAX_FACILITY 128
54#define GSM_MAX_SSVERSION 128
55#define GSM_MAX_USERUSER 128
56
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020057void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020058
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020059extern int ipacc_rtp_direct;
Harald Welte805f6442009-07-28 18:25:29 +020060
Harald Welte09e38af2009-02-16 22:52:23 +000061static const struct tlv_definition rsl_att_tlvdef = {
62 .def = {
63 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
65 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
66 [GSM48_IE_UTC] = { TLV_TYPE_TV },
67 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
68 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
69
70 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
71 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
72 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
73 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
74 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
75 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
76 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080077 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000078 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
79 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080080 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000082 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
85 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
87 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
88 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
90 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
91 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
92 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
94 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
95 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080096 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
97 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000098 /* FIXME: more elements */
99 },
100};
Harald Weltecf5b3592009-05-01 18:28:42 +0000101
102static const char *rr_cause_names[] = {
103 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
104 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
105 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
106 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
107 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
108 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
109 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
110 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
111 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
112 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
113 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
114 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
115 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
116 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
117 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
118 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
119 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
120};
121
Harald Welte4bfdfe72009-06-10 23:11:52 +0800122static const char *cc_state_names[] = {
123 "NULL",
124 "INITIATED",
125 "illegal state 2",
126 "MO_CALL_PROC",
127 "CALL_DELIVERED",
128 "illegal state 5",
129 "CALL_PRESENT",
130 "CALL_RECEIVED",
131 "CONNECT_REQUEST",
132 "MO_TERM_CALL_CONF",
133 "ACTIVE",
134 "DISCONNECT_REQ",
135 "DISCONNECT_IND",
136 "illegal state 13",
137 "illegal state 14",
138 "illegal state 15",
139 "illegal state 16",
140 "illegal state 17",
141 "illegal state 18",
142 "RELEASE_REQ",
143 "illegal state 20",
144 "illegal state 21",
145 "illegal state 22",
146 "illegal state 23",
147 "illegal state 24",
148 "illegal state 25",
149 "MO_ORIG_MODIFY",
150 "MO_TERM_MODIFY",
151 "CONNECT_IND",
152 "illegal state 29",
153 "illegal state 30",
154 "illegal state 31",
155};
156
Harald Weltecf5b3592009-05-01 18:28:42 +0000157static char strbuf[64];
158
159static const char *rr_cause_name(u_int8_t cause)
160{
161 if (cause < ARRAY_SIZE(rr_cause_names) &&
162 rr_cause_names[cause])
163 return rr_cause_names[cause];
164
165 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
166 return strbuf;
167}
168
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100169int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +0000170{
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100171 struct gsm48_hdr *gh = msgb_l3(msg);
172 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
173 u_int8_t *data = gh->data;
174
175 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
176 return -EINVAL;
Harald Weltef7c43522009-06-09 20:24:21 +0000177
178 if (data[0] & 0x80)
179 rep->flags |= MEAS_REP_F_BA1;
180 if (data[0] & 0x40)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100181 rep->flags |= MEAS_REP_F_UL_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200182 if ((data[1] & 0x40) == 0x00)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100183 rep->flags |= MEAS_REP_F_DL_VALID;
Harald Weltef7c43522009-06-09 20:24:21 +0000184
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100185 rep->dl.full.rx_lev = data[0] & 0x3f;
186 rep->dl.sub.rx_lev = data[1] & 0x3f;
187 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
188 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
189
Harald Weltef7c43522009-06-09 20:24:21 +0000190 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
191 if (rep->num_cell < 1)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100192 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000193
194 /* an encoding nightmare in perfection */
195
196 rep->cell[0].rxlev = data[4] & 0x3f;
197 rep->cell[0].bcch_freq = data[5] >> 2;
198 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
199 if (rep->num_cell < 2)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100200 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000201
202 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
203 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
204 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
205 if (rep->num_cell < 3)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100206 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000207
208 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
209 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
210 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
211 if (rep->num_cell < 4)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100212 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000213
214 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
215 rep->cell[3].bcch_freq = data[11] & 0x1f;
216 rep->cell[3].bsic = data[12] >> 2;
217 if (rep->num_cell < 5)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100218 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000219
220 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
221 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
222 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
223 if (rep->num_cell < 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100224 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000225
226 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
227 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
228 rep->cell[5].bsic = data[16] & 0x3f;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100229
230 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000231}
232
Holger Freytherd51524f2009-06-09 08:27:07 +0000233int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000234static int gsm48_tx_simple(struct gsm_lchan *lchan,
235 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000236static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000237
Harald Welte52b1f982008-12-23 20:25:15 +0000238struct gsm_lai {
239 u_int16_t mcc;
240 u_int16_t mnc;
241 u_int16_t lac;
242};
243
Harald Welte4bfdfe72009-06-10 23:11:52 +0800244static u_int32_t new_callref = 0x80000001;
245
Holger Freyther73487a22008-12-31 18:53:57 +0000246static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
247 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000248{
249 if (!subscriber)
250 return 0;
251
Holger Freyther73487a22008-12-31 18:53:57 +0000252 /*
253 * Do not send accept yet as more information should arrive. Some
254 * phones will not send us the information and we will have to check
255 * what we want to do with that.
256 */
257 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
258 return 0;
259
Jan Luebbe06513f22009-08-12 12:48:00 +0200260 switch (subscriber->net->auth_policy) {
261 case GSM_AUTH_POLICY_CLOSED:
262 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200263 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200264 if (subscriber->authorized)
265 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200266 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000268 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200269 default:
270 return 0;
271 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000272}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000273
Holger Freyther73487a22008-12-31 18:53:57 +0000274static void release_loc_updating_req(struct gsm_lchan *lchan)
275{
Harald Welte179f0642008-12-31 23:59:18 +0000276 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000277 return;
278
Harald Welteff117a82009-05-23 05:22:08 +0000279 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200280 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000281 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000282 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000283}
284
285static void allocate_loc_updating_req(struct gsm_lchan *lchan)
286{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000287 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000288 release_loc_updating_req(lchan);
289
Harald Welte470ec292009-06-26 20:25:23 +0200290 lchan->loc_operation = talloc_zero(tall_locop_ctx,
291 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000292}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000293
Holger Freytherd51524f2009-06-09 08:27:07 +0000294static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
295{
Holger Freytherd51524f2009-06-09 08:27:07 +0000296 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200297 int rc;
298
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000300 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200301 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100302 if (lchan->ts->trx->bts->network->send_mm_info) {
303 /* send MM INFO with network name */
304 rc = gsm48_tx_mm_info(msg->lchan);
305 }
Harald Welteb83d9382009-12-12 21:00:48 +0100306
Harald Welteee5ad162009-08-09 19:07:00 +0200307 /* call subscr_update after putting the loc_upd_acc
308 * in the transmit queue, since S_SUBSCR_ATTACHED might
309 * trigger further action like SMS delivery */
310 subscr_update(lchan->subscr, msg->trx->bts,
311 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100312 /* try to close channel ASAP */
313 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200314 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000315 }
316
317 return 0;
318}
319
Holger Freyther7c19f742009-06-06 13:54:35 +0000320static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
321 void *handler_data, void *signal_data)
322{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800323 struct gsm_trans *trans, *temp;
324
Holger Freyther7c19f742009-06-06 13:54:35 +0000325 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
326 return 0;
327
328 /*
329 * Cancel any outstanding location updating request
330 * operation taking place on the lchan.
331 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200332 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200333 if (!lchan)
334 return 0;
335
Holger Freyther7c19f742009-06-06 13:54:35 +0000336 release_loc_updating_req(lchan);
337
Harald Welte4bfdfe72009-06-10 23:11:52 +0800338 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200339 /* FIXME: this is not neccessarily the right thing to do, we should
340 * only set trans->lchan to NULL and wait for another lchan to be
341 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800342 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
343 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200344 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800345 }
346
Holger Freyther7c19f742009-06-06 13:54:35 +0000347 return 0;
348}
349
Harald Welte09e38af2009-02-16 22:52:23 +0000350static const char bcd_num_digits[] = {
351 '0', '1', '2', '3', '4', '5', '6', '7',
352 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
353};
354
Harald Welte0c389302009-06-10 12:08:54 +0800355/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
356int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
357 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000358{
359 u_int8_t in_len = bcd_lv[0];
360 int i;
361
Harald Welte0c389302009-06-10 12:08:54 +0800362 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000363 /* lower nibble */
364 output_len--;
365 if (output_len <= 1)
366 break;
367 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
368
369 /* higher nibble */
370 output_len--;
371 if (output_len <= 1)
372 break;
373 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
374 }
375 if (output_len >= 1)
376 *output++ = '\0';
377
Harald Welte0c389302009-06-10 12:08:54 +0800378 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000379}
380
381/* convert a single ASCII character to call-control BCD */
382static int asc_to_bcd(const char asc)
383{
384 int i;
385
386 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
387 if (bcd_num_digits[i] == asc)
388 return i;
389 }
390 return -EINVAL;
391}
392
Harald Welte0c389302009-06-10 12:08:54 +0800393/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000394int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800395 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000396{
397 int in_len = strlen(input);
398 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800399 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000400
401 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800402 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000403 if (in_len % 2)
404 bcd_lv[0]++;
405
Harald Welte0c389302009-06-10 12:08:54 +0800406 if (bcd_lv[0] > max_len)
407 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000408
409 for (i = 0; i < in_len; i++) {
410 int rc = asc_to_bcd(input[i]);
411 if (rc < 0)
412 return rc;
413 if (i % 2 == 0)
414 *bcd_cur = rc;
415 else
416 *bcd_cur++ |= (rc << 4);
417 }
418 /* append padding nibble in case of odd length */
419 if (i % 2)
420 *bcd_cur++ |= 0xf0;
421
422 /* return how many bytes we used */
423 return (bcd_cur - bcd_lv);
424}
425
Harald Welte0c389302009-06-10 12:08:54 +0800426/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800427static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800428 const u_int8_t *lv)
429{
430 u_int8_t in_len = lv[0];
431 int i, s;
432
433 if (in_len < 1)
434 return -EINVAL;
435
Harald Welte4bfdfe72009-06-10 23:11:52 +0800436 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800437
438 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800439 bcap->transfer = lv[1] & 0x07;
440 bcap->mode = (lv[1] & 0x08) >> 3;
441 bcap->coding = (lv[1] & 0x10) >> 4;
442 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800443
Harald Welte09b7e7f2009-12-12 21:36:53 +0100444 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
445 i = 1;
446 s = 0;
447 while(!(lv[i] & 0x80)) {
448 i++; /* octet 3a etc */
449 if (in_len < i)
450 return 0;
451 bcap->speech_ver[s++] = lv[i] & 0x0f;
452 bcap->speech_ver[s] = -1; /* end of list */
453 if (i == 2) /* octet 3a */
454 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
455 if (s == 7) /* maximum speech versions + end of list */
456 return 0;
457 }
458 } else {
459 i = 1;
460 while (!(lv[i] & 0x80)) {
461 i++; /* octet 3a etc */
462 if (in_len < i)
463 return 0;
464 /* ignore them */
465 }
466 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800467 }
468
469 return 0;
470}
471
472/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800473static int encode_bearer_cap(struct msgb *msg, int lv_only,
474 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800475{
476 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100477 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800478
Harald Welte4bfdfe72009-06-10 23:11:52 +0800479 lv[1] = bcap->transfer;
480 lv[1] |= bcap->mode << 3;
481 lv[1] |= bcap->coding << 4;
482 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800483
Harald Welte09b7e7f2009-12-12 21:36:53 +0100484 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
485 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
486 i++; /* octet 3a etc */
487 lv[i] = bcap->speech_ver[s];
488 if (i == 2) /* octet 3a */
489 lv[i] |= bcap->speech_ctm << 5;
490 }
491 lv[i] |= 0x80; /* last IE of octet 3 etc */
492 } else {
493 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800494 }
Harald Welte0c389302009-06-10 12:08:54 +0800495
496 lv[0] = i;
497 if (lv_only)
498 msgb_lv_put(msg, lv[0], lv+1);
499 else
500 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
501
502 return 0;
503}
504
505/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800506static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800507{
508 u_int8_t in_len = lv[0];
509
510 if (in_len < 1)
511 return -EINVAL;
512
513 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800514 ccap->dtmf = lv[1] & 0x01;
515 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800516
517 return 0;
518}
519
520/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800521static int decode_called(struct gsm_mncc_number *called,
522 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800523{
524 u_int8_t in_len = lv[0];
525
526 if (in_len < 1)
527 return -EINVAL;
528
529 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800530 called->plan = lv[1] & 0x0f;
531 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800532
533 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800534 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800535
536 return 0;
537}
538
539/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800540static int encode_called(struct msgb *msg,
541 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800542{
543 u_int8_t lv[18];
544 int ret;
545
546 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800547 lv[1] = called->plan;
548 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800549
550 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800552 if (ret < 0)
553 return ret;
554
555 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
556
557 return 0;
558}
559
560/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800561static int encode_callerid(struct msgb *msg, int ie,
562 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800563{
564 u_int8_t lv[13];
565 int h_len = 1;
566 int ret;
567
568 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800569 lv[1] = callerid->plan;
570 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800571
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800573 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800574 lv[2] = callerid->screen;
575 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800576 lv[2] |= 0x80;
577 h_len++;
578 } else
579 lv[1] |= 0x80;
580
581 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800582 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800583 if (ret < 0)
584 return ret;
585
586 msgb_tlv_put(msg, ie, lv[0], lv+1);
587
588 return 0;
589}
590
591/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800593 const u_int8_t *lv)
594{
595 u_int8_t in_len = lv[0];
596 int i;
597
598 if (in_len < 2)
599 return -EINVAL;
600
Harald Welte4bfdfe72009-06-10 23:11:52 +0800601 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800602
603 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800604 cause->location = lv[1] & 0x0f;
605 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800606
607 i = 1;
608 if (!(lv[i] & 0x80)) {
609 i++; /* octet 3a */
610 if (in_len < i+1)
611 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800612 cause->rec = 1;
613 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800614
615 }
616 i++;
617
618 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800620 i++;
621
622 if (in_len < i) /* no diag */
623 return 0;
624
625 if (in_len - (i-1) > 32) /* maximum 32 octets */
626 return 0;
627
628 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 memcpy(cause->diag, lv + i, in_len - (i-1));
630 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800631
632 return 0;
633}
634
635/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800636static int encode_cause(struct msgb *msg, int lv_only,
637 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800638{
639 u_int8_t lv[32+4];
640 int i;
641
Harald Welte4bfdfe72009-06-10 23:11:52 +0800642 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800643 return -EINVAL;
644
645 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800646 lv[1] = cause->location;
647 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800648
649 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800650 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800651 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800652 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800653 }
654 lv[i] |= 0x80; /* end of octet 3 */
655
656 /* octet 4 */
657 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800659
660 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661 if (cause->diag_len) {
662 memcpy(lv + i, cause->diag, cause->diag_len);
663 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800664 }
665
666 lv[0] = i;
667 if (lv_only)
668 msgb_lv_put(msg, lv[0], lv+1);
669 else
670 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
671
672 return 0;
673}
674
675/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800676static int encode_calling(struct msgb *msg,
677 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800678{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800679 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800680}
681
682/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683static int encode_connected(struct msgb *msg,
684 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800685{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800686 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800687}
688
689/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800690static int encode_redirecting(struct msgb *msg,
691 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800692{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800693 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800694}
695
696/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800697static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800698 const u_int8_t *lv)
699{
700 u_int8_t in_len = lv[0];
701
702 if (in_len < 1)
703 return -EINVAL;
704
Harald Welte4bfdfe72009-06-10 23:11:52 +0800705 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800706 return -EINVAL;
707
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 memcpy(facility->info, lv+1, in_len);
709 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800710
711 return 0;
712}
713
714/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800715static int encode_facility(struct msgb *msg, int lv_only,
716 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800717{
718 u_int8_t lv[GSM_MAX_FACILITY + 1];
719
Harald Welte4bfdfe72009-06-10 23:11:52 +0800720 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800721 return -EINVAL;
722
Harald Welte4bfdfe72009-06-10 23:11:52 +0800723 memcpy(lv+1, facility->info, facility->len);
724 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800725 if (lv_only)
726 msgb_lv_put(msg, lv[0], lv+1);
727 else
728 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
729
730 return 0;
731}
732
733/* decode 'notify' */
734static int decode_notify(int *notify, const u_int8_t *v)
735{
736 *notify = v[0] & 0x7f;
737
738 return 0;
739}
740
741/* encode 'notify' */
742static int encode_notify(struct msgb *msg, int notify)
743{
744 msgb_v_put(msg, notify | 0x80);
745
746 return 0;
747}
748
749/* encode 'signal' */
750static int encode_signal(struct msgb *msg, int signal)
751{
752 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
753
754 return 0;
755}
756
757/* decode 'keypad' */
758static int decode_keypad(int *keypad, const u_int8_t *lv)
759{
760 u_int8_t in_len = lv[0];
761
762 if (in_len < 1)
763 return -EINVAL;
764
765 *keypad = lv[1] & 0x7f;
766
767 return 0;
768}
769
770/* encode 'keypad' */
771static int encode_keypad(struct msgb *msg, int keypad)
772{
773 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
774
775 return 0;
776}
777
778/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800779static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800780 const u_int8_t *lv)
781{
782 u_int8_t in_len = lv[0];
783
784 if (in_len < 2)
785 return -EINVAL;
786
Harald Welte4bfdfe72009-06-10 23:11:52 +0800787 progress->coding = (lv[1] & 0x60) >> 5;
788 progress->location = lv[1] & 0x0f;
789 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800790
791 return 0;
792}
793
794/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800795static int encode_progress(struct msgb *msg, int lv_only,
796 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800797{
798 u_int8_t lv[3];
799
800 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800801 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
802 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800803 if (lv_only)
804 msgb_lv_put(msg, lv[0], lv+1);
805 else
806 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
807
808 return 0;
809}
810
811/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800812static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800813 const u_int8_t *lv)
814{
815 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800816 char *info = uu->info;
817 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800818 int i;
819
820 if (in_len < 1)
821 return -EINVAL;
822
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800824
825 for (i = 2; i <= in_len; i++) {
826 info_len--;
827 if (info_len <= 1)
828 break;
829 *info++ = lv[i];
830 }
831 if (info_len >= 1)
832 *info++ = '\0';
833
834 return 0;
835}
836
837/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800838static int encode_useruser(struct msgb *msg, int lv_only,
839 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800840{
841 u_int8_t lv[GSM_MAX_USERUSER + 2];
842
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800844 return -EINVAL;
845
Harald Welte4bfdfe72009-06-10 23:11:52 +0800846 lv[0] = 1 + strlen(uu->info);
847 lv[1] = uu->proto;
848 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800849 if (lv_only)
850 msgb_lv_put(msg, lv[0], lv+1);
851 else
852 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
853
854 return 0;
855}
856
857/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800858static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800859 const u_int8_t *lv)
860{
861 u_int8_t in_len = lv[0];
862
Harald Welte4bfdfe72009-06-10 23:11:52 +0800863 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800864 return -EINVAL;
865
Harald Welte4bfdfe72009-06-10 23:11:52 +0800866 memcpy(ssv->info, lv + 1, in_len);
867 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800868
869 return 0;
870}
871
872/* encode 'more data' */
873static int encode_more(struct msgb *msg)
874{
875 u_int8_t *ie;
876
877 ie = msgb_put(msg, 1);
878 ie[0] = GSM48_IE_MORE_DATA;
879
880 return 0;
881}
882
Holger Freyther429e7762008-12-30 13:28:30 +0000883/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000884int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000885{
Harald Welte8470bf22008-12-25 23:28:35 +0000886 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000887 struct gsm48_hdr *gh;
888
Harald Welte8470bf22008-12-25 23:28:35 +0000889 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000890
891 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
892 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000893 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000894 gh->data[0] = cause;
895
Harald Weltedb253af2008-12-30 17:56:55 +0000896 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
897
Harald Welte39e2ead2009-07-23 21:13:03 +0200898 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000899}
900
901/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000902int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000903{
Harald Welte8470bf22008-12-25 23:28:35 +0000904 struct gsm_bts *bts = lchan->ts->trx->bts;
905 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000906 struct gsm48_hdr *gh;
907 struct gsm48_loc_area_id *lai;
908 u_int8_t *mid;
909
Harald Welte8470bf22008-12-25 23:28:35 +0000910 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000911
912 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
913 gh->proto_discr = GSM48_PDISC_MM;
914 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
915
916 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000917 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000918 bts->network->network_code, bts->location_area_code);
919
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200920 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200921 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000922
923 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
924
Harald Welteb83d9382009-12-12 21:00:48 +0100925 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000926}
927
Harald Weltebf5e8df2009-02-03 12:59:45 +0000928/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000929static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
930{
931 struct msgb *msg = gsm48_msgb_alloc();
932 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000933
Harald Welte231ad4f2008-12-27 11:15:38 +0000934 msg->lchan = lchan;
935
936 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
937 gh->proto_discr = GSM48_PDISC_MM;
938 gh->msg_type = GSM48_MT_MM_ID_REQ;
939 gh->data[0] = id_type;
940
Harald Welte39e2ead2009-07-23 21:13:03 +0200941 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000942}
943
Harald Welte231ad4f2008-12-27 11:15:38 +0000944
Harald Weltebf5e8df2009-02-03 12:59:45 +0000945/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000946static int mm_rx_id_resp(struct msgb *msg)
947{
948 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000949 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200950 struct gsm_bts *bts = lchan->ts->trx->bts;
951 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000952 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200953 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000954
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200955 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000956 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000957 mi_type, mi_string);
958
Harald Welte7659de12009-12-13 12:39:18 +0100959 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
960
Harald Welte75a983f2008-12-27 21:34:06 +0000961 switch (mi_type) {
962 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200963 /* look up subscriber based on IMSI, create if not found */
964 if (!lchan->subscr) {
965 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200966 if (!lchan->subscr)
967 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200968 }
Holger Freyther73487a22008-12-31 18:53:57 +0000969 if (lchan->loc_operation)
970 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000971 break;
972 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000973 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000974 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200975 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000976 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200977 db_sync_equipment(&lchan->subscr->equipment);
978 }
Holger Freyther73487a22008-12-31 18:53:57 +0000979 if (lchan->loc_operation)
980 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000981 break;
982 }
Holger Freyther73487a22008-12-31 18:53:57 +0000983
984 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000985 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000986}
987
Harald Welte255539c2008-12-28 02:26:27 +0000988
989static void loc_upd_rej_cb(void *data)
990{
991 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100992 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000993
Holger Freyther73487a22008-12-31 18:53:57 +0000994 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100995 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000996 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000997}
998
Holger Freytherb7193e42008-12-29 17:44:08 +0000999static void schedule_reject(struct gsm_lchan *lchan)
1000{
Holger Freyther73487a22008-12-31 18:53:57 +00001001 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1002 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001003 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001004}
1005
Harald Welte2a139372009-02-22 21:14:55 +00001006static const char *lupd_name(u_int8_t type)
1007{
1008 switch (type) {
1009 case GSM48_LUPD_NORMAL:
1010 return "NORMAL";
1011 case GSM48_LUPD_PERIODIC:
1012 return "PEROIDOC";
1013 case GSM48_LUPD_IMSI_ATT:
1014 return "IMSI ATTACH";
1015 default:
1016 return "UNKNOWN";
1017 }
1018}
1019
Harald Weltebf5e8df2009-02-03 12:59:45 +00001020/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001021static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001022{
Harald Welte8470bf22008-12-25 23:28:35 +00001023 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001024 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001025 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001026 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001027 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001028 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001029 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001030 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001031
Harald Welte8470bf22008-12-25 23:28:35 +00001032 lu = (struct gsm48_loc_upd_req *) gh->data;
1033
1034 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001035
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001036 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001037
Harald Weltea0368542009-06-27 02:58:43 +02001038 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001039 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001040
Harald Welte7659de12009-12-13 12:39:18 +01001041 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
1042
Holger Freythereaf04692009-06-06 13:54:44 +00001043 /*
1044 * Pseudo Spoof detection: Just drop a second/concurrent
1045 * location updating request.
1046 */
1047 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001048 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001049 lchan->loc_operation);
1050 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1051 return 0;
1052 }
1053
Holger Freyther73487a22008-12-31 18:53:57 +00001054 allocate_loc_updating_req(lchan);
1055
Harald Welte52b1f982008-12-23 20:25:15 +00001056 switch (mi_type) {
1057 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001058 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001059 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001060 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001061 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001062
Jan Luebbe370b41d2009-08-12 10:19:34 +02001063 /* look up subscriber based on IMSI, create if not found */
1064 subscr = subscr_get_by_imsi(bts->network, mi_string);
1065 if (!subscr) {
1066 subscr = db_create_subscriber(bts->network, mi_string);
1067 }
Harald Welte4b634542008-12-27 01:55:51 +00001068 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001069 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001070 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001071 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001072 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001073 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001074
Harald Welte52b1f982008-12-23 20:25:15 +00001075 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001076 subscr = subscr_get_by_tmsi(bts->network,
1077 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001078 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001079 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001080 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001081 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001082 }
1083 break;
1084 case GSM_MI_TYPE_IMEI:
1085 case GSM_MI_TYPE_IMEISV:
1086 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001087 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001088 break;
1089 default:
Harald Weltea0368542009-06-27 02:58:43 +02001090 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001091 break;
1092 }
1093
Harald Welte24516ea2009-07-04 10:18:00 +02001094 /* schedule the reject timer */
1095 schedule_reject(lchan);
1096
Harald Welte4bfdfe72009-06-10 23:11:52 +08001097 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001098 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001099 /* FIXME: request id? close channel? */
1100 return -EINVAL;
1101 }
1102
Harald Welte255539c2008-12-28 02:26:27 +00001103 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001104 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001105
Harald Welte24516ea2009-07-04 10:18:00 +02001106 /* check if we can let the subscriber into our network immediately
1107 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001108 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001109}
1110
Harald Welte4bfdfe72009-06-10 23:11:52 +08001111#if 0
1112static u_int8_t to_bcd8(u_int8_t val)
1113{
1114 return ((val / 10) << 4) | (val % 10);
1115}
1116#endif
1117
Harald Weltedb253af2008-12-30 17:56:55 +00001118/* Section 9.2.15a */
1119int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1120{
1121 struct msgb *msg = gsm48_msgb_alloc();
1122 struct gsm48_hdr *gh;
1123 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001124 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001125 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001126#if 0
1127 time_t cur_t;
1128 struct tm* cur_time;
1129 int tz15min;
1130#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001131
1132 msg->lchan = lchan;
1133
1134 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1135 gh->proto_discr = GSM48_PDISC_MM;
1136 gh->msg_type = GSM48_MT_MM_INFO;
1137
1138 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001139#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001140 name_len = strlen(net->name_long);
1141 /* 10.5.3.5a */
1142 ptr8 = msgb_put(msg, 3);
1143 ptr8[0] = GSM48_IE_NAME_LONG;
1144 ptr8[1] = name_len*2 +1;
1145 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1146
1147 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1148 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001149 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001150
1151 /* FIXME: Use Cell Broadcast, not UCS-2, since
1152 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001153#endif
1154 name_len = (strlen(net->name_long)*7)/8;
1155 name_pad = (8 - strlen(net->name_long)*7)%8;
1156 if (name_pad > 0)
1157 name_len++;
1158 /* 10.5.3.5a */
1159 ptr8 = msgb_put(msg, 3);
1160 ptr8[0] = GSM48_IE_NAME_LONG;
1161 ptr8[1] = name_len +1;
1162 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1163
1164 ptr8 = msgb_put(msg, name_len);
1165 gsm_7bit_encode(ptr8, net->name_long);
1166
Harald Weltedb253af2008-12-30 17:56:55 +00001167 }
1168
1169 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001170#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001171 name_len = strlen(net->name_short);
1172 /* 10.5.3.5a */
1173 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001174 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001175 ptr8[1] = name_len*2 + 1;
1176 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1177
Harald Weltee872cb12009-01-01 00:33:37 +00001178 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001179 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001180 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001181#endif
1182 name_len = (strlen(net->name_short)*7)/8;
1183 name_pad = (8 - strlen(net->name_short)*7)%8;
1184 if (name_pad > 0)
1185 name_len++;
1186 /* 10.5.3.5a */
1187 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1188 ptr8[0] = GSM48_IE_NAME_SHORT;
1189 ptr8[1] = name_len +1;
1190 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1191
1192 ptr8 = msgb_put(msg, name_len);
1193 gsm_7bit_encode(ptr8, net->name_short);
1194
Harald Weltedb253af2008-12-30 17:56:55 +00001195 }
1196
1197#if 0
1198 /* Section 10.5.3.9 */
1199 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001200 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001201 ptr8 = msgb_put(msg, 8);
1202 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1203 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1204 ptr8[2] = to_bcd8(cur_time->tm_mon);
1205 ptr8[3] = to_bcd8(cur_time->tm_mday);
1206 ptr8[4] = to_bcd8(cur_time->tm_hour);
1207 ptr8[5] = to_bcd8(cur_time->tm_min);
1208 ptr8[6] = to_bcd8(cur_time->tm_sec);
1209 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1210 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001211 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001212 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001213 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001214#endif
1215
Daniel Willmanneea93372009-08-13 03:42:07 +02001216 DEBUGP(DMM, "-> MM INFO\n");
1217
Harald Welte39e2ead2009-07-23 21:13:03 +02001218 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001219}
1220
Harald Welte7984d5c2009-08-12 22:56:50 +02001221/* Section 9.2.2 */
1222int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1223{
1224 struct msgb *msg = gsm48_msgb_alloc();
1225 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001226 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001227
1228 DEBUGP(DMM, "-> AUTH REQ\n");
1229
1230 msg->lchan = lchan;
1231 gh->proto_discr = GSM48_PDISC_MM;
1232 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1233
Sylvain Munaut849f5542009-09-27 11:10:17 +02001234 /* Key Sequence: FIXME fixed to 0 */
1235 ar->key_seq = 0;
1236
Harald Welte7984d5c2009-08-12 22:56:50 +02001237 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001238 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001239 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001240
1241 return gsm48_sendmsg(msg, NULL);
1242}
1243
1244/* Section 9.2.1 */
1245int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1246{
1247 DEBUGP(DMM, "-> AUTH REJECT\n");
1248 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1249}
1250
Harald Welte4b634542008-12-27 01:55:51 +00001251static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1252{
Harald Welte4b634542008-12-27 01:55:51 +00001253 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001254 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001255}
Harald Welteba4cf162009-01-10 01:49:35 +00001256
1257/* 9.2.6 CM service reject */
1258static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1259 enum gsm48_reject_value value)
1260{
1261 struct msgb *msg = gsm48_msgb_alloc();
1262 struct gsm48_hdr *gh;
1263
1264 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1265
1266 msg->lchan = lchan;
1267 use_lchan(lchan);
1268
1269 gh->proto_discr = GSM48_PDISC_MM;
1270 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1271 gh->data[0] = value;
1272 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1273
Harald Welte39e2ead2009-07-23 21:13:03 +02001274 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001275}
1276
Harald Welte4ed0e922009-01-10 03:17:30 +00001277/*
1278 * Handle CM Service Requests
1279 * a) Verify that the packet is long enough to contain the information
1280 * we require otherwsie reject with INCORRECT_MESSAGE
1281 * b) Try to parse the TMSI. If we do not have one reject
1282 * c) Check that we know the subscriber with the TMSI otherwise reject
1283 * with a HLR cause
1284 * d) Set the subscriber on the gsm_lchan and accept
1285 */
Harald Welte4b634542008-12-27 01:55:51 +00001286static int gsm48_rx_mm_serv_req(struct msgb *msg)
1287{
Harald Welteba4cf162009-01-10 01:49:35 +00001288 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001289 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001290
Harald Welte9176bd42009-07-23 18:46:00 +02001291 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001292 struct gsm_subscriber *subscr;
1293 struct gsm48_hdr *gh = msgb_l3(msg);
1294 struct gsm48_service_request *req =
1295 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001296 /* unfortunately in Phase1 the classmar2 length is variable */
1297 u_int8_t classmark2_len = gh->data[1];
1298 u_int8_t *classmark2 = gh->data+2;
1299 u_int8_t mi_len = *(classmark2 + classmark2_len);
1300 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001301
Harald Weltec9e02182009-05-01 19:07:53 +00001302 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001303 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001304 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001305 return gsm48_tx_mm_serv_rej(msg->lchan,
1306 GSM48_REJECT_INCORRECT_MESSAGE);
1307 }
1308
1309 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001310 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001311 return gsm48_tx_mm_serv_rej(msg->lchan,
1312 GSM48_REJECT_INCORRECT_MESSAGE);
1313 }
1314
Harald Weltec9e02182009-05-01 19:07:53 +00001315 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001316 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001317 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001318 return gsm48_tx_mm_serv_rej(msg->lchan,
1319 GSM48_REJECT_INCORRECT_MESSAGE);
1320 }
1321
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001322 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001323 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001324 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001325
Harald Welte7659de12009-12-13 12:39:18 +01001326 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1327
Harald Welte3ac7f102009-08-10 10:12:45 +02001328 if (is_siemens_bts(bts))
1329 send_siemens_mrpci(msg->lchan, classmark2-1);
1330
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001331 subscr = subscr_get_by_tmsi(bts->network,
1332 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001333
Harald Welte2a139372009-02-22 21:14:55 +00001334 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001335 if (!subscr)
1336 return gsm48_tx_mm_serv_rej(msg->lchan,
1337 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1338
1339 if (!msg->lchan->subscr)
1340 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001341 else if (msg->lchan->subscr != subscr) {
1342 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1343 subscr_put(subscr);
1344 }
1345
Harald Weltec2e302d2009-07-05 14:08:13 +02001346 subscr->equipment.classmark2_len = classmark2_len;
1347 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1348 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001349
Harald Welte4b634542008-12-27 01:55:51 +00001350 return gsm48_tx_mm_serv_ack(msg->lchan);
1351}
1352
Harald Welte2a139372009-02-22 21:14:55 +00001353static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1354{
Harald Welte9176bd42009-07-23 18:46:00 +02001355 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001356 struct gsm48_hdr *gh = msgb_l3(msg);
1357 struct gsm48_imsi_detach_ind *idi =
1358 (struct gsm48_imsi_detach_ind *) gh->data;
1359 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001360 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001362
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001363 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001364 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1365 mi_type, mi_string);
1366
1367 switch (mi_type) {
1368 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001369 subscr = subscr_get_by_tmsi(bts->network,
1370 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001371 break;
1372 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001373 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001374 break;
1375 case GSM_MI_TYPE_IMEI:
1376 case GSM_MI_TYPE_IMEISV:
1377 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001378 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001379 break;
1380 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001381 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001382 break;
1383 }
1384
Holger Freyther4a49e772009-04-12 05:37:29 +00001385 if (subscr) {
1386 subscr_update(subscr, msg->trx->bts,
1387 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001388 DEBUGP(DMM, "Subscriber: %s\n",
1389 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001390
1391 subscr->equipment.classmark1 = idi->classmark1;
1392 db_sync_equipment(&subscr->equipment);
1393
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001394 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001395 } else
Harald Welte2a139372009-02-22 21:14:55 +00001396 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1397
Harald Welteb83d9382009-12-12 21:00:48 +01001398 /* subscriber is detached: should we release lchan? */
1399
Harald Welte2a139372009-02-22 21:14:55 +00001400 return 0;
1401}
1402
Harald Welted2a7f5a2009-06-05 20:08:20 +00001403static int gsm48_rx_mm_status(struct msgb *msg)
1404{
1405 struct gsm48_hdr *gh = msgb_l3(msg);
1406
1407 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1408
1409 return 0;
1410}
1411
Harald Weltebf5e8df2009-02-03 12:59:45 +00001412/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001413static int gsm0408_rcv_mm(struct msgb *msg)
1414{
1415 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001417
1418 switch (gh->msg_type & 0xbf) {
1419 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001420 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001421 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001422 break;
1423 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001424 rc = mm_rx_id_resp(msg);
1425 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001426 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001427 rc = gsm48_rx_mm_serv_req(msg);
1428 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001429 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001430 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001431 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001432 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001433 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1434 msg->lchan->subscr ?
1435 msg->lchan->subscr->imsi :
1436 "unknown subscriber");
1437 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001438 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001439 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1440 break;
1441 case GSM48_MT_MM_CM_REEST_REQ:
1442 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1443 break;
1444 case GSM48_MT_MM_AUTH_RESP:
1445 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001446 break;
1447 default:
1448 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1449 gh->msg_type);
1450 break;
1451 }
1452
1453 return rc;
1454}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001455
Harald Welte2d35ae62009-02-06 12:02:13 +00001456/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001457static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001458{
Harald Welte9176bd42009-07-23 18:46:00 +02001459 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001460 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001461 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001462 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001463 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001464 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001465 int rc = 0;
1466
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001467 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001468 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1469 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001470
Harald Weltefe18d8f2009-02-22 21:14:24 +00001471 switch (mi_type) {
1472 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001473 subscr = subscr_get_by_tmsi(bts->network,
1474 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001475 break;
1476 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001477 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001478 break;
1479 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001480
1481 if (!subscr) {
1482 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001483 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001484 return -EINVAL;
1485 }
1486 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001487 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001488
Harald Weltec2e302d2009-07-05 14:08:13 +02001489 subscr->equipment.classmark2_len = *classmark2_lv;
1490 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1491 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001492
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001493 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001494 return rc;
1495}
1496
Harald Weltef7c43522009-06-09 20:24:21 +00001497static int gsm48_rx_rr_classmark(struct msgb *msg)
1498{
1499 struct gsm48_hdr *gh = msgb_l3(msg);
1500 struct gsm_subscriber *subscr = msg->lchan->subscr;
1501 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1502 u_int8_t cm2_len, cm3_len = 0;
1503 u_int8_t *cm2, *cm3 = NULL;
1504
1505 DEBUGP(DRR, "CLASSMARK CHANGE ");
1506
1507 /* classmark 2 */
1508 cm2_len = gh->data[0];
1509 cm2 = &gh->data[1];
1510 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1511
1512 if (payload_len > cm2_len + 1) {
1513 /* we must have a classmark3 */
1514 if (gh->data[cm2_len+1] != 0x20) {
1515 DEBUGPC(DRR, "ERR CM3 TAG\n");
1516 return -EINVAL;
1517 }
1518 if (cm2_len > 3) {
1519 DEBUGPC(DRR, "CM2 too long!\n");
1520 return -EINVAL;
1521 }
1522
1523 cm3_len = gh->data[cm2_len+2];
1524 cm3 = &gh->data[cm2_len+3];
1525 if (cm3_len > 14) {
1526 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1527 return -EINVAL;
1528 }
1529 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1530 }
1531 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001532 subscr->equipment.classmark2_len = cm2_len;
1533 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001534 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001535 subscr->equipment.classmark3_len = cm3_len;
1536 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001537 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001538 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001539 }
1540
Harald Weltef7c43522009-06-09 20:24:21 +00001541 return 0;
1542}
1543
Harald Weltecf5b3592009-05-01 18:28:42 +00001544static int gsm48_rx_rr_status(struct msgb *msg)
1545{
1546 struct gsm48_hdr *gh = msgb_l3(msg);
1547
1548 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1549 rr_cause_name(gh->data[0]));
1550
1551 return 0;
1552}
1553
Harald Weltef7c43522009-06-09 20:24:21 +00001554static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1555{
Harald Weltef7c43522009-06-09 20:24:21 +00001556 static struct gsm_meas_rep meas_rep;
1557
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001558 /* This shouldn't actually end up here, as RSL treats
1559 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1560 * directly into gsm48_parse_meas_rep */
1561 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1562 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001563
1564 return 0;
1565}
1566
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001567static int gsm48_rx_rr_app_info(struct msgb *msg)
1568{
1569 struct gsm48_hdr *gh = msgb_l3(msg);
1570 u_int8_t apdu_id_flags;
1571 u_int8_t apdu_len;
1572 u_int8_t *apdu_data;
1573
1574 apdu_id_flags = gh->data[0];
1575 apdu_len = gh->data[1];
1576 apdu_data = gh->data+2;
1577
1578 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1579 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1580
Harald Welte (local)026531e2009-08-16 10:40:10 +02001581 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001582}
1583
Harald Welted011e8b2009-11-29 22:45:52 +01001584/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001585static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001586{
1587 struct gsm48_hdr *gh = msgb_l3(msg);
1588
1589 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1590 rr_cause_name(gh->data[0]));
1591
1592 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1593 /* FIXME: release old channel */
1594
1595 return 0;
1596}
1597
1598/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001599static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001600{
1601 struct gsm48_hdr *gh = msgb_l3(msg);
1602
1603 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1604 rr_cause_name(gh->data[0]));
1605
1606 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1607 /* FIXME: release allocated new channel */
1608
1609 return 0;
1610}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001611
Harald Weltebf5e8df2009-02-03 12:59:45 +00001612/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001613static int gsm0408_rcv_rr(struct msgb *msg)
1614{
1615 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001616 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001617
1618 switch (gh->msg_type) {
1619 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001620 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001621 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001622 case GSM48_MT_RR_GPRS_SUSP_REQ:
1623 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1624 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001625 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001626 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001627 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001628 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001629 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001630 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001631 case GSM48_MT_RR_STATUS:
1632 rc = gsm48_rx_rr_status(msg);
1633 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001634 case GSM48_MT_RR_MEAS_REP:
1635 rc = gsm48_rx_rr_meas_rep(msg);
1636 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001637 case GSM48_MT_RR_APP_INFO:
1638 rc = gsm48_rx_rr_app_info(msg);
1639 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001640 case GSM48_MT_RR_CIPH_M_COMPL:
1641 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1642 /* FIXME: check for MI (if any) */
1643 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001644 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001645 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001646 break;
1647 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001648 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001649 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001650 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001651 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001652 gh->msg_type);
1653 break;
1654 }
1655
Harald Welte2d35ae62009-02-06 12:02:13 +00001656 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001657}
1658
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001659int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001660 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001661{
1662 struct msgb *msg = gsm48_msgb_alloc();
1663 struct gsm48_hdr *gh;
1664
1665 msg->lchan = lchan;
1666
1667 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1668 apdu_id, apdu_len);
1669
1670 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1671 gh->proto_discr = GSM48_PDISC_RR;
1672 gh->msg_type = GSM48_MT_RR_APP_INFO;
1673 gh->data[0] = apdu_id;
1674 gh->data[1] = apdu_len;
1675 memcpy(gh->data+2, apdu, apdu_len);
1676
1677 return gsm48_sendmsg(msg, NULL);
1678}
1679
Harald Welte4bc90a12008-12-27 16:32:52 +00001680/* Call Control */
1681
Harald Welte7584aea2009-02-11 11:44:12 +00001682/* The entire call control code is written in accordance with Figure 7.10c
1683 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1684 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1685 * it for voice */
1686
Harald Welte4bfdfe72009-06-10 23:11:52 +08001687static void new_cc_state(struct gsm_trans *trans, int state)
1688{
1689 if (state > 31 || state < 0)
1690 return;
1691
1692 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694
Harald Weltedcaf5652009-07-23 18:56:43 +02001695 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696}
1697
1698static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001699{
1700 struct msgb *msg = gsm48_msgb_alloc();
1701 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1702 u_int8_t *cause, *call_state;
1703
Harald Welte4bc90a12008-12-27 16:32:52 +00001704 gh->msg_type = GSM48_MT_CC_STATUS;
1705
1706 cause = msgb_put(msg, 3);
1707 cause[0] = 2;
1708 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1709 cause[2] = 0x80 | 30; /* response to status inquiry */
1710
1711 call_state = msgb_put(msg, 1);
1712 call_state[0] = 0xc0 | 0x00;
1713
Harald Welte39e2ead2009-07-23 21:13:03 +02001714 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001715}
1716
Harald Welte6f4b7532008-12-29 00:39:37 +00001717static int gsm48_tx_simple(struct gsm_lchan *lchan,
1718 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001719{
1720 struct msgb *msg = gsm48_msgb_alloc();
1721 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1722
1723 msg->lchan = lchan;
1724
Harald Welte6f4b7532008-12-29 00:39:37 +00001725 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001726 gh->msg_type = msg_type;
1727
Harald Welte39e2ead2009-07-23 21:13:03 +02001728 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001729}
1730
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1732{
Harald Weltedcaf5652009-07-23 18:56:43 +02001733 if (bsc_timer_pending(&trans->cc.timer)) {
1734 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1735 bsc_del_timer(&trans->cc.timer);
1736 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 }
1738}
1739
1740static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1741 int msg_type, struct gsm_mncc *mncc)
1742{
1743 struct msgb *msg;
1744
1745 if (trans)
1746 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001747 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 "Sending '%s' to MNCC.\n",
1749 trans->lchan->ts->trx->bts->nr,
1750 trans->lchan->ts->trx->nr,
1751 trans->lchan->ts->nr, trans->transaction_id,
1752 (trans->subscr)?(trans->subscr->extension):"-",
1753 get_mncc_name(msg_type));
1754 else
1755 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1756 "Sending '%s' to MNCC.\n",
1757 (trans->subscr)?(trans->subscr->extension):"-",
1758 get_mncc_name(msg_type));
1759 else
1760 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1761 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1762
1763 mncc->msg_type = msg_type;
1764
Harald Welte966636f2009-06-26 19:39:35 +02001765 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 if (!msg)
1767 return -ENOMEM;
1768 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1769 msgb_enqueue(&net->upqueue, msg);
1770
1771 return 0;
1772}
1773
1774int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1775 u_int32_t callref, int location, int value)
1776{
1777 struct gsm_mncc rel;
1778
Harald Welte92f70c52009-06-12 01:54:08 +08001779 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001781 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1783}
1784
Harald Weltedcaf5652009-07-23 18:56:43 +02001785/* Call Control Specific transaction release.
1786 * gets called by trans_free, DO NOT CALL YOURSELF! */
1787void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 gsm48_stop_cc_timer(trans);
1790
1791 /* send release to L4, if callref still exists */
1792 if (trans->callref) {
1793 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001794 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001795 GSM48_CAUSE_LOC_PRN_S_LU,
1796 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001798 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001800 if (trans->lchan)
1801 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802}
1803
1804static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1805
Harald Welte09e38af2009-02-16 22:52:23 +00001806/* call-back from paging the B-end of the connection */
1807static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001808 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001809{
Harald Welte7ccf7782009-02-17 01:43:01 +00001810 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 struct gsm_subscriber *subscr = param;
1812 struct gsm_trans *transt, *tmp;
1813 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001814
Harald Welte09e38af2009-02-16 22:52:23 +00001815 if (hooknum != GSM_HOOK_RR_PAGING)
1816 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817
1818 if (!subscr)
1819 return -EINVAL;
1820 net = subscr->net;
1821 if (!net) {
1822 DEBUGP(DCC, "Error Network not set!\n");
1823 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001824 }
Harald Welte7584aea2009-02-11 11:44:12 +00001825
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 /* check all tranactions (without lchan) for subscriber */
1827 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1828 if (transt->subscr != subscr || transt->lchan)
1829 continue;
1830 switch (event) {
1831 case GSM_PAGING_SUCCEEDED:
1832 if (!lchan) // paranoid
1833 break;
1834 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1835 subscr->extension);
1836 /* Assign lchan */
1837 if (!transt->lchan) {
1838 transt->lchan = lchan;
1839 use_lchan(lchan);
1840 }
1841 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001842 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 break;
1844 case GSM_PAGING_EXPIRED:
1845 DEBUGP(DCC, "Paging subscr %s expired!\n",
1846 subscr->extension);
1847 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001848 mncc_release_ind(transt->subscr->net, transt,
1849 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001850 GSM48_CAUSE_LOC_PRN_S_LU,
1851 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001853 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 break;
1855 }
1856 }
Harald Welte09e38af2009-02-16 22:52:23 +00001857 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001858}
Harald Welte7584aea2009-02-11 11:44:12 +00001859
Harald Welte805f6442009-07-28 18:25:29 +02001860/* some other part of the code sends us a signal */
1861static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1862 void *handler_data, void *signal_data)
1863{
1864 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001865 int rc;
1866
1867 if (subsys != SS_ABISIP)
1868 return 0;
1869
1870 /* in case we use direct BTS-to-BTS RTP */
1871 if (ipacc_rtp_direct)
1872 return 0;
1873
Harald Welte805f6442009-07-28 18:25:29 +02001874 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001875 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001876 /* the BTS has successfully bound a TCH to a local ip/port,
1877 * which means we can connect our UDP socket to it */
Harald Welte2c828992009-12-02 01:56:49 +05301878 if (lchan->abis_ip.rtp_socket) {
1879 rtp_socket_free(lchan->abis_ip.rtp_socket);
1880 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001881 }
1882
Harald Welte2c828992009-12-02 01:56:49 +05301883 lchan->abis_ip.rtp_socket = rtp_socket_create();
1884 if (!lchan->abis_ip.rtp_socket)
Harald Welte805f6442009-07-28 18:25:29 +02001885 goto out_err;
1886
Harald Welte2c828992009-12-02 01:56:49 +05301887 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1888 lchan->abis_ip.bound_ip,
1889 lchan->abis_ip.bound_port);
Harald Welte805f6442009-07-28 18:25:29 +02001890 if (rc < 0)
1891 goto out_err;
1892 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001893 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001894 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte2c828992009-12-02 01:56:49 +05301895 if (lchan->abis_ip.rtp_socket) {
1896 rtp_socket_free(lchan->abis_ip.rtp_socket);
1897 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001898 }
1899 break;
1900 }
1901
1902 return 0;
1903out_err:
1904 /* FIXME: do something */
1905 return 0;
1906}
1907
1908/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1909static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1910{
Harald Welte2c828992009-12-02 01:56:49 +05301911 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte805f6442009-07-28 18:25:29 +02001912 int rc;
1913
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001914 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001915 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte2c828992009-12-02 01:56:49 +05301916 lchan->abis_ip.conn_id,
Harald Welte805f6442009-07-28 18:25:29 +02001917 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte2c828992009-12-02 01:56:49 +05301918 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001919
1920 return rc;
1921}
1922
Harald Welte49f48b82009-02-17 15:29:33 +00001923/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001924static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001925{
Harald Welte11fa29c2009-02-19 17:24:39 +00001926 struct gsm_bts *bts = lchan->ts->trx->bts;
1927 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001928 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001929
Harald Welte11fa29c2009-02-19 17:24:39 +00001930 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1931 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1932 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1933
1934 if (bts->type != remote_bts->type) {
1935 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1936 return -EINVAL;
1937 }
Harald Welte49f48b82009-02-17 15:29:33 +00001938
Harald Welte11fa29c2009-02-19 17:24:39 +00001939 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001940 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001941 if (!ipacc_rtp_direct) {
1942 /* connect the TCH's to our RTP proxy */
1943 rc = ipacc_connect_proxy_bind(lchan);
1944 if (rc < 0)
1945 return rc;
1946 rc = ipacc_connect_proxy_bind(remote_lchan);
1947
1948 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301949 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1950 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001951 } else {
1952 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301953 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1954 remote_lchan->abis_ip.bound_port,
1955 lchan->abis_ip.conn_id,
1956 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001957 if (rc < 0)
1958 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301959 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1960 lchan->abis_ip.bound_port,
1961 remote_lchan->abis_ip.conn_id,
1962 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001963 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001964 break;
1965 case GSM_BTS_TYPE_BS11:
1966 trau_mux_map_lchan(lchan, remote_lchan);
1967 break;
1968 default:
1969 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001970 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001971 break;
1972 }
Harald Welte49f48b82009-02-17 15:29:33 +00001973
1974 return 0;
1975}
1976
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977/* bridge channels of two transactions */
1978static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001979{
Harald Weltedcaf5652009-07-23 18:56:43 +02001980 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1981 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001982
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001984 return -EIO;
1985
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 if (!trans1->lchan || !trans2->lchan)
1987 return -EIO;
1988
1989 /* through-connect channel */
1990 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001991}
1992
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993/* enable receive of channels to upqueue */
1994static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1995{
1996 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001999 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 if (!trans)
2001 return -EIO;
2002 if (!trans->lchan)
2003 return 0;
2004
2005 // todo IPACCESS
2006 if (enable)
2007 return trau_recv_lchan(trans->lchan, data->callref);
2008 return trau_mux_unmap(NULL, data->callref);
2009}
2010
2011/* send a frame to channel */
2012static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2013{
2014 struct gsm_trans *trans;
2015
2016 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002017 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 if (!trans)
2019 return -EIO;
2020 if (!trans->lchan)
2021 return 0;
2022 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2023 trans->lchan->type != GSM_LCHAN_TCH_H)
2024 return 0;
2025
2026 // todo IPACCESS
2027 return trau_send_lchan(trans->lchan,
2028 (struct decoded_trau_frame *)frame->data);
2029}
2030
2031
2032static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2033{
2034 DEBUGP(DCC, "-> STATUS ENQ\n");
2035 return gsm48_cc_tx_status(trans, msg);
2036}
2037
2038static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2039static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2040
2041static void gsm48_cc_timeout(void *arg)
2042{
2043 struct gsm_trans *trans = arg;
2044 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002045 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2046 int mo_location = GSM48_CAUSE_LOC_USER;
2047 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2048 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 struct gsm_mncc mo_rel, l4_rel;
2050
2051 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2052 mo_rel.callref = trans->callref;
2053 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2054 l4_rel.callref = trans->callref;
2055
Harald Weltedcaf5652009-07-23 18:56:43 +02002056 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 case 0x303:
2058 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002059 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 break;
2061 case 0x310:
2062 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002063 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 break;
2065 case 0x313:
2066 disconnect = 1;
2067 /* unknown, did not find it in the specs */
2068 break;
2069 case 0x301:
2070 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002071 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 break;
2073 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002074 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002076 gsm48_cc_tx_release(trans, &trans->cc.msg);
2077 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 break; /* stay in release state */
2079 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002080 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 return;
2082// release = 1;
2083// l4_cause = 14;
2084// break;
2085 case 0x306:
2086 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002087 mo_cause = trans->cc.msg.cause.value;
2088 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 break;
2090 case 0x323:
2091 disconnect = 1;
2092 break;
2093 default:
2094 release = 1;
2095 }
2096
2097 if (release && trans->callref) {
2098 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002099 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 l4_location, l4_cause);
2101 trans->callref = 0;
2102 }
2103
2104 if (disconnect && trans->callref) {
2105 /* process disconnect towards layer 4 */
2106 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002107 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 }
2109
2110 /* process disconnect towards mobile station */
2111 if (disconnect || release) {
2112 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002113 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2114 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2115 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116 mo_rel.cause.diag_len = 3;
2117
2118 if (disconnect)
2119 gsm48_cc_tx_disconnect(trans, &mo_rel);
2120 if (release)
2121 gsm48_cc_tx_release(trans, &mo_rel);
2122 }
2123
2124}
2125
2126static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2127 int sec, int micro)
2128{
2129 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002130 trans->cc.timer.cb = gsm48_cc_timeout;
2131 trans->cc.timer.data = trans;
2132 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2133 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134}
2135
2136static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2137{
2138 struct gsm48_hdr *gh = msgb_l3(msg);
2139 u_int8_t msg_type = gh->msg_type & 0xbf;
2140 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2141 struct tlv_parsed tp;
2142 struct gsm_mncc setup;
2143
2144 memset(&setup, 0, sizeof(struct gsm_mncc));
2145 setup.callref = trans->callref;
2146 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2147 /* emergency setup is identified by msg_type */
2148 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2149 setup.emergency = 1;
2150
2151 /* use subscriber as calling party number */
2152 if (trans->subscr) {
2153 setup.fields |= MNCC_F_CALLING;
2154 strncpy(setup.calling.number, trans->subscr->extension,
2155 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002156 strncpy(setup.imsi, trans->subscr->imsi,
2157 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 }
2159 /* bearer capability */
2160 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2161 setup.fields |= MNCC_F_BEARER_CAP;
2162 decode_bearer_cap(&setup.bearer_cap,
2163 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2164 }
2165 /* facility */
2166 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2167 setup.fields |= MNCC_F_FACILITY;
2168 decode_facility(&setup.facility,
2169 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2170 }
2171 /* called party bcd number */
2172 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2173 setup.fields |= MNCC_F_CALLED;
2174 decode_called(&setup.called,
2175 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2176 }
2177 /* user-user */
2178 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2179 setup.fields |= MNCC_F_USERUSER;
2180 decode_useruser(&setup.useruser,
2181 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2182 }
2183 /* ss-version */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2185 setup.fields |= MNCC_F_SSVERSION;
2186 decode_ssversion(&setup.ssversion,
2187 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2188 }
2189 /* CLIR suppression */
2190 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2191 setup.clir.sup = 1;
2192 /* CLIR invocation */
2193 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2194 setup.clir.inv = 1;
2195 /* cc cap */
2196 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2197 setup.fields |= MNCC_F_CCCAP;
2198 decode_cccap(&setup.cccap,
2199 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2200 }
2201
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202 new_cc_state(trans, GSM_CSTATE_INITIATED);
2203
2204 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002205 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206
Harald Welte13cac662009-07-29 12:10:35 +02002207 /* MNCC code will modify the channel asynchronously, we should
2208 * ipaccess-bind only after the modification has been made to the
2209 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 return 0;
2211}
2212
2213static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002214{
2215 struct msgb *msg = gsm48_msgb_alloc();
2216 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002218 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002219
Harald Welte7ccf7782009-02-17 01:43:01 +00002220 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002221
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 /* transaction id must not be assigned */
2223 if (trans->transaction_id != 0xff) { /* unasssigned */
2224 DEBUGP(DCC, "TX Setup with assigned transaction. "
2225 "This is not allowed!\n");
2226 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002227 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002228 GSM48_CAUSE_LOC_PRN_S_LU,
2229 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002231 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 return rc;
2233 }
2234
2235 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002236 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2237 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002239 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002240 GSM48_CAUSE_LOC_PRN_S_LU,
2241 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002243 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 return rc;
2245 }
Harald Welte78283ef2009-07-23 21:36:44 +02002246 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002247
Harald Welte65e74cc2008-12-29 01:55:35 +00002248 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002249
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002251
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 /* bearer capability */
2253 if (setup->fields & MNCC_F_BEARER_CAP)
2254 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2255 /* facility */
2256 if (setup->fields & MNCC_F_FACILITY)
2257 encode_facility(msg, 0, &setup->facility);
2258 /* progress */
2259 if (setup->fields & MNCC_F_PROGRESS)
2260 encode_progress(msg, 0, &setup->progress);
2261 /* calling party BCD number */
2262 if (setup->fields & MNCC_F_CALLING)
2263 encode_calling(msg, &setup->calling);
2264 /* called party BCD number */
2265 if (setup->fields & MNCC_F_CALLED)
2266 encode_called(msg, &setup->called);
2267 /* user-user */
2268 if (setup->fields & MNCC_F_USERUSER)
2269 encode_useruser(msg, 0, &setup->useruser);
2270 /* redirecting party BCD number */
2271 if (setup->fields & MNCC_F_REDIRECTING)
2272 encode_redirecting(msg, &setup->redirecting);
2273 /* signal */
2274 if (setup->fields & MNCC_F_SIGNAL)
2275 encode_signal(msg, setup->signal);
2276
2277 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002278
Harald Welte39e2ead2009-07-23 21:13:03 +02002279 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002280}
2281
Harald Welte4bfdfe72009-06-10 23:11:52 +08002282static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2283{
2284 struct gsm48_hdr *gh = msgb_l3(msg);
2285 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2286 struct tlv_parsed tp;
2287 struct gsm_mncc call_conf;
2288
2289 gsm48_stop_cc_timer(trans);
2290 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2291
2292 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2293 call_conf.callref = trans->callref;
2294 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2295#if 0
2296 /* repeat */
2297 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2298 call_conf.repeat = 1;
2299 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2300 call_conf.repeat = 2;
2301#endif
2302 /* bearer capability */
2303 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2304 call_conf.fields |= MNCC_F_BEARER_CAP;
2305 decode_bearer_cap(&call_conf.bearer_cap,
2306 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2307 }
2308 /* cause */
2309 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2310 call_conf.fields |= MNCC_F_CAUSE;
2311 decode_cause(&call_conf.cause,
2312 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2313 }
2314 /* cc cap */
2315 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2316 call_conf.fields |= MNCC_F_CCCAP;
2317 decode_cccap(&call_conf.cccap,
2318 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2319 }
2320
2321 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2322
Harald Welte596fed42009-07-23 19:06:52 +02002323 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2324 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325}
2326
2327static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2328{
2329 struct gsm_mncc *proceeding = arg;
2330 struct msgb *msg = gsm48_msgb_alloc();
2331 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2332
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2334
2335 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2336
2337 /* bearer capability */
2338 if (proceeding->fields & MNCC_F_BEARER_CAP)
2339 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2340 /* facility */
2341 if (proceeding->fields & MNCC_F_FACILITY)
2342 encode_facility(msg, 0, &proceeding->facility);
2343 /* progress */
2344 if (proceeding->fields & MNCC_F_PROGRESS)
2345 encode_progress(msg, 0, &proceeding->progress);
2346
Harald Welte39e2ead2009-07-23 21:13:03 +02002347 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348}
2349
2350static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2351{
2352 struct gsm48_hdr *gh = msgb_l3(msg);
2353 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2354 struct tlv_parsed tp;
2355 struct gsm_mncc alerting;
2356
2357 gsm48_stop_cc_timer(trans);
2358 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2359
2360 memset(&alerting, 0, sizeof(struct gsm_mncc));
2361 alerting.callref = trans->callref;
2362 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2363 /* facility */
2364 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2365 alerting.fields |= MNCC_F_FACILITY;
2366 decode_facility(&alerting.facility,
2367 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2368 }
2369
2370 /* progress */
2371 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2372 alerting.fields |= MNCC_F_PROGRESS;
2373 decode_progress(&alerting.progress,
2374 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2375 }
2376 /* ss-version */
2377 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2378 alerting.fields |= MNCC_F_SSVERSION;
2379 decode_ssversion(&alerting.ssversion,
2380 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2381 }
2382
2383 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2384
Harald Welte596fed42009-07-23 19:06:52 +02002385 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2386 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387}
2388
2389static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2390{
2391 struct gsm_mncc *alerting = arg;
2392 struct msgb *msg = gsm48_msgb_alloc();
2393 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2394
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 gh->msg_type = GSM48_MT_CC_ALERTING;
2396
2397 /* facility */
2398 if (alerting->fields & MNCC_F_FACILITY)
2399 encode_facility(msg, 0, &alerting->facility);
2400 /* progress */
2401 if (alerting->fields & MNCC_F_PROGRESS)
2402 encode_progress(msg, 0, &alerting->progress);
2403 /* user-user */
2404 if (alerting->fields & MNCC_F_USERUSER)
2405 encode_useruser(msg, 0, &alerting->useruser);
2406
2407 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2408
Harald Welte39e2ead2009-07-23 21:13:03 +02002409 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2413{
2414 struct gsm_mncc *progress = arg;
2415 struct msgb *msg = gsm48_msgb_alloc();
2416 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2417
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 gh->msg_type = GSM48_MT_CC_PROGRESS;
2419
2420 /* progress */
2421 encode_progress(msg, 1, &progress->progress);
2422 /* user-user */
2423 if (progress->fields & MNCC_F_USERUSER)
2424 encode_useruser(msg, 0, &progress->useruser);
2425
Harald Welte39e2ead2009-07-23 21:13:03 +02002426 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2430{
2431 struct gsm_mncc *connect = arg;
2432 struct msgb *msg = gsm48_msgb_alloc();
2433 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2434
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 gh->msg_type = GSM48_MT_CC_CONNECT;
2436
2437 gsm48_stop_cc_timer(trans);
2438 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2439
2440 /* facility */
2441 if (connect->fields & MNCC_F_FACILITY)
2442 encode_facility(msg, 0, &connect->facility);
2443 /* progress */
2444 if (connect->fields & MNCC_F_PROGRESS)
2445 encode_progress(msg, 0, &connect->progress);
2446 /* connected number */
2447 if (connect->fields & MNCC_F_CONNECTED)
2448 encode_connected(msg, &connect->connected);
2449 /* user-user */
2450 if (connect->fields & MNCC_F_USERUSER)
2451 encode_useruser(msg, 0, &connect->useruser);
2452
2453 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2454
Harald Welte39e2ead2009-07-23 21:13:03 +02002455 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456}
2457
2458static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2459{
2460 struct gsm48_hdr *gh = msgb_l3(msg);
2461 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2462 struct tlv_parsed tp;
2463 struct gsm_mncc connect;
2464
2465 gsm48_stop_cc_timer(trans);
2466
2467 memset(&connect, 0, sizeof(struct gsm_mncc));
2468 connect.callref = trans->callref;
2469 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2470 /* use subscriber as connected party number */
2471 if (trans->subscr) {
2472 connect.fields |= MNCC_F_CONNECTED;
2473 strncpy(connect.connected.number, trans->subscr->extension,
2474 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002475 strncpy(connect.imsi, trans->subscr->imsi,
2476 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 }
2478 /* facility */
2479 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2480 connect.fields |= MNCC_F_FACILITY;
2481 decode_facility(&connect.facility,
2482 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2483 }
2484 /* user-user */
2485 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2486 connect.fields |= MNCC_F_USERUSER;
2487 decode_useruser(&connect.useruser,
2488 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2489 }
2490 /* ss-version */
2491 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2492 connect.fields |= MNCC_F_SSVERSION;
2493 decode_ssversion(&connect.ssversion,
2494 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2495 }
2496
2497 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2498
Harald Welte596fed42009-07-23 19:06:52 +02002499 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500}
2501
2502
2503static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2504{
2505 struct gsm_mncc connect_ack;
2506
2507 gsm48_stop_cc_timer(trans);
2508
2509 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2510
2511 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2512 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002513 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 &connect_ack);
2515}
2516
2517static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2518{
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2523
2524 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2525
Harald Welte39e2ead2009-07-23 21:13:03 +02002526 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527}
2528
2529static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2530{
2531 struct gsm48_hdr *gh = msgb_l3(msg);
2532 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2533 struct tlv_parsed tp;
2534 struct gsm_mncc disc;
2535
2536 gsm48_stop_cc_timer(trans);
2537
2538 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2539
2540 memset(&disc, 0, sizeof(struct gsm_mncc));
2541 disc.callref = trans->callref;
2542 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2543 /* cause */
2544 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2545 disc.fields |= MNCC_F_CAUSE;
2546 decode_cause(&disc.cause,
2547 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2548 }
2549 /* facility */
2550 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2551 disc.fields |= MNCC_F_FACILITY;
2552 decode_facility(&disc.facility,
2553 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2554 }
2555 /* user-user */
2556 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2557 disc.fields |= MNCC_F_USERUSER;
2558 decode_useruser(&disc.useruser,
2559 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2560 }
2561 /* ss-version */
2562 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2563 disc.fields |= MNCC_F_SSVERSION;
2564 decode_ssversion(&disc.ssversion,
2565 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2566 }
2567
Harald Welte596fed42009-07-23 19:06:52 +02002568 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569
2570}
2571
Harald Weltec66b71c2009-06-11 14:23:20 +08002572static struct gsm_mncc_cause default_cause = {
2573 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2574 .coding = 0,
2575 .rec = 0,
2576 .rec_val = 0,
2577 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2578 .diag_len = 0,
2579 .diag = { 0 },
2580};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581
2582static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2583{
2584 struct gsm_mncc *disc = arg;
2585 struct msgb *msg = gsm48_msgb_alloc();
2586 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2587
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2589
2590 gsm48_stop_cc_timer(trans);
2591 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2592
2593 /* cause */
2594 if (disc->fields & MNCC_F_CAUSE)
2595 encode_cause(msg, 1, &disc->cause);
2596 else
2597 encode_cause(msg, 1, &default_cause);
2598
2599 /* facility */
2600 if (disc->fields & MNCC_F_FACILITY)
2601 encode_facility(msg, 0, &disc->facility);
2602 /* progress */
2603 if (disc->fields & MNCC_F_PROGRESS)
2604 encode_progress(msg, 0, &disc->progress);
2605 /* user-user */
2606 if (disc->fields & MNCC_F_USERUSER)
2607 encode_useruser(msg, 0, &disc->useruser);
2608
2609 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002610 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611
2612 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2613
Harald Welte39e2ead2009-07-23 21:13:03 +02002614 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615}
2616
2617static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2618{
2619 struct gsm48_hdr *gh = msgb_l3(msg);
2620 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2621 struct tlv_parsed tp;
2622 struct gsm_mncc rel;
2623 int rc;
2624
2625 gsm48_stop_cc_timer(trans);
2626
2627 memset(&rel, 0, sizeof(struct gsm_mncc));
2628 rel.callref = trans->callref;
2629 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2630 /* cause */
2631 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2632 rel.fields |= MNCC_F_CAUSE;
2633 decode_cause(&rel.cause,
2634 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2635 }
2636 /* facility */
2637 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2638 rel.fields |= MNCC_F_FACILITY;
2639 decode_facility(&rel.facility,
2640 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2641 }
2642 /* user-user */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2644 rel.fields |= MNCC_F_USERUSER;
2645 decode_useruser(&rel.useruser,
2646 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2647 }
2648 /* ss-version */
2649 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2650 rel.fields |= MNCC_F_SSVERSION;
2651 decode_ssversion(&rel.ssversion,
2652 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2653 }
2654
Harald Weltedcaf5652009-07-23 18:56:43 +02002655 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002657 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002659 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002660 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002661 GSM48_MT_CC_RELEASE_COMPL);
2662 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 }
2664
2665 new_cc_state(trans, GSM_CSTATE_NULL);
2666
2667 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002668 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669
2670 return rc;
2671}
2672
2673static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2674{
2675 struct gsm_mncc *rel = arg;
2676 struct msgb *msg = gsm48_msgb_alloc();
2677 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2678
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 gh->msg_type = GSM48_MT_CC_RELEASE;
2680
2681 trans->callref = 0;
2682
2683 gsm48_stop_cc_timer(trans);
2684 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2685
2686 /* cause */
2687 if (rel->fields & MNCC_F_CAUSE)
2688 encode_cause(msg, 0, &rel->cause);
2689 /* facility */
2690 if (rel->fields & MNCC_F_FACILITY)
2691 encode_facility(msg, 0, &rel->facility);
2692 /* user-user */
2693 if (rel->fields & MNCC_F_USERUSER)
2694 encode_useruser(msg, 0, &rel->useruser);
2695
Harald Weltedcaf5652009-07-23 18:56:43 +02002696 trans->cc.T308_second = 0;
2697 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698
Harald Weltedcaf5652009-07-23 18:56:43 +02002699 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2701
Harald Welte39e2ead2009-07-23 21:13:03 +02002702 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703}
2704
2705static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2706{
2707 struct gsm48_hdr *gh = msgb_l3(msg);
2708 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2709 struct tlv_parsed tp;
2710 struct gsm_mncc rel;
2711 int rc = 0;
2712
2713 gsm48_stop_cc_timer(trans);
2714
2715 memset(&rel, 0, sizeof(struct gsm_mncc));
2716 rel.callref = trans->callref;
2717 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2718 /* cause */
2719 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2720 rel.fields |= MNCC_F_CAUSE;
2721 decode_cause(&rel.cause,
2722 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2723 }
2724 /* facility */
2725 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2726 rel.fields |= MNCC_F_FACILITY;
2727 decode_facility(&rel.facility,
2728 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2729 }
2730 /* user-user */
2731 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2732 rel.fields |= MNCC_F_USERUSER;
2733 decode_useruser(&rel.useruser,
2734 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2735 }
2736 /* ss-version */
2737 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2738 rel.fields |= MNCC_F_SSVERSION;
2739 decode_ssversion(&rel.ssversion,
2740 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2741 }
2742
2743 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002744 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002746 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 MNCC_REJ_IND, &rel);
2748 break;
2749 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002750 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002752 /* FIXME: in case of multiple calls, we can't simply
2753 * hang up here ! */
2754 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 break;
2756 default:
Harald Welte596fed42009-07-23 19:06:52 +02002757 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758 MNCC_REL_IND, &rel);
2759 }
2760 }
2761
2762 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002763 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764
2765 return rc;
2766}
2767
2768static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *rel = arg;
2771 struct msgb *msg = gsm48_msgb_alloc();
2772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2775
2776 trans->callref = 0;
2777
2778 gsm48_stop_cc_timer(trans);
2779
2780 /* cause */
2781 if (rel->fields & MNCC_F_CAUSE)
2782 encode_cause(msg, 0, &rel->cause);
2783 /* facility */
2784 if (rel->fields & MNCC_F_FACILITY)
2785 encode_facility(msg, 0, &rel->facility);
2786 /* user-user */
2787 if (rel->fields & MNCC_F_USERUSER)
2788 encode_useruser(msg, 0, &rel->useruser);
2789
Harald Weltedcaf5652009-07-23 18:56:43 +02002790 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791
Harald Welte39e2ead2009-07-23 21:13:03 +02002792 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793}
2794
2795static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2796{
2797 struct gsm48_hdr *gh = msgb_l3(msg);
2798 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2799 struct tlv_parsed tp;
2800 struct gsm_mncc fac;
2801
2802 memset(&fac, 0, sizeof(struct gsm_mncc));
2803 fac.callref = trans->callref;
2804 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2805 /* facility */
2806 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2807 fac.fields |= MNCC_F_FACILITY;
2808 decode_facility(&fac.facility,
2809 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2810 }
2811 /* ss-version */
2812 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2813 fac.fields |= MNCC_F_SSVERSION;
2814 decode_ssversion(&fac.ssversion,
2815 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2816 }
2817
Harald Welte596fed42009-07-23 19:06:52 +02002818 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819}
2820
2821static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2822{
2823 struct gsm_mncc *fac = arg;
2824 struct msgb *msg = gsm48_msgb_alloc();
2825 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2826
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 gh->msg_type = GSM48_MT_CC_FACILITY;
2828
2829 /* facility */
2830 encode_facility(msg, 1, &fac->facility);
2831
Harald Welte39e2ead2009-07-23 21:13:03 +02002832 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833}
2834
2835static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2836{
2837 struct gsm_mncc hold;
2838
2839 memset(&hold, 0, sizeof(struct gsm_mncc));
2840 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002841 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842}
2843
2844static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2845{
2846 struct msgb *msg = gsm48_msgb_alloc();
2847 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2848
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2850
Harald Welte39e2ead2009-07-23 21:13:03 +02002851 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852}
2853
2854static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2855{
2856 struct gsm_mncc *hold_rej = arg;
2857 struct msgb *msg = gsm48_msgb_alloc();
2858 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2859
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2861
2862 /* cause */
2863 if (hold_rej->fields & MNCC_F_CAUSE)
2864 encode_cause(msg, 1, &hold_rej->cause);
2865 else
2866 encode_cause(msg, 1, &default_cause);
2867
Harald Welte39e2ead2009-07-23 21:13:03 +02002868 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869}
2870
2871static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2872{
2873 struct gsm_mncc retrieve;
2874
2875 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2876 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002877 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2878 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2882{
2883 struct msgb *msg = gsm48_msgb_alloc();
2884 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2885
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2887
Harald Welte39e2ead2009-07-23 21:13:03 +02002888 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889}
2890
2891static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2892{
2893 struct gsm_mncc *retrieve_rej = arg;
2894 struct msgb *msg = gsm48_msgb_alloc();
2895 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2896
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2898
2899 /* cause */
2900 if (retrieve_rej->fields & MNCC_F_CAUSE)
2901 encode_cause(msg, 1, &retrieve_rej->cause);
2902 else
2903 encode_cause(msg, 1, &default_cause);
2904
Harald Welte39e2ead2009-07-23 21:13:03 +02002905 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906}
2907
2908static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2909{
2910 struct gsm48_hdr *gh = msgb_l3(msg);
2911 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2912 struct tlv_parsed tp;
2913 struct gsm_mncc dtmf;
2914
2915 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2916 dtmf.callref = trans->callref;
2917 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2918 /* keypad facility */
2919 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2920 dtmf.fields |= MNCC_F_KEYPAD;
2921 decode_keypad(&dtmf.keypad,
2922 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2923 }
2924
Harald Welte596fed42009-07-23 19:06:52 +02002925 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926}
2927
2928static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2929{
2930 struct gsm_mncc *dtmf = arg;
2931 struct msgb *msg = gsm48_msgb_alloc();
2932 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2933
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2935
2936 /* keypad */
2937 if (dtmf->fields & MNCC_F_KEYPAD)
2938 encode_keypad(msg, dtmf->keypad);
2939
Harald Welte39e2ead2009-07-23 21:13:03 +02002940 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941}
2942
2943static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2944{
2945 struct gsm_mncc *dtmf = arg;
2946 struct msgb *msg = gsm48_msgb_alloc();
2947 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2948
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2950
2951 /* cause */
2952 if (dtmf->fields & MNCC_F_CAUSE)
2953 encode_cause(msg, 1, &dtmf->cause);
2954 else
2955 encode_cause(msg, 1, &default_cause);
2956
Harald Welte39e2ead2009-07-23 21:13:03 +02002957 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958}
2959
2960static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2961{
2962 struct msgb *msg = gsm48_msgb_alloc();
2963 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2964
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2966
Harald Welte39e2ead2009-07-23 21:13:03 +02002967 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968}
2969
2970static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2971{
2972 struct gsm_mncc dtmf;
2973
2974 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2975 dtmf.callref = trans->callref;
2976
Harald Welte596fed42009-07-23 19:06:52 +02002977 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978}
2979
2980static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2981{
2982 struct gsm48_hdr *gh = msgb_l3(msg);
2983 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2984 struct tlv_parsed tp;
2985 struct gsm_mncc modify;
2986
2987 memset(&modify, 0, sizeof(struct gsm_mncc));
2988 modify.callref = trans->callref;
2989 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2990 /* bearer capability */
2991 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2992 modify.fields |= MNCC_F_BEARER_CAP;
2993 decode_bearer_cap(&modify.bearer_cap,
2994 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2995 }
2996
2997 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2998
Harald Welte596fed42009-07-23 19:06:52 +02002999 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000}
3001
3002static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3003{
3004 struct gsm_mncc *modify = arg;
3005 struct msgb *msg = gsm48_msgb_alloc();
3006 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3007
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 gh->msg_type = GSM48_MT_CC_MODIFY;
3009
3010 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3011
3012 /* bearer capability */
3013 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3014
3015 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3016
Harald Welte39e2ead2009-07-23 21:13:03 +02003017 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018}
3019
3020static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3021{
3022 struct gsm48_hdr *gh = msgb_l3(msg);
3023 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3024 struct tlv_parsed tp;
3025 struct gsm_mncc modify;
3026
3027 gsm48_stop_cc_timer(trans);
3028
3029 memset(&modify, 0, sizeof(struct gsm_mncc));
3030 modify.callref = trans->callref;
3031 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3032 /* bearer capability */
3033 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3034 modify.fields |= MNCC_F_BEARER_CAP;
3035 decode_bearer_cap(&modify.bearer_cap,
3036 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3037 }
3038
3039 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3040
Harald Welte596fed42009-07-23 19:06:52 +02003041 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042}
3043
3044static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3045{
3046 struct gsm_mncc *modify = arg;
3047 struct msgb *msg = gsm48_msgb_alloc();
3048 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3049
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3051
3052 /* bearer capability */
3053 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3054
3055 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3056
Harald Welte39e2ead2009-07-23 21:13:03 +02003057 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058}
3059
3060static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3061{
3062 struct gsm48_hdr *gh = msgb_l3(msg);
3063 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3064 struct tlv_parsed tp;
3065 struct gsm_mncc modify;
3066
3067 gsm48_stop_cc_timer(trans);
3068
3069 memset(&modify, 0, sizeof(struct gsm_mncc));
3070 modify.callref = trans->callref;
3071 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3072 /* bearer capability */
3073 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3074 modify.fields |= GSM48_IE_BEARER_CAP;
3075 decode_bearer_cap(&modify.bearer_cap,
3076 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3077 }
3078 /* cause */
3079 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3080 modify.fields |= MNCC_F_CAUSE;
3081 decode_cause(&modify.cause,
3082 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3083 }
3084
3085 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3086
Harald Welte596fed42009-07-23 19:06:52 +02003087 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088}
3089
3090static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3091{
3092 struct gsm_mncc *modify = arg;
3093 struct msgb *msg = gsm48_msgb_alloc();
3094 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3095
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3097
3098 /* bearer capability */
3099 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3100 /* cause */
3101 encode_cause(msg, 1, &modify->cause);
3102
3103 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3104
Harald Welte39e2ead2009-07-23 21:13:03 +02003105 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106}
3107
3108static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3109{
3110 struct gsm_mncc *notify = arg;
3111 struct msgb *msg = gsm48_msgb_alloc();
3112 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3113
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114 gh->msg_type = GSM48_MT_CC_NOTIFY;
3115
3116 /* notify */
3117 encode_notify(msg, notify->notify);
3118
Harald Welte39e2ead2009-07-23 21:13:03 +02003119 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003120}
3121
3122static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3123{
3124 struct gsm48_hdr *gh = msgb_l3(msg);
3125 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3126// struct tlv_parsed tp;
3127 struct gsm_mncc notify;
3128
3129 memset(&notify, 0, sizeof(struct gsm_mncc));
3130 notify.callref = trans->callref;
3131// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3132 if (payload_len >= 1)
3133 decode_notify(&notify.notify, gh->data);
3134
Harald Welte596fed42009-07-23 19:06:52 +02003135 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136}
3137
3138static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3139{
3140 struct gsm_mncc *user = arg;
3141 struct msgb *msg = gsm48_msgb_alloc();
3142 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3143
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 gh->msg_type = GSM48_MT_CC_USER_INFO;
3145
3146 /* user-user */
3147 if (user->fields & MNCC_F_USERUSER)
3148 encode_useruser(msg, 1, &user->useruser);
3149 /* more data */
3150 if (user->more)
3151 encode_more(msg);
3152
Harald Welte39e2ead2009-07-23 21:13:03 +02003153 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154}
3155
3156static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3157{
3158 struct gsm48_hdr *gh = msgb_l3(msg);
3159 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3160 struct tlv_parsed tp;
3161 struct gsm_mncc user;
3162
3163 memset(&user, 0, sizeof(struct gsm_mncc));
3164 user.callref = trans->callref;
3165 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3166 /* user-user */
3167 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3168 user.fields |= MNCC_F_USERUSER;
3169 decode_useruser(&user.useruser,
3170 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3171 }
3172 /* more data */
3173 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3174 user.more = 1;
3175
Harald Welte596fed42009-07-23 19:06:52 +02003176 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177}
3178
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003179static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180{
3181 struct gsm_mncc *mode = arg;
3182
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003183 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003184}
3185
3186static struct downstate {
3187 u_int32_t states;
3188 int type;
3189 int (*rout) (struct gsm_trans *trans, void *arg);
3190} downstatelist[] = {
3191 /* mobile originating call establishment */
3192 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3193 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3194 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3195 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3196 {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 */
3197 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3198 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3199 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3200 /* mobile terminating call establishment */
3201 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3202 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3203 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3204 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3205 /* signalling during call */
3206 {SBIT(GSM_CSTATE_ACTIVE),
3207 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3208 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3209 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3210 {ALL_STATES,
3211 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3212 {ALL_STATES,
3213 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3214 {ALL_STATES,
3215 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3218 {SBIT(GSM_CSTATE_ACTIVE),
3219 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3220 {SBIT(GSM_CSTATE_ACTIVE),
3221 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3222 {SBIT(GSM_CSTATE_ACTIVE),
3223 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3224 {SBIT(GSM_CSTATE_ACTIVE),
3225 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3226 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3227 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3228 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3229 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3230 {SBIT(GSM_CSTATE_ACTIVE),
3231 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3232 /* clearing */
3233 {SBIT(GSM_CSTATE_INITIATED),
3234 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3235 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3236 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3237 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3238 MNCC_REL_REQ, gsm48_cc_tx_release},
3239 /* special */
3240 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003241 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003242};
3243
3244#define DOWNSLLEN \
3245 (sizeof(downstatelist) / sizeof(struct downstate))
3246
3247
3248int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3249{
Harald Welte1a6f7982009-08-09 18:52:33 +02003250 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003252 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003254 struct gsm_mncc *data = arg, rel;
3255
3256 /* handle special messages */
3257 switch(msg_type) {
3258 case MNCC_BRIDGE:
3259 return tch_bridge(net, arg);
3260 case MNCC_FRAME_DROP:
3261 return tch_recv(net, arg, 0);
3262 case MNCC_FRAME_RECV:
3263 return tch_recv(net, arg, 1);
3264 case GSM_TRAU_FRAME:
3265 return tch_frame(net, arg);
3266 }
3267
3268 memset(&rel, 0, sizeof(struct gsm_mncc));
3269 rel.callref = data->callref;
3270
3271 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003272 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273
3274 /* Callref unknown */
3275 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003276 struct gsm_subscriber *subscr;
3277
Harald Welte4a3464c2009-07-04 10:11:24 +02003278 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3280 "Received '%s' from MNCC with "
3281 "unknown callref %d\n", data->called.number,
3282 get_mncc_name(msg_type), data->callref);
3283 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003284 return mncc_release_ind(net, NULL, data->callref,
3285 GSM48_CAUSE_LOC_PRN_S_LU,
3286 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003288 if (!data->called.number[0] && !data->imsi[0]) {
3289 DEBUGP(DCC, "(bts - trx - ts - ti) "
3290 "Received '%s' from MNCC with "
3291 "no number or IMSI\n", get_mncc_name(msg_type));
3292 /* Invalid number */
3293 return mncc_release_ind(net, NULL, data->callref,
3294 GSM48_CAUSE_LOC_PRN_S_LU,
3295 GSM48_CC_CAUSE_INV_NR_FORMAT);
3296 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003298 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003299 subscr = subscr_get_by_extension(net,
3300 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003301 else
Harald Welte9176bd42009-07-23 18:46:00 +02003302 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 /* If subscriber is not found */
3304 if (!subscr) {
3305 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3306 "Received '%s' from MNCC with "
3307 "unknown subscriber %s\n", data->called.number,
3308 get_mncc_name(msg_type), data->called.number);
3309 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003310 return mncc_release_ind(net, NULL, data->callref,
3311 GSM48_CAUSE_LOC_PRN_S_LU,
3312 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 }
3314 /* If subscriber is not "attached" */
3315 if (!subscr->lac) {
3316 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3317 "Received '%s' from MNCC with "
3318 "detached subscriber %s\n", data->called.number,
3319 get_mncc_name(msg_type), data->called.number);
3320 subscr_put(subscr);
3321 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003322 return mncc_release_ind(net, NULL, data->callref,
3323 GSM48_CAUSE_LOC_PRN_S_LU,
3324 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003325 }
3326 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003327 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3328 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 DEBUGP(DCC, "No memory for trans.\n");
3330 subscr_put(subscr);
3331 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003332 mncc_release_ind(net, NULL, data->callref,
3333 GSM48_CAUSE_LOC_PRN_S_LU,
3334 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 return -ENOMEM;
3336 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003337 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003338 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003339 /* If subscriber has no lchan */
3340 if (!lchan) {
3341 /* find transaction with this subscriber already paging */
3342 llist_for_each_entry(transt, &net->trans_list, entry) {
3343 /* Transaction of our lchan? */
3344 if (transt == trans ||
3345 transt->subscr != subscr)
3346 continue;
3347 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3348 "Received '%s' from MNCC with "
3349 "unallocated channel, paging already "
3350 "started.\n", bts->nr,
3351 data->called.number,
3352 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003353 subscr_put(subscr);
3354 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 return 0;
3356 }
3357 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003358 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003359 /* Trigger paging */
3360 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3361 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003362 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003363 return 0;
3364 }
3365 /* Assign lchan */
3366 trans->lchan = lchan;
3367 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003368 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 }
3370 lchan = trans->lchan;
3371
3372 /* if paging did not respond yet */
3373 if (!lchan) {
3374 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3375 "Received '%s' from MNCC in paging state\n",
3376 (trans->subscr)?(trans->subscr->extension):"-",
3377 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003378 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3379 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003380 if (msg_type == MNCC_REL_REQ)
3381 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3382 else
3383 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3384 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003385 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003386 return rc;
3387 }
3388
3389 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3390 "Received '%s' from MNCC in state %d (%s)\n",
3391 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3392 trans->transaction_id,
3393 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003394 get_mncc_name(msg_type), trans->cc.state,
3395 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003396
3397 /* Find function for current state and message */
3398 for (i = 0; i < DOWNSLLEN; i++)
3399 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003400 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003401 break;
3402 if (i == DOWNSLLEN) {
3403 DEBUGP(DCC, "Message unhandled at this state.\n");
3404 return 0;
3405 }
3406
3407 rc = downstatelist[i].rout(trans, arg);
3408
3409 return rc;
3410}
3411
3412
3413static struct datastate {
3414 u_int32_t states;
3415 int type;
3416 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3417} datastatelist[] = {
3418 /* mobile originating call establishment */
3419 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3420 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3421 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3422 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3423 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3424 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3425 /* mobile terminating call establishment */
3426 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3427 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3428 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3429 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3430 {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 */
3431 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3432 /* signalling during call */
3433 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3434 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3435 {SBIT(GSM_CSTATE_ACTIVE),
3436 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3437 {ALL_STATES,
3438 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3439 {ALL_STATES,
3440 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3441 {ALL_STATES,
3442 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3443 {SBIT(GSM_CSTATE_ACTIVE),
3444 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3445 {SBIT(GSM_CSTATE_ACTIVE),
3446 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3447 {SBIT(GSM_CSTATE_ACTIVE),
3448 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3449 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3450 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3451 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3452 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3453 {SBIT(GSM_CSTATE_ACTIVE),
3454 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3455 /* clearing */
3456 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3457 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3458 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3459 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3460 {ALL_STATES, /* 5.4.3.4 */
3461 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3462};
3463
3464#define DATASLLEN \
3465 (sizeof(datastatelist) / sizeof(struct datastate))
3466
Harald Welte4bc90a12008-12-27 16:32:52 +00003467static int gsm0408_rcv_cc(struct msgb *msg)
3468{
3469 struct gsm48_hdr *gh = msgb_l3(msg);
3470 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003471 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003473 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003474 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003475
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476 if (msg_type & 0x80) {
3477 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3478 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003479 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480
3481 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003482 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003483
Harald Welte6f5aee02009-07-23 21:21:14 +02003484 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003485 "Received '%s' from MS in state %d (%s)\n",
3486 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3487 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003488 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003489 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003490
3491 /* Create transaction */
3492 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003493 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003494 "creating new trans.\n", transaction_id);
3495 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003496 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3497 transaction_id, new_callref++);
3498 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003499 DEBUGP(DCC, "No memory for trans.\n");
3500 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003501 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003502 GSM48_MT_CC_RELEASE_COMPL);
3503 return -ENOMEM;
3504 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003505 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 trans->lchan = lchan;
3507 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003508 }
3509
3510 /* find function for current state and message */
3511 for (i = 0; i < DATASLLEN; i++)
3512 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003513 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003514 break;
3515 if (i == DATASLLEN) {
3516 DEBUGP(DCC, "Message unhandled at this state.\n");
3517 return 0;
3518 }
3519
3520 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003521
3522 return rc;
3523}
3524
Harald Welte52b1f982008-12-23 20:25:15 +00003525/* 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 +02003526int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003527{
3528 struct gsm48_hdr *gh = msgb_l3(msg);
3529 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003530 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003531
3532 switch (pdisc) {
3533 case GSM48_PDISC_CC:
3534 rc = gsm0408_rcv_cc(msg);
3535 break;
3536 case GSM48_PDISC_MM:
3537 rc = gsm0408_rcv_mm(msg);
3538 break;
3539 case GSM48_PDISC_RR:
3540 rc = gsm0408_rcv_rr(msg);
3541 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003542 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003543 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003544 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003545 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003546 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003547 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003548 pdisc);
3549 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003550 case GSM48_PDISC_NC_SS:
3551 rc = handle_rcv_ussd(msg);
3552 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003553 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003554 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003555 pdisc);
3556 break;
3557 }
3558
3559 return rc;
3560}
Harald Welte8470bf22008-12-25 23:28:35 +00003561
Harald Welte4bfdfe72009-06-10 23:11:52 +08003562/* dequeue messages to layer 4 */
3563int bsc_upqueue(struct gsm_network *net)
3564{
3565 struct gsm_mncc *mncc;
3566 struct msgb *msg;
3567 int work = 0;
3568
3569 if (net)
3570 while ((msg = msgb_dequeue(&net->upqueue))) {
3571 mncc = (struct gsm_mncc *)msg->data;
3572 if (net->mncc_recv)
3573 net->mncc_recv(net, mncc->msg_type, mncc);
3574 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003575 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003576 }
3577
3578 return work;
3579}
Harald Weltedcaf5652009-07-23 18:56:43 +02003580
Harald Welte805f6442009-07-28 18:25:29 +02003581/*
3582 * This will be ran by the linker when loading the DSO. We use it to
3583 * do system initialization, e.g. registration of signal handlers.
3584 */
3585static __attribute__((constructor)) void on_dso_load_0408(void)
3586{
Harald Welte805f6442009-07-28 18:25:29 +02003587 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3588 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3589}