blob: ce93f01302085d9ce9dc2a412a97850270639cfc [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 Welteb83d9382009-12-12 21:00:48 +0100302 /* send MM INFO with network name */
303 rc = gsm48_tx_mm_info(msg->lchan);
304
Harald Welteee5ad162009-08-09 19:07:00 +0200305 /* call subscr_update after putting the loc_upd_acc
306 * in the transmit queue, since S_SUBSCR_ATTACHED might
307 * trigger further action like SMS delivery */
308 subscr_update(lchan->subscr, msg->trx->bts,
309 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100310 /* try to close channel ASAP */
311 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200312 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000313 }
314
315 return 0;
316}
317
Holger Freyther7c19f742009-06-06 13:54:35 +0000318static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
319 void *handler_data, void *signal_data)
320{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800321 struct gsm_trans *trans, *temp;
322
Holger Freyther7c19f742009-06-06 13:54:35 +0000323 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
324 return 0;
325
326 /*
327 * Cancel any outstanding location updating request
328 * operation taking place on the lchan.
329 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200330 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200331 if (!lchan)
332 return 0;
333
Holger Freyther7c19f742009-06-06 13:54:35 +0000334 release_loc_updating_req(lchan);
335
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200337 /* FIXME: this is not neccessarily the right thing to do, we should
338 * only set trans->lchan to NULL and wait for another lchan to be
339 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800340 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
341 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200342 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800343 }
344
Holger Freyther7c19f742009-06-06 13:54:35 +0000345 return 0;
346}
347
Harald Welte09e38af2009-02-16 22:52:23 +0000348static const char bcd_num_digits[] = {
349 '0', '1', '2', '3', '4', '5', '6', '7',
350 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
351};
352
Harald Welte0c389302009-06-10 12:08:54 +0800353/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
354int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
355 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000356{
357 u_int8_t in_len = bcd_lv[0];
358 int i;
359
Harald Welte0c389302009-06-10 12:08:54 +0800360 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000361 /* lower nibble */
362 output_len--;
363 if (output_len <= 1)
364 break;
365 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
366
367 /* higher nibble */
368 output_len--;
369 if (output_len <= 1)
370 break;
371 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
372 }
373 if (output_len >= 1)
374 *output++ = '\0';
375
Harald Welte0c389302009-06-10 12:08:54 +0800376 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000377}
378
379/* convert a single ASCII character to call-control BCD */
380static int asc_to_bcd(const char asc)
381{
382 int i;
383
384 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
385 if (bcd_num_digits[i] == asc)
386 return i;
387 }
388 return -EINVAL;
389}
390
Harald Welte0c389302009-06-10 12:08:54 +0800391/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000392int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800393 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000394{
395 int in_len = strlen(input);
396 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800397 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000398
399 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800400 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000401 if (in_len % 2)
402 bcd_lv[0]++;
403
Harald Welte0c389302009-06-10 12:08:54 +0800404 if (bcd_lv[0] > max_len)
405 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000406
407 for (i = 0; i < in_len; i++) {
408 int rc = asc_to_bcd(input[i]);
409 if (rc < 0)
410 return rc;
411 if (i % 2 == 0)
412 *bcd_cur = rc;
413 else
414 *bcd_cur++ |= (rc << 4);
415 }
416 /* append padding nibble in case of odd length */
417 if (i % 2)
418 *bcd_cur++ |= 0xf0;
419
420 /* return how many bytes we used */
421 return (bcd_cur - bcd_lv);
422}
423
Harald Welte0c389302009-06-10 12:08:54 +0800424/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800425static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800426 const u_int8_t *lv)
427{
428 u_int8_t in_len = lv[0];
429 int i, s;
430
431 if (in_len < 1)
432 return -EINVAL;
433
Harald Welte4bfdfe72009-06-10 23:11:52 +0800434 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800435
436 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800437 bcap->transfer = lv[1] & 0x07;
438 bcap->mode = (lv[1] & 0x08) >> 3;
439 bcap->coding = (lv[1] & 0x10) >> 4;
440 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800441
Harald Welte09b7e7f2009-12-12 21:36:53 +0100442 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
443 i = 1;
444 s = 0;
445 while(!(lv[i] & 0x80)) {
446 i++; /* octet 3a etc */
447 if (in_len < i)
448 return 0;
449 bcap->speech_ver[s++] = lv[i] & 0x0f;
450 bcap->speech_ver[s] = -1; /* end of list */
451 if (i == 2) /* octet 3a */
452 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
453 if (s == 7) /* maximum speech versions + end of list */
454 return 0;
455 }
456 } else {
457 i = 1;
458 while (!(lv[i] & 0x80)) {
459 i++; /* octet 3a etc */
460 if (in_len < i)
461 return 0;
462 /* ignore them */
463 }
464 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800465 }
466
467 return 0;
468}
469
470/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800471static int encode_bearer_cap(struct msgb *msg, int lv_only,
472 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800473{
474 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100475 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800476
Harald Welte4bfdfe72009-06-10 23:11:52 +0800477 lv[1] = bcap->transfer;
478 lv[1] |= bcap->mode << 3;
479 lv[1] |= bcap->coding << 4;
480 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800481
Harald Welte09b7e7f2009-12-12 21:36:53 +0100482 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
483 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
484 i++; /* octet 3a etc */
485 lv[i] = bcap->speech_ver[s];
486 if (i == 2) /* octet 3a */
487 lv[i] |= bcap->speech_ctm << 5;
488 }
489 lv[i] |= 0x80; /* last IE of octet 3 etc */
490 } else {
491 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800492 }
Harald Welte0c389302009-06-10 12:08:54 +0800493
494 lv[0] = i;
495 if (lv_only)
496 msgb_lv_put(msg, lv[0], lv+1);
497 else
498 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
499
500 return 0;
501}
502
503/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800504static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800505{
506 u_int8_t in_len = lv[0];
507
508 if (in_len < 1)
509 return -EINVAL;
510
511 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800512 ccap->dtmf = lv[1] & 0x01;
513 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800514
515 return 0;
516}
517
518/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519static int decode_called(struct gsm_mncc_number *called,
520 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800521{
522 u_int8_t in_len = lv[0];
523
524 if (in_len < 1)
525 return -EINVAL;
526
527 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800528 called->plan = lv[1] & 0x0f;
529 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800530
531 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800532 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800533
534 return 0;
535}
536
537/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800538static int encode_called(struct msgb *msg,
539 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800540{
541 u_int8_t lv[18];
542 int ret;
543
544 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800545 lv[1] = called->plan;
546 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800547
548 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800550 if (ret < 0)
551 return ret;
552
553 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
554
555 return 0;
556}
557
558/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800559static int encode_callerid(struct msgb *msg, int ie,
560 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800561{
562 u_int8_t lv[13];
563 int h_len = 1;
564 int ret;
565
566 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800567 lv[1] = callerid->plan;
568 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800569
Harald Welte4bfdfe72009-06-10 23:11:52 +0800570 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800571 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572 lv[2] = callerid->screen;
573 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800574 lv[2] |= 0x80;
575 h_len++;
576 } else
577 lv[1] |= 0x80;
578
579 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800580 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800581 if (ret < 0)
582 return ret;
583
584 msgb_tlv_put(msg, ie, lv[0], lv+1);
585
586 return 0;
587}
588
589/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800590static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800591 const u_int8_t *lv)
592{
593 u_int8_t in_len = lv[0];
594 int i;
595
596 if (in_len < 2)
597 return -EINVAL;
598
Harald Welte4bfdfe72009-06-10 23:11:52 +0800599 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800600
601 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800602 cause->location = lv[1] & 0x0f;
603 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800604
605 i = 1;
606 if (!(lv[i] & 0x80)) {
607 i++; /* octet 3a */
608 if (in_len < i+1)
609 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800610 cause->rec = 1;
611 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800612
613 }
614 i++;
615
616 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800617 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800618 i++;
619
620 if (in_len < i) /* no diag */
621 return 0;
622
623 if (in_len - (i-1) > 32) /* maximum 32 octets */
624 return 0;
625
626 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627 memcpy(cause->diag, lv + i, in_len - (i-1));
628 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800629
630 return 0;
631}
632
633/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800634static int encode_cause(struct msgb *msg, int lv_only,
635 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800636{
637 u_int8_t lv[32+4];
638 int i;
639
Harald Welte4bfdfe72009-06-10 23:11:52 +0800640 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800641 return -EINVAL;
642
643 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 lv[1] = cause->location;
645 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800646
647 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800648 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800649 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800650 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800651 }
652 lv[i] |= 0x80; /* end of octet 3 */
653
654 /* octet 4 */
655 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800657
658 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800659 if (cause->diag_len) {
660 memcpy(lv + i, cause->diag, cause->diag_len);
661 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800662 }
663
664 lv[0] = i;
665 if (lv_only)
666 msgb_lv_put(msg, lv[0], lv+1);
667 else
668 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
669
670 return 0;
671}
672
673/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674static int encode_calling(struct msgb *msg,
675 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800676{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800677 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800678}
679
680/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800681static int encode_connected(struct msgb *msg,
682 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800683{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800684 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800685}
686
687/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800688static int encode_redirecting(struct msgb *msg,
689 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800690{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800691 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800692}
693
694/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800696 const u_int8_t *lv)
697{
698 u_int8_t in_len = lv[0];
699
700 if (in_len < 1)
701 return -EINVAL;
702
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800704 return -EINVAL;
705
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 memcpy(facility->info, lv+1, in_len);
707 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800708
709 return 0;
710}
711
712/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800713static int encode_facility(struct msgb *msg, int lv_only,
714 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800715{
716 u_int8_t lv[GSM_MAX_FACILITY + 1];
717
Harald Welte4bfdfe72009-06-10 23:11:52 +0800718 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800719 return -EINVAL;
720
Harald Welte4bfdfe72009-06-10 23:11:52 +0800721 memcpy(lv+1, facility->info, facility->len);
722 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800723 if (lv_only)
724 msgb_lv_put(msg, lv[0], lv+1);
725 else
726 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
727
728 return 0;
729}
730
731/* decode 'notify' */
732static int decode_notify(int *notify, const u_int8_t *v)
733{
734 *notify = v[0] & 0x7f;
735
736 return 0;
737}
738
739/* encode 'notify' */
740static int encode_notify(struct msgb *msg, int notify)
741{
742 msgb_v_put(msg, notify | 0x80);
743
744 return 0;
745}
746
747/* encode 'signal' */
748static int encode_signal(struct msgb *msg, int signal)
749{
750 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
751
752 return 0;
753}
754
755/* decode 'keypad' */
756static int decode_keypad(int *keypad, const u_int8_t *lv)
757{
758 u_int8_t in_len = lv[0];
759
760 if (in_len < 1)
761 return -EINVAL;
762
763 *keypad = lv[1] & 0x7f;
764
765 return 0;
766}
767
768/* encode 'keypad' */
769static int encode_keypad(struct msgb *msg, int keypad)
770{
771 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
772
773 return 0;
774}
775
776/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800777static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800778 const u_int8_t *lv)
779{
780 u_int8_t in_len = lv[0];
781
782 if (in_len < 2)
783 return -EINVAL;
784
Harald Welte4bfdfe72009-06-10 23:11:52 +0800785 progress->coding = (lv[1] & 0x60) >> 5;
786 progress->location = lv[1] & 0x0f;
787 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800788
789 return 0;
790}
791
792/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800793static int encode_progress(struct msgb *msg, int lv_only,
794 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800795{
796 u_int8_t lv[3];
797
798 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800799 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
800 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800801 if (lv_only)
802 msgb_lv_put(msg, lv[0], lv+1);
803 else
804 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
805
806 return 0;
807}
808
809/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800810static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800811 const u_int8_t *lv)
812{
813 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800814 char *info = uu->info;
815 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800816 int i;
817
818 if (in_len < 1)
819 return -EINVAL;
820
Harald Welte4bfdfe72009-06-10 23:11:52 +0800821 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800822
823 for (i = 2; i <= in_len; i++) {
824 info_len--;
825 if (info_len <= 1)
826 break;
827 *info++ = lv[i];
828 }
829 if (info_len >= 1)
830 *info++ = '\0';
831
832 return 0;
833}
834
835/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800836static int encode_useruser(struct msgb *msg, int lv_only,
837 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800838{
839 u_int8_t lv[GSM_MAX_USERUSER + 2];
840
Harald Welte4bfdfe72009-06-10 23:11:52 +0800841 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800842 return -EINVAL;
843
Harald Welte4bfdfe72009-06-10 23:11:52 +0800844 lv[0] = 1 + strlen(uu->info);
845 lv[1] = uu->proto;
846 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800847 if (lv_only)
848 msgb_lv_put(msg, lv[0], lv+1);
849 else
850 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
851
852 return 0;
853}
854
855/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800856static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800857 const u_int8_t *lv)
858{
859 u_int8_t in_len = lv[0];
860
Harald Welte4bfdfe72009-06-10 23:11:52 +0800861 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800862 return -EINVAL;
863
Harald Welte4bfdfe72009-06-10 23:11:52 +0800864 memcpy(ssv->info, lv + 1, in_len);
865 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800866
867 return 0;
868}
869
870/* encode 'more data' */
871static int encode_more(struct msgb *msg)
872{
873 u_int8_t *ie;
874
875 ie = msgb_put(msg, 1);
876 ie[0] = GSM48_IE_MORE_DATA;
877
878 return 0;
879}
880
Holger Freyther429e7762008-12-30 13:28:30 +0000881/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000882int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000883{
Harald Welte8470bf22008-12-25 23:28:35 +0000884 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000885 struct gsm48_hdr *gh;
886
Harald Welte8470bf22008-12-25 23:28:35 +0000887 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000888
889 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
890 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000891 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000892 gh->data[0] = cause;
893
Harald Weltedb253af2008-12-30 17:56:55 +0000894 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
895
Harald Welte39e2ead2009-07-23 21:13:03 +0200896 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000897}
898
899/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000900int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000901{
Harald Welte8470bf22008-12-25 23:28:35 +0000902 struct gsm_bts *bts = lchan->ts->trx->bts;
903 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000904 struct gsm48_hdr *gh;
905 struct gsm48_loc_area_id *lai;
906 u_int8_t *mid;
907
Harald Welte8470bf22008-12-25 23:28:35 +0000908 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000909
910 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
911 gh->proto_discr = GSM48_PDISC_MM;
912 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
913
914 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000915 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000916 bts->network->network_code, bts->location_area_code);
917
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200918 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200919 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000920
921 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
922
Harald Welteb83d9382009-12-12 21:00:48 +0100923 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000924}
925
Harald Weltebf5e8df2009-02-03 12:59:45 +0000926/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000927static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
928{
929 struct msgb *msg = gsm48_msgb_alloc();
930 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000931
Harald Welte231ad4f2008-12-27 11:15:38 +0000932 msg->lchan = lchan;
933
934 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
935 gh->proto_discr = GSM48_PDISC_MM;
936 gh->msg_type = GSM48_MT_MM_ID_REQ;
937 gh->data[0] = id_type;
938
Harald Welte39e2ead2009-07-23 21:13:03 +0200939 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000940}
941
Harald Welte231ad4f2008-12-27 11:15:38 +0000942
Harald Weltebf5e8df2009-02-03 12:59:45 +0000943/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000944static int mm_rx_id_resp(struct msgb *msg)
945{
946 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000947 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200948 struct gsm_bts *bts = lchan->ts->trx->bts;
949 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000950 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200951 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000952
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200953 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000954 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000955 mi_type, mi_string);
956
Harald Welte7659de12009-12-13 12:39:18 +0100957 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
958
Harald Welte75a983f2008-12-27 21:34:06 +0000959 switch (mi_type) {
960 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200961 /* look up subscriber based on IMSI, create if not found */
962 if (!lchan->subscr) {
963 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200964 if (!lchan->subscr)
965 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200966 }
Holger Freyther73487a22008-12-31 18:53:57 +0000967 if (lchan->loc_operation)
968 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000969 break;
970 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000971 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000972 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200973 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000974 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200975 db_sync_equipment(&lchan->subscr->equipment);
976 }
Holger Freyther73487a22008-12-31 18:53:57 +0000977 if (lchan->loc_operation)
978 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000979 break;
980 }
Holger Freyther73487a22008-12-31 18:53:57 +0000981
982 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000983 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000984}
985
Harald Welte255539c2008-12-28 02:26:27 +0000986
987static void loc_upd_rej_cb(void *data)
988{
989 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100990 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000991
Holger Freyther73487a22008-12-31 18:53:57 +0000992 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100993 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000994 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000995}
996
Holger Freytherb7193e42008-12-29 17:44:08 +0000997static void schedule_reject(struct gsm_lchan *lchan)
998{
Holger Freyther73487a22008-12-31 18:53:57 +0000999 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1000 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001001 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001002}
1003
Harald Welte2a139372009-02-22 21:14:55 +00001004static const char *lupd_name(u_int8_t type)
1005{
1006 switch (type) {
1007 case GSM48_LUPD_NORMAL:
1008 return "NORMAL";
1009 case GSM48_LUPD_PERIODIC:
1010 return "PEROIDOC";
1011 case GSM48_LUPD_IMSI_ATT:
1012 return "IMSI ATTACH";
1013 default:
1014 return "UNKNOWN";
1015 }
1016}
1017
Harald Weltebf5e8df2009-02-03 12:59:45 +00001018/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001019static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001020{
Harald Welte8470bf22008-12-25 23:28:35 +00001021 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001022 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001023 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001024 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001025 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001026 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001027 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001028 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001029
Harald Welte8470bf22008-12-25 23:28:35 +00001030 lu = (struct gsm48_loc_upd_req *) gh->data;
1031
1032 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001033
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001034 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001035
Harald Weltea0368542009-06-27 02:58:43 +02001036 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001037 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001038
Harald Welte7659de12009-12-13 12:39:18 +01001039 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
1040
Holger Freythereaf04692009-06-06 13:54:44 +00001041 /*
1042 * Pseudo Spoof detection: Just drop a second/concurrent
1043 * location updating request.
1044 */
1045 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001046 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001047 lchan->loc_operation);
1048 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1049 return 0;
1050 }
1051
Holger Freyther73487a22008-12-31 18:53:57 +00001052 allocate_loc_updating_req(lchan);
1053
Harald Welte52b1f982008-12-23 20:25:15 +00001054 switch (mi_type) {
1055 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001056 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001057 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001058 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001059 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001060
Jan Luebbe370b41d2009-08-12 10:19:34 +02001061 /* look up subscriber based on IMSI, create if not found */
1062 subscr = subscr_get_by_imsi(bts->network, mi_string);
1063 if (!subscr) {
1064 subscr = db_create_subscriber(bts->network, mi_string);
1065 }
Harald Welte4b634542008-12-27 01:55:51 +00001066 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001067 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001069 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001070 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001071 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001072
Harald Welte52b1f982008-12-23 20:25:15 +00001073 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001074 subscr = subscr_get_by_tmsi(bts->network,
1075 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001076 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001077 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001078 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001079 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001080 }
1081 break;
1082 case GSM_MI_TYPE_IMEI:
1083 case GSM_MI_TYPE_IMEISV:
1084 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001085 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001086 break;
1087 default:
Harald Weltea0368542009-06-27 02:58:43 +02001088 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001089 break;
1090 }
1091
Harald Welte24516ea2009-07-04 10:18:00 +02001092 /* schedule the reject timer */
1093 schedule_reject(lchan);
1094
Harald Welte4bfdfe72009-06-10 23:11:52 +08001095 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001096 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001097 /* FIXME: request id? close channel? */
1098 return -EINVAL;
1099 }
1100
Harald Welte255539c2008-12-28 02:26:27 +00001101 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001102 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001103
Harald Welte24516ea2009-07-04 10:18:00 +02001104 /* check if we can let the subscriber into our network immediately
1105 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001106 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001107}
1108
Harald Welte4bfdfe72009-06-10 23:11:52 +08001109#if 0
1110static u_int8_t to_bcd8(u_int8_t val)
1111{
1112 return ((val / 10) << 4) | (val % 10);
1113}
1114#endif
1115
Harald Weltedb253af2008-12-30 17:56:55 +00001116/* Section 9.2.15a */
1117int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1118{
1119 struct msgb *msg = gsm48_msgb_alloc();
1120 struct gsm48_hdr *gh;
1121 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001122 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001123 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001124#if 0
1125 time_t cur_t;
1126 struct tm* cur_time;
1127 int tz15min;
1128#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001129
1130 msg->lchan = lchan;
1131
1132 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1133 gh->proto_discr = GSM48_PDISC_MM;
1134 gh->msg_type = GSM48_MT_MM_INFO;
1135
1136 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001137#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001138 name_len = strlen(net->name_long);
1139 /* 10.5.3.5a */
1140 ptr8 = msgb_put(msg, 3);
1141 ptr8[0] = GSM48_IE_NAME_LONG;
1142 ptr8[1] = name_len*2 +1;
1143 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1144
1145 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1146 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001147 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001148
1149 /* FIXME: Use Cell Broadcast, not UCS-2, since
1150 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001151#endif
1152 name_len = (strlen(net->name_long)*7)/8;
1153 name_pad = (8 - strlen(net->name_long)*7)%8;
1154 if (name_pad > 0)
1155 name_len++;
1156 /* 10.5.3.5a */
1157 ptr8 = msgb_put(msg, 3);
1158 ptr8[0] = GSM48_IE_NAME_LONG;
1159 ptr8[1] = name_len +1;
1160 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1161
1162 ptr8 = msgb_put(msg, name_len);
1163 gsm_7bit_encode(ptr8, net->name_long);
1164
Harald Weltedb253af2008-12-30 17:56:55 +00001165 }
1166
1167 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001168#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001169 name_len = strlen(net->name_short);
1170 /* 10.5.3.5a */
1171 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001172 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001173 ptr8[1] = name_len*2 + 1;
1174 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1175
Harald Weltee872cb12009-01-01 00:33:37 +00001176 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001177 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001178 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001179#endif
1180 name_len = (strlen(net->name_short)*7)/8;
1181 name_pad = (8 - strlen(net->name_short)*7)%8;
1182 if (name_pad > 0)
1183 name_len++;
1184 /* 10.5.3.5a */
1185 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1186 ptr8[0] = GSM48_IE_NAME_SHORT;
1187 ptr8[1] = name_len +1;
1188 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1189
1190 ptr8 = msgb_put(msg, name_len);
1191 gsm_7bit_encode(ptr8, net->name_short);
1192
Harald Weltedb253af2008-12-30 17:56:55 +00001193 }
1194
1195#if 0
1196 /* Section 10.5.3.9 */
1197 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001198 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001199 ptr8 = msgb_put(msg, 8);
1200 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1201 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1202 ptr8[2] = to_bcd8(cur_time->tm_mon);
1203 ptr8[3] = to_bcd8(cur_time->tm_mday);
1204 ptr8[4] = to_bcd8(cur_time->tm_hour);
1205 ptr8[5] = to_bcd8(cur_time->tm_min);
1206 ptr8[6] = to_bcd8(cur_time->tm_sec);
1207 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1208 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001209 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001210 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001211 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001212#endif
1213
Daniel Willmanneea93372009-08-13 03:42:07 +02001214 DEBUGP(DMM, "-> MM INFO\n");
1215
Harald Welte39e2ead2009-07-23 21:13:03 +02001216 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001217}
1218
Harald Welte7984d5c2009-08-12 22:56:50 +02001219/* Section 9.2.2 */
1220int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1221{
1222 struct msgb *msg = gsm48_msgb_alloc();
1223 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001224 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001225
1226 DEBUGP(DMM, "-> AUTH REQ\n");
1227
1228 msg->lchan = lchan;
1229 gh->proto_discr = GSM48_PDISC_MM;
1230 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1231
Sylvain Munaut849f5542009-09-27 11:10:17 +02001232 /* Key Sequence: FIXME fixed to 0 */
1233 ar->key_seq = 0;
1234
Harald Welte7984d5c2009-08-12 22:56:50 +02001235 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001236 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001237 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001238
1239 return gsm48_sendmsg(msg, NULL);
1240}
1241
1242/* Section 9.2.1 */
1243int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1244{
1245 DEBUGP(DMM, "-> AUTH REJECT\n");
1246 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1247}
1248
Harald Welte4b634542008-12-27 01:55:51 +00001249static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1250{
Harald Welte4b634542008-12-27 01:55:51 +00001251 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001252 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001253}
Harald Welteba4cf162009-01-10 01:49:35 +00001254
1255/* 9.2.6 CM service reject */
1256static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1257 enum gsm48_reject_value value)
1258{
1259 struct msgb *msg = gsm48_msgb_alloc();
1260 struct gsm48_hdr *gh;
1261
1262 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1263
1264 msg->lchan = lchan;
1265 use_lchan(lchan);
1266
1267 gh->proto_discr = GSM48_PDISC_MM;
1268 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1269 gh->data[0] = value;
1270 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1271
Harald Welte39e2ead2009-07-23 21:13:03 +02001272 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001273}
1274
Harald Welte4ed0e922009-01-10 03:17:30 +00001275/*
1276 * Handle CM Service Requests
1277 * a) Verify that the packet is long enough to contain the information
1278 * we require otherwsie reject with INCORRECT_MESSAGE
1279 * b) Try to parse the TMSI. If we do not have one reject
1280 * c) Check that we know the subscriber with the TMSI otherwise reject
1281 * with a HLR cause
1282 * d) Set the subscriber on the gsm_lchan and accept
1283 */
Harald Welte4b634542008-12-27 01:55:51 +00001284static int gsm48_rx_mm_serv_req(struct msgb *msg)
1285{
Harald Welteba4cf162009-01-10 01:49:35 +00001286 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001287 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001288
Harald Welte9176bd42009-07-23 18:46:00 +02001289 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001290 struct gsm_subscriber *subscr;
1291 struct gsm48_hdr *gh = msgb_l3(msg);
1292 struct gsm48_service_request *req =
1293 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001294 /* unfortunately in Phase1 the classmar2 length is variable */
1295 u_int8_t classmark2_len = gh->data[1];
1296 u_int8_t *classmark2 = gh->data+2;
1297 u_int8_t mi_len = *(classmark2 + classmark2_len);
1298 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001299
Harald Weltec9e02182009-05-01 19:07:53 +00001300 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001301 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001302 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001303 return gsm48_tx_mm_serv_rej(msg->lchan,
1304 GSM48_REJECT_INCORRECT_MESSAGE);
1305 }
1306
1307 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001308 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001309 return gsm48_tx_mm_serv_rej(msg->lchan,
1310 GSM48_REJECT_INCORRECT_MESSAGE);
1311 }
1312
Harald Weltec9e02182009-05-01 19:07:53 +00001313 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001314 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001315 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001316 return gsm48_tx_mm_serv_rej(msg->lchan,
1317 GSM48_REJECT_INCORRECT_MESSAGE);
1318 }
1319
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001320 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001321 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001322 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001323
Harald Welte7659de12009-12-13 12:39:18 +01001324 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1325
Harald Welte3ac7f102009-08-10 10:12:45 +02001326 if (is_siemens_bts(bts))
1327 send_siemens_mrpci(msg->lchan, classmark2-1);
1328
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001329 subscr = subscr_get_by_tmsi(bts->network,
1330 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001331
Harald Welte2a139372009-02-22 21:14:55 +00001332 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001333 if (!subscr)
1334 return gsm48_tx_mm_serv_rej(msg->lchan,
1335 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1336
1337 if (!msg->lchan->subscr)
1338 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001339 else if (msg->lchan->subscr != subscr) {
1340 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1341 subscr_put(subscr);
1342 }
1343
Harald Weltec2e302d2009-07-05 14:08:13 +02001344 subscr->equipment.classmark2_len = classmark2_len;
1345 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1346 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001347
Harald Welte4b634542008-12-27 01:55:51 +00001348 return gsm48_tx_mm_serv_ack(msg->lchan);
1349}
1350
Harald Welte2a139372009-02-22 21:14:55 +00001351static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1352{
Harald Welte9176bd42009-07-23 18:46:00 +02001353 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001354 struct gsm48_hdr *gh = msgb_l3(msg);
1355 struct gsm48_imsi_detach_ind *idi =
1356 (struct gsm48_imsi_detach_ind *) gh->data;
1357 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001358 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001360
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001361 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001362 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1363 mi_type, mi_string);
1364
1365 switch (mi_type) {
1366 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001367 subscr = subscr_get_by_tmsi(bts->network,
1368 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001369 break;
1370 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001371 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001372 break;
1373 case GSM_MI_TYPE_IMEI:
1374 case GSM_MI_TYPE_IMEISV:
1375 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001376 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001377 break;
1378 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001379 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001380 break;
1381 }
1382
Holger Freyther4a49e772009-04-12 05:37:29 +00001383 if (subscr) {
1384 subscr_update(subscr, msg->trx->bts,
1385 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001386 DEBUGP(DMM, "Subscriber: %s\n",
1387 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001388
1389 subscr->equipment.classmark1 = idi->classmark1;
1390 db_sync_equipment(&subscr->equipment);
1391
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001392 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001393 } else
Harald Welte2a139372009-02-22 21:14:55 +00001394 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1395
Harald Welteb83d9382009-12-12 21:00:48 +01001396 /* subscriber is detached: should we release lchan? */
1397
Harald Welte2a139372009-02-22 21:14:55 +00001398 return 0;
1399}
1400
Harald Welted2a7f5a2009-06-05 20:08:20 +00001401static int gsm48_rx_mm_status(struct msgb *msg)
1402{
1403 struct gsm48_hdr *gh = msgb_l3(msg);
1404
1405 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1406
1407 return 0;
1408}
1409
Harald Weltebf5e8df2009-02-03 12:59:45 +00001410/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001411static int gsm0408_rcv_mm(struct msgb *msg)
1412{
1413 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001415
1416 switch (gh->msg_type & 0xbf) {
1417 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001418 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001419 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001420 break;
1421 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001422 rc = mm_rx_id_resp(msg);
1423 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001424 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001425 rc = gsm48_rx_mm_serv_req(msg);
1426 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001427 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001428 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001429 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001430 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001431 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1432 msg->lchan->subscr ?
1433 msg->lchan->subscr->imsi :
1434 "unknown subscriber");
1435 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001436 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001437 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1438 break;
1439 case GSM48_MT_MM_CM_REEST_REQ:
1440 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1441 break;
1442 case GSM48_MT_MM_AUTH_RESP:
1443 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001444 break;
1445 default:
1446 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1447 gh->msg_type);
1448 break;
1449 }
1450
1451 return rc;
1452}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001453
Harald Welte2d35ae62009-02-06 12:02:13 +00001454/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001455static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001456{
Harald Welte9176bd42009-07-23 18:46:00 +02001457 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001458 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001459 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001460 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001461 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001463 int rc = 0;
1464
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001465 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001466 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1467 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001468
Harald Weltefe18d8f2009-02-22 21:14:24 +00001469 switch (mi_type) {
1470 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001471 subscr = subscr_get_by_tmsi(bts->network,
1472 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001473 break;
1474 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001475 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001476 break;
1477 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001478
1479 if (!subscr) {
1480 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001481 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001482 return -EINVAL;
1483 }
1484 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001485 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001486
Harald Weltec2e302d2009-07-05 14:08:13 +02001487 subscr->equipment.classmark2_len = *classmark2_lv;
1488 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1489 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001490
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001491 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001492 return rc;
1493}
1494
Harald Weltef7c43522009-06-09 20:24:21 +00001495static int gsm48_rx_rr_classmark(struct msgb *msg)
1496{
1497 struct gsm48_hdr *gh = msgb_l3(msg);
1498 struct gsm_subscriber *subscr = msg->lchan->subscr;
1499 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1500 u_int8_t cm2_len, cm3_len = 0;
1501 u_int8_t *cm2, *cm3 = NULL;
1502
1503 DEBUGP(DRR, "CLASSMARK CHANGE ");
1504
1505 /* classmark 2 */
1506 cm2_len = gh->data[0];
1507 cm2 = &gh->data[1];
1508 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1509
1510 if (payload_len > cm2_len + 1) {
1511 /* we must have a classmark3 */
1512 if (gh->data[cm2_len+1] != 0x20) {
1513 DEBUGPC(DRR, "ERR CM3 TAG\n");
1514 return -EINVAL;
1515 }
1516 if (cm2_len > 3) {
1517 DEBUGPC(DRR, "CM2 too long!\n");
1518 return -EINVAL;
1519 }
1520
1521 cm3_len = gh->data[cm2_len+2];
1522 cm3 = &gh->data[cm2_len+3];
1523 if (cm3_len > 14) {
1524 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1525 return -EINVAL;
1526 }
1527 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1528 }
1529 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001530 subscr->equipment.classmark2_len = cm2_len;
1531 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001532 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001533 subscr->equipment.classmark3_len = cm3_len;
1534 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001535 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001536 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001537 }
1538
Harald Weltef7c43522009-06-09 20:24:21 +00001539 return 0;
1540}
1541
Harald Weltecf5b3592009-05-01 18:28:42 +00001542static int gsm48_rx_rr_status(struct msgb *msg)
1543{
1544 struct gsm48_hdr *gh = msgb_l3(msg);
1545
1546 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1547 rr_cause_name(gh->data[0]));
1548
1549 return 0;
1550}
1551
Harald Weltef7c43522009-06-09 20:24:21 +00001552static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1553{
Harald Weltef7c43522009-06-09 20:24:21 +00001554 static struct gsm_meas_rep meas_rep;
1555
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001556 /* This shouldn't actually end up here, as RSL treats
1557 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1558 * directly into gsm48_parse_meas_rep */
1559 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1560 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001561
1562 return 0;
1563}
1564
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001565static int gsm48_rx_rr_app_info(struct msgb *msg)
1566{
1567 struct gsm48_hdr *gh = msgb_l3(msg);
1568 u_int8_t apdu_id_flags;
1569 u_int8_t apdu_len;
1570 u_int8_t *apdu_data;
1571
1572 apdu_id_flags = gh->data[0];
1573 apdu_len = gh->data[1];
1574 apdu_data = gh->data+2;
1575
1576 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1577 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1578
Harald Welte (local)026531e2009-08-16 10:40:10 +02001579 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001580}
1581
Harald Welted011e8b2009-11-29 22:45:52 +01001582/* Chapter 9.1.16 Handover complete */
1583static gsm48_rx_rr_ho_compl(struct msgb *msg)
1584{
1585 struct gsm48_hdr *gh = msgb_l3(msg);
1586
1587 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1588 rr_cause_name(gh->data[0]));
1589
1590 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1591 /* FIXME: release old channel */
1592
1593 return 0;
1594}
1595
1596/* Chapter 9.1.17 Handover Failure */
1597static gsm48_rx_rr_ho_fail(struct msgb *msg)
1598{
1599 struct gsm48_hdr *gh = msgb_l3(msg);
1600
1601 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1602 rr_cause_name(gh->data[0]));
1603
1604 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1605 /* FIXME: release allocated new channel */
1606
1607 return 0;
1608}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001609
Harald Weltebf5e8df2009-02-03 12:59:45 +00001610/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001611static int gsm0408_rcv_rr(struct msgb *msg)
1612{
1613 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001614 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001615
1616 switch (gh->msg_type) {
1617 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001618 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001619 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001620 case GSM48_MT_RR_GPRS_SUSP_REQ:
1621 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1622 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001623 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001624 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001625 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001626 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001627 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001628 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001629 case GSM48_MT_RR_STATUS:
1630 rc = gsm48_rx_rr_status(msg);
1631 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001632 case GSM48_MT_RR_MEAS_REP:
1633 rc = gsm48_rx_rr_meas_rep(msg);
1634 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001635 case GSM48_MT_RR_APP_INFO:
1636 rc = gsm48_rx_rr_app_info(msg);
1637 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001638 case GSM48_MT_RR_CIPH_M_COMPL:
1639 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1640 /* FIXME: check for MI (if any) */
1641 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001642 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001643 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001644 break;
1645 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001646 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001647 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001648 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001649 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001650 gh->msg_type);
1651 break;
1652 }
1653
Harald Welte2d35ae62009-02-06 12:02:13 +00001654 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001655}
1656
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001657int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001658 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001659{
1660 struct msgb *msg = gsm48_msgb_alloc();
1661 struct gsm48_hdr *gh;
1662
1663 msg->lchan = lchan;
1664
1665 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1666 apdu_id, apdu_len);
1667
1668 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1669 gh->proto_discr = GSM48_PDISC_RR;
1670 gh->msg_type = GSM48_MT_RR_APP_INFO;
1671 gh->data[0] = apdu_id;
1672 gh->data[1] = apdu_len;
1673 memcpy(gh->data+2, apdu, apdu_len);
1674
1675 return gsm48_sendmsg(msg, NULL);
1676}
1677
Harald Welte4bc90a12008-12-27 16:32:52 +00001678/* Call Control */
1679
Harald Welte7584aea2009-02-11 11:44:12 +00001680/* The entire call control code is written in accordance with Figure 7.10c
1681 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1682 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1683 * it for voice */
1684
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685static void new_cc_state(struct gsm_trans *trans, int state)
1686{
1687 if (state > 31 || state < 0)
1688 return;
1689
1690 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001691 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694}
1695
1696static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001697{
1698 struct msgb *msg = gsm48_msgb_alloc();
1699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1700 u_int8_t *cause, *call_state;
1701
Harald Welte4bc90a12008-12-27 16:32:52 +00001702 gh->msg_type = GSM48_MT_CC_STATUS;
1703
1704 cause = msgb_put(msg, 3);
1705 cause[0] = 2;
1706 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1707 cause[2] = 0x80 | 30; /* response to status inquiry */
1708
1709 call_state = msgb_put(msg, 1);
1710 call_state[0] = 0xc0 | 0x00;
1711
Harald Welte39e2ead2009-07-23 21:13:03 +02001712 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001713}
1714
Harald Welte6f4b7532008-12-29 00:39:37 +00001715static int gsm48_tx_simple(struct gsm_lchan *lchan,
1716 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001717{
1718 struct msgb *msg = gsm48_msgb_alloc();
1719 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1720
1721 msg->lchan = lchan;
1722
Harald Welte6f4b7532008-12-29 00:39:37 +00001723 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001724 gh->msg_type = msg_type;
1725
Harald Welte39e2ead2009-07-23 21:13:03 +02001726 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001727}
1728
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1730{
Harald Weltedcaf5652009-07-23 18:56:43 +02001731 if (bsc_timer_pending(&trans->cc.timer)) {
1732 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1733 bsc_del_timer(&trans->cc.timer);
1734 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 }
1736}
1737
1738static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1739 int msg_type, struct gsm_mncc *mncc)
1740{
1741 struct msgb *msg;
1742
1743 if (trans)
1744 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001745 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 "Sending '%s' to MNCC.\n",
1747 trans->lchan->ts->trx->bts->nr,
1748 trans->lchan->ts->trx->nr,
1749 trans->lchan->ts->nr, trans->transaction_id,
1750 (trans->subscr)?(trans->subscr->extension):"-",
1751 get_mncc_name(msg_type));
1752 else
1753 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1754 "Sending '%s' to MNCC.\n",
1755 (trans->subscr)?(trans->subscr->extension):"-",
1756 get_mncc_name(msg_type));
1757 else
1758 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1759 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1760
1761 mncc->msg_type = msg_type;
1762
Harald Welte966636f2009-06-26 19:39:35 +02001763 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 if (!msg)
1765 return -ENOMEM;
1766 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1767 msgb_enqueue(&net->upqueue, msg);
1768
1769 return 0;
1770}
1771
1772int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1773 u_int32_t callref, int location, int value)
1774{
1775 struct gsm_mncc rel;
1776
Harald Welte92f70c52009-06-12 01:54:08 +08001777 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001779 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1781}
1782
Harald Weltedcaf5652009-07-23 18:56:43 +02001783/* Call Control Specific transaction release.
1784 * gets called by trans_free, DO NOT CALL YOURSELF! */
1785void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 gsm48_stop_cc_timer(trans);
1788
1789 /* send release to L4, if callref still exists */
1790 if (trans->callref) {
1791 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001792 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001793 GSM48_CAUSE_LOC_PRN_S_LU,
1794 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001796 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001798 if (trans->lchan)
1799 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800}
1801
1802static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1803
Harald Welte09e38af2009-02-16 22:52:23 +00001804/* call-back from paging the B-end of the connection */
1805static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001806 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001807{
Harald Welte7ccf7782009-02-17 01:43:01 +00001808 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 struct gsm_subscriber *subscr = param;
1810 struct gsm_trans *transt, *tmp;
1811 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001812
Harald Welte09e38af2009-02-16 22:52:23 +00001813 if (hooknum != GSM_HOOK_RR_PAGING)
1814 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815
1816 if (!subscr)
1817 return -EINVAL;
1818 net = subscr->net;
1819 if (!net) {
1820 DEBUGP(DCC, "Error Network not set!\n");
1821 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001822 }
Harald Welte7584aea2009-02-11 11:44:12 +00001823
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 /* check all tranactions (without lchan) for subscriber */
1825 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1826 if (transt->subscr != subscr || transt->lchan)
1827 continue;
1828 switch (event) {
1829 case GSM_PAGING_SUCCEEDED:
1830 if (!lchan) // paranoid
1831 break;
1832 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1833 subscr->extension);
1834 /* Assign lchan */
1835 if (!transt->lchan) {
1836 transt->lchan = lchan;
1837 use_lchan(lchan);
1838 }
1839 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001840 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 break;
1842 case GSM_PAGING_EXPIRED:
1843 DEBUGP(DCC, "Paging subscr %s expired!\n",
1844 subscr->extension);
1845 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001846 mncc_release_ind(transt->subscr->net, transt,
1847 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001848 GSM48_CAUSE_LOC_PRN_S_LU,
1849 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001851 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 break;
1853 }
1854 }
Harald Welte09e38af2009-02-16 22:52:23 +00001855 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001856}
Harald Welte7584aea2009-02-11 11:44:12 +00001857
Harald Welte805f6442009-07-28 18:25:29 +02001858/* some other part of the code sends us a signal */
1859static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1860 void *handler_data, void *signal_data)
1861{
1862 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001863 int rc;
1864
1865 if (subsys != SS_ABISIP)
1866 return 0;
1867
1868 /* in case we use direct BTS-to-BTS RTP */
1869 if (ipacc_rtp_direct)
1870 return 0;
1871
Harald Welte805f6442009-07-28 18:25:29 +02001872 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001873 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001874 /* the BTS has successfully bound a TCH to a local ip/port,
1875 * which means we can connect our UDP socket to it */
Harald Welte2c828992009-12-02 01:56:49 +05301876 if (lchan->abis_ip.rtp_socket) {
1877 rtp_socket_free(lchan->abis_ip.rtp_socket);
1878 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001879 }
1880
Harald Welte2c828992009-12-02 01:56:49 +05301881 lchan->abis_ip.rtp_socket = rtp_socket_create();
1882 if (!lchan->abis_ip.rtp_socket)
Harald Welte805f6442009-07-28 18:25:29 +02001883 goto out_err;
1884
Harald Welte2c828992009-12-02 01:56:49 +05301885 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1886 lchan->abis_ip.bound_ip,
1887 lchan->abis_ip.bound_port);
Harald Welte805f6442009-07-28 18:25:29 +02001888 if (rc < 0)
1889 goto out_err;
1890 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001891 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001892 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte2c828992009-12-02 01:56:49 +05301893 if (lchan->abis_ip.rtp_socket) {
1894 rtp_socket_free(lchan->abis_ip.rtp_socket);
1895 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001896 }
1897 break;
1898 }
1899
1900 return 0;
1901out_err:
1902 /* FIXME: do something */
1903 return 0;
1904}
1905
1906/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1907static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1908{
Harald Welte2c828992009-12-02 01:56:49 +05301909 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte805f6442009-07-28 18:25:29 +02001910 int rc;
1911
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001912 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001913 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte2c828992009-12-02 01:56:49 +05301914 lchan->abis_ip.conn_id,
Harald Welte805f6442009-07-28 18:25:29 +02001915 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte2c828992009-12-02 01:56:49 +05301916 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001917
1918 return rc;
1919}
1920
Harald Welte49f48b82009-02-17 15:29:33 +00001921/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001922static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001923{
Harald Welte11fa29c2009-02-19 17:24:39 +00001924 struct gsm_bts *bts = lchan->ts->trx->bts;
1925 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001926 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001927
Harald Welte11fa29c2009-02-19 17:24:39 +00001928 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1929 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1930 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1931
1932 if (bts->type != remote_bts->type) {
1933 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1934 return -EINVAL;
1935 }
Harald Welte49f48b82009-02-17 15:29:33 +00001936
Harald Welte11fa29c2009-02-19 17:24:39 +00001937 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001938 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001939 if (!ipacc_rtp_direct) {
1940 /* connect the TCH's to our RTP proxy */
1941 rc = ipacc_connect_proxy_bind(lchan);
1942 if (rc < 0)
1943 return rc;
1944 rc = ipacc_connect_proxy_bind(remote_lchan);
1945
1946 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301947 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1948 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001949 } else {
1950 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301951 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1952 remote_lchan->abis_ip.bound_port,
1953 lchan->abis_ip.conn_id,
1954 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001955 if (rc < 0)
1956 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301957 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1958 lchan->abis_ip.bound_port,
1959 remote_lchan->abis_ip.conn_id,
1960 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001961 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001962 break;
1963 case GSM_BTS_TYPE_BS11:
1964 trau_mux_map_lchan(lchan, remote_lchan);
1965 break;
1966 default:
1967 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001968 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001969 break;
1970 }
Harald Welte49f48b82009-02-17 15:29:33 +00001971
1972 return 0;
1973}
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975/* bridge channels of two transactions */
1976static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001977{
Harald Weltedcaf5652009-07-23 18:56:43 +02001978 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1979 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001980
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001982 return -EIO;
1983
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 if (!trans1->lchan || !trans2->lchan)
1985 return -EIO;
1986
1987 /* through-connect channel */
1988 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001989}
1990
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991/* enable receive of channels to upqueue */
1992static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1993{
1994 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001995
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001997 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 if (!trans)
1999 return -EIO;
2000 if (!trans->lchan)
2001 return 0;
2002
2003 // todo IPACCESS
2004 if (enable)
2005 return trau_recv_lchan(trans->lchan, data->callref);
2006 return trau_mux_unmap(NULL, data->callref);
2007}
2008
2009/* send a frame to channel */
2010static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2011{
2012 struct gsm_trans *trans;
2013
2014 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002015 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016 if (!trans)
2017 return -EIO;
2018 if (!trans->lchan)
2019 return 0;
2020 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2021 trans->lchan->type != GSM_LCHAN_TCH_H)
2022 return 0;
2023
2024 // todo IPACCESS
2025 return trau_send_lchan(trans->lchan,
2026 (struct decoded_trau_frame *)frame->data);
2027}
2028
2029
2030static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2031{
2032 DEBUGP(DCC, "-> STATUS ENQ\n");
2033 return gsm48_cc_tx_status(trans, msg);
2034}
2035
2036static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2037static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2038
2039static void gsm48_cc_timeout(void *arg)
2040{
2041 struct gsm_trans *trans = arg;
2042 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002043 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2044 int mo_location = GSM48_CAUSE_LOC_USER;
2045 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2046 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 struct gsm_mncc mo_rel, l4_rel;
2048
2049 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2050 mo_rel.callref = trans->callref;
2051 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2052 l4_rel.callref = trans->callref;
2053
Harald Weltedcaf5652009-07-23 18:56:43 +02002054 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 case 0x303:
2056 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002057 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 break;
2059 case 0x310:
2060 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002061 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 break;
2063 case 0x313:
2064 disconnect = 1;
2065 /* unknown, did not find it in the specs */
2066 break;
2067 case 0x301:
2068 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002069 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 break;
2071 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002072 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002074 gsm48_cc_tx_release(trans, &trans->cc.msg);
2075 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 break; /* stay in release state */
2077 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002078 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 return;
2080// release = 1;
2081// l4_cause = 14;
2082// break;
2083 case 0x306:
2084 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002085 mo_cause = trans->cc.msg.cause.value;
2086 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 break;
2088 case 0x323:
2089 disconnect = 1;
2090 break;
2091 default:
2092 release = 1;
2093 }
2094
2095 if (release && trans->callref) {
2096 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002097 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 l4_location, l4_cause);
2099 trans->callref = 0;
2100 }
2101
2102 if (disconnect && trans->callref) {
2103 /* process disconnect towards layer 4 */
2104 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002105 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 }
2107
2108 /* process disconnect towards mobile station */
2109 if (disconnect || release) {
2110 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002111 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2112 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2113 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 mo_rel.cause.diag_len = 3;
2115
2116 if (disconnect)
2117 gsm48_cc_tx_disconnect(trans, &mo_rel);
2118 if (release)
2119 gsm48_cc_tx_release(trans, &mo_rel);
2120 }
2121
2122}
2123
2124static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2125 int sec, int micro)
2126{
2127 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002128 trans->cc.timer.cb = gsm48_cc_timeout;
2129 trans->cc.timer.data = trans;
2130 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2131 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132}
2133
2134static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2135{
2136 struct gsm48_hdr *gh = msgb_l3(msg);
2137 u_int8_t msg_type = gh->msg_type & 0xbf;
2138 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2139 struct tlv_parsed tp;
2140 struct gsm_mncc setup;
2141
2142 memset(&setup, 0, sizeof(struct gsm_mncc));
2143 setup.callref = trans->callref;
2144 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2145 /* emergency setup is identified by msg_type */
2146 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2147 setup.emergency = 1;
2148
2149 /* use subscriber as calling party number */
2150 if (trans->subscr) {
2151 setup.fields |= MNCC_F_CALLING;
2152 strncpy(setup.calling.number, trans->subscr->extension,
2153 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002154 strncpy(setup.imsi, trans->subscr->imsi,
2155 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 }
2157 /* bearer capability */
2158 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2159 setup.fields |= MNCC_F_BEARER_CAP;
2160 decode_bearer_cap(&setup.bearer_cap,
2161 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2162 }
2163 /* facility */
2164 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2165 setup.fields |= MNCC_F_FACILITY;
2166 decode_facility(&setup.facility,
2167 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2168 }
2169 /* called party bcd number */
2170 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2171 setup.fields |= MNCC_F_CALLED;
2172 decode_called(&setup.called,
2173 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2174 }
2175 /* user-user */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2177 setup.fields |= MNCC_F_USERUSER;
2178 decode_useruser(&setup.useruser,
2179 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2180 }
2181 /* ss-version */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2183 setup.fields |= MNCC_F_SSVERSION;
2184 decode_ssversion(&setup.ssversion,
2185 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2186 }
2187 /* CLIR suppression */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2189 setup.clir.sup = 1;
2190 /* CLIR invocation */
2191 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2192 setup.clir.inv = 1;
2193 /* cc cap */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2195 setup.fields |= MNCC_F_CCCAP;
2196 decode_cccap(&setup.cccap,
2197 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2198 }
2199
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 new_cc_state(trans, GSM_CSTATE_INITIATED);
2201
2202 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002203 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204
Harald Welte13cac662009-07-29 12:10:35 +02002205 /* MNCC code will modify the channel asynchronously, we should
2206 * ipaccess-bind only after the modification has been made to the
2207 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 return 0;
2209}
2210
2211static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002212{
2213 struct msgb *msg = gsm48_msgb_alloc();
2214 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002216 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002217
Harald Welte7ccf7782009-02-17 01:43:01 +00002218 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002219
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 /* transaction id must not be assigned */
2221 if (trans->transaction_id != 0xff) { /* unasssigned */
2222 DEBUGP(DCC, "TX Setup with assigned transaction. "
2223 "This is not allowed!\n");
2224 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002225 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002226 GSM48_CAUSE_LOC_PRN_S_LU,
2227 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002229 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 return rc;
2231 }
2232
2233 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002234 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2235 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002237 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002238 GSM48_CAUSE_LOC_PRN_S_LU,
2239 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002241 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 return rc;
2243 }
Harald Welte78283ef2009-07-23 21:36:44 +02002244 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002245
Harald Welte65e74cc2008-12-29 01:55:35 +00002246 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002247
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002249
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 /* bearer capability */
2251 if (setup->fields & MNCC_F_BEARER_CAP)
2252 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2253 /* facility */
2254 if (setup->fields & MNCC_F_FACILITY)
2255 encode_facility(msg, 0, &setup->facility);
2256 /* progress */
2257 if (setup->fields & MNCC_F_PROGRESS)
2258 encode_progress(msg, 0, &setup->progress);
2259 /* calling party BCD number */
2260 if (setup->fields & MNCC_F_CALLING)
2261 encode_calling(msg, &setup->calling);
2262 /* called party BCD number */
2263 if (setup->fields & MNCC_F_CALLED)
2264 encode_called(msg, &setup->called);
2265 /* user-user */
2266 if (setup->fields & MNCC_F_USERUSER)
2267 encode_useruser(msg, 0, &setup->useruser);
2268 /* redirecting party BCD number */
2269 if (setup->fields & MNCC_F_REDIRECTING)
2270 encode_redirecting(msg, &setup->redirecting);
2271 /* signal */
2272 if (setup->fields & MNCC_F_SIGNAL)
2273 encode_signal(msg, setup->signal);
2274
2275 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002276
Harald Welte39e2ead2009-07-23 21:13:03 +02002277 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002278}
2279
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2281{
2282 struct gsm48_hdr *gh = msgb_l3(msg);
2283 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2284 struct tlv_parsed tp;
2285 struct gsm_mncc call_conf;
2286
2287 gsm48_stop_cc_timer(trans);
2288 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2289
2290 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2291 call_conf.callref = trans->callref;
2292 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2293#if 0
2294 /* repeat */
2295 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2296 call_conf.repeat = 1;
2297 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2298 call_conf.repeat = 2;
2299#endif
2300 /* bearer capability */
2301 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2302 call_conf.fields |= MNCC_F_BEARER_CAP;
2303 decode_bearer_cap(&call_conf.bearer_cap,
2304 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2305 }
2306 /* cause */
2307 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2308 call_conf.fields |= MNCC_F_CAUSE;
2309 decode_cause(&call_conf.cause,
2310 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2311 }
2312 /* cc cap */
2313 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2314 call_conf.fields |= MNCC_F_CCCAP;
2315 decode_cccap(&call_conf.cccap,
2316 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2317 }
2318
2319 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2320
Harald Welte596fed42009-07-23 19:06:52 +02002321 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2322 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323}
2324
2325static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2326{
2327 struct gsm_mncc *proceeding = arg;
2328 struct msgb *msg = gsm48_msgb_alloc();
2329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2332
2333 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2334
2335 /* bearer capability */
2336 if (proceeding->fields & MNCC_F_BEARER_CAP)
2337 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2338 /* facility */
2339 if (proceeding->fields & MNCC_F_FACILITY)
2340 encode_facility(msg, 0, &proceeding->facility);
2341 /* progress */
2342 if (proceeding->fields & MNCC_F_PROGRESS)
2343 encode_progress(msg, 0, &proceeding->progress);
2344
Harald Welte39e2ead2009-07-23 21:13:03 +02002345 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346}
2347
2348static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2349{
2350 struct gsm48_hdr *gh = msgb_l3(msg);
2351 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2352 struct tlv_parsed tp;
2353 struct gsm_mncc alerting;
2354
2355 gsm48_stop_cc_timer(trans);
2356 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2357
2358 memset(&alerting, 0, sizeof(struct gsm_mncc));
2359 alerting.callref = trans->callref;
2360 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2361 /* facility */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2363 alerting.fields |= MNCC_F_FACILITY;
2364 decode_facility(&alerting.facility,
2365 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2366 }
2367
2368 /* progress */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2370 alerting.fields |= MNCC_F_PROGRESS;
2371 decode_progress(&alerting.progress,
2372 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2373 }
2374 /* ss-version */
2375 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2376 alerting.fields |= MNCC_F_SSVERSION;
2377 decode_ssversion(&alerting.ssversion,
2378 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2379 }
2380
2381 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2382
Harald Welte596fed42009-07-23 19:06:52 +02002383 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2384 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385}
2386
2387static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2388{
2389 struct gsm_mncc *alerting = arg;
2390 struct msgb *msg = gsm48_msgb_alloc();
2391 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2392
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393 gh->msg_type = GSM48_MT_CC_ALERTING;
2394
2395 /* facility */
2396 if (alerting->fields & MNCC_F_FACILITY)
2397 encode_facility(msg, 0, &alerting->facility);
2398 /* progress */
2399 if (alerting->fields & MNCC_F_PROGRESS)
2400 encode_progress(msg, 0, &alerting->progress);
2401 /* user-user */
2402 if (alerting->fields & MNCC_F_USERUSER)
2403 encode_useruser(msg, 0, &alerting->useruser);
2404
2405 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2406
Harald Welte39e2ead2009-07-23 21:13:03 +02002407 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408}
2409
2410static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2411{
2412 struct gsm_mncc *progress = arg;
2413 struct msgb *msg = gsm48_msgb_alloc();
2414 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2415
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416 gh->msg_type = GSM48_MT_CC_PROGRESS;
2417
2418 /* progress */
2419 encode_progress(msg, 1, &progress->progress);
2420 /* user-user */
2421 if (progress->fields & MNCC_F_USERUSER)
2422 encode_useruser(msg, 0, &progress->useruser);
2423
Harald Welte39e2ead2009-07-23 21:13:03 +02002424 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *connect = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_CONNECT;
2434
2435 gsm48_stop_cc_timer(trans);
2436 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2437
2438 /* facility */
2439 if (connect->fields & MNCC_F_FACILITY)
2440 encode_facility(msg, 0, &connect->facility);
2441 /* progress */
2442 if (connect->fields & MNCC_F_PROGRESS)
2443 encode_progress(msg, 0, &connect->progress);
2444 /* connected number */
2445 if (connect->fields & MNCC_F_CONNECTED)
2446 encode_connected(msg, &connect->connected);
2447 /* user-user */
2448 if (connect->fields & MNCC_F_USERUSER)
2449 encode_useruser(msg, 0, &connect->useruser);
2450
2451 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2452
Harald Welte39e2ead2009-07-23 21:13:03 +02002453 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm48_hdr *gh = msgb_l3(msg);
2459 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2460 struct tlv_parsed tp;
2461 struct gsm_mncc connect;
2462
2463 gsm48_stop_cc_timer(trans);
2464
2465 memset(&connect, 0, sizeof(struct gsm_mncc));
2466 connect.callref = trans->callref;
2467 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2468 /* use subscriber as connected party number */
2469 if (trans->subscr) {
2470 connect.fields |= MNCC_F_CONNECTED;
2471 strncpy(connect.connected.number, trans->subscr->extension,
2472 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002473 strncpy(connect.imsi, trans->subscr->imsi,
2474 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 }
2476 /* facility */
2477 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2478 connect.fields |= MNCC_F_FACILITY;
2479 decode_facility(&connect.facility,
2480 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2481 }
2482 /* user-user */
2483 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2484 connect.fields |= MNCC_F_USERUSER;
2485 decode_useruser(&connect.useruser,
2486 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2487 }
2488 /* ss-version */
2489 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2490 connect.fields |= MNCC_F_SSVERSION;
2491 decode_ssversion(&connect.ssversion,
2492 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2493 }
2494
2495 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2496
Harald Welte596fed42009-07-23 19:06:52 +02002497 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
2500
2501static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2502{
2503 struct gsm_mncc connect_ack;
2504
2505 gsm48_stop_cc_timer(trans);
2506
2507 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2508
2509 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2510 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002511 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512 &connect_ack);
2513}
2514
2515static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2516{
2517 struct msgb *msg = gsm48_msgb_alloc();
2518 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2519
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2521
2522 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2523
Harald Welte39e2ead2009-07-23 21:13:03 +02002524 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2528{
2529 struct gsm48_hdr *gh = msgb_l3(msg);
2530 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2531 struct tlv_parsed tp;
2532 struct gsm_mncc disc;
2533
2534 gsm48_stop_cc_timer(trans);
2535
2536 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2537
2538 memset(&disc, 0, sizeof(struct gsm_mncc));
2539 disc.callref = trans->callref;
2540 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2541 /* cause */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2543 disc.fields |= MNCC_F_CAUSE;
2544 decode_cause(&disc.cause,
2545 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2546 }
2547 /* facility */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2549 disc.fields |= MNCC_F_FACILITY;
2550 decode_facility(&disc.facility,
2551 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2552 }
2553 /* user-user */
2554 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2555 disc.fields |= MNCC_F_USERUSER;
2556 decode_useruser(&disc.useruser,
2557 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2558 }
2559 /* ss-version */
2560 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2561 disc.fields |= MNCC_F_SSVERSION;
2562 decode_ssversion(&disc.ssversion,
2563 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2564 }
2565
Harald Welte596fed42009-07-23 19:06:52 +02002566 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567
2568}
2569
Harald Weltec66b71c2009-06-11 14:23:20 +08002570static struct gsm_mncc_cause default_cause = {
2571 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2572 .coding = 0,
2573 .rec = 0,
2574 .rec_val = 0,
2575 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2576 .diag_len = 0,
2577 .diag = { 0 },
2578};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579
2580static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2581{
2582 struct gsm_mncc *disc = arg;
2583 struct msgb *msg = gsm48_msgb_alloc();
2584 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2585
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2587
2588 gsm48_stop_cc_timer(trans);
2589 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2590
2591 /* cause */
2592 if (disc->fields & MNCC_F_CAUSE)
2593 encode_cause(msg, 1, &disc->cause);
2594 else
2595 encode_cause(msg, 1, &default_cause);
2596
2597 /* facility */
2598 if (disc->fields & MNCC_F_FACILITY)
2599 encode_facility(msg, 0, &disc->facility);
2600 /* progress */
2601 if (disc->fields & MNCC_F_PROGRESS)
2602 encode_progress(msg, 0, &disc->progress);
2603 /* user-user */
2604 if (disc->fields & MNCC_F_USERUSER)
2605 encode_useruser(msg, 0, &disc->useruser);
2606
2607 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002608 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609
2610 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2611
Harald Welte39e2ead2009-07-23 21:13:03 +02002612 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2616{
2617 struct gsm48_hdr *gh = msgb_l3(msg);
2618 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2619 struct tlv_parsed tp;
2620 struct gsm_mncc rel;
2621 int rc;
2622
2623 gsm48_stop_cc_timer(trans);
2624
2625 memset(&rel, 0, sizeof(struct gsm_mncc));
2626 rel.callref = trans->callref;
2627 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2628 /* cause */
2629 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2630 rel.fields |= MNCC_F_CAUSE;
2631 decode_cause(&rel.cause,
2632 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2633 }
2634 /* facility */
2635 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2636 rel.fields |= MNCC_F_FACILITY;
2637 decode_facility(&rel.facility,
2638 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2639 }
2640 /* user-user */
2641 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2642 rel.fields |= MNCC_F_USERUSER;
2643 decode_useruser(&rel.useruser,
2644 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2645 }
2646 /* ss-version */
2647 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2648 rel.fields |= MNCC_F_SSVERSION;
2649 decode_ssversion(&rel.ssversion,
2650 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2651 }
2652
Harald Weltedcaf5652009-07-23 18:56:43 +02002653 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002655 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002657 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002658 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002659 GSM48_MT_CC_RELEASE_COMPL);
2660 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 }
2662
2663 new_cc_state(trans, GSM_CSTATE_NULL);
2664
2665 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002666 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667
2668 return rc;
2669}
2670
2671static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2672{
2673 struct gsm_mncc *rel = arg;
2674 struct msgb *msg = gsm48_msgb_alloc();
2675 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2676
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 gh->msg_type = GSM48_MT_CC_RELEASE;
2678
2679 trans->callref = 0;
2680
2681 gsm48_stop_cc_timer(trans);
2682 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2683
2684 /* cause */
2685 if (rel->fields & MNCC_F_CAUSE)
2686 encode_cause(msg, 0, &rel->cause);
2687 /* facility */
2688 if (rel->fields & MNCC_F_FACILITY)
2689 encode_facility(msg, 0, &rel->facility);
2690 /* user-user */
2691 if (rel->fields & MNCC_F_USERUSER)
2692 encode_useruser(msg, 0, &rel->useruser);
2693
Harald Weltedcaf5652009-07-23 18:56:43 +02002694 trans->cc.T308_second = 0;
2695 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696
Harald Weltedcaf5652009-07-23 18:56:43 +02002697 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2699
Harald Welte39e2ead2009-07-23 21:13:03 +02002700 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701}
2702
2703static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2704{
2705 struct gsm48_hdr *gh = msgb_l3(msg);
2706 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2707 struct tlv_parsed tp;
2708 struct gsm_mncc rel;
2709 int rc = 0;
2710
2711 gsm48_stop_cc_timer(trans);
2712
2713 memset(&rel, 0, sizeof(struct gsm_mncc));
2714 rel.callref = trans->callref;
2715 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2716 /* cause */
2717 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2718 rel.fields |= MNCC_F_CAUSE;
2719 decode_cause(&rel.cause,
2720 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2721 }
2722 /* facility */
2723 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2724 rel.fields |= MNCC_F_FACILITY;
2725 decode_facility(&rel.facility,
2726 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2727 }
2728 /* user-user */
2729 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2730 rel.fields |= MNCC_F_USERUSER;
2731 decode_useruser(&rel.useruser,
2732 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2733 }
2734 /* ss-version */
2735 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2736 rel.fields |= MNCC_F_SSVERSION;
2737 decode_ssversion(&rel.ssversion,
2738 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2739 }
2740
2741 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002742 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002744 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 MNCC_REJ_IND, &rel);
2746 break;
2747 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002748 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002750 /* FIXME: in case of multiple calls, we can't simply
2751 * hang up here ! */
2752 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 break;
2754 default:
Harald Welte596fed42009-07-23 19:06:52 +02002755 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756 MNCC_REL_IND, &rel);
2757 }
2758 }
2759
2760 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002761 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762
2763 return rc;
2764}
2765
2766static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2767{
2768 struct gsm_mncc *rel = arg;
2769 struct msgb *msg = gsm48_msgb_alloc();
2770 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2771
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2773
2774 trans->callref = 0;
2775
2776 gsm48_stop_cc_timer(trans);
2777
2778 /* cause */
2779 if (rel->fields & MNCC_F_CAUSE)
2780 encode_cause(msg, 0, &rel->cause);
2781 /* facility */
2782 if (rel->fields & MNCC_F_FACILITY)
2783 encode_facility(msg, 0, &rel->facility);
2784 /* user-user */
2785 if (rel->fields & MNCC_F_USERUSER)
2786 encode_useruser(msg, 0, &rel->useruser);
2787
Harald Weltedcaf5652009-07-23 18:56:43 +02002788 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789
Harald Welte39e2ead2009-07-23 21:13:03 +02002790 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791}
2792
2793static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2794{
2795 struct gsm48_hdr *gh = msgb_l3(msg);
2796 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2797 struct tlv_parsed tp;
2798 struct gsm_mncc fac;
2799
2800 memset(&fac, 0, sizeof(struct gsm_mncc));
2801 fac.callref = trans->callref;
2802 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2803 /* facility */
2804 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2805 fac.fields |= MNCC_F_FACILITY;
2806 decode_facility(&fac.facility,
2807 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2808 }
2809 /* ss-version */
2810 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2811 fac.fields |= MNCC_F_SSVERSION;
2812 decode_ssversion(&fac.ssversion,
2813 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2814 }
2815
Harald Welte596fed42009-07-23 19:06:52 +02002816 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817}
2818
2819static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2820{
2821 struct gsm_mncc *fac = arg;
2822 struct msgb *msg = gsm48_msgb_alloc();
2823 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2824
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 gh->msg_type = GSM48_MT_CC_FACILITY;
2826
2827 /* facility */
2828 encode_facility(msg, 1, &fac->facility);
2829
Harald Welte39e2ead2009-07-23 21:13:03 +02002830 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002831}
2832
2833static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2834{
2835 struct gsm_mncc hold;
2836
2837 memset(&hold, 0, sizeof(struct gsm_mncc));
2838 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002839 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840}
2841
2842static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2843{
2844 struct msgb *msg = gsm48_msgb_alloc();
2845 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2846
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2848
Harald Welte39e2ead2009-07-23 21:13:03 +02002849 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850}
2851
2852static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2853{
2854 struct gsm_mncc *hold_rej = arg;
2855 struct msgb *msg = gsm48_msgb_alloc();
2856 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2857
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2859
2860 /* cause */
2861 if (hold_rej->fields & MNCC_F_CAUSE)
2862 encode_cause(msg, 1, &hold_rej->cause);
2863 else
2864 encode_cause(msg, 1, &default_cause);
2865
Harald Welte39e2ead2009-07-23 21:13:03 +02002866 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867}
2868
2869static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2870{
2871 struct gsm_mncc retrieve;
2872
2873 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2874 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002875 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2876 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877}
2878
2879static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2880{
2881 struct msgb *msg = gsm48_msgb_alloc();
2882 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2883
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2885
Harald Welte39e2ead2009-07-23 21:13:03 +02002886 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887}
2888
2889static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2890{
2891 struct gsm_mncc *retrieve_rej = arg;
2892 struct msgb *msg = gsm48_msgb_alloc();
2893 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2894
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2896
2897 /* cause */
2898 if (retrieve_rej->fields & MNCC_F_CAUSE)
2899 encode_cause(msg, 1, &retrieve_rej->cause);
2900 else
2901 encode_cause(msg, 1, &default_cause);
2902
Harald Welte39e2ead2009-07-23 21:13:03 +02002903 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002904}
2905
2906static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2907{
2908 struct gsm48_hdr *gh = msgb_l3(msg);
2909 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2910 struct tlv_parsed tp;
2911 struct gsm_mncc dtmf;
2912
2913 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2914 dtmf.callref = trans->callref;
2915 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2916 /* keypad facility */
2917 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2918 dtmf.fields |= MNCC_F_KEYPAD;
2919 decode_keypad(&dtmf.keypad,
2920 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2921 }
2922
Harald Welte596fed42009-07-23 19:06:52 +02002923 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924}
2925
2926static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2927{
2928 struct gsm_mncc *dtmf = arg;
2929 struct msgb *msg = gsm48_msgb_alloc();
2930 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2931
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2933
2934 /* keypad */
2935 if (dtmf->fields & MNCC_F_KEYPAD)
2936 encode_keypad(msg, dtmf->keypad);
2937
Harald Welte39e2ead2009-07-23 21:13:03 +02002938 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939}
2940
2941static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2942{
2943 struct gsm_mncc *dtmf = arg;
2944 struct msgb *msg = gsm48_msgb_alloc();
2945 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2946
Harald Welte4bfdfe72009-06-10 23:11:52 +08002947 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2948
2949 /* cause */
2950 if (dtmf->fields & MNCC_F_CAUSE)
2951 encode_cause(msg, 1, &dtmf->cause);
2952 else
2953 encode_cause(msg, 1, &default_cause);
2954
Harald Welte39e2ead2009-07-23 21:13:03 +02002955 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956}
2957
2958static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2959{
2960 struct msgb *msg = gsm48_msgb_alloc();
2961 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2962
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2964
Harald Welte39e2ead2009-07-23 21:13:03 +02002965 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966}
2967
2968static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2969{
2970 struct gsm_mncc dtmf;
2971
2972 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2973 dtmf.callref = trans->callref;
2974
Harald Welte596fed42009-07-23 19:06:52 +02002975 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976}
2977
2978static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2979{
2980 struct gsm48_hdr *gh = msgb_l3(msg);
2981 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2982 struct tlv_parsed tp;
2983 struct gsm_mncc modify;
2984
2985 memset(&modify, 0, sizeof(struct gsm_mncc));
2986 modify.callref = trans->callref;
2987 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2988 /* bearer capability */
2989 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2990 modify.fields |= MNCC_F_BEARER_CAP;
2991 decode_bearer_cap(&modify.bearer_cap,
2992 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2993 }
2994
2995 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2996
Harald Welte596fed42009-07-23 19:06:52 +02002997 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002998}
2999
3000static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3001{
3002 struct gsm_mncc *modify = arg;
3003 struct msgb *msg = gsm48_msgb_alloc();
3004 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3005
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 gh->msg_type = GSM48_MT_CC_MODIFY;
3007
3008 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3009
3010 /* bearer capability */
3011 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3012
3013 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3014
Harald Welte39e2ead2009-07-23 21:13:03 +02003015 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016}
3017
3018static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3019{
3020 struct gsm48_hdr *gh = msgb_l3(msg);
3021 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3022 struct tlv_parsed tp;
3023 struct gsm_mncc modify;
3024
3025 gsm48_stop_cc_timer(trans);
3026
3027 memset(&modify, 0, sizeof(struct gsm_mncc));
3028 modify.callref = trans->callref;
3029 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3030 /* bearer capability */
3031 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3032 modify.fields |= MNCC_F_BEARER_CAP;
3033 decode_bearer_cap(&modify.bearer_cap,
3034 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3035 }
3036
3037 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3038
Harald Welte596fed42009-07-23 19:06:52 +02003039 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040}
3041
3042static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3043{
3044 struct gsm_mncc *modify = arg;
3045 struct msgb *msg = gsm48_msgb_alloc();
3046 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3047
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3049
3050 /* bearer capability */
3051 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3052
3053 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3054
Harald Welte39e2ead2009-07-23 21:13:03 +02003055 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056}
3057
3058static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3059{
3060 struct gsm48_hdr *gh = msgb_l3(msg);
3061 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3062 struct tlv_parsed tp;
3063 struct gsm_mncc modify;
3064
3065 gsm48_stop_cc_timer(trans);
3066
3067 memset(&modify, 0, sizeof(struct gsm_mncc));
3068 modify.callref = trans->callref;
3069 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3070 /* bearer capability */
3071 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3072 modify.fields |= GSM48_IE_BEARER_CAP;
3073 decode_bearer_cap(&modify.bearer_cap,
3074 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3075 }
3076 /* cause */
3077 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3078 modify.fields |= MNCC_F_CAUSE;
3079 decode_cause(&modify.cause,
3080 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3081 }
3082
3083 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3084
Harald Welte596fed42009-07-23 19:06:52 +02003085 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086}
3087
3088static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3089{
3090 struct gsm_mncc *modify = arg;
3091 struct msgb *msg = gsm48_msgb_alloc();
3092 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3093
Harald Welte4bfdfe72009-06-10 23:11:52 +08003094 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3095
3096 /* bearer capability */
3097 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3098 /* cause */
3099 encode_cause(msg, 1, &modify->cause);
3100
3101 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3102
Harald Welte39e2ead2009-07-23 21:13:03 +02003103 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104}
3105
3106static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3107{
3108 struct gsm_mncc *notify = arg;
3109 struct msgb *msg = gsm48_msgb_alloc();
3110 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3111
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112 gh->msg_type = GSM48_MT_CC_NOTIFY;
3113
3114 /* notify */
3115 encode_notify(msg, notify->notify);
3116
Harald Welte39e2ead2009-07-23 21:13:03 +02003117 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003118}
3119
3120static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3121{
3122 struct gsm48_hdr *gh = msgb_l3(msg);
3123 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3124// struct tlv_parsed tp;
3125 struct gsm_mncc notify;
3126
3127 memset(&notify, 0, sizeof(struct gsm_mncc));
3128 notify.callref = trans->callref;
3129// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3130 if (payload_len >= 1)
3131 decode_notify(&notify.notify, gh->data);
3132
Harald Welte596fed42009-07-23 19:06:52 +02003133 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134}
3135
3136static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3137{
3138 struct gsm_mncc *user = arg;
3139 struct msgb *msg = gsm48_msgb_alloc();
3140 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3141
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142 gh->msg_type = GSM48_MT_CC_USER_INFO;
3143
3144 /* user-user */
3145 if (user->fields & MNCC_F_USERUSER)
3146 encode_useruser(msg, 1, &user->useruser);
3147 /* more data */
3148 if (user->more)
3149 encode_more(msg);
3150
Harald Welte39e2ead2009-07-23 21:13:03 +02003151 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152}
3153
3154static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3155{
3156 struct gsm48_hdr *gh = msgb_l3(msg);
3157 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3158 struct tlv_parsed tp;
3159 struct gsm_mncc user;
3160
3161 memset(&user, 0, sizeof(struct gsm_mncc));
3162 user.callref = trans->callref;
3163 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3164 /* user-user */
3165 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3166 user.fields |= MNCC_F_USERUSER;
3167 decode_useruser(&user.useruser,
3168 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3169 }
3170 /* more data */
3171 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3172 user.more = 1;
3173
Harald Welte596fed42009-07-23 19:06:52 +02003174 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003175}
3176
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003177static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178{
3179 struct gsm_mncc *mode = arg;
3180
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003181 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003182}
3183
3184static struct downstate {
3185 u_int32_t states;
3186 int type;
3187 int (*rout) (struct gsm_trans *trans, void *arg);
3188} downstatelist[] = {
3189 /* mobile originating call establishment */
3190 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3191 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3192 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3193 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3194 {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 */
3195 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3196 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3197 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3198 /* mobile terminating call establishment */
3199 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3200 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3201 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3202 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3203 /* signalling during call */
3204 {SBIT(GSM_CSTATE_ACTIVE),
3205 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3206 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3207 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3208 {ALL_STATES,
3209 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3210 {ALL_STATES,
3211 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3212 {ALL_STATES,
3213 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3214 {SBIT(GSM_CSTATE_ACTIVE),
3215 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3218 {SBIT(GSM_CSTATE_ACTIVE),
3219 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3220 {SBIT(GSM_CSTATE_ACTIVE),
3221 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3222 {SBIT(GSM_CSTATE_ACTIVE),
3223 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3224 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3225 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3226 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3227 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3228 {SBIT(GSM_CSTATE_ACTIVE),
3229 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3230 /* clearing */
3231 {SBIT(GSM_CSTATE_INITIATED),
3232 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3233 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3234 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3235 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3236 MNCC_REL_REQ, gsm48_cc_tx_release},
3237 /* special */
3238 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003239 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003240};
3241
3242#define DOWNSLLEN \
3243 (sizeof(downstatelist) / sizeof(struct downstate))
3244
3245
3246int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3247{
Harald Welte1a6f7982009-08-09 18:52:33 +02003248 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003250 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003252 struct gsm_mncc *data = arg, rel;
3253
3254 /* handle special messages */
3255 switch(msg_type) {
3256 case MNCC_BRIDGE:
3257 return tch_bridge(net, arg);
3258 case MNCC_FRAME_DROP:
3259 return tch_recv(net, arg, 0);
3260 case MNCC_FRAME_RECV:
3261 return tch_recv(net, arg, 1);
3262 case GSM_TRAU_FRAME:
3263 return tch_frame(net, arg);
3264 }
3265
3266 memset(&rel, 0, sizeof(struct gsm_mncc));
3267 rel.callref = data->callref;
3268
3269 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003270 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003271
3272 /* Callref unknown */
3273 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003274 struct gsm_subscriber *subscr;
3275
Harald Welte4a3464c2009-07-04 10:11:24 +02003276 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003277 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3278 "Received '%s' from MNCC with "
3279 "unknown callref %d\n", data->called.number,
3280 get_mncc_name(msg_type), data->callref);
3281 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003282 return mncc_release_ind(net, NULL, data->callref,
3283 GSM48_CAUSE_LOC_PRN_S_LU,
3284 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003285 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003286 if (!data->called.number[0] && !data->imsi[0]) {
3287 DEBUGP(DCC, "(bts - trx - ts - ti) "
3288 "Received '%s' from MNCC with "
3289 "no number or IMSI\n", get_mncc_name(msg_type));
3290 /* Invalid number */
3291 return mncc_release_ind(net, NULL, data->callref,
3292 GSM48_CAUSE_LOC_PRN_S_LU,
3293 GSM48_CC_CAUSE_INV_NR_FORMAT);
3294 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003296 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003297 subscr = subscr_get_by_extension(net,
3298 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003299 else
Harald Welte9176bd42009-07-23 18:46:00 +02003300 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003301 /* If subscriber is not found */
3302 if (!subscr) {
3303 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3304 "Received '%s' from MNCC with "
3305 "unknown subscriber %s\n", data->called.number,
3306 get_mncc_name(msg_type), data->called.number);
3307 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003308 return mncc_release_ind(net, NULL, data->callref,
3309 GSM48_CAUSE_LOC_PRN_S_LU,
3310 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003311 }
3312 /* If subscriber is not "attached" */
3313 if (!subscr->lac) {
3314 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3315 "Received '%s' from MNCC with "
3316 "detached subscriber %s\n", data->called.number,
3317 get_mncc_name(msg_type), data->called.number);
3318 subscr_put(subscr);
3319 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003320 return mncc_release_ind(net, NULL, data->callref,
3321 GSM48_CAUSE_LOC_PRN_S_LU,
3322 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003323 }
3324 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003325 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3326 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003327 DEBUGP(DCC, "No memory for trans.\n");
3328 subscr_put(subscr);
3329 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003330 mncc_release_ind(net, NULL, data->callref,
3331 GSM48_CAUSE_LOC_PRN_S_LU,
3332 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003333 return -ENOMEM;
3334 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003336 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003337 /* If subscriber has no lchan */
3338 if (!lchan) {
3339 /* find transaction with this subscriber already paging */
3340 llist_for_each_entry(transt, &net->trans_list, entry) {
3341 /* Transaction of our lchan? */
3342 if (transt == trans ||
3343 transt->subscr != subscr)
3344 continue;
3345 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3346 "Received '%s' from MNCC with "
3347 "unallocated channel, paging already "
3348 "started.\n", bts->nr,
3349 data->called.number,
3350 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003351 subscr_put(subscr);
3352 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003353 return 0;
3354 }
3355 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003356 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003357 /* Trigger paging */
3358 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3359 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003360 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003361 return 0;
3362 }
3363 /* Assign lchan */
3364 trans->lchan = lchan;
3365 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003366 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003367 }
3368 lchan = trans->lchan;
3369
3370 /* if paging did not respond yet */
3371 if (!lchan) {
3372 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3373 "Received '%s' from MNCC in paging state\n",
3374 (trans->subscr)?(trans->subscr->extension):"-",
3375 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003376 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3377 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003378 if (msg_type == MNCC_REL_REQ)
3379 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3380 else
3381 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3382 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003383 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003384 return rc;
3385 }
3386
3387 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3388 "Received '%s' from MNCC in state %d (%s)\n",
3389 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3390 trans->transaction_id,
3391 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003392 get_mncc_name(msg_type), trans->cc.state,
3393 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003394
3395 /* Find function for current state and message */
3396 for (i = 0; i < DOWNSLLEN; i++)
3397 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003398 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003399 break;
3400 if (i == DOWNSLLEN) {
3401 DEBUGP(DCC, "Message unhandled at this state.\n");
3402 return 0;
3403 }
3404
3405 rc = downstatelist[i].rout(trans, arg);
3406
3407 return rc;
3408}
3409
3410
3411static struct datastate {
3412 u_int32_t states;
3413 int type;
3414 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3415} datastatelist[] = {
3416 /* mobile originating call establishment */
3417 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3418 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3419 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3420 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3421 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3422 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3423 /* mobile terminating call establishment */
3424 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3425 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3426 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3427 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3428 {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 */
3429 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3430 /* signalling during call */
3431 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3432 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3433 {SBIT(GSM_CSTATE_ACTIVE),
3434 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3435 {ALL_STATES,
3436 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3437 {ALL_STATES,
3438 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3439 {ALL_STATES,
3440 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3441 {SBIT(GSM_CSTATE_ACTIVE),
3442 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3443 {SBIT(GSM_CSTATE_ACTIVE),
3444 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3445 {SBIT(GSM_CSTATE_ACTIVE),
3446 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3447 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3448 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3449 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3450 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3451 {SBIT(GSM_CSTATE_ACTIVE),
3452 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3453 /* clearing */
3454 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3455 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3456 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3457 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3458 {ALL_STATES, /* 5.4.3.4 */
3459 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3460};
3461
3462#define DATASLLEN \
3463 (sizeof(datastatelist) / sizeof(struct datastate))
3464
Harald Welte4bc90a12008-12-27 16:32:52 +00003465static int gsm0408_rcv_cc(struct msgb *msg)
3466{
3467 struct gsm48_hdr *gh = msgb_l3(msg);
3468 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003469 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003470 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003471 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003473
Harald Welte4bfdfe72009-06-10 23:11:52 +08003474 if (msg_type & 0x80) {
3475 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3476 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003477 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003478
3479 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003480 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003481
Harald Welte6f5aee02009-07-23 21:21:14 +02003482 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003483 "Received '%s' from MS in state %d (%s)\n",
3484 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3485 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003486 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003487 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003488
3489 /* Create transaction */
3490 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003491 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003492 "creating new trans.\n", transaction_id);
3493 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003494 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3495 transaction_id, new_callref++);
3496 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003497 DEBUGP(DCC, "No memory for trans.\n");
3498 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003499 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003500 GSM48_MT_CC_RELEASE_COMPL);
3501 return -ENOMEM;
3502 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003503 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003504 trans->lchan = lchan;
3505 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 }
3507
3508 /* find function for current state and message */
3509 for (i = 0; i < DATASLLEN; i++)
3510 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003511 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003512 break;
3513 if (i == DATASLLEN) {
3514 DEBUGP(DCC, "Message unhandled at this state.\n");
3515 return 0;
3516 }
3517
3518 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003519
3520 return rc;
3521}
3522
Harald Welte52b1f982008-12-23 20:25:15 +00003523/* 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 +02003524int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003525{
3526 struct gsm48_hdr *gh = msgb_l3(msg);
3527 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003528 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003529
3530 switch (pdisc) {
3531 case GSM48_PDISC_CC:
3532 rc = gsm0408_rcv_cc(msg);
3533 break;
3534 case GSM48_PDISC_MM:
3535 rc = gsm0408_rcv_mm(msg);
3536 break;
3537 case GSM48_PDISC_RR:
3538 rc = gsm0408_rcv_rr(msg);
3539 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003540 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003541 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003542 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003543 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003544 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003545 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003546 pdisc);
3547 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003548 case GSM48_PDISC_NC_SS:
3549 rc = handle_rcv_ussd(msg);
3550 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003551 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003552 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003553 pdisc);
3554 break;
3555 }
3556
3557 return rc;
3558}
Harald Welte8470bf22008-12-25 23:28:35 +00003559
Harald Welte4bfdfe72009-06-10 23:11:52 +08003560/* dequeue messages to layer 4 */
3561int bsc_upqueue(struct gsm_network *net)
3562{
3563 struct gsm_mncc *mncc;
3564 struct msgb *msg;
3565 int work = 0;
3566
3567 if (net)
3568 while ((msg = msgb_dequeue(&net->upqueue))) {
3569 mncc = (struct gsm_mncc *)msg->data;
3570 if (net->mncc_recv)
3571 net->mncc_recv(net, mncc->msg_type, mncc);
3572 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003573 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003574 }
3575
3576 return work;
3577}
Harald Weltedcaf5652009-07-23 18:56:43 +02003578
Harald Welte805f6442009-07-28 18:25:29 +02003579/*
3580 * This will be ran by the linker when loading the DSO. We use it to
3581 * do system initialization, e.g. registration of signal handlers.
3582 */
3583static __attribute__((constructor)) void on_dso_load_0408(void)
3584{
Harald Welte805f6442009-07-28 18:25:29 +02003585 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3586 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3587}