blob: e1374c9ae6826730269c704e86536e81ac1ba2a8 [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 Welte75a983f2008-12-27 21:34:06 +0000957 switch (mi_type) {
958 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200959 /* look up subscriber based on IMSI, create if not found */
960 if (!lchan->subscr) {
961 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200962 if (!lchan->subscr)
963 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200964 }
Holger Freyther73487a22008-12-31 18:53:57 +0000965 if (lchan->loc_operation)
966 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000967 break;
968 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000969 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000970 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200971 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000972 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200973 db_sync_equipment(&lchan->subscr->equipment);
974 }
Holger Freyther73487a22008-12-31 18:53:57 +0000975 if (lchan->loc_operation)
976 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000977 break;
978 }
Holger Freyther73487a22008-12-31 18:53:57 +0000979
980 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000981 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000982}
983
Harald Welte255539c2008-12-28 02:26:27 +0000984
985static void loc_upd_rej_cb(void *data)
986{
987 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100988 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000989
Holger Freyther73487a22008-12-31 18:53:57 +0000990 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100991 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000992 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000993}
994
Holger Freytherb7193e42008-12-29 17:44:08 +0000995static void schedule_reject(struct gsm_lchan *lchan)
996{
Holger Freyther73487a22008-12-31 18:53:57 +0000997 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
998 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000999 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001000}
1001
Harald Welte2a139372009-02-22 21:14:55 +00001002static const char *lupd_name(u_int8_t type)
1003{
1004 switch (type) {
1005 case GSM48_LUPD_NORMAL:
1006 return "NORMAL";
1007 case GSM48_LUPD_PERIODIC:
1008 return "PEROIDOC";
1009 case GSM48_LUPD_IMSI_ATT:
1010 return "IMSI ATTACH";
1011 default:
1012 return "UNKNOWN";
1013 }
1014}
1015
Harald Weltebf5e8df2009-02-03 12:59:45 +00001016/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001017static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001018{
Harald Welte8470bf22008-12-25 23:28:35 +00001019 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001020 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001021 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001022 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001023 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001024 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001025 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001026 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001027
Harald Welte8470bf22008-12-25 23:28:35 +00001028 lu = (struct gsm48_loc_upd_req *) gh->data;
1029
1030 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001031
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001032 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001033
Harald Weltea0368542009-06-27 02:58:43 +02001034 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001035 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001036
Holger Freythereaf04692009-06-06 13:54:44 +00001037 /*
1038 * Pseudo Spoof detection: Just drop a second/concurrent
1039 * location updating request.
1040 */
1041 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001042 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001043 lchan->loc_operation);
1044 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1045 return 0;
1046 }
1047
Holger Freyther73487a22008-12-31 18:53:57 +00001048 allocate_loc_updating_req(lchan);
1049
Harald Welte52b1f982008-12-23 20:25:15 +00001050 switch (mi_type) {
1051 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001052 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001053 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001054 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001055 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001056
Jan Luebbe370b41d2009-08-12 10:19:34 +02001057 /* look up subscriber based on IMSI, create if not found */
1058 subscr = subscr_get_by_imsi(bts->network, mi_string);
1059 if (!subscr) {
1060 subscr = db_create_subscriber(bts->network, mi_string);
1061 }
Harald Welte4b634542008-12-27 01:55:51 +00001062 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001063 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001064 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001065 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001066 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001067 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001068
Harald Welte52b1f982008-12-23 20:25:15 +00001069 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001070 subscr = subscr_get_by_tmsi(bts->network,
1071 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001072 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001073 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001074 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001075 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001076 }
1077 break;
1078 case GSM_MI_TYPE_IMEI:
1079 case GSM_MI_TYPE_IMEISV:
1080 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001081 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001082 break;
1083 default:
Harald Weltea0368542009-06-27 02:58:43 +02001084 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001085 break;
1086 }
1087
Harald Welte24516ea2009-07-04 10:18:00 +02001088 /* schedule the reject timer */
1089 schedule_reject(lchan);
1090
Harald Welte4bfdfe72009-06-10 23:11:52 +08001091 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001092 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001093 /* FIXME: request id? close channel? */
1094 return -EINVAL;
1095 }
1096
Harald Welte255539c2008-12-28 02:26:27 +00001097 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001098 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001099
Harald Welte24516ea2009-07-04 10:18:00 +02001100 /* check if we can let the subscriber into our network immediately
1101 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001102 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001103}
1104
Harald Welte4bfdfe72009-06-10 23:11:52 +08001105#if 0
1106static u_int8_t to_bcd8(u_int8_t val)
1107{
1108 return ((val / 10) << 4) | (val % 10);
1109}
1110#endif
1111
Harald Weltedb253af2008-12-30 17:56:55 +00001112/* Section 9.2.15a */
1113int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1114{
1115 struct msgb *msg = gsm48_msgb_alloc();
1116 struct gsm48_hdr *gh;
1117 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001118 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001119 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001120#if 0
1121 time_t cur_t;
1122 struct tm* cur_time;
1123 int tz15min;
1124#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001125
1126 msg->lchan = lchan;
1127
1128 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1129 gh->proto_discr = GSM48_PDISC_MM;
1130 gh->msg_type = GSM48_MT_MM_INFO;
1131
1132 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001133#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001134 name_len = strlen(net->name_long);
1135 /* 10.5.3.5a */
1136 ptr8 = msgb_put(msg, 3);
1137 ptr8[0] = GSM48_IE_NAME_LONG;
1138 ptr8[1] = name_len*2 +1;
1139 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1140
1141 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1142 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001143 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001144
1145 /* FIXME: Use Cell Broadcast, not UCS-2, since
1146 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001147#endif
1148 name_len = (strlen(net->name_long)*7)/8;
1149 name_pad = (8 - strlen(net->name_long)*7)%8;
1150 if (name_pad > 0)
1151 name_len++;
1152 /* 10.5.3.5a */
1153 ptr8 = msgb_put(msg, 3);
1154 ptr8[0] = GSM48_IE_NAME_LONG;
1155 ptr8[1] = name_len +1;
1156 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1157
1158 ptr8 = msgb_put(msg, name_len);
1159 gsm_7bit_encode(ptr8, net->name_long);
1160
Harald Weltedb253af2008-12-30 17:56:55 +00001161 }
1162
1163 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001164#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001165 name_len = strlen(net->name_short);
1166 /* 10.5.3.5a */
1167 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001168 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001169 ptr8[1] = name_len*2 + 1;
1170 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1171
Harald Weltee872cb12009-01-01 00:33:37 +00001172 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001173 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001174 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001175#endif
1176 name_len = (strlen(net->name_short)*7)/8;
1177 name_pad = (8 - strlen(net->name_short)*7)%8;
1178 if (name_pad > 0)
1179 name_len++;
1180 /* 10.5.3.5a */
1181 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1182 ptr8[0] = GSM48_IE_NAME_SHORT;
1183 ptr8[1] = name_len +1;
1184 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1185
1186 ptr8 = msgb_put(msg, name_len);
1187 gsm_7bit_encode(ptr8, net->name_short);
1188
Harald Weltedb253af2008-12-30 17:56:55 +00001189 }
1190
1191#if 0
1192 /* Section 10.5.3.9 */
1193 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001194 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001195 ptr8 = msgb_put(msg, 8);
1196 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1197 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1198 ptr8[2] = to_bcd8(cur_time->tm_mon);
1199 ptr8[3] = to_bcd8(cur_time->tm_mday);
1200 ptr8[4] = to_bcd8(cur_time->tm_hour);
1201 ptr8[5] = to_bcd8(cur_time->tm_min);
1202 ptr8[6] = to_bcd8(cur_time->tm_sec);
1203 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1204 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001205 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001206 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001207 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001208#endif
1209
Daniel Willmanneea93372009-08-13 03:42:07 +02001210 DEBUGP(DMM, "-> MM INFO\n");
1211
Harald Welte39e2ead2009-07-23 21:13:03 +02001212 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001213}
1214
Harald Welte7984d5c2009-08-12 22:56:50 +02001215/* Section 9.2.2 */
1216int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1217{
1218 struct msgb *msg = gsm48_msgb_alloc();
1219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001220 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001221
1222 DEBUGP(DMM, "-> AUTH REQ\n");
1223
1224 msg->lchan = lchan;
1225 gh->proto_discr = GSM48_PDISC_MM;
1226 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1227
Sylvain Munaut849f5542009-09-27 11:10:17 +02001228 /* Key Sequence: FIXME fixed to 0 */
1229 ar->key_seq = 0;
1230
Harald Welte7984d5c2009-08-12 22:56:50 +02001231 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001232 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001233 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001234
1235 return gsm48_sendmsg(msg, NULL);
1236}
1237
1238/* Section 9.2.1 */
1239int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1240{
1241 DEBUGP(DMM, "-> AUTH REJECT\n");
1242 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1243}
1244
Harald Welte4b634542008-12-27 01:55:51 +00001245static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1246{
Harald Welte4b634542008-12-27 01:55:51 +00001247 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001248 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001249}
Harald Welteba4cf162009-01-10 01:49:35 +00001250
1251/* 9.2.6 CM service reject */
1252static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1253 enum gsm48_reject_value value)
1254{
1255 struct msgb *msg = gsm48_msgb_alloc();
1256 struct gsm48_hdr *gh;
1257
1258 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1259
1260 msg->lchan = lchan;
1261 use_lchan(lchan);
1262
1263 gh->proto_discr = GSM48_PDISC_MM;
1264 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1265 gh->data[0] = value;
1266 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1267
Harald Welte39e2ead2009-07-23 21:13:03 +02001268 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001269}
1270
Harald Welte4ed0e922009-01-10 03:17:30 +00001271/*
1272 * Handle CM Service Requests
1273 * a) Verify that the packet is long enough to contain the information
1274 * we require otherwsie reject with INCORRECT_MESSAGE
1275 * b) Try to parse the TMSI. If we do not have one reject
1276 * c) Check that we know the subscriber with the TMSI otherwise reject
1277 * with a HLR cause
1278 * d) Set the subscriber on the gsm_lchan and accept
1279 */
Harald Welte4b634542008-12-27 01:55:51 +00001280static int gsm48_rx_mm_serv_req(struct msgb *msg)
1281{
Harald Welteba4cf162009-01-10 01:49:35 +00001282 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001283 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001284
Harald Welte9176bd42009-07-23 18:46:00 +02001285 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001286 struct gsm_subscriber *subscr;
1287 struct gsm48_hdr *gh = msgb_l3(msg);
1288 struct gsm48_service_request *req =
1289 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001290 /* unfortunately in Phase1 the classmar2 length is variable */
1291 u_int8_t classmark2_len = gh->data[1];
1292 u_int8_t *classmark2 = gh->data+2;
1293 u_int8_t mi_len = *(classmark2 + classmark2_len);
1294 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001295
Harald Weltec9e02182009-05-01 19:07:53 +00001296 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001297 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001298 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001299 return gsm48_tx_mm_serv_rej(msg->lchan,
1300 GSM48_REJECT_INCORRECT_MESSAGE);
1301 }
1302
1303 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001304 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001305 return gsm48_tx_mm_serv_rej(msg->lchan,
1306 GSM48_REJECT_INCORRECT_MESSAGE);
1307 }
1308
Harald Weltec9e02182009-05-01 19:07:53 +00001309 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001310 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001311 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001312 return gsm48_tx_mm_serv_rej(msg->lchan,
1313 GSM48_REJECT_INCORRECT_MESSAGE);
1314 }
1315
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001316 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001317 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001318 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001319
Harald Welte3ac7f102009-08-10 10:12:45 +02001320 if (is_siemens_bts(bts))
1321 send_siemens_mrpci(msg->lchan, classmark2-1);
1322
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001323 subscr = subscr_get_by_tmsi(bts->network,
1324 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001325
Harald Welte2a139372009-02-22 21:14:55 +00001326 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001327 if (!subscr)
1328 return gsm48_tx_mm_serv_rej(msg->lchan,
1329 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1330
1331 if (!msg->lchan->subscr)
1332 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001333 else if (msg->lchan->subscr != subscr) {
1334 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1335 subscr_put(subscr);
1336 }
1337
Harald Weltec2e302d2009-07-05 14:08:13 +02001338 subscr->equipment.classmark2_len = classmark2_len;
1339 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1340 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001341
Harald Welte4b634542008-12-27 01:55:51 +00001342 return gsm48_tx_mm_serv_ack(msg->lchan);
1343}
1344
Harald Welte2a139372009-02-22 21:14:55 +00001345static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1346{
Harald Welte9176bd42009-07-23 18:46:00 +02001347 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001348 struct gsm48_hdr *gh = msgb_l3(msg);
1349 struct gsm48_imsi_detach_ind *idi =
1350 (struct gsm48_imsi_detach_ind *) gh->data;
1351 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001352 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001354
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001355 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001356 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1357 mi_type, mi_string);
1358
1359 switch (mi_type) {
1360 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001361 subscr = subscr_get_by_tmsi(bts->network,
1362 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001363 break;
1364 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001365 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001366 break;
1367 case GSM_MI_TYPE_IMEI:
1368 case GSM_MI_TYPE_IMEISV:
1369 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001370 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001371 break;
1372 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001373 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001374 break;
1375 }
1376
Holger Freyther4a49e772009-04-12 05:37:29 +00001377 if (subscr) {
1378 subscr_update(subscr, msg->trx->bts,
1379 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001380 DEBUGP(DMM, "Subscriber: %s\n",
1381 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001382
1383 subscr->equipment.classmark1 = idi->classmark1;
1384 db_sync_equipment(&subscr->equipment);
1385
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001386 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001387 } else
Harald Welte2a139372009-02-22 21:14:55 +00001388 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1389
Harald Welteb83d9382009-12-12 21:00:48 +01001390 /* subscriber is detached: should we release lchan? */
1391
Harald Welte2a139372009-02-22 21:14:55 +00001392 return 0;
1393}
1394
Harald Welted2a7f5a2009-06-05 20:08:20 +00001395static int gsm48_rx_mm_status(struct msgb *msg)
1396{
1397 struct gsm48_hdr *gh = msgb_l3(msg);
1398
1399 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1400
1401 return 0;
1402}
1403
Harald Weltebf5e8df2009-02-03 12:59:45 +00001404/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001405static int gsm0408_rcv_mm(struct msgb *msg)
1406{
1407 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001409
1410 switch (gh->msg_type & 0xbf) {
1411 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001412 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001413 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001414 break;
1415 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001416 rc = mm_rx_id_resp(msg);
1417 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001418 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001419 rc = gsm48_rx_mm_serv_req(msg);
1420 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001421 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001422 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001423 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001424 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001425 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1426 msg->lchan->subscr ?
1427 msg->lchan->subscr->imsi :
1428 "unknown subscriber");
1429 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001430 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001431 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1432 break;
1433 case GSM48_MT_MM_CM_REEST_REQ:
1434 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1435 break;
1436 case GSM48_MT_MM_AUTH_RESP:
1437 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001438 break;
1439 default:
1440 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1441 gh->msg_type);
1442 break;
1443 }
1444
1445 return rc;
1446}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001447
Harald Welte2d35ae62009-02-06 12:02:13 +00001448/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001449static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001450{
Harald Welte9176bd42009-07-23 18:46:00 +02001451 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001452 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001453 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001454 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001455 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001456 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001457 int rc = 0;
1458
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001459 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001460 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1461 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001462
Harald Weltefe18d8f2009-02-22 21:14:24 +00001463 switch (mi_type) {
1464 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001465 subscr = subscr_get_by_tmsi(bts->network,
1466 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001467 break;
1468 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001469 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001470 break;
1471 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001472
1473 if (!subscr) {
1474 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001475 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001476 return -EINVAL;
1477 }
1478 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001479 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001480
Harald Weltec2e302d2009-07-05 14:08:13 +02001481 subscr->equipment.classmark2_len = *classmark2_lv;
1482 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1483 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001484
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001485 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001486 return rc;
1487}
1488
Harald Weltef7c43522009-06-09 20:24:21 +00001489static int gsm48_rx_rr_classmark(struct msgb *msg)
1490{
1491 struct gsm48_hdr *gh = msgb_l3(msg);
1492 struct gsm_subscriber *subscr = msg->lchan->subscr;
1493 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1494 u_int8_t cm2_len, cm3_len = 0;
1495 u_int8_t *cm2, *cm3 = NULL;
1496
1497 DEBUGP(DRR, "CLASSMARK CHANGE ");
1498
1499 /* classmark 2 */
1500 cm2_len = gh->data[0];
1501 cm2 = &gh->data[1];
1502 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1503
1504 if (payload_len > cm2_len + 1) {
1505 /* we must have a classmark3 */
1506 if (gh->data[cm2_len+1] != 0x20) {
1507 DEBUGPC(DRR, "ERR CM3 TAG\n");
1508 return -EINVAL;
1509 }
1510 if (cm2_len > 3) {
1511 DEBUGPC(DRR, "CM2 too long!\n");
1512 return -EINVAL;
1513 }
1514
1515 cm3_len = gh->data[cm2_len+2];
1516 cm3 = &gh->data[cm2_len+3];
1517 if (cm3_len > 14) {
1518 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1519 return -EINVAL;
1520 }
1521 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1522 }
1523 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001524 subscr->equipment.classmark2_len = cm2_len;
1525 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001526 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001527 subscr->equipment.classmark3_len = cm3_len;
1528 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001529 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001530 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001531 }
1532
Harald Weltef7c43522009-06-09 20:24:21 +00001533 return 0;
1534}
1535
Harald Weltecf5b3592009-05-01 18:28:42 +00001536static int gsm48_rx_rr_status(struct msgb *msg)
1537{
1538 struct gsm48_hdr *gh = msgb_l3(msg);
1539
1540 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1541 rr_cause_name(gh->data[0]));
1542
1543 return 0;
1544}
1545
Harald Weltef7c43522009-06-09 20:24:21 +00001546static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1547{
Harald Weltef7c43522009-06-09 20:24:21 +00001548 static struct gsm_meas_rep meas_rep;
1549
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001550 /* This shouldn't actually end up here, as RSL treats
1551 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1552 * directly into gsm48_parse_meas_rep */
1553 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1554 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001555
1556 return 0;
1557}
1558
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001559static int gsm48_rx_rr_app_info(struct msgb *msg)
1560{
1561 struct gsm48_hdr *gh = msgb_l3(msg);
1562 u_int8_t apdu_id_flags;
1563 u_int8_t apdu_len;
1564 u_int8_t *apdu_data;
1565
1566 apdu_id_flags = gh->data[0];
1567 apdu_len = gh->data[1];
1568 apdu_data = gh->data+2;
1569
1570 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1571 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1572
Harald Welte (local)026531e2009-08-16 10:40:10 +02001573 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001574}
1575
Harald Welted011e8b2009-11-29 22:45:52 +01001576/* Chapter 9.1.16 Handover complete */
1577static gsm48_rx_rr_ho_compl(struct msgb *msg)
1578{
1579 struct gsm48_hdr *gh = msgb_l3(msg);
1580
1581 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1582 rr_cause_name(gh->data[0]));
1583
1584 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1585 /* FIXME: release old channel */
1586
1587 return 0;
1588}
1589
1590/* Chapter 9.1.17 Handover Failure */
1591static gsm48_rx_rr_ho_fail(struct msgb *msg)
1592{
1593 struct gsm48_hdr *gh = msgb_l3(msg);
1594
1595 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1596 rr_cause_name(gh->data[0]));
1597
1598 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1599 /* FIXME: release allocated new channel */
1600
1601 return 0;
1602}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001603
Harald Weltebf5e8df2009-02-03 12:59:45 +00001604/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001605static int gsm0408_rcv_rr(struct msgb *msg)
1606{
1607 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001608 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001609
1610 switch (gh->msg_type) {
1611 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001612 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001613 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001614 case GSM48_MT_RR_GPRS_SUSP_REQ:
1615 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1616 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001617 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001618 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001619 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001620 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001621 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001622 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001623 case GSM48_MT_RR_STATUS:
1624 rc = gsm48_rx_rr_status(msg);
1625 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001626 case GSM48_MT_RR_MEAS_REP:
1627 rc = gsm48_rx_rr_meas_rep(msg);
1628 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001629 case GSM48_MT_RR_APP_INFO:
1630 rc = gsm48_rx_rr_app_info(msg);
1631 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001632 case GSM48_MT_RR_CIPH_M_COMPL:
1633 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1634 /* FIXME: check for MI (if any) */
1635 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001636 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001637 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001638 break;
1639 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001640 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001641 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001642 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001643 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001644 gh->msg_type);
1645 break;
1646 }
1647
Harald Welte2d35ae62009-02-06 12:02:13 +00001648 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001649}
1650
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001651int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001652 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001653{
1654 struct msgb *msg = gsm48_msgb_alloc();
1655 struct gsm48_hdr *gh;
1656
1657 msg->lchan = lchan;
1658
1659 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1660 apdu_id, apdu_len);
1661
1662 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1663 gh->proto_discr = GSM48_PDISC_RR;
1664 gh->msg_type = GSM48_MT_RR_APP_INFO;
1665 gh->data[0] = apdu_id;
1666 gh->data[1] = apdu_len;
1667 memcpy(gh->data+2, apdu, apdu_len);
1668
1669 return gsm48_sendmsg(msg, NULL);
1670}
1671
Harald Welte4bc90a12008-12-27 16:32:52 +00001672/* Call Control */
1673
Harald Welte7584aea2009-02-11 11:44:12 +00001674/* The entire call control code is written in accordance with Figure 7.10c
1675 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1676 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1677 * it for voice */
1678
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679static void new_cc_state(struct gsm_trans *trans, int state)
1680{
1681 if (state > 31 || state < 0)
1682 return;
1683
1684 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001685 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686
Harald Weltedcaf5652009-07-23 18:56:43 +02001687 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688}
1689
1690static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001691{
1692 struct msgb *msg = gsm48_msgb_alloc();
1693 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1694 u_int8_t *cause, *call_state;
1695
Harald Welte4bc90a12008-12-27 16:32:52 +00001696 gh->msg_type = GSM48_MT_CC_STATUS;
1697
1698 cause = msgb_put(msg, 3);
1699 cause[0] = 2;
1700 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1701 cause[2] = 0x80 | 30; /* response to status inquiry */
1702
1703 call_state = msgb_put(msg, 1);
1704 call_state[0] = 0xc0 | 0x00;
1705
Harald Welte39e2ead2009-07-23 21:13:03 +02001706 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001707}
1708
Harald Welte6f4b7532008-12-29 00:39:37 +00001709static int gsm48_tx_simple(struct gsm_lchan *lchan,
1710 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001711{
1712 struct msgb *msg = gsm48_msgb_alloc();
1713 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1714
1715 msg->lchan = lchan;
1716
Harald Welte6f4b7532008-12-29 00:39:37 +00001717 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001718 gh->msg_type = msg_type;
1719
Harald Welte39e2ead2009-07-23 21:13:03 +02001720 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001721}
1722
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1724{
Harald Weltedcaf5652009-07-23 18:56:43 +02001725 if (bsc_timer_pending(&trans->cc.timer)) {
1726 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1727 bsc_del_timer(&trans->cc.timer);
1728 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 }
1730}
1731
1732static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1733 int msg_type, struct gsm_mncc *mncc)
1734{
1735 struct msgb *msg;
1736
1737 if (trans)
1738 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001739 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 "Sending '%s' to MNCC.\n",
1741 trans->lchan->ts->trx->bts->nr,
1742 trans->lchan->ts->trx->nr,
1743 trans->lchan->ts->nr, trans->transaction_id,
1744 (trans->subscr)?(trans->subscr->extension):"-",
1745 get_mncc_name(msg_type));
1746 else
1747 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1748 "Sending '%s' to MNCC.\n",
1749 (trans->subscr)?(trans->subscr->extension):"-",
1750 get_mncc_name(msg_type));
1751 else
1752 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1753 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1754
1755 mncc->msg_type = msg_type;
1756
Harald Welte966636f2009-06-26 19:39:35 +02001757 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 if (!msg)
1759 return -ENOMEM;
1760 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1761 msgb_enqueue(&net->upqueue, msg);
1762
1763 return 0;
1764}
1765
1766int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1767 u_int32_t callref, int location, int value)
1768{
1769 struct gsm_mncc rel;
1770
Harald Welte92f70c52009-06-12 01:54:08 +08001771 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001773 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1775}
1776
Harald Weltedcaf5652009-07-23 18:56:43 +02001777/* Call Control Specific transaction release.
1778 * gets called by trans_free, DO NOT CALL YOURSELF! */
1779void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 gsm48_stop_cc_timer(trans);
1782
1783 /* send release to L4, if callref still exists */
1784 if (trans->callref) {
1785 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001786 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001787 GSM48_CAUSE_LOC_PRN_S_LU,
1788 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001790 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001792 if (trans->lchan)
1793 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794}
1795
1796static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1797
Harald Welte09e38af2009-02-16 22:52:23 +00001798/* call-back from paging the B-end of the connection */
1799static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001800 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001801{
Harald Welte7ccf7782009-02-17 01:43:01 +00001802 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 struct gsm_subscriber *subscr = param;
1804 struct gsm_trans *transt, *tmp;
1805 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001806
Harald Welte09e38af2009-02-16 22:52:23 +00001807 if (hooknum != GSM_HOOK_RR_PAGING)
1808 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809
1810 if (!subscr)
1811 return -EINVAL;
1812 net = subscr->net;
1813 if (!net) {
1814 DEBUGP(DCC, "Error Network not set!\n");
1815 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001816 }
Harald Welte7584aea2009-02-11 11:44:12 +00001817
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 /* check all tranactions (without lchan) for subscriber */
1819 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1820 if (transt->subscr != subscr || transt->lchan)
1821 continue;
1822 switch (event) {
1823 case GSM_PAGING_SUCCEEDED:
1824 if (!lchan) // paranoid
1825 break;
1826 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1827 subscr->extension);
1828 /* Assign lchan */
1829 if (!transt->lchan) {
1830 transt->lchan = lchan;
1831 use_lchan(lchan);
1832 }
1833 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001834 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 break;
1836 case GSM_PAGING_EXPIRED:
1837 DEBUGP(DCC, "Paging subscr %s expired!\n",
1838 subscr->extension);
1839 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001840 mncc_release_ind(transt->subscr->net, transt,
1841 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001842 GSM48_CAUSE_LOC_PRN_S_LU,
1843 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001845 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 break;
1847 }
1848 }
Harald Welte09e38af2009-02-16 22:52:23 +00001849 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001850}
Harald Welte7584aea2009-02-11 11:44:12 +00001851
Harald Welte805f6442009-07-28 18:25:29 +02001852/* some other part of the code sends us a signal */
1853static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1854 void *handler_data, void *signal_data)
1855{
1856 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001857 int rc;
1858
1859 if (subsys != SS_ABISIP)
1860 return 0;
1861
1862 /* in case we use direct BTS-to-BTS RTP */
1863 if (ipacc_rtp_direct)
1864 return 0;
1865
Harald Welte805f6442009-07-28 18:25:29 +02001866 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001867 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001868 /* the BTS has successfully bound a TCH to a local ip/port,
1869 * which means we can connect our UDP socket to it */
Harald Welte2c828992009-12-02 01:56:49 +05301870 if (lchan->abis_ip.rtp_socket) {
1871 rtp_socket_free(lchan->abis_ip.rtp_socket);
1872 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001873 }
1874
Harald Welte2c828992009-12-02 01:56:49 +05301875 lchan->abis_ip.rtp_socket = rtp_socket_create();
1876 if (!lchan->abis_ip.rtp_socket)
Harald Welte805f6442009-07-28 18:25:29 +02001877 goto out_err;
1878
Harald Welte2c828992009-12-02 01:56:49 +05301879 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1880 lchan->abis_ip.bound_ip,
1881 lchan->abis_ip.bound_port);
Harald Welte805f6442009-07-28 18:25:29 +02001882 if (rc < 0)
1883 goto out_err;
1884 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001885 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001886 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte2c828992009-12-02 01:56:49 +05301887 if (lchan->abis_ip.rtp_socket) {
1888 rtp_socket_free(lchan->abis_ip.rtp_socket);
1889 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001890 }
1891 break;
1892 }
1893
1894 return 0;
1895out_err:
1896 /* FIXME: do something */
1897 return 0;
1898}
1899
1900/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1901static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1902{
Harald Welte2c828992009-12-02 01:56:49 +05301903 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte805f6442009-07-28 18:25:29 +02001904 int rc;
1905
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001906 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001907 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte2c828992009-12-02 01:56:49 +05301908 lchan->abis_ip.conn_id,
Harald Welte805f6442009-07-28 18:25:29 +02001909 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte2c828992009-12-02 01:56:49 +05301910 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001911
1912 return rc;
1913}
1914
Harald Welte49f48b82009-02-17 15:29:33 +00001915/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001916static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001917{
Harald Welte11fa29c2009-02-19 17:24:39 +00001918 struct gsm_bts *bts = lchan->ts->trx->bts;
1919 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001920 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001921
Harald Welte11fa29c2009-02-19 17:24:39 +00001922 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1923 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1924 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1925
1926 if (bts->type != remote_bts->type) {
1927 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1928 return -EINVAL;
1929 }
Harald Welte49f48b82009-02-17 15:29:33 +00001930
Harald Welte11fa29c2009-02-19 17:24:39 +00001931 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001932 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001933 if (!ipacc_rtp_direct) {
1934 /* connect the TCH's to our RTP proxy */
1935 rc = ipacc_connect_proxy_bind(lchan);
1936 if (rc < 0)
1937 return rc;
1938 rc = ipacc_connect_proxy_bind(remote_lchan);
1939
1940 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301941 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1942 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001943 } else {
1944 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301945 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1946 remote_lchan->abis_ip.bound_port,
1947 lchan->abis_ip.conn_id,
1948 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001949 if (rc < 0)
1950 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301951 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1952 lchan->abis_ip.bound_port,
1953 remote_lchan->abis_ip.conn_id,
1954 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001955 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001956 break;
1957 case GSM_BTS_TYPE_BS11:
1958 trau_mux_map_lchan(lchan, remote_lchan);
1959 break;
1960 default:
1961 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001962 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001963 break;
1964 }
Harald Welte49f48b82009-02-17 15:29:33 +00001965
1966 return 0;
1967}
1968
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969/* bridge channels of two transactions */
1970static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001971{
Harald Weltedcaf5652009-07-23 18:56:43 +02001972 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1973 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001976 return -EIO;
1977
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 if (!trans1->lchan || !trans2->lchan)
1979 return -EIO;
1980
1981 /* through-connect channel */
1982 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001983}
1984
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985/* enable receive of channels to upqueue */
1986static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1987{
1988 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001989
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001991 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 if (!trans)
1993 return -EIO;
1994 if (!trans->lchan)
1995 return 0;
1996
1997 // todo IPACCESS
1998 if (enable)
1999 return trau_recv_lchan(trans->lchan, data->callref);
2000 return trau_mux_unmap(NULL, data->callref);
2001}
2002
2003/* send a frame to channel */
2004static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2005{
2006 struct gsm_trans *trans;
2007
2008 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002009 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 if (!trans)
2011 return -EIO;
2012 if (!trans->lchan)
2013 return 0;
2014 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2015 trans->lchan->type != GSM_LCHAN_TCH_H)
2016 return 0;
2017
2018 // todo IPACCESS
2019 return trau_send_lchan(trans->lchan,
2020 (struct decoded_trau_frame *)frame->data);
2021}
2022
2023
2024static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2025{
2026 DEBUGP(DCC, "-> STATUS ENQ\n");
2027 return gsm48_cc_tx_status(trans, msg);
2028}
2029
2030static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2031static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2032
2033static void gsm48_cc_timeout(void *arg)
2034{
2035 struct gsm_trans *trans = arg;
2036 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002037 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2038 int mo_location = GSM48_CAUSE_LOC_USER;
2039 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2040 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 struct gsm_mncc mo_rel, l4_rel;
2042
2043 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2044 mo_rel.callref = trans->callref;
2045 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2046 l4_rel.callref = trans->callref;
2047
Harald Weltedcaf5652009-07-23 18:56:43 +02002048 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 case 0x303:
2050 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002051 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 break;
2053 case 0x310:
2054 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002055 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 break;
2057 case 0x313:
2058 disconnect = 1;
2059 /* unknown, did not find it in the specs */
2060 break;
2061 case 0x301:
2062 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002063 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 break;
2065 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002066 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002068 gsm48_cc_tx_release(trans, &trans->cc.msg);
2069 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 break; /* stay in release state */
2071 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002072 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 return;
2074// release = 1;
2075// l4_cause = 14;
2076// break;
2077 case 0x306:
2078 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002079 mo_cause = trans->cc.msg.cause.value;
2080 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 break;
2082 case 0x323:
2083 disconnect = 1;
2084 break;
2085 default:
2086 release = 1;
2087 }
2088
2089 if (release && trans->callref) {
2090 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002091 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 l4_location, l4_cause);
2093 trans->callref = 0;
2094 }
2095
2096 if (disconnect && trans->callref) {
2097 /* process disconnect towards layer 4 */
2098 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002099 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 }
2101
2102 /* process disconnect towards mobile station */
2103 if (disconnect || release) {
2104 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002105 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2106 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2107 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 mo_rel.cause.diag_len = 3;
2109
2110 if (disconnect)
2111 gsm48_cc_tx_disconnect(trans, &mo_rel);
2112 if (release)
2113 gsm48_cc_tx_release(trans, &mo_rel);
2114 }
2115
2116}
2117
2118static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2119 int sec, int micro)
2120{
2121 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002122 trans->cc.timer.cb = gsm48_cc_timeout;
2123 trans->cc.timer.data = trans;
2124 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2125 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126}
2127
2128static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2129{
2130 struct gsm48_hdr *gh = msgb_l3(msg);
2131 u_int8_t msg_type = gh->msg_type & 0xbf;
2132 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2133 struct tlv_parsed tp;
2134 struct gsm_mncc setup;
2135
2136 memset(&setup, 0, sizeof(struct gsm_mncc));
2137 setup.callref = trans->callref;
2138 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2139 /* emergency setup is identified by msg_type */
2140 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2141 setup.emergency = 1;
2142
2143 /* use subscriber as calling party number */
2144 if (trans->subscr) {
2145 setup.fields |= MNCC_F_CALLING;
2146 strncpy(setup.calling.number, trans->subscr->extension,
2147 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002148 strncpy(setup.imsi, trans->subscr->imsi,
2149 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150 }
2151 /* bearer capability */
2152 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2153 setup.fields |= MNCC_F_BEARER_CAP;
2154 decode_bearer_cap(&setup.bearer_cap,
2155 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2156 }
2157 /* facility */
2158 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2159 setup.fields |= MNCC_F_FACILITY;
2160 decode_facility(&setup.facility,
2161 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2162 }
2163 /* called party bcd number */
2164 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2165 setup.fields |= MNCC_F_CALLED;
2166 decode_called(&setup.called,
2167 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2168 }
2169 /* user-user */
2170 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2171 setup.fields |= MNCC_F_USERUSER;
2172 decode_useruser(&setup.useruser,
2173 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2174 }
2175 /* ss-version */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2177 setup.fields |= MNCC_F_SSVERSION;
2178 decode_ssversion(&setup.ssversion,
2179 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2180 }
2181 /* CLIR suppression */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2183 setup.clir.sup = 1;
2184 /* CLIR invocation */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2186 setup.clir.inv = 1;
2187 /* cc cap */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2189 setup.fields |= MNCC_F_CCCAP;
2190 decode_cccap(&setup.cccap,
2191 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2192 }
2193
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 new_cc_state(trans, GSM_CSTATE_INITIATED);
2195
2196 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002197 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198
Harald Welte13cac662009-07-29 12:10:35 +02002199 /* MNCC code will modify the channel asynchronously, we should
2200 * ipaccess-bind only after the modification has been made to the
2201 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202 return 0;
2203}
2204
2205static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002206{
2207 struct msgb *msg = gsm48_msgb_alloc();
2208 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002210 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002211
Harald Welte7ccf7782009-02-17 01:43:01 +00002212 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002213
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* transaction id must not be assigned */
2215 if (trans->transaction_id != 0xff) { /* unasssigned */
2216 DEBUGP(DCC, "TX Setup with assigned transaction. "
2217 "This is not allowed!\n");
2218 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002219 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002220 GSM48_CAUSE_LOC_PRN_S_LU,
2221 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002223 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224 return rc;
2225 }
2226
2227 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002228 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2229 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002231 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002232 GSM48_CAUSE_LOC_PRN_S_LU,
2233 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002235 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 return rc;
2237 }
Harald Welte78283ef2009-07-23 21:36:44 +02002238 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002239
Harald Welte65e74cc2008-12-29 01:55:35 +00002240 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002241
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002243
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 /* bearer capability */
2245 if (setup->fields & MNCC_F_BEARER_CAP)
2246 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2247 /* facility */
2248 if (setup->fields & MNCC_F_FACILITY)
2249 encode_facility(msg, 0, &setup->facility);
2250 /* progress */
2251 if (setup->fields & MNCC_F_PROGRESS)
2252 encode_progress(msg, 0, &setup->progress);
2253 /* calling party BCD number */
2254 if (setup->fields & MNCC_F_CALLING)
2255 encode_calling(msg, &setup->calling);
2256 /* called party BCD number */
2257 if (setup->fields & MNCC_F_CALLED)
2258 encode_called(msg, &setup->called);
2259 /* user-user */
2260 if (setup->fields & MNCC_F_USERUSER)
2261 encode_useruser(msg, 0, &setup->useruser);
2262 /* redirecting party BCD number */
2263 if (setup->fields & MNCC_F_REDIRECTING)
2264 encode_redirecting(msg, &setup->redirecting);
2265 /* signal */
2266 if (setup->fields & MNCC_F_SIGNAL)
2267 encode_signal(msg, setup->signal);
2268
2269 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002270
Harald Welte39e2ead2009-07-23 21:13:03 +02002271 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002272}
2273
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2275{
2276 struct gsm48_hdr *gh = msgb_l3(msg);
2277 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2278 struct tlv_parsed tp;
2279 struct gsm_mncc call_conf;
2280
2281 gsm48_stop_cc_timer(trans);
2282 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2283
2284 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2285 call_conf.callref = trans->callref;
2286 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2287#if 0
2288 /* repeat */
2289 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2290 call_conf.repeat = 1;
2291 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2292 call_conf.repeat = 2;
2293#endif
2294 /* bearer capability */
2295 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2296 call_conf.fields |= MNCC_F_BEARER_CAP;
2297 decode_bearer_cap(&call_conf.bearer_cap,
2298 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2299 }
2300 /* cause */
2301 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2302 call_conf.fields |= MNCC_F_CAUSE;
2303 decode_cause(&call_conf.cause,
2304 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2305 }
2306 /* cc cap */
2307 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2308 call_conf.fields |= MNCC_F_CCCAP;
2309 decode_cccap(&call_conf.cccap,
2310 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2311 }
2312
2313 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2314
Harald Welte596fed42009-07-23 19:06:52 +02002315 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2316 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317}
2318
2319static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2320{
2321 struct gsm_mncc *proceeding = arg;
2322 struct msgb *msg = gsm48_msgb_alloc();
2323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2324
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2326
2327 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2328
2329 /* bearer capability */
2330 if (proceeding->fields & MNCC_F_BEARER_CAP)
2331 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2332 /* facility */
2333 if (proceeding->fields & MNCC_F_FACILITY)
2334 encode_facility(msg, 0, &proceeding->facility);
2335 /* progress */
2336 if (proceeding->fields & MNCC_F_PROGRESS)
2337 encode_progress(msg, 0, &proceeding->progress);
2338
Harald Welte39e2ead2009-07-23 21:13:03 +02002339 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2343{
2344 struct gsm48_hdr *gh = msgb_l3(msg);
2345 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2346 struct tlv_parsed tp;
2347 struct gsm_mncc alerting;
2348
2349 gsm48_stop_cc_timer(trans);
2350 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2351
2352 memset(&alerting, 0, sizeof(struct gsm_mncc));
2353 alerting.callref = trans->callref;
2354 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2355 /* facility */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2357 alerting.fields |= MNCC_F_FACILITY;
2358 decode_facility(&alerting.facility,
2359 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2360 }
2361
2362 /* progress */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2364 alerting.fields |= MNCC_F_PROGRESS;
2365 decode_progress(&alerting.progress,
2366 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2367 }
2368 /* ss-version */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2370 alerting.fields |= MNCC_F_SSVERSION;
2371 decode_ssversion(&alerting.ssversion,
2372 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2373 }
2374
2375 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2376
Harald Welte596fed42009-07-23 19:06:52 +02002377 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2378 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379}
2380
2381static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2382{
2383 struct gsm_mncc *alerting = arg;
2384 struct msgb *msg = gsm48_msgb_alloc();
2385 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2386
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 gh->msg_type = GSM48_MT_CC_ALERTING;
2388
2389 /* facility */
2390 if (alerting->fields & MNCC_F_FACILITY)
2391 encode_facility(msg, 0, &alerting->facility);
2392 /* progress */
2393 if (alerting->fields & MNCC_F_PROGRESS)
2394 encode_progress(msg, 0, &alerting->progress);
2395 /* user-user */
2396 if (alerting->fields & MNCC_F_USERUSER)
2397 encode_useruser(msg, 0, &alerting->useruser);
2398
2399 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2400
Harald Welte39e2ead2009-07-23 21:13:03 +02002401 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402}
2403
2404static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2405{
2406 struct gsm_mncc *progress = arg;
2407 struct msgb *msg = gsm48_msgb_alloc();
2408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2409
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 gh->msg_type = GSM48_MT_CC_PROGRESS;
2411
2412 /* progress */
2413 encode_progress(msg, 1, &progress->progress);
2414 /* user-user */
2415 if (progress->fields & MNCC_F_USERUSER)
2416 encode_useruser(msg, 0, &progress->useruser);
2417
Harald Welte39e2ead2009-07-23 21:13:03 +02002418 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419}
2420
2421static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2422{
2423 struct gsm_mncc *connect = arg;
2424 struct msgb *msg = gsm48_msgb_alloc();
2425 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2426
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 gh->msg_type = GSM48_MT_CC_CONNECT;
2428
2429 gsm48_stop_cc_timer(trans);
2430 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2431
2432 /* facility */
2433 if (connect->fields & MNCC_F_FACILITY)
2434 encode_facility(msg, 0, &connect->facility);
2435 /* progress */
2436 if (connect->fields & MNCC_F_PROGRESS)
2437 encode_progress(msg, 0, &connect->progress);
2438 /* connected number */
2439 if (connect->fields & MNCC_F_CONNECTED)
2440 encode_connected(msg, &connect->connected);
2441 /* user-user */
2442 if (connect->fields & MNCC_F_USERUSER)
2443 encode_useruser(msg, 0, &connect->useruser);
2444
2445 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2446
Harald Welte39e2ead2009-07-23 21:13:03 +02002447 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448}
2449
2450static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2451{
2452 struct gsm48_hdr *gh = msgb_l3(msg);
2453 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2454 struct tlv_parsed tp;
2455 struct gsm_mncc connect;
2456
2457 gsm48_stop_cc_timer(trans);
2458
2459 memset(&connect, 0, sizeof(struct gsm_mncc));
2460 connect.callref = trans->callref;
2461 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2462 /* use subscriber as connected party number */
2463 if (trans->subscr) {
2464 connect.fields |= MNCC_F_CONNECTED;
2465 strncpy(connect.connected.number, trans->subscr->extension,
2466 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002467 strncpy(connect.imsi, trans->subscr->imsi,
2468 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 }
2470 /* facility */
2471 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2472 connect.fields |= MNCC_F_FACILITY;
2473 decode_facility(&connect.facility,
2474 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2475 }
2476 /* user-user */
2477 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2478 connect.fields |= MNCC_F_USERUSER;
2479 decode_useruser(&connect.useruser,
2480 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2481 }
2482 /* ss-version */
2483 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2484 connect.fields |= MNCC_F_SSVERSION;
2485 decode_ssversion(&connect.ssversion,
2486 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2487 }
2488
2489 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2490
Harald Welte596fed42009-07-23 19:06:52 +02002491 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492}
2493
2494
2495static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2496{
2497 struct gsm_mncc connect_ack;
2498
2499 gsm48_stop_cc_timer(trans);
2500
2501 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2502
2503 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2504 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002505 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 &connect_ack);
2507}
2508
2509static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2510{
2511 struct msgb *msg = gsm48_msgb_alloc();
2512 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2513
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2515
2516 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2517
Harald Welte39e2ead2009-07-23 21:13:03 +02002518 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2522{
2523 struct gsm48_hdr *gh = msgb_l3(msg);
2524 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2525 struct tlv_parsed tp;
2526 struct gsm_mncc disc;
2527
2528 gsm48_stop_cc_timer(trans);
2529
2530 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2531
2532 memset(&disc, 0, sizeof(struct gsm_mncc));
2533 disc.callref = trans->callref;
2534 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2535 /* cause */
2536 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2537 disc.fields |= MNCC_F_CAUSE;
2538 decode_cause(&disc.cause,
2539 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2540 }
2541 /* facility */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2543 disc.fields |= MNCC_F_FACILITY;
2544 decode_facility(&disc.facility,
2545 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2546 }
2547 /* user-user */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2549 disc.fields |= MNCC_F_USERUSER;
2550 decode_useruser(&disc.useruser,
2551 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2552 }
2553 /* ss-version */
2554 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2555 disc.fields |= MNCC_F_SSVERSION;
2556 decode_ssversion(&disc.ssversion,
2557 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2558 }
2559
Harald Welte596fed42009-07-23 19:06:52 +02002560 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561
2562}
2563
Harald Weltec66b71c2009-06-11 14:23:20 +08002564static struct gsm_mncc_cause default_cause = {
2565 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2566 .coding = 0,
2567 .rec = 0,
2568 .rec_val = 0,
2569 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2570 .diag_len = 0,
2571 .diag = { 0 },
2572};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573
2574static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2575{
2576 struct gsm_mncc *disc = arg;
2577 struct msgb *msg = gsm48_msgb_alloc();
2578 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2579
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2581
2582 gsm48_stop_cc_timer(trans);
2583 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2584
2585 /* cause */
2586 if (disc->fields & MNCC_F_CAUSE)
2587 encode_cause(msg, 1, &disc->cause);
2588 else
2589 encode_cause(msg, 1, &default_cause);
2590
2591 /* facility */
2592 if (disc->fields & MNCC_F_FACILITY)
2593 encode_facility(msg, 0, &disc->facility);
2594 /* progress */
2595 if (disc->fields & MNCC_F_PROGRESS)
2596 encode_progress(msg, 0, &disc->progress);
2597 /* user-user */
2598 if (disc->fields & MNCC_F_USERUSER)
2599 encode_useruser(msg, 0, &disc->useruser);
2600
2601 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002602 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603
2604 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2605
Harald Welte39e2ead2009-07-23 21:13:03 +02002606 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2610{
2611 struct gsm48_hdr *gh = msgb_l3(msg);
2612 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2613 struct tlv_parsed tp;
2614 struct gsm_mncc rel;
2615 int rc;
2616
2617 gsm48_stop_cc_timer(trans);
2618
2619 memset(&rel, 0, sizeof(struct gsm_mncc));
2620 rel.callref = trans->callref;
2621 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2622 /* cause */
2623 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2624 rel.fields |= MNCC_F_CAUSE;
2625 decode_cause(&rel.cause,
2626 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2627 }
2628 /* facility */
2629 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2630 rel.fields |= MNCC_F_FACILITY;
2631 decode_facility(&rel.facility,
2632 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2633 }
2634 /* user-user */
2635 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2636 rel.fields |= MNCC_F_USERUSER;
2637 decode_useruser(&rel.useruser,
2638 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2639 }
2640 /* ss-version */
2641 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2642 rel.fields |= MNCC_F_SSVERSION;
2643 decode_ssversion(&rel.ssversion,
2644 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2645 }
2646
Harald Weltedcaf5652009-07-23 18:56:43 +02002647 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002649 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002651 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002652 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002653 GSM48_MT_CC_RELEASE_COMPL);
2654 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 }
2656
2657 new_cc_state(trans, GSM_CSTATE_NULL);
2658
2659 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002660 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661
2662 return rc;
2663}
2664
2665static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2666{
2667 struct gsm_mncc *rel = arg;
2668 struct msgb *msg = gsm48_msgb_alloc();
2669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2670
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 gh->msg_type = GSM48_MT_CC_RELEASE;
2672
2673 trans->callref = 0;
2674
2675 gsm48_stop_cc_timer(trans);
2676 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2677
2678 /* cause */
2679 if (rel->fields & MNCC_F_CAUSE)
2680 encode_cause(msg, 0, &rel->cause);
2681 /* facility */
2682 if (rel->fields & MNCC_F_FACILITY)
2683 encode_facility(msg, 0, &rel->facility);
2684 /* user-user */
2685 if (rel->fields & MNCC_F_USERUSER)
2686 encode_useruser(msg, 0, &rel->useruser);
2687
Harald Weltedcaf5652009-07-23 18:56:43 +02002688 trans->cc.T308_second = 0;
2689 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690
Harald Weltedcaf5652009-07-23 18:56:43 +02002691 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2693
Harald Welte39e2ead2009-07-23 21:13:03 +02002694 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695}
2696
2697static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2698{
2699 struct gsm48_hdr *gh = msgb_l3(msg);
2700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2701 struct tlv_parsed tp;
2702 struct gsm_mncc rel;
2703 int rc = 0;
2704
2705 gsm48_stop_cc_timer(trans);
2706
2707 memset(&rel, 0, sizeof(struct gsm_mncc));
2708 rel.callref = trans->callref;
2709 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2710 /* cause */
2711 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2712 rel.fields |= MNCC_F_CAUSE;
2713 decode_cause(&rel.cause,
2714 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2715 }
2716 /* facility */
2717 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2718 rel.fields |= MNCC_F_FACILITY;
2719 decode_facility(&rel.facility,
2720 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2721 }
2722 /* user-user */
2723 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2724 rel.fields |= MNCC_F_USERUSER;
2725 decode_useruser(&rel.useruser,
2726 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2727 }
2728 /* ss-version */
2729 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2730 rel.fields |= MNCC_F_SSVERSION;
2731 decode_ssversion(&rel.ssversion,
2732 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2733 }
2734
2735 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002736 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002738 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 MNCC_REJ_IND, &rel);
2740 break;
2741 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002742 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002744 /* FIXME: in case of multiple calls, we can't simply
2745 * hang up here ! */
2746 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 break;
2748 default:
Harald Welte596fed42009-07-23 19:06:52 +02002749 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750 MNCC_REL_IND, &rel);
2751 }
2752 }
2753
2754 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002755 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756
2757 return rc;
2758}
2759
2760static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2761{
2762 struct gsm_mncc *rel = arg;
2763 struct msgb *msg = gsm48_msgb_alloc();
2764 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2765
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2767
2768 trans->callref = 0;
2769
2770 gsm48_stop_cc_timer(trans);
2771
2772 /* cause */
2773 if (rel->fields & MNCC_F_CAUSE)
2774 encode_cause(msg, 0, &rel->cause);
2775 /* facility */
2776 if (rel->fields & MNCC_F_FACILITY)
2777 encode_facility(msg, 0, &rel->facility);
2778 /* user-user */
2779 if (rel->fields & MNCC_F_USERUSER)
2780 encode_useruser(msg, 0, &rel->useruser);
2781
Harald Weltedcaf5652009-07-23 18:56:43 +02002782 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783
Harald Welte39e2ead2009-07-23 21:13:03 +02002784 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002785}
2786
2787static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2788{
2789 struct gsm48_hdr *gh = msgb_l3(msg);
2790 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2791 struct tlv_parsed tp;
2792 struct gsm_mncc fac;
2793
2794 memset(&fac, 0, sizeof(struct gsm_mncc));
2795 fac.callref = trans->callref;
2796 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2797 /* facility */
2798 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2799 fac.fields |= MNCC_F_FACILITY;
2800 decode_facility(&fac.facility,
2801 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2802 }
2803 /* ss-version */
2804 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2805 fac.fields |= MNCC_F_SSVERSION;
2806 decode_ssversion(&fac.ssversion,
2807 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2808 }
2809
Harald Welte596fed42009-07-23 19:06:52 +02002810 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811}
2812
2813static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2814{
2815 struct gsm_mncc *fac = arg;
2816 struct msgb *msg = gsm48_msgb_alloc();
2817 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2818
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819 gh->msg_type = GSM48_MT_CC_FACILITY;
2820
2821 /* facility */
2822 encode_facility(msg, 1, &fac->facility);
2823
Harald Welte39e2ead2009-07-23 21:13:03 +02002824 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825}
2826
2827static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2828{
2829 struct gsm_mncc hold;
2830
2831 memset(&hold, 0, sizeof(struct gsm_mncc));
2832 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002833 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834}
2835
2836static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2837{
2838 struct msgb *msg = gsm48_msgb_alloc();
2839 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2840
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2842
Harald Welte39e2ead2009-07-23 21:13:03 +02002843 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844}
2845
2846static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2847{
2848 struct gsm_mncc *hold_rej = arg;
2849 struct msgb *msg = gsm48_msgb_alloc();
2850 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2851
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2853
2854 /* cause */
2855 if (hold_rej->fields & MNCC_F_CAUSE)
2856 encode_cause(msg, 1, &hold_rej->cause);
2857 else
2858 encode_cause(msg, 1, &default_cause);
2859
Harald Welte39e2ead2009-07-23 21:13:03 +02002860 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861}
2862
2863static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2864{
2865 struct gsm_mncc retrieve;
2866
2867 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2868 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002869 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2870 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871}
2872
2873static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2874{
2875 struct msgb *msg = gsm48_msgb_alloc();
2876 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2877
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2879
Harald Welte39e2ead2009-07-23 21:13:03 +02002880 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881}
2882
2883static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2884{
2885 struct gsm_mncc *retrieve_rej = arg;
2886 struct msgb *msg = gsm48_msgb_alloc();
2887 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2888
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2890
2891 /* cause */
2892 if (retrieve_rej->fields & MNCC_F_CAUSE)
2893 encode_cause(msg, 1, &retrieve_rej->cause);
2894 else
2895 encode_cause(msg, 1, &default_cause);
2896
Harald Welte39e2ead2009-07-23 21:13:03 +02002897 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898}
2899
2900static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2901{
2902 struct gsm48_hdr *gh = msgb_l3(msg);
2903 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2904 struct tlv_parsed tp;
2905 struct gsm_mncc dtmf;
2906
2907 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2908 dtmf.callref = trans->callref;
2909 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2910 /* keypad facility */
2911 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2912 dtmf.fields |= MNCC_F_KEYPAD;
2913 decode_keypad(&dtmf.keypad,
2914 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2915 }
2916
Harald Welte596fed42009-07-23 19:06:52 +02002917 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918}
2919
2920static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2921{
2922 struct gsm_mncc *dtmf = arg;
2923 struct msgb *msg = gsm48_msgb_alloc();
2924 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2925
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2927
2928 /* keypad */
2929 if (dtmf->fields & MNCC_F_KEYPAD)
2930 encode_keypad(msg, dtmf->keypad);
2931
Harald Welte39e2ead2009-07-23 21:13:03 +02002932 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933}
2934
2935static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2936{
2937 struct gsm_mncc *dtmf = arg;
2938 struct msgb *msg = gsm48_msgb_alloc();
2939 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2940
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2942
2943 /* cause */
2944 if (dtmf->fields & MNCC_F_CAUSE)
2945 encode_cause(msg, 1, &dtmf->cause);
2946 else
2947 encode_cause(msg, 1, &default_cause);
2948
Harald Welte39e2ead2009-07-23 21:13:03 +02002949 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950}
2951
2952static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2953{
2954 struct msgb *msg = gsm48_msgb_alloc();
2955 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2956
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2958
Harald Welte39e2ead2009-07-23 21:13:03 +02002959 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960}
2961
2962static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2963{
2964 struct gsm_mncc dtmf;
2965
2966 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2967 dtmf.callref = trans->callref;
2968
Harald Welte596fed42009-07-23 19:06:52 +02002969 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970}
2971
2972static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2973{
2974 struct gsm48_hdr *gh = msgb_l3(msg);
2975 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2976 struct tlv_parsed tp;
2977 struct gsm_mncc modify;
2978
2979 memset(&modify, 0, sizeof(struct gsm_mncc));
2980 modify.callref = trans->callref;
2981 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2982 /* bearer capability */
2983 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2984 modify.fields |= MNCC_F_BEARER_CAP;
2985 decode_bearer_cap(&modify.bearer_cap,
2986 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2987 }
2988
2989 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2990
Harald Welte596fed42009-07-23 19:06:52 +02002991 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992}
2993
2994static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2995{
2996 struct gsm_mncc *modify = arg;
2997 struct msgb *msg = gsm48_msgb_alloc();
2998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2999
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 gh->msg_type = GSM48_MT_CC_MODIFY;
3001
3002 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3003
3004 /* bearer capability */
3005 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3006
3007 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3008
Harald Welte39e2ead2009-07-23 21:13:03 +02003009 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010}
3011
3012static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3013{
3014 struct gsm48_hdr *gh = msgb_l3(msg);
3015 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3016 struct tlv_parsed tp;
3017 struct gsm_mncc modify;
3018
3019 gsm48_stop_cc_timer(trans);
3020
3021 memset(&modify, 0, sizeof(struct gsm_mncc));
3022 modify.callref = trans->callref;
3023 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3024 /* bearer capability */
3025 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3026 modify.fields |= MNCC_F_BEARER_CAP;
3027 decode_bearer_cap(&modify.bearer_cap,
3028 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3029 }
3030
3031 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3032
Harald Welte596fed42009-07-23 19:06:52 +02003033 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034}
3035
3036static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3037{
3038 struct gsm_mncc *modify = arg;
3039 struct msgb *msg = gsm48_msgb_alloc();
3040 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3041
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3043
3044 /* bearer capability */
3045 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3046
3047 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3048
Harald Welte39e2ead2009-07-23 21:13:03 +02003049 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050}
3051
3052static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3053{
3054 struct gsm48_hdr *gh = msgb_l3(msg);
3055 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3056 struct tlv_parsed tp;
3057 struct gsm_mncc modify;
3058
3059 gsm48_stop_cc_timer(trans);
3060
3061 memset(&modify, 0, sizeof(struct gsm_mncc));
3062 modify.callref = trans->callref;
3063 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3064 /* bearer capability */
3065 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3066 modify.fields |= GSM48_IE_BEARER_CAP;
3067 decode_bearer_cap(&modify.bearer_cap,
3068 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3069 }
3070 /* cause */
3071 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3072 modify.fields |= MNCC_F_CAUSE;
3073 decode_cause(&modify.cause,
3074 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3075 }
3076
3077 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3078
Harald Welte596fed42009-07-23 19:06:52 +02003079 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080}
3081
3082static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3083{
3084 struct gsm_mncc *modify = arg;
3085 struct msgb *msg = gsm48_msgb_alloc();
3086 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3087
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3089
3090 /* bearer capability */
3091 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3092 /* cause */
3093 encode_cause(msg, 1, &modify->cause);
3094
3095 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3096
Harald Welte39e2ead2009-07-23 21:13:03 +02003097 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003098}
3099
3100static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3101{
3102 struct gsm_mncc *notify = arg;
3103 struct msgb *msg = gsm48_msgb_alloc();
3104 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3105
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 gh->msg_type = GSM48_MT_CC_NOTIFY;
3107
3108 /* notify */
3109 encode_notify(msg, notify->notify);
3110
Harald Welte39e2ead2009-07-23 21:13:03 +02003111 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112}
3113
3114static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3115{
3116 struct gsm48_hdr *gh = msgb_l3(msg);
3117 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3118// struct tlv_parsed tp;
3119 struct gsm_mncc notify;
3120
3121 memset(&notify, 0, sizeof(struct gsm_mncc));
3122 notify.callref = trans->callref;
3123// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3124 if (payload_len >= 1)
3125 decode_notify(&notify.notify, gh->data);
3126
Harald Welte596fed42009-07-23 19:06:52 +02003127 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128}
3129
3130static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3131{
3132 struct gsm_mncc *user = arg;
3133 struct msgb *msg = gsm48_msgb_alloc();
3134 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3135
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136 gh->msg_type = GSM48_MT_CC_USER_INFO;
3137
3138 /* user-user */
3139 if (user->fields & MNCC_F_USERUSER)
3140 encode_useruser(msg, 1, &user->useruser);
3141 /* more data */
3142 if (user->more)
3143 encode_more(msg);
3144
Harald Welte39e2ead2009-07-23 21:13:03 +02003145 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146}
3147
3148static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3149{
3150 struct gsm48_hdr *gh = msgb_l3(msg);
3151 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3152 struct tlv_parsed tp;
3153 struct gsm_mncc user;
3154
3155 memset(&user, 0, sizeof(struct gsm_mncc));
3156 user.callref = trans->callref;
3157 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3158 /* user-user */
3159 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3160 user.fields |= MNCC_F_USERUSER;
3161 decode_useruser(&user.useruser,
3162 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3163 }
3164 /* more data */
3165 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3166 user.more = 1;
3167
Harald Welte596fed42009-07-23 19:06:52 +02003168 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169}
3170
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003171static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172{
3173 struct gsm_mncc *mode = arg;
3174
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003175 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003176}
3177
3178static struct downstate {
3179 u_int32_t states;
3180 int type;
3181 int (*rout) (struct gsm_trans *trans, void *arg);
3182} downstatelist[] = {
3183 /* mobile originating call establishment */
3184 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3185 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3186 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3187 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3188 {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 */
3189 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3190 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3191 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3192 /* mobile terminating call establishment */
3193 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3194 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3195 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3196 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3197 /* signalling during call */
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3200 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3201 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3202 {ALL_STATES,
3203 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3204 {ALL_STATES,
3205 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3206 {ALL_STATES,
3207 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3208 {SBIT(GSM_CSTATE_ACTIVE),
3209 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3210 {SBIT(GSM_CSTATE_ACTIVE),
3211 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3212 {SBIT(GSM_CSTATE_ACTIVE),
3213 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3214 {SBIT(GSM_CSTATE_ACTIVE),
3215 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3218 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3219 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3220 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3221 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3222 {SBIT(GSM_CSTATE_ACTIVE),
3223 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3224 /* clearing */
3225 {SBIT(GSM_CSTATE_INITIATED),
3226 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3227 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3228 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3229 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3230 MNCC_REL_REQ, gsm48_cc_tx_release},
3231 /* special */
3232 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003233 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003234};
3235
3236#define DOWNSLLEN \
3237 (sizeof(downstatelist) / sizeof(struct downstate))
3238
3239
3240int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3241{
Harald Welte1a6f7982009-08-09 18:52:33 +02003242 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003244 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003246 struct gsm_mncc *data = arg, rel;
3247
3248 /* handle special messages */
3249 switch(msg_type) {
3250 case MNCC_BRIDGE:
3251 return tch_bridge(net, arg);
3252 case MNCC_FRAME_DROP:
3253 return tch_recv(net, arg, 0);
3254 case MNCC_FRAME_RECV:
3255 return tch_recv(net, arg, 1);
3256 case GSM_TRAU_FRAME:
3257 return tch_frame(net, arg);
3258 }
3259
3260 memset(&rel, 0, sizeof(struct gsm_mncc));
3261 rel.callref = data->callref;
3262
3263 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003264 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265
3266 /* Callref unknown */
3267 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003268 struct gsm_subscriber *subscr;
3269
Harald Welte4a3464c2009-07-04 10:11:24 +02003270 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003271 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3272 "Received '%s' from MNCC with "
3273 "unknown callref %d\n", data->called.number,
3274 get_mncc_name(msg_type), data->callref);
3275 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003276 return mncc_release_ind(net, NULL, data->callref,
3277 GSM48_CAUSE_LOC_PRN_S_LU,
3278 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003280 if (!data->called.number[0] && !data->imsi[0]) {
3281 DEBUGP(DCC, "(bts - trx - ts - ti) "
3282 "Received '%s' from MNCC with "
3283 "no number or IMSI\n", get_mncc_name(msg_type));
3284 /* Invalid number */
3285 return mncc_release_ind(net, NULL, data->callref,
3286 GSM48_CAUSE_LOC_PRN_S_LU,
3287 GSM48_CC_CAUSE_INV_NR_FORMAT);
3288 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003290 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003291 subscr = subscr_get_by_extension(net,
3292 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003293 else
Harald Welte9176bd42009-07-23 18:46:00 +02003294 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 /* If subscriber is not found */
3296 if (!subscr) {
3297 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3298 "Received '%s' from MNCC with "
3299 "unknown subscriber %s\n", data->called.number,
3300 get_mncc_name(msg_type), data->called.number);
3301 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003302 return mncc_release_ind(net, NULL, data->callref,
3303 GSM48_CAUSE_LOC_PRN_S_LU,
3304 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003305 }
3306 /* If subscriber is not "attached" */
3307 if (!subscr->lac) {
3308 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3309 "Received '%s' from MNCC with "
3310 "detached subscriber %s\n", data->called.number,
3311 get_mncc_name(msg_type), data->called.number);
3312 subscr_put(subscr);
3313 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003314 return mncc_release_ind(net, NULL, data->callref,
3315 GSM48_CAUSE_LOC_PRN_S_LU,
3316 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003317 }
3318 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003319 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3320 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003321 DEBUGP(DCC, "No memory for trans.\n");
3322 subscr_put(subscr);
3323 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003324 mncc_release_ind(net, NULL, data->callref,
3325 GSM48_CAUSE_LOC_PRN_S_LU,
3326 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003327 return -ENOMEM;
3328 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003330 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003331 /* If subscriber has no lchan */
3332 if (!lchan) {
3333 /* find transaction with this subscriber already paging */
3334 llist_for_each_entry(transt, &net->trans_list, entry) {
3335 /* Transaction of our lchan? */
3336 if (transt == trans ||
3337 transt->subscr != subscr)
3338 continue;
3339 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3340 "Received '%s' from MNCC with "
3341 "unallocated channel, paging already "
3342 "started.\n", bts->nr,
3343 data->called.number,
3344 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003345 subscr_put(subscr);
3346 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003347 return 0;
3348 }
3349 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003350 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003351 /* Trigger paging */
3352 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3353 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003354 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 return 0;
3356 }
3357 /* Assign lchan */
3358 trans->lchan = lchan;
3359 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003360 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003361 }
3362 lchan = trans->lchan;
3363
3364 /* if paging did not respond yet */
3365 if (!lchan) {
3366 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3367 "Received '%s' from MNCC in paging state\n",
3368 (trans->subscr)?(trans->subscr->extension):"-",
3369 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003370 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3371 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003372 if (msg_type == MNCC_REL_REQ)
3373 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3374 else
3375 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3376 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003377 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003378 return rc;
3379 }
3380
3381 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3382 "Received '%s' from MNCC in state %d (%s)\n",
3383 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3384 trans->transaction_id,
3385 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003386 get_mncc_name(msg_type), trans->cc.state,
3387 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003388
3389 /* Find function for current state and message */
3390 for (i = 0; i < DOWNSLLEN; i++)
3391 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003392 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003393 break;
3394 if (i == DOWNSLLEN) {
3395 DEBUGP(DCC, "Message unhandled at this state.\n");
3396 return 0;
3397 }
3398
3399 rc = downstatelist[i].rout(trans, arg);
3400
3401 return rc;
3402}
3403
3404
3405static struct datastate {
3406 u_int32_t states;
3407 int type;
3408 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3409} datastatelist[] = {
3410 /* mobile originating call establishment */
3411 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3412 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3413 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3414 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3415 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3416 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3417 /* mobile terminating call establishment */
3418 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3419 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3420 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3421 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3422 {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 */
3423 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3424 /* signalling during call */
3425 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3426 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3427 {SBIT(GSM_CSTATE_ACTIVE),
3428 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3429 {ALL_STATES,
3430 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3431 {ALL_STATES,
3432 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3433 {ALL_STATES,
3434 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3435 {SBIT(GSM_CSTATE_ACTIVE),
3436 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3437 {SBIT(GSM_CSTATE_ACTIVE),
3438 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3439 {SBIT(GSM_CSTATE_ACTIVE),
3440 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3441 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3442 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3443 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3444 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3445 {SBIT(GSM_CSTATE_ACTIVE),
3446 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3447 /* clearing */
3448 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3449 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3450 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3451 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3452 {ALL_STATES, /* 5.4.3.4 */
3453 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3454};
3455
3456#define DATASLLEN \
3457 (sizeof(datastatelist) / sizeof(struct datastate))
3458
Harald Welte4bc90a12008-12-27 16:32:52 +00003459static int gsm0408_rcv_cc(struct msgb *msg)
3460{
3461 struct gsm48_hdr *gh = msgb_l3(msg);
3462 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003463 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003464 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003465 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003466 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003467
Harald Welte4bfdfe72009-06-10 23:11:52 +08003468 if (msg_type & 0x80) {
3469 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3470 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003471 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472
3473 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003474 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003475
Harald Welte6f5aee02009-07-23 21:21:14 +02003476 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003477 "Received '%s' from MS in state %d (%s)\n",
3478 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3479 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003480 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003481 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003482
3483 /* Create transaction */
3484 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003485 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003486 "creating new trans.\n", transaction_id);
3487 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003488 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3489 transaction_id, new_callref++);
3490 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003491 DEBUGP(DCC, "No memory for trans.\n");
3492 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003493 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003494 GSM48_MT_CC_RELEASE_COMPL);
3495 return -ENOMEM;
3496 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003497 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003498 trans->lchan = lchan;
3499 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003500 }
3501
3502 /* find function for current state and message */
3503 for (i = 0; i < DATASLLEN; i++)
3504 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003505 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 break;
3507 if (i == DATASLLEN) {
3508 DEBUGP(DCC, "Message unhandled at this state.\n");
3509 return 0;
3510 }
3511
3512 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003513
3514 return rc;
3515}
3516
Harald Welte52b1f982008-12-23 20:25:15 +00003517/* 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 +02003518int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003519{
3520 struct gsm48_hdr *gh = msgb_l3(msg);
3521 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003522 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003523
3524 switch (pdisc) {
3525 case GSM48_PDISC_CC:
3526 rc = gsm0408_rcv_cc(msg);
3527 break;
3528 case GSM48_PDISC_MM:
3529 rc = gsm0408_rcv_mm(msg);
3530 break;
3531 case GSM48_PDISC_RR:
3532 rc = gsm0408_rcv_rr(msg);
3533 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003534 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003535 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003536 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003537 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003538 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003539 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003540 pdisc);
3541 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003542 case GSM48_PDISC_NC_SS:
3543 rc = handle_rcv_ussd(msg);
3544 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003545 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003546 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003547 pdisc);
3548 break;
3549 }
3550
3551 return rc;
3552}
Harald Welte8470bf22008-12-25 23:28:35 +00003553
Harald Welte4bfdfe72009-06-10 23:11:52 +08003554/* dequeue messages to layer 4 */
3555int bsc_upqueue(struct gsm_network *net)
3556{
3557 struct gsm_mncc *mncc;
3558 struct msgb *msg;
3559 int work = 0;
3560
3561 if (net)
3562 while ((msg = msgb_dequeue(&net->upqueue))) {
3563 mncc = (struct gsm_mncc *)msg->data;
3564 if (net->mncc_recv)
3565 net->mncc_recv(net, mncc->msg_type, mncc);
3566 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003567 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003568 }
3569
3570 return work;
3571}
Harald Weltedcaf5652009-07-23 18:56:43 +02003572
Harald Welte805f6442009-07-28 18:25:29 +02003573/*
3574 * This will be ran by the linker when loading the DSO. We use it to
3575 * do system initialization, e.g. registration of signal handlers.
3576 */
3577static __attribute__((constructor)) void on_dso_load_0408(void)
3578{
Harald Welte805f6442009-07-28 18:25:29 +02003579 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3580 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3581}