blob: 12cc36245290c5ed9315b9dab397b388d844e680 [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 Weltef7c43522009-06-09 20:24:21 +0000169static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
170 int len)
171{
172 memset(rep, 0, sizeof(*rep));
173
174 if (data[0] & 0x80)
175 rep->flags |= MEAS_REP_F_BA1;
176 if (data[0] & 0x40)
177 rep->flags |= MEAS_REP_F_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200178 if ((data[1] & 0x40) == 0x00)
Harald Weltef7c43522009-06-09 20:24:21 +0000179 rep->flags |= MEAS_REP_F_VALID;
180
181 rep->rxlev_full = data[0] & 0x3f;
182 rep->rxlev_sub = data[1] & 0x3f;
183 rep->rxqual_full = (data[3] >> 4) & 0x7;
184 rep->rxqual_sub = (data[3] >> 1) & 0x7;
185 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
186 if (rep->num_cell < 1)
187 return;
188
189 /* an encoding nightmare in perfection */
190
191 rep->cell[0].rxlev = data[4] & 0x3f;
192 rep->cell[0].bcch_freq = data[5] >> 2;
193 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
194 if (rep->num_cell < 2)
195 return;
196
197 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
198 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
199 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
200 if (rep->num_cell < 3)
201 return;
202
203 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
204 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
205 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
206 if (rep->num_cell < 4)
207 return;
208
209 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
210 rep->cell[3].bcch_freq = data[11] & 0x1f;
211 rep->cell[3].bsic = data[12] >> 2;
212 if (rep->num_cell < 5)
213 return;
214
215 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
216 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
217 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
218 if (rep->num_cell < 6)
219 return;
220
221 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
222 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
223 rep->cell[5].bsic = data[16] & 0x3f;
224}
225
Holger Freytherd51524f2009-06-09 08:27:07 +0000226int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000227static int gsm48_tx_simple(struct gsm_lchan *lchan,
228 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000229static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000230
Harald Welte52b1f982008-12-23 20:25:15 +0000231struct gsm_lai {
232 u_int16_t mcc;
233 u_int16_t mnc;
234 u_int16_t lac;
235};
236
Holger Freythere97f7fb2008-12-31 18:52:11 +0000237static int reject_cause = 0;
238void gsm0408_set_reject_cause(int cause)
239{
240 reject_cause = cause;
241}
242
Harald Welte4bfdfe72009-06-10 23:11:52 +0800243static u_int32_t new_callref = 0x80000001;
244
Holger Freyther73487a22008-12-31 18:53:57 +0000245static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
246 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000247{
248 if (!subscriber)
249 return 0;
250
Holger Freyther73487a22008-12-31 18:53:57 +0000251 /*
252 * Do not send accept yet as more information should arrive. Some
253 * phones will not send us the information and we will have to check
254 * what we want to do with that.
255 */
256 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
257 return 0;
258
Jan Luebbe06513f22009-08-12 12:48:00 +0200259 switch (subscriber->net->auth_policy) {
260 case GSM_AUTH_POLICY_CLOSED:
261 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200262 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200263 if (subscriber->authorized)
264 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200265 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200266 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000267 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200268 default:
269 return 0;
270 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000271}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000272
Holger Freyther73487a22008-12-31 18:53:57 +0000273static void release_loc_updating_req(struct gsm_lchan *lchan)
274{
Harald Welte179f0642008-12-31 23:59:18 +0000275 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000276 return;
277
Harald Welteff117a82009-05-23 05:22:08 +0000278 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200279 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000280 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000281 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000282}
283
284static void allocate_loc_updating_req(struct gsm_lchan *lchan)
285{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000286 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000287 release_loc_updating_req(lchan);
288
Harald Welte470ec292009-06-26 20:25:23 +0200289 lchan->loc_operation = talloc_zero(tall_locop_ctx,
290 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000291}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000292
Holger Freytherd51524f2009-06-09 08:27:07 +0000293static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
294{
Holger Freytherd51524f2009-06-09 08:27:07 +0000295 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200296 int rc;
297
Holger Freytherd51524f2009-06-09 08:27:07 +0000298 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200300 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welteee5ad162009-08-09 19:07:00 +0200301 /* call subscr_update after putting the loc_upd_acc
302 * in the transmit queue, since S_SUBSCR_ATTACHED might
303 * trigger further action like SMS delivery */
304 subscr_update(lchan->subscr, msg->trx->bts,
305 GSM_SUBSCRIBER_UPDATE_ATTACHED);
306 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000307 }
308
309 return 0;
310}
311
Holger Freyther7c19f742009-06-06 13:54:35 +0000312static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
313 void *handler_data, void *signal_data)
314{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800315 struct gsm_trans *trans, *temp;
316
Holger Freyther7c19f742009-06-06 13:54:35 +0000317 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
318 return 0;
319
320 /*
321 * Cancel any outstanding location updating request
322 * operation taking place on the lchan.
323 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200324 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200325 if (!lchan)
326 return 0;
327
Holger Freyther7c19f742009-06-06 13:54:35 +0000328 release_loc_updating_req(lchan);
329
Harald Welte4bfdfe72009-06-10 23:11:52 +0800330 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200331 /* FIXME: this is not neccessarily the right thing to do, we should
332 * only set trans->lchan to NULL and wait for another lchan to be
333 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800334 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
335 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200336 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800337 }
338
Holger Freyther7c19f742009-06-06 13:54:35 +0000339 return 0;
340}
341
Harald Welte09e38af2009-02-16 22:52:23 +0000342static const char bcd_num_digits[] = {
343 '0', '1', '2', '3', '4', '5', '6', '7',
344 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
345};
346
Harald Welte0c389302009-06-10 12:08:54 +0800347/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
348int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
349 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000350{
351 u_int8_t in_len = bcd_lv[0];
352 int i;
353
Harald Welte0c389302009-06-10 12:08:54 +0800354 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000355 /* lower nibble */
356 output_len--;
357 if (output_len <= 1)
358 break;
359 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
360
361 /* higher nibble */
362 output_len--;
363 if (output_len <= 1)
364 break;
365 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
366 }
367 if (output_len >= 1)
368 *output++ = '\0';
369
Harald Welte0c389302009-06-10 12:08:54 +0800370 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000371}
372
373/* convert a single ASCII character to call-control BCD */
374static int asc_to_bcd(const char asc)
375{
376 int i;
377
378 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
379 if (bcd_num_digits[i] == asc)
380 return i;
381 }
382 return -EINVAL;
383}
384
Harald Welte0c389302009-06-10 12:08:54 +0800385/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000386int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800387 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000388{
389 int in_len = strlen(input);
390 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800391 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000392
393 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800394 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000395 if (in_len % 2)
396 bcd_lv[0]++;
397
Harald Welte0c389302009-06-10 12:08:54 +0800398 if (bcd_lv[0] > max_len)
399 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000400
401 for (i = 0; i < in_len; i++) {
402 int rc = asc_to_bcd(input[i]);
403 if (rc < 0)
404 return rc;
405 if (i % 2 == 0)
406 *bcd_cur = rc;
407 else
408 *bcd_cur++ |= (rc << 4);
409 }
410 /* append padding nibble in case of odd length */
411 if (i % 2)
412 *bcd_cur++ |= 0xf0;
413
414 /* return how many bytes we used */
415 return (bcd_cur - bcd_lv);
416}
417
Harald Welte0c389302009-06-10 12:08:54 +0800418/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800419static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800420 const u_int8_t *lv)
421{
422 u_int8_t in_len = lv[0];
423 int i, s;
424
425 if (in_len < 1)
426 return -EINVAL;
427
Harald Welte4bfdfe72009-06-10 23:11:52 +0800428 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800429
430 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800431 bcap->transfer = lv[1] & 0x07;
432 bcap->mode = (lv[1] & 0x08) >> 3;
433 bcap->coding = (lv[1] & 0x10) >> 4;
434 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800435
436 i = 1;
437 s = 0;
438 while(!(lv[i] & 0x80)) {
439 i++; /* octet 3a etc */
440 if (in_len < i)
441 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442 bcap->speech_ver[s++] = lv[i] & 0x0f;
443 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800444 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800445 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800446 if (s == 7) /* maximum speech versions + end of list */
447 return 0;
448 }
449
450 return 0;
451}
452
453/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800454static int encode_bearer_cap(struct msgb *msg, int lv_only,
455 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800456{
457 u_int8_t lv[32 + 1];
458 int i, s;
459
Harald Welte4bfdfe72009-06-10 23:11:52 +0800460 lv[1] = bcap->transfer;
461 lv[1] |= bcap->mode << 3;
462 lv[1] |= bcap->coding << 4;
463 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800464
465 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800466 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800467 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800468 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800469 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800471 }
472 lv[i] |= 0x80; /* last IE of octet 3 etc */
473
474 lv[0] = i;
475 if (lv_only)
476 msgb_lv_put(msg, lv[0], lv+1);
477 else
478 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
479
480 return 0;
481}
482
483/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800484static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800485{
486 u_int8_t in_len = lv[0];
487
488 if (in_len < 1)
489 return -EINVAL;
490
491 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800492 ccap->dtmf = lv[1] & 0x01;
493 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800494
495 return 0;
496}
497
498/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800499static int decode_called(struct gsm_mncc_number *called,
500 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800501{
502 u_int8_t in_len = lv[0];
503
504 if (in_len < 1)
505 return -EINVAL;
506
507 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800508 called->plan = lv[1] & 0x0f;
509 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800510
511 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800512 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800513
514 return 0;
515}
516
517/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800518static int encode_called(struct msgb *msg,
519 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800520{
521 u_int8_t lv[18];
522 int ret;
523
524 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800525 lv[1] = called->plan;
526 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800527
528 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800529 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800530 if (ret < 0)
531 return ret;
532
533 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
534
535 return 0;
536}
537
538/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539static int encode_callerid(struct msgb *msg, int ie,
540 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800541{
542 u_int8_t lv[13];
543 int h_len = 1;
544 int ret;
545
546 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800547 lv[1] = callerid->plan;
548 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800549
Harald Welte4bfdfe72009-06-10 23:11:52 +0800550 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800551 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800552 lv[2] = callerid->screen;
553 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800554 lv[2] |= 0x80;
555 h_len++;
556 } else
557 lv[1] |= 0x80;
558
559 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800560 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800561 if (ret < 0)
562 return ret;
563
564 msgb_tlv_put(msg, ie, lv[0], lv+1);
565
566 return 0;
567}
568
569/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800570static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800571 const u_int8_t *lv)
572{
573 u_int8_t in_len = lv[0];
574 int i;
575
576 if (in_len < 2)
577 return -EINVAL;
578
Harald Welte4bfdfe72009-06-10 23:11:52 +0800579 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800580
581 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800582 cause->location = lv[1] & 0x0f;
583 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800584
585 i = 1;
586 if (!(lv[i] & 0x80)) {
587 i++; /* octet 3a */
588 if (in_len < i+1)
589 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800590 cause->rec = 1;
591 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800592
593 }
594 i++;
595
596 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800597 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800598 i++;
599
600 if (in_len < i) /* no diag */
601 return 0;
602
603 if (in_len - (i-1) > 32) /* maximum 32 octets */
604 return 0;
605
606 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800607 memcpy(cause->diag, lv + i, in_len - (i-1));
608 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800609
610 return 0;
611}
612
613/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800614static int encode_cause(struct msgb *msg, int lv_only,
615 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800616{
617 u_int8_t lv[32+4];
618 int i;
619
Harald Welte4bfdfe72009-06-10 23:11:52 +0800620 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800621 return -EINVAL;
622
623 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624 lv[1] = cause->location;
625 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800626
627 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800628 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800629 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800630 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800631 }
632 lv[i] |= 0x80; /* end of octet 3 */
633
634 /* octet 4 */
635 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800636 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800637
638 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800639 if (cause->diag_len) {
640 memcpy(lv + i, cause->diag, cause->diag_len);
641 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800642 }
643
644 lv[0] = i;
645 if (lv_only)
646 msgb_lv_put(msg, lv[0], lv+1);
647 else
648 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
649
650 return 0;
651}
652
653/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800654static int encode_calling(struct msgb *msg,
655 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800656{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800657 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800658}
659
660/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661static int encode_connected(struct msgb *msg,
662 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800663{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800664 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800665}
666
667/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800668static int encode_redirecting(struct msgb *msg,
669 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800670{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800671 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800672}
673
674/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800675static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800676 const u_int8_t *lv)
677{
678 u_int8_t in_len = lv[0];
679
680 if (in_len < 1)
681 return -EINVAL;
682
Harald Welte4bfdfe72009-06-10 23:11:52 +0800683 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800684 return -EINVAL;
685
Harald Welte4bfdfe72009-06-10 23:11:52 +0800686 memcpy(facility->info, lv+1, in_len);
687 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800688
689 return 0;
690}
691
692/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800693static int encode_facility(struct msgb *msg, int lv_only,
694 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800695{
696 u_int8_t lv[GSM_MAX_FACILITY + 1];
697
Harald Welte4bfdfe72009-06-10 23:11:52 +0800698 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800699 return -EINVAL;
700
Harald Welte4bfdfe72009-06-10 23:11:52 +0800701 memcpy(lv+1, facility->info, facility->len);
702 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800703 if (lv_only)
704 msgb_lv_put(msg, lv[0], lv+1);
705 else
706 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
707
708 return 0;
709}
710
711/* decode 'notify' */
712static int decode_notify(int *notify, const u_int8_t *v)
713{
714 *notify = v[0] & 0x7f;
715
716 return 0;
717}
718
719/* encode 'notify' */
720static int encode_notify(struct msgb *msg, int notify)
721{
722 msgb_v_put(msg, notify | 0x80);
723
724 return 0;
725}
726
727/* encode 'signal' */
728static int encode_signal(struct msgb *msg, int signal)
729{
730 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
731
732 return 0;
733}
734
735/* decode 'keypad' */
736static int decode_keypad(int *keypad, const u_int8_t *lv)
737{
738 u_int8_t in_len = lv[0];
739
740 if (in_len < 1)
741 return -EINVAL;
742
743 *keypad = lv[1] & 0x7f;
744
745 return 0;
746}
747
748/* encode 'keypad' */
749static int encode_keypad(struct msgb *msg, int keypad)
750{
751 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
752
753 return 0;
754}
755
756/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800757static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800758 const u_int8_t *lv)
759{
760 u_int8_t in_len = lv[0];
761
762 if (in_len < 2)
763 return -EINVAL;
764
Harald Welte4bfdfe72009-06-10 23:11:52 +0800765 progress->coding = (lv[1] & 0x60) >> 5;
766 progress->location = lv[1] & 0x0f;
767 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800768
769 return 0;
770}
771
772/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800773static int encode_progress(struct msgb *msg, int lv_only,
774 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800775{
776 u_int8_t lv[3];
777
778 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800779 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
780 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800781 if (lv_only)
782 msgb_lv_put(msg, lv[0], lv+1);
783 else
784 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
785
786 return 0;
787}
788
789/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800790static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800791 const u_int8_t *lv)
792{
793 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800794 char *info = uu->info;
795 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800796 int i;
797
798 if (in_len < 1)
799 return -EINVAL;
800
Harald Welte4bfdfe72009-06-10 23:11:52 +0800801 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800802
803 for (i = 2; i <= in_len; i++) {
804 info_len--;
805 if (info_len <= 1)
806 break;
807 *info++ = lv[i];
808 }
809 if (info_len >= 1)
810 *info++ = '\0';
811
812 return 0;
813}
814
815/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800816static int encode_useruser(struct msgb *msg, int lv_only,
817 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800818{
819 u_int8_t lv[GSM_MAX_USERUSER + 2];
820
Harald Welte4bfdfe72009-06-10 23:11:52 +0800821 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800822 return -EINVAL;
823
Harald Welte4bfdfe72009-06-10 23:11:52 +0800824 lv[0] = 1 + strlen(uu->info);
825 lv[1] = uu->proto;
826 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800827 if (lv_only)
828 msgb_lv_put(msg, lv[0], lv+1);
829 else
830 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
831
832 return 0;
833}
834
835/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800836static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800837 const u_int8_t *lv)
838{
839 u_int8_t in_len = lv[0];
840
Harald Welte4bfdfe72009-06-10 23:11:52 +0800841 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800842 return -EINVAL;
843
Harald Welte4bfdfe72009-06-10 23:11:52 +0800844 memcpy(ssv->info, lv + 1, in_len);
845 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800846
847 return 0;
848}
849
850/* encode 'more data' */
851static int encode_more(struct msgb *msg)
852{
853 u_int8_t *ie;
854
855 ie = msgb_put(msg, 1);
856 ie[0] = GSM48_IE_MORE_DATA;
857
858 return 0;
859}
860
Holger Freyther429e7762008-12-30 13:28:30 +0000861/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000862int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000863{
Harald Welte8470bf22008-12-25 23:28:35 +0000864 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000865 struct gsm48_hdr *gh;
866
Harald Welte8470bf22008-12-25 23:28:35 +0000867 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000868
869 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
870 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000871 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000872 gh->data[0] = cause;
873
Harald Weltedb253af2008-12-30 17:56:55 +0000874 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
875
Harald Welte39e2ead2009-07-23 21:13:03 +0200876 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000877}
878
879/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000880int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000881{
Harald Welte8470bf22008-12-25 23:28:35 +0000882 struct gsm_bts *bts = lchan->ts->trx->bts;
883 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000884 struct gsm48_hdr *gh;
885 struct gsm48_loc_area_id *lai;
886 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000887 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000888
Harald Welte8470bf22008-12-25 23:28:35 +0000889 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000890
891 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
892 gh->proto_discr = GSM48_PDISC_MM;
893 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
894
895 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000896 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000897 bts->network->network_code, bts->location_area_code);
898
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200899 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200900 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000901
902 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
903
Harald Welte39e2ead2009-07-23 21:13:03 +0200904 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000905
Harald Welteee5ad162009-08-09 19:07:00 +0200906 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000907 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000908
Holger Freyther07cc8d82008-12-29 06:23:46 +0000909 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000910}
911
Harald Weltebf5e8df2009-02-03 12:59:45 +0000912/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000913static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
914{
915 struct msgb *msg = gsm48_msgb_alloc();
916 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000917
Harald Welte231ad4f2008-12-27 11:15:38 +0000918 msg->lchan = lchan;
919
920 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
921 gh->proto_discr = GSM48_PDISC_MM;
922 gh->msg_type = GSM48_MT_MM_ID_REQ;
923 gh->data[0] = id_type;
924
Harald Welte39e2ead2009-07-23 21:13:03 +0200925 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000926}
927
Harald Welte231ad4f2008-12-27 11:15:38 +0000928
Harald Weltebf5e8df2009-02-03 12:59:45 +0000929/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000930static int mm_rx_id_resp(struct msgb *msg)
931{
932 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000933 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200934 struct gsm_bts *bts = lchan->ts->trx->bts;
935 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000936 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200937 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000938
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200939 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000940 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000941 mi_type, mi_string);
942
Harald Welte75a983f2008-12-27 21:34:06 +0000943 switch (mi_type) {
944 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200945 /* look up subscriber based on IMSI, create if not found */
946 if (!lchan->subscr) {
947 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200948 if (!lchan->subscr)
949 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200950 }
Holger Freyther73487a22008-12-31 18:53:57 +0000951 if (lchan->loc_operation)
952 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000953 break;
954 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000955 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000956 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200957 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000958 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200959 db_sync_equipment(&lchan->subscr->equipment);
960 }
Holger Freyther73487a22008-12-31 18:53:57 +0000961 if (lchan->loc_operation)
962 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000963 break;
964 }
Holger Freyther73487a22008-12-31 18:53:57 +0000965
966 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000967 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000968}
969
Harald Welte255539c2008-12-28 02:26:27 +0000970
971static void loc_upd_rej_cb(void *data)
972{
973 struct gsm_lchan *lchan = data;
974
Holger Freyther73487a22008-12-31 18:53:57 +0000975 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000976 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000977 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000978}
979
Holger Freytherb7193e42008-12-29 17:44:08 +0000980static void schedule_reject(struct gsm_lchan *lchan)
981{
Holger Freyther73487a22008-12-31 18:53:57 +0000982 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
983 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000984 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000985}
986
Harald Welte2a139372009-02-22 21:14:55 +0000987static const char *lupd_name(u_int8_t type)
988{
989 switch (type) {
990 case GSM48_LUPD_NORMAL:
991 return "NORMAL";
992 case GSM48_LUPD_PERIODIC:
993 return "PEROIDOC";
994 case GSM48_LUPD_IMSI_ATT:
995 return "IMSI ATTACH";
996 default:
997 return "UNKNOWN";
998 }
999}
1000
Harald Weltebf5e8df2009-02-03 12:59:45 +00001001/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001002static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001003{
Harald Welte8470bf22008-12-25 23:28:35 +00001004 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001005 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001006 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001007 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001008 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001009 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001010 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001011 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001012
Harald Welte8470bf22008-12-25 23:28:35 +00001013 lu = (struct gsm48_loc_upd_req *) gh->data;
1014
1015 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001016
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001017 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001018
Harald Weltea0368542009-06-27 02:58:43 +02001019 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001020 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001021
Holger Freythereaf04692009-06-06 13:54:44 +00001022 /*
1023 * Pseudo Spoof detection: Just drop a second/concurrent
1024 * location updating request.
1025 */
1026 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001027 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001028 lchan->loc_operation);
1029 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1030 return 0;
1031 }
1032
Holger Freyther73487a22008-12-31 18:53:57 +00001033 allocate_loc_updating_req(lchan);
1034
Harald Welte52b1f982008-12-23 20:25:15 +00001035 switch (mi_type) {
1036 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001037 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001038 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001039 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001040 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001041
Jan Luebbe370b41d2009-08-12 10:19:34 +02001042 /* look up subscriber based on IMSI, create if not found */
1043 subscr = subscr_get_by_imsi(bts->network, mi_string);
1044 if (!subscr) {
1045 subscr = db_create_subscriber(bts->network, mi_string);
1046 }
Harald Welte4b634542008-12-27 01:55:51 +00001047 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001048 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001049 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001050 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001051 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001052 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001053
Harald Welte52b1f982008-12-23 20:25:15 +00001054 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001055 subscr = subscr_get_by_tmsi(bts->network,
1056 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001057 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001059 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001060 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001061 }
1062 break;
1063 case GSM_MI_TYPE_IMEI:
1064 case GSM_MI_TYPE_IMEISV:
1065 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001066 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001067 break;
1068 default:
Harald Weltea0368542009-06-27 02:58:43 +02001069 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001070 break;
1071 }
1072
Harald Welte24516ea2009-07-04 10:18:00 +02001073 /* schedule the reject timer */
1074 schedule_reject(lchan);
1075
Harald Welte4bfdfe72009-06-10 23:11:52 +08001076 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001077 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001078 /* FIXME: request id? close channel? */
1079 return -EINVAL;
1080 }
1081
Harald Welte255539c2008-12-28 02:26:27 +00001082 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001083 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001084
Harald Welte24516ea2009-07-04 10:18:00 +02001085 /* check if we can let the subscriber into our network immediately
1086 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001087 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001088}
1089
Harald Welte4bfdfe72009-06-10 23:11:52 +08001090#if 0
1091static u_int8_t to_bcd8(u_int8_t val)
1092{
1093 return ((val / 10) << 4) | (val % 10);
1094}
1095#endif
1096
Harald Weltedb253af2008-12-30 17:56:55 +00001097/* Section 9.2.15a */
1098int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1099{
1100 struct msgb *msg = gsm48_msgb_alloc();
1101 struct gsm48_hdr *gh;
1102 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001103 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001104 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001105#if 0
1106 time_t cur_t;
1107 struct tm* cur_time;
1108 int tz15min;
1109#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001110
1111 msg->lchan = lchan;
1112
1113 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1114 gh->proto_discr = GSM48_PDISC_MM;
1115 gh->msg_type = GSM48_MT_MM_INFO;
1116
1117 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001118#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001119 name_len = strlen(net->name_long);
1120 /* 10.5.3.5a */
1121 ptr8 = msgb_put(msg, 3);
1122 ptr8[0] = GSM48_IE_NAME_LONG;
1123 ptr8[1] = name_len*2 +1;
1124 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1125
1126 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1127 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001128 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001129
1130 /* FIXME: Use Cell Broadcast, not UCS-2, since
1131 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001132#endif
1133 name_len = (strlen(net->name_long)*7)/8;
1134 name_pad = (8 - strlen(net->name_long)*7)%8;
1135 if (name_pad > 0)
1136 name_len++;
1137 /* 10.5.3.5a */
1138 ptr8 = msgb_put(msg, 3);
1139 ptr8[0] = GSM48_IE_NAME_LONG;
1140 ptr8[1] = name_len +1;
1141 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1142
1143 ptr8 = msgb_put(msg, name_len);
1144 gsm_7bit_encode(ptr8, net->name_long);
1145
Harald Weltedb253af2008-12-30 17:56:55 +00001146 }
1147
1148 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001149#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001150 name_len = strlen(net->name_short);
1151 /* 10.5.3.5a */
1152 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001153 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001154 ptr8[1] = name_len*2 + 1;
1155 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1156
Harald Weltee872cb12009-01-01 00:33:37 +00001157 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001158 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001159 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001160#endif
1161 name_len = (strlen(net->name_short)*7)/8;
1162 name_pad = (8 - strlen(net->name_short)*7)%8;
1163 if (name_pad > 0)
1164 name_len++;
1165 /* 10.5.3.5a */
1166 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1167 ptr8[0] = GSM48_IE_NAME_SHORT;
1168 ptr8[1] = name_len +1;
1169 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1170
1171 ptr8 = msgb_put(msg, name_len);
1172 gsm_7bit_encode(ptr8, net->name_short);
1173
Harald Weltedb253af2008-12-30 17:56:55 +00001174 }
1175
1176#if 0
1177 /* Section 10.5.3.9 */
1178 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001179 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001180 ptr8 = msgb_put(msg, 8);
1181 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1182 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1183 ptr8[2] = to_bcd8(cur_time->tm_mon);
1184 ptr8[3] = to_bcd8(cur_time->tm_mday);
1185 ptr8[4] = to_bcd8(cur_time->tm_hour);
1186 ptr8[5] = to_bcd8(cur_time->tm_min);
1187 ptr8[6] = to_bcd8(cur_time->tm_sec);
1188 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1189 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001190 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001191 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001192 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001193#endif
1194
Daniel Willmanneea93372009-08-13 03:42:07 +02001195 DEBUGP(DMM, "-> MM INFO\n");
1196
Harald Welte39e2ead2009-07-23 21:13:03 +02001197 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001198}
1199
Harald Welte7984d5c2009-08-12 22:56:50 +02001200/* Section 9.2.2 */
1201int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1202{
1203 struct msgb *msg = gsm48_msgb_alloc();
1204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001205 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001206
1207 DEBUGP(DMM, "-> AUTH REQ\n");
1208
1209 msg->lchan = lchan;
1210 gh->proto_discr = GSM48_PDISC_MM;
1211 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1212
Sylvain Munaut849f5542009-09-27 11:10:17 +02001213 /* Key Sequence: FIXME fixed to 0 */
1214 ar->key_seq = 0;
1215
Harald Welte7984d5c2009-08-12 22:56:50 +02001216 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001217 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001218 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001219
1220 return gsm48_sendmsg(msg, NULL);
1221}
1222
1223/* Section 9.2.1 */
1224int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1225{
1226 DEBUGP(DMM, "-> AUTH REJECT\n");
1227 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1228}
1229
Harald Welte4b634542008-12-27 01:55:51 +00001230static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1231{
Harald Welte4b634542008-12-27 01:55:51 +00001232 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001233 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001234}
Harald Welteba4cf162009-01-10 01:49:35 +00001235
1236/* 9.2.6 CM service reject */
1237static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1238 enum gsm48_reject_value value)
1239{
1240 struct msgb *msg = gsm48_msgb_alloc();
1241 struct gsm48_hdr *gh;
1242
1243 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1244
1245 msg->lchan = lchan;
1246 use_lchan(lchan);
1247
1248 gh->proto_discr = GSM48_PDISC_MM;
1249 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1250 gh->data[0] = value;
1251 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1252
Harald Welte39e2ead2009-07-23 21:13:03 +02001253 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001254}
1255
Harald Welte4ed0e922009-01-10 03:17:30 +00001256/*
1257 * Handle CM Service Requests
1258 * a) Verify that the packet is long enough to contain the information
1259 * we require otherwsie reject with INCORRECT_MESSAGE
1260 * b) Try to parse the TMSI. If we do not have one reject
1261 * c) Check that we know the subscriber with the TMSI otherwise reject
1262 * with a HLR cause
1263 * d) Set the subscriber on the gsm_lchan and accept
1264 */
Harald Welte4b634542008-12-27 01:55:51 +00001265static int gsm48_rx_mm_serv_req(struct msgb *msg)
1266{
Harald Welteba4cf162009-01-10 01:49:35 +00001267 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001268 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001269
Harald Welte9176bd42009-07-23 18:46:00 +02001270 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001271 struct gsm_subscriber *subscr;
1272 struct gsm48_hdr *gh = msgb_l3(msg);
1273 struct gsm48_service_request *req =
1274 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001275 /* unfortunately in Phase1 the classmar2 length is variable */
1276 u_int8_t classmark2_len = gh->data[1];
1277 u_int8_t *classmark2 = gh->data+2;
1278 u_int8_t mi_len = *(classmark2 + classmark2_len);
1279 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001280
Harald Weltec9e02182009-05-01 19:07:53 +00001281 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001282 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001283 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001284 return gsm48_tx_mm_serv_rej(msg->lchan,
1285 GSM48_REJECT_INCORRECT_MESSAGE);
1286 }
1287
1288 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001289 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001290 return gsm48_tx_mm_serv_rej(msg->lchan,
1291 GSM48_REJECT_INCORRECT_MESSAGE);
1292 }
1293
Harald Weltec9e02182009-05-01 19:07:53 +00001294 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001295 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001296 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001297 return gsm48_tx_mm_serv_rej(msg->lchan,
1298 GSM48_REJECT_INCORRECT_MESSAGE);
1299 }
1300
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001301 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001302 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001303 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001304
Harald Welte3ac7f102009-08-10 10:12:45 +02001305 if (is_siemens_bts(bts))
1306 send_siemens_mrpci(msg->lchan, classmark2-1);
1307
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001308 subscr = subscr_get_by_tmsi(bts->network,
1309 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001310
Harald Welte2a139372009-02-22 21:14:55 +00001311 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001312 if (!subscr)
1313 return gsm48_tx_mm_serv_rej(msg->lchan,
1314 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1315
1316 if (!msg->lchan->subscr)
1317 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001318 else if (msg->lchan->subscr != subscr) {
1319 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1320 subscr_put(subscr);
1321 }
1322
Harald Weltec2e302d2009-07-05 14:08:13 +02001323 subscr->equipment.classmark2_len = classmark2_len;
1324 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1325 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001326
Harald Welte4b634542008-12-27 01:55:51 +00001327 return gsm48_tx_mm_serv_ack(msg->lchan);
1328}
1329
Harald Welte2a139372009-02-22 21:14:55 +00001330static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1331{
Harald Welte9176bd42009-07-23 18:46:00 +02001332 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001333 struct gsm48_hdr *gh = msgb_l3(msg);
1334 struct gsm48_imsi_detach_ind *idi =
1335 (struct gsm48_imsi_detach_ind *) gh->data;
1336 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001337 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001339
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001340 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001341 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1342 mi_type, mi_string);
1343
1344 switch (mi_type) {
1345 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001346 subscr = subscr_get_by_tmsi(bts->network,
1347 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001348 break;
1349 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001350 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001351 break;
1352 case GSM_MI_TYPE_IMEI:
1353 case GSM_MI_TYPE_IMEISV:
1354 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001355 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001356 break;
1357 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001358 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001359 break;
1360 }
1361
Holger Freyther4a49e772009-04-12 05:37:29 +00001362 if (subscr) {
1363 subscr_update(subscr, msg->trx->bts,
1364 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001365 DEBUGP(DMM, "Subscriber: %s\n",
1366 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001367
1368 subscr->equipment.classmark1 = idi->classmark1;
1369 db_sync_equipment(&subscr->equipment);
1370
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001371 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001372 } else
Harald Welte2a139372009-02-22 21:14:55 +00001373 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1374
Harald Welte2a139372009-02-22 21:14:55 +00001375 return 0;
1376}
1377
Harald Welted2a7f5a2009-06-05 20:08:20 +00001378static int gsm48_rx_mm_status(struct msgb *msg)
1379{
1380 struct gsm48_hdr *gh = msgb_l3(msg);
1381
1382 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1383
1384 return 0;
1385}
1386
Harald Weltebf5e8df2009-02-03 12:59:45 +00001387/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001388static int gsm0408_rcv_mm(struct msgb *msg)
1389{
1390 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001391 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001392
1393 switch (gh->msg_type & 0xbf) {
1394 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001395 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001396 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001397 break;
1398 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001399 rc = mm_rx_id_resp(msg);
1400 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001401 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001402 rc = gsm48_rx_mm_serv_req(msg);
1403 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001404 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001405 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001406 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001407 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001408 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1409 msg->lchan->subscr ?
1410 msg->lchan->subscr->imsi :
1411 "unknown subscriber");
1412 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001413 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001414 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1415 break;
1416 case GSM48_MT_MM_CM_REEST_REQ:
1417 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1418 break;
1419 case GSM48_MT_MM_AUTH_RESP:
1420 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001421 break;
1422 default:
1423 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1424 gh->msg_type);
1425 break;
1426 }
1427
1428 return rc;
1429}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001430
Harald Welte2d35ae62009-02-06 12:02:13 +00001431/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001432static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001433{
Harald Welte9176bd42009-07-23 18:46:00 +02001434 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001435 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001436 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001437 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001438 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001440 int rc = 0;
1441
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001442 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001443 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1444 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001445
Harald Weltefe18d8f2009-02-22 21:14:24 +00001446 switch (mi_type) {
1447 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001448 subscr = subscr_get_by_tmsi(bts->network,
1449 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001450 break;
1451 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001452 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001453 break;
1454 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001455
1456 if (!subscr) {
1457 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001458 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001459 return -EINVAL;
1460 }
1461 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001462 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001463
Harald Weltec2e302d2009-07-05 14:08:13 +02001464 subscr->equipment.classmark2_len = *classmark2_lv;
1465 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1466 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001467
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001468 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001469 return rc;
1470}
1471
Harald Weltef7c43522009-06-09 20:24:21 +00001472static int gsm48_rx_rr_classmark(struct msgb *msg)
1473{
1474 struct gsm48_hdr *gh = msgb_l3(msg);
1475 struct gsm_subscriber *subscr = msg->lchan->subscr;
1476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1477 u_int8_t cm2_len, cm3_len = 0;
1478 u_int8_t *cm2, *cm3 = NULL;
1479
1480 DEBUGP(DRR, "CLASSMARK CHANGE ");
1481
1482 /* classmark 2 */
1483 cm2_len = gh->data[0];
1484 cm2 = &gh->data[1];
1485 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1486
1487 if (payload_len > cm2_len + 1) {
1488 /* we must have a classmark3 */
1489 if (gh->data[cm2_len+1] != 0x20) {
1490 DEBUGPC(DRR, "ERR CM3 TAG\n");
1491 return -EINVAL;
1492 }
1493 if (cm2_len > 3) {
1494 DEBUGPC(DRR, "CM2 too long!\n");
1495 return -EINVAL;
1496 }
1497
1498 cm3_len = gh->data[cm2_len+2];
1499 cm3 = &gh->data[cm2_len+3];
1500 if (cm3_len > 14) {
1501 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1502 return -EINVAL;
1503 }
1504 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1505 }
1506 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001507 subscr->equipment.classmark2_len = cm2_len;
1508 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001509 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001510 subscr->equipment.classmark3_len = cm3_len;
1511 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001512 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001513 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001514 }
1515
Harald Weltef7c43522009-06-09 20:24:21 +00001516 return 0;
1517}
1518
Harald Weltecf5b3592009-05-01 18:28:42 +00001519static int gsm48_rx_rr_status(struct msgb *msg)
1520{
1521 struct gsm48_hdr *gh = msgb_l3(msg);
1522
1523 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1524 rr_cause_name(gh->data[0]));
1525
1526 return 0;
1527}
1528
Harald Weltef7c43522009-06-09 20:24:21 +00001529static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1530{
1531 struct gsm48_hdr *gh = msgb_l3(msg);
1532 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1533 static struct gsm_meas_rep meas_rep;
1534
Harald Welte10d0e672009-06-27 02:53:10 +02001535 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001536 parse_meas_rep(&meas_rep, gh->data, payload_len);
1537 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001538 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001539 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001540 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001541 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001542 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001543 else
Harald Welte10d0e672009-06-27 02:53:10 +02001544 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001545 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1546 meas_rep.rxqual_sub);
1547
Harald Welte10d0e672009-06-27 02:53:10 +02001548 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001549
1550 /* FIXME: put the results somwhere */
1551
1552 return 0;
1553}
1554
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001555static int gsm48_rx_rr_app_info(struct msgb *msg)
1556{
1557 struct gsm48_hdr *gh = msgb_l3(msg);
1558 u_int8_t apdu_id_flags;
1559 u_int8_t apdu_len;
1560 u_int8_t *apdu_data;
1561
1562 apdu_id_flags = gh->data[0];
1563 apdu_len = gh->data[1];
1564 apdu_data = gh->data+2;
1565
1566 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1567 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1568
Harald Welte (local)026531e2009-08-16 10:40:10 +02001569 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001570}
1571
1572
Harald Weltebf5e8df2009-02-03 12:59:45 +00001573/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001574static int gsm0408_rcv_rr(struct msgb *msg)
1575{
1576 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001577 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001578
1579 switch (gh->msg_type) {
1580 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001581 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001582 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001583 case GSM48_MT_RR_GPRS_SUSP_REQ:
1584 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1585 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001586 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001587 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001588 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001589 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001590 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001591 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001592 case GSM48_MT_RR_STATUS:
1593 rc = gsm48_rx_rr_status(msg);
1594 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001595 case GSM48_MT_RR_MEAS_REP:
1596 rc = gsm48_rx_rr_meas_rep(msg);
1597 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001598 case GSM48_MT_RR_APP_INFO:
1599 rc = gsm48_rx_rr_app_info(msg);
1600 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001601 case GSM48_MT_RR_CIPH_M_COMPL:
1602 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1603 /* FIXME: check for MI (if any) */
1604 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001605 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001606 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001607 gh->msg_type);
1608 break;
1609 }
1610
Harald Welte2d35ae62009-02-06 12:02:13 +00001611 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001612}
1613
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001614int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001615 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001616{
1617 struct msgb *msg = gsm48_msgb_alloc();
1618 struct gsm48_hdr *gh;
1619
1620 msg->lchan = lchan;
1621
1622 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1623 apdu_id, apdu_len);
1624
1625 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1626 gh->proto_discr = GSM48_PDISC_RR;
1627 gh->msg_type = GSM48_MT_RR_APP_INFO;
1628 gh->data[0] = apdu_id;
1629 gh->data[1] = apdu_len;
1630 memcpy(gh->data+2, apdu, apdu_len);
1631
1632 return gsm48_sendmsg(msg, NULL);
1633}
1634
Harald Welte4bc90a12008-12-27 16:32:52 +00001635/* Call Control */
1636
Harald Welte7584aea2009-02-11 11:44:12 +00001637/* The entire call control code is written in accordance with Figure 7.10c
1638 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1639 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1640 * it for voice */
1641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642static void new_cc_state(struct gsm_trans *trans, int state)
1643{
1644 if (state > 31 || state < 0)
1645 return;
1646
1647 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001648 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649
Harald Weltedcaf5652009-07-23 18:56:43 +02001650 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651}
1652
1653static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001654{
1655 struct msgb *msg = gsm48_msgb_alloc();
1656 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1657 u_int8_t *cause, *call_state;
1658
Harald Welte4bc90a12008-12-27 16:32:52 +00001659 gh->msg_type = GSM48_MT_CC_STATUS;
1660
1661 cause = msgb_put(msg, 3);
1662 cause[0] = 2;
1663 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1664 cause[2] = 0x80 | 30; /* response to status inquiry */
1665
1666 call_state = msgb_put(msg, 1);
1667 call_state[0] = 0xc0 | 0x00;
1668
Harald Welte39e2ead2009-07-23 21:13:03 +02001669 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001670}
1671
Harald Welte6f4b7532008-12-29 00:39:37 +00001672static int gsm48_tx_simple(struct gsm_lchan *lchan,
1673 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001674{
1675 struct msgb *msg = gsm48_msgb_alloc();
1676 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1677
1678 msg->lchan = lchan;
1679
Harald Welte6f4b7532008-12-29 00:39:37 +00001680 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001681 gh->msg_type = msg_type;
1682
Harald Welte39e2ead2009-07-23 21:13:03 +02001683 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001684}
1685
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1687{
Harald Weltedcaf5652009-07-23 18:56:43 +02001688 if (bsc_timer_pending(&trans->cc.timer)) {
1689 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1690 bsc_del_timer(&trans->cc.timer);
1691 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 }
1693}
1694
1695static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1696 int msg_type, struct gsm_mncc *mncc)
1697{
1698 struct msgb *msg;
1699
1700 if (trans)
1701 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001702 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 "Sending '%s' to MNCC.\n",
1704 trans->lchan->ts->trx->bts->nr,
1705 trans->lchan->ts->trx->nr,
1706 trans->lchan->ts->nr, trans->transaction_id,
1707 (trans->subscr)?(trans->subscr->extension):"-",
1708 get_mncc_name(msg_type));
1709 else
1710 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1711 "Sending '%s' to MNCC.\n",
1712 (trans->subscr)?(trans->subscr->extension):"-",
1713 get_mncc_name(msg_type));
1714 else
1715 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1716 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1717
1718 mncc->msg_type = msg_type;
1719
Harald Welte966636f2009-06-26 19:39:35 +02001720 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 if (!msg)
1722 return -ENOMEM;
1723 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1724 msgb_enqueue(&net->upqueue, msg);
1725
1726 return 0;
1727}
1728
1729int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1730 u_int32_t callref, int location, int value)
1731{
1732 struct gsm_mncc rel;
1733
Harald Welte92f70c52009-06-12 01:54:08 +08001734 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001736 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1738}
1739
Harald Weltedcaf5652009-07-23 18:56:43 +02001740/* Call Control Specific transaction release.
1741 * gets called by trans_free, DO NOT CALL YOURSELF! */
1742void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 gsm48_stop_cc_timer(trans);
1745
1746 /* send release to L4, if callref still exists */
1747 if (trans->callref) {
1748 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001749 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001750 GSM48_CAUSE_LOC_PRN_S_LU,
1751 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001753 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001755 if (trans->lchan)
1756 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757}
1758
1759static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1760
Harald Welte09e38af2009-02-16 22:52:23 +00001761/* call-back from paging the B-end of the connection */
1762static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001763 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001764{
Harald Welte7ccf7782009-02-17 01:43:01 +00001765 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 struct gsm_subscriber *subscr = param;
1767 struct gsm_trans *transt, *tmp;
1768 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001769
Harald Welte09e38af2009-02-16 22:52:23 +00001770 if (hooknum != GSM_HOOK_RR_PAGING)
1771 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772
1773 if (!subscr)
1774 return -EINVAL;
1775 net = subscr->net;
1776 if (!net) {
1777 DEBUGP(DCC, "Error Network not set!\n");
1778 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001779 }
Harald Welte7584aea2009-02-11 11:44:12 +00001780
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 /* check all tranactions (without lchan) for subscriber */
1782 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1783 if (transt->subscr != subscr || transt->lchan)
1784 continue;
1785 switch (event) {
1786 case GSM_PAGING_SUCCEEDED:
1787 if (!lchan) // paranoid
1788 break;
1789 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1790 subscr->extension);
1791 /* Assign lchan */
1792 if (!transt->lchan) {
1793 transt->lchan = lchan;
1794 use_lchan(lchan);
1795 }
1796 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001797 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 break;
1799 case GSM_PAGING_EXPIRED:
1800 DEBUGP(DCC, "Paging subscr %s expired!\n",
1801 subscr->extension);
1802 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001803 mncc_release_ind(transt->subscr->net, transt,
1804 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001805 GSM48_CAUSE_LOC_PRN_S_LU,
1806 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001808 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 break;
1810 }
1811 }
Harald Welte09e38af2009-02-16 22:52:23 +00001812 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001813}
Harald Welte7584aea2009-02-11 11:44:12 +00001814
Harald Welte805f6442009-07-28 18:25:29 +02001815/* some other part of the code sends us a signal */
1816static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1817 void *handler_data, void *signal_data)
1818{
1819 struct gsm_lchan *lchan = signal_data;
1820 struct gsm_bts_trx_ts *ts;
1821 int rc;
1822
1823 if (subsys != SS_ABISIP)
1824 return 0;
1825
1826 /* in case we use direct BTS-to-BTS RTP */
1827 if (ipacc_rtp_direct)
1828 return 0;
1829
1830 ts = lchan->ts;
1831
1832 switch (signal) {
1833 case S_ABISIP_BIND_ACK:
1834 /* the BTS has successfully bound a TCH to a local ip/port,
1835 * which means we can connect our UDP socket to it */
1836 if (ts->abis_ip.rtp_socket) {
1837 rtp_socket_free(ts->abis_ip.rtp_socket);
1838 ts->abis_ip.rtp_socket = NULL;
1839 }
1840
1841 ts->abis_ip.rtp_socket = rtp_socket_create();
1842 if (!ts->abis_ip.rtp_socket)
1843 goto out_err;
1844
1845 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1846 ts->abis_ip.bound_ip,
1847 ts->abis_ip.bound_port);
1848 if (rc < 0)
1849 goto out_err;
1850 break;
1851 case S_ABISIP_DISC_IND:
1852 /* the BTS tells us a RTP stream has been disconnected */
1853 if (ts->abis_ip.rtp_socket) {
1854 rtp_socket_free(ts->abis_ip.rtp_socket);
1855 ts->abis_ip.rtp_socket = NULL;
1856 }
1857 break;
1858 }
1859
1860 return 0;
1861out_err:
1862 /* FIXME: do something */
1863 return 0;
1864}
1865
1866/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1867static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1868{
1869 struct gsm_bts_trx_ts *ts = lchan->ts;
1870 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1871 int rc;
1872
1873 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1874 ntohs(rs->rtp.sin_local.sin_port),
1875 ts->abis_ip.conn_id,
1876 /* FIXME: use RTP payload of bound socket, not BTS*/
1877 ts->abis_ip.rtp_payload2);
1878
1879 return rc;
1880}
1881
Harald Welte49f48b82009-02-17 15:29:33 +00001882/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001883static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001884{
Harald Welte11fa29c2009-02-19 17:24:39 +00001885 struct gsm_bts *bts = lchan->ts->trx->bts;
1886 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001887 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02001888 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001889
Harald Welte11fa29c2009-02-19 17:24:39 +00001890 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1891 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1892 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1893
1894 if (bts->type != remote_bts->type) {
1895 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1896 return -EINVAL;
1897 }
Harald Welte49f48b82009-02-17 15:29:33 +00001898
Harald Welte11fa29c2009-02-19 17:24:39 +00001899 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001900 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001901 if (!ipacc_rtp_direct) {
1902 /* connect the TCH's to our RTP proxy */
1903 rc = ipacc_connect_proxy_bind(lchan);
1904 if (rc < 0)
1905 return rc;
1906 rc = ipacc_connect_proxy_bind(remote_lchan);
1907
1908 /* connect them with each other */
1909 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
1910 remote_lchan->ts->abis_ip.rtp_socket);
1911 } else {
1912 /* directly connect TCH RTP streams to each other */
1913 ts = remote_lchan->ts;
1914 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
1915 ts->abis_ip.bound_port,
1916 lchan->ts->abis_ip.conn_id,
1917 ts->abis_ip.rtp_payload2);
1918 if (rc < 0)
1919 return rc;
1920 ts = lchan->ts;
1921 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
1922 ts->abis_ip.bound_port,
1923 remote_lchan->ts->abis_ip.conn_id,
1924 ts->abis_ip.rtp_payload2);
1925 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001926 break;
1927 case GSM_BTS_TYPE_BS11:
1928 trau_mux_map_lchan(lchan, remote_lchan);
1929 break;
1930 default:
1931 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001932 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001933 break;
1934 }
Harald Welte49f48b82009-02-17 15:29:33 +00001935
1936 return 0;
1937}
1938
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939/* bridge channels of two transactions */
1940static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001941{
Harald Weltedcaf5652009-07-23 18:56:43 +02001942 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1943 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001944
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001946 return -EIO;
1947
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 if (!trans1->lchan || !trans2->lchan)
1949 return -EIO;
1950
1951 /* through-connect channel */
1952 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001953}
1954
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955/* enable receive of channels to upqueue */
1956static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1957{
1958 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001959
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001961 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 if (!trans)
1963 return -EIO;
1964 if (!trans->lchan)
1965 return 0;
1966
1967 // todo IPACCESS
1968 if (enable)
1969 return trau_recv_lchan(trans->lchan, data->callref);
1970 return trau_mux_unmap(NULL, data->callref);
1971}
1972
1973/* send a frame to channel */
1974static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
1975{
1976 struct gsm_trans *trans;
1977
1978 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001979 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 if (!trans)
1981 return -EIO;
1982 if (!trans->lchan)
1983 return 0;
1984 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
1985 trans->lchan->type != GSM_LCHAN_TCH_H)
1986 return 0;
1987
1988 // todo IPACCESS
1989 return trau_send_lchan(trans->lchan,
1990 (struct decoded_trau_frame *)frame->data);
1991}
1992
1993
1994static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1995{
1996 DEBUGP(DCC, "-> STATUS ENQ\n");
1997 return gsm48_cc_tx_status(trans, msg);
1998}
1999
2000static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2001static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2002
2003static void gsm48_cc_timeout(void *arg)
2004{
2005 struct gsm_trans *trans = arg;
2006 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002007 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2008 int mo_location = GSM48_CAUSE_LOC_USER;
2009 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2010 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 struct gsm_mncc mo_rel, l4_rel;
2012
2013 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2014 mo_rel.callref = trans->callref;
2015 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2016 l4_rel.callref = trans->callref;
2017
Harald Weltedcaf5652009-07-23 18:56:43 +02002018 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 case 0x303:
2020 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002021 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 break;
2023 case 0x310:
2024 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002025 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 break;
2027 case 0x313:
2028 disconnect = 1;
2029 /* unknown, did not find it in the specs */
2030 break;
2031 case 0x301:
2032 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002033 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 break;
2035 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002036 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002038 gsm48_cc_tx_release(trans, &trans->cc.msg);
2039 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 break; /* stay in release state */
2041 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002042 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 return;
2044// release = 1;
2045// l4_cause = 14;
2046// break;
2047 case 0x306:
2048 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002049 mo_cause = trans->cc.msg.cause.value;
2050 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 break;
2052 case 0x323:
2053 disconnect = 1;
2054 break;
2055 default:
2056 release = 1;
2057 }
2058
2059 if (release && trans->callref) {
2060 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002061 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 l4_location, l4_cause);
2063 trans->callref = 0;
2064 }
2065
2066 if (disconnect && trans->callref) {
2067 /* process disconnect towards layer 4 */
2068 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002069 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 }
2071
2072 /* process disconnect towards mobile station */
2073 if (disconnect || release) {
2074 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002075 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2076 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2077 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 mo_rel.cause.diag_len = 3;
2079
2080 if (disconnect)
2081 gsm48_cc_tx_disconnect(trans, &mo_rel);
2082 if (release)
2083 gsm48_cc_tx_release(trans, &mo_rel);
2084 }
2085
2086}
2087
2088static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2089 int sec, int micro)
2090{
2091 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002092 trans->cc.timer.cb = gsm48_cc_timeout;
2093 trans->cc.timer.data = trans;
2094 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2095 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096}
2097
2098static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2099{
2100 struct gsm48_hdr *gh = msgb_l3(msg);
2101 u_int8_t msg_type = gh->msg_type & 0xbf;
2102 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2103 struct tlv_parsed tp;
2104 struct gsm_mncc setup;
2105
2106 memset(&setup, 0, sizeof(struct gsm_mncc));
2107 setup.callref = trans->callref;
2108 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2109 /* emergency setup is identified by msg_type */
2110 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2111 setup.emergency = 1;
2112
2113 /* use subscriber as calling party number */
2114 if (trans->subscr) {
2115 setup.fields |= MNCC_F_CALLING;
2116 strncpy(setup.calling.number, trans->subscr->extension,
2117 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002118 strncpy(setup.imsi, trans->subscr->imsi,
2119 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 }
2121 /* bearer capability */
2122 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2123 setup.fields |= MNCC_F_BEARER_CAP;
2124 decode_bearer_cap(&setup.bearer_cap,
2125 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2126 }
2127 /* facility */
2128 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2129 setup.fields |= MNCC_F_FACILITY;
2130 decode_facility(&setup.facility,
2131 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2132 }
2133 /* called party bcd number */
2134 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2135 setup.fields |= MNCC_F_CALLED;
2136 decode_called(&setup.called,
2137 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2138 }
2139 /* user-user */
2140 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2141 setup.fields |= MNCC_F_USERUSER;
2142 decode_useruser(&setup.useruser,
2143 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2144 }
2145 /* ss-version */
2146 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2147 setup.fields |= MNCC_F_SSVERSION;
2148 decode_ssversion(&setup.ssversion,
2149 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2150 }
2151 /* CLIR suppression */
2152 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2153 setup.clir.sup = 1;
2154 /* CLIR invocation */
2155 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2156 setup.clir.inv = 1;
2157 /* cc cap */
2158 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2159 setup.fields |= MNCC_F_CCCAP;
2160 decode_cccap(&setup.cccap,
2161 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2162 }
2163
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 new_cc_state(trans, GSM_CSTATE_INITIATED);
2165
2166 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002167 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168
Harald Welte13cac662009-07-29 12:10:35 +02002169 /* MNCC code will modify the channel asynchronously, we should
2170 * ipaccess-bind only after the modification has been made to the
2171 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 return 0;
2173}
2174
2175static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002176{
2177 struct msgb *msg = gsm48_msgb_alloc();
2178 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002180 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002181
Harald Welte7ccf7782009-02-17 01:43:01 +00002182 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002183
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184 /* transaction id must not be assigned */
2185 if (trans->transaction_id != 0xff) { /* unasssigned */
2186 DEBUGP(DCC, "TX Setup with assigned transaction. "
2187 "This is not allowed!\n");
2188 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002189 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002190 GSM48_CAUSE_LOC_PRN_S_LU,
2191 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002193 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 return rc;
2195 }
2196
2197 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002198 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2199 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002201 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002202 GSM48_CAUSE_LOC_PRN_S_LU,
2203 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002205 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 return rc;
2207 }
Harald Welte78283ef2009-07-23 21:36:44 +02002208 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002209
Harald Welte65e74cc2008-12-29 01:55:35 +00002210 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002211
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002213
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* bearer capability */
2215 if (setup->fields & MNCC_F_BEARER_CAP)
2216 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2217 /* facility */
2218 if (setup->fields & MNCC_F_FACILITY)
2219 encode_facility(msg, 0, &setup->facility);
2220 /* progress */
2221 if (setup->fields & MNCC_F_PROGRESS)
2222 encode_progress(msg, 0, &setup->progress);
2223 /* calling party BCD number */
2224 if (setup->fields & MNCC_F_CALLING)
2225 encode_calling(msg, &setup->calling);
2226 /* called party BCD number */
2227 if (setup->fields & MNCC_F_CALLED)
2228 encode_called(msg, &setup->called);
2229 /* user-user */
2230 if (setup->fields & MNCC_F_USERUSER)
2231 encode_useruser(msg, 0, &setup->useruser);
2232 /* redirecting party BCD number */
2233 if (setup->fields & MNCC_F_REDIRECTING)
2234 encode_redirecting(msg, &setup->redirecting);
2235 /* signal */
2236 if (setup->fields & MNCC_F_SIGNAL)
2237 encode_signal(msg, setup->signal);
2238
2239 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002240
Harald Welte39e2ead2009-07-23 21:13:03 +02002241 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002242}
2243
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2245{
2246 struct gsm48_hdr *gh = msgb_l3(msg);
2247 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2248 struct tlv_parsed tp;
2249 struct gsm_mncc call_conf;
2250
2251 gsm48_stop_cc_timer(trans);
2252 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2253
2254 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2255 call_conf.callref = trans->callref;
2256 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2257#if 0
2258 /* repeat */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2260 call_conf.repeat = 1;
2261 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2262 call_conf.repeat = 2;
2263#endif
2264 /* bearer capability */
2265 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2266 call_conf.fields |= MNCC_F_BEARER_CAP;
2267 decode_bearer_cap(&call_conf.bearer_cap,
2268 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2269 }
2270 /* cause */
2271 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2272 call_conf.fields |= MNCC_F_CAUSE;
2273 decode_cause(&call_conf.cause,
2274 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2275 }
2276 /* cc cap */
2277 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2278 call_conf.fields |= MNCC_F_CCCAP;
2279 decode_cccap(&call_conf.cccap,
2280 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2281 }
2282
2283 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2284
Harald Welte596fed42009-07-23 19:06:52 +02002285 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2286 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287}
2288
2289static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2290{
2291 struct gsm_mncc *proceeding = arg;
2292 struct msgb *msg = gsm48_msgb_alloc();
2293 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2294
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2296
2297 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2298
2299 /* bearer capability */
2300 if (proceeding->fields & MNCC_F_BEARER_CAP)
2301 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2302 /* facility */
2303 if (proceeding->fields & MNCC_F_FACILITY)
2304 encode_facility(msg, 0, &proceeding->facility);
2305 /* progress */
2306 if (proceeding->fields & MNCC_F_PROGRESS)
2307 encode_progress(msg, 0, &proceeding->progress);
2308
Harald Welte39e2ead2009-07-23 21:13:03 +02002309 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310}
2311
2312static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2313{
2314 struct gsm48_hdr *gh = msgb_l3(msg);
2315 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2316 struct tlv_parsed tp;
2317 struct gsm_mncc alerting;
2318
2319 gsm48_stop_cc_timer(trans);
2320 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2321
2322 memset(&alerting, 0, sizeof(struct gsm_mncc));
2323 alerting.callref = trans->callref;
2324 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2325 /* facility */
2326 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2327 alerting.fields |= MNCC_F_FACILITY;
2328 decode_facility(&alerting.facility,
2329 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2330 }
2331
2332 /* progress */
2333 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2334 alerting.fields |= MNCC_F_PROGRESS;
2335 decode_progress(&alerting.progress,
2336 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2337 }
2338 /* ss-version */
2339 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2340 alerting.fields |= MNCC_F_SSVERSION;
2341 decode_ssversion(&alerting.ssversion,
2342 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2343 }
2344
2345 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2346
Harald Welte596fed42009-07-23 19:06:52 +02002347 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2348 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349}
2350
2351static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2352{
2353 struct gsm_mncc *alerting = arg;
2354 struct msgb *msg = gsm48_msgb_alloc();
2355 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2356
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 gh->msg_type = GSM48_MT_CC_ALERTING;
2358
2359 /* facility */
2360 if (alerting->fields & MNCC_F_FACILITY)
2361 encode_facility(msg, 0, &alerting->facility);
2362 /* progress */
2363 if (alerting->fields & MNCC_F_PROGRESS)
2364 encode_progress(msg, 0, &alerting->progress);
2365 /* user-user */
2366 if (alerting->fields & MNCC_F_USERUSER)
2367 encode_useruser(msg, 0, &alerting->useruser);
2368
2369 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2370
Harald Welte39e2ead2009-07-23 21:13:03 +02002371 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372}
2373
2374static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2375{
2376 struct gsm_mncc *progress = arg;
2377 struct msgb *msg = gsm48_msgb_alloc();
2378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2379
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 gh->msg_type = GSM48_MT_CC_PROGRESS;
2381
2382 /* progress */
2383 encode_progress(msg, 1, &progress->progress);
2384 /* user-user */
2385 if (progress->fields & MNCC_F_USERUSER)
2386 encode_useruser(msg, 0, &progress->useruser);
2387
Harald Welte39e2ead2009-07-23 21:13:03 +02002388 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389}
2390
2391static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2392{
2393 struct gsm_mncc *connect = arg;
2394 struct msgb *msg = gsm48_msgb_alloc();
2395 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2396
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397 gh->msg_type = GSM48_MT_CC_CONNECT;
2398
2399 gsm48_stop_cc_timer(trans);
2400 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2401
2402 /* facility */
2403 if (connect->fields & MNCC_F_FACILITY)
2404 encode_facility(msg, 0, &connect->facility);
2405 /* progress */
2406 if (connect->fields & MNCC_F_PROGRESS)
2407 encode_progress(msg, 0, &connect->progress);
2408 /* connected number */
2409 if (connect->fields & MNCC_F_CONNECTED)
2410 encode_connected(msg, &connect->connected);
2411 /* user-user */
2412 if (connect->fields & MNCC_F_USERUSER)
2413 encode_useruser(msg, 0, &connect->useruser);
2414
2415 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2416
Harald Welte39e2ead2009-07-23 21:13:03 +02002417 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418}
2419
2420static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2421{
2422 struct gsm48_hdr *gh = msgb_l3(msg);
2423 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2424 struct tlv_parsed tp;
2425 struct gsm_mncc connect;
2426
2427 gsm48_stop_cc_timer(trans);
2428
2429 memset(&connect, 0, sizeof(struct gsm_mncc));
2430 connect.callref = trans->callref;
2431 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2432 /* use subscriber as connected party number */
2433 if (trans->subscr) {
2434 connect.fields |= MNCC_F_CONNECTED;
2435 strncpy(connect.connected.number, trans->subscr->extension,
2436 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002437 strncpy(connect.imsi, trans->subscr->imsi,
2438 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 }
2440 /* facility */
2441 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2442 connect.fields |= MNCC_F_FACILITY;
2443 decode_facility(&connect.facility,
2444 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2445 }
2446 /* user-user */
2447 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2448 connect.fields |= MNCC_F_USERUSER;
2449 decode_useruser(&connect.useruser,
2450 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2451 }
2452 /* ss-version */
2453 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2454 connect.fields |= MNCC_F_SSVERSION;
2455 decode_ssversion(&connect.ssversion,
2456 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2457 }
2458
2459 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2460
Harald Welte596fed42009-07-23 19:06:52 +02002461 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462}
2463
2464
2465static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm_mncc connect_ack;
2468
2469 gsm48_stop_cc_timer(trans);
2470
2471 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2472
2473 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2474 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002475 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 &connect_ack);
2477}
2478
2479static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2480{
2481 struct msgb *msg = gsm48_msgb_alloc();
2482 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2483
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2485
2486 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2487
Harald Welte39e2ead2009-07-23 21:13:03 +02002488 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489}
2490
2491static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2492{
2493 struct gsm48_hdr *gh = msgb_l3(msg);
2494 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2495 struct tlv_parsed tp;
2496 struct gsm_mncc disc;
2497
2498 gsm48_stop_cc_timer(trans);
2499
2500 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2501
2502 memset(&disc, 0, sizeof(struct gsm_mncc));
2503 disc.callref = trans->callref;
2504 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2505 /* cause */
2506 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2507 disc.fields |= MNCC_F_CAUSE;
2508 decode_cause(&disc.cause,
2509 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2510 }
2511 /* facility */
2512 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2513 disc.fields |= MNCC_F_FACILITY;
2514 decode_facility(&disc.facility,
2515 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2516 }
2517 /* user-user */
2518 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2519 disc.fields |= MNCC_F_USERUSER;
2520 decode_useruser(&disc.useruser,
2521 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2522 }
2523 /* ss-version */
2524 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2525 disc.fields |= MNCC_F_SSVERSION;
2526 decode_ssversion(&disc.ssversion,
2527 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2528 }
2529
Harald Welte596fed42009-07-23 19:06:52 +02002530 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531
2532}
2533
Harald Weltec66b71c2009-06-11 14:23:20 +08002534static struct gsm_mncc_cause default_cause = {
2535 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2536 .coding = 0,
2537 .rec = 0,
2538 .rec_val = 0,
2539 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2540 .diag_len = 0,
2541 .diag = { 0 },
2542};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543
2544static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2545{
2546 struct gsm_mncc *disc = arg;
2547 struct msgb *msg = gsm48_msgb_alloc();
2548 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2549
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2551
2552 gsm48_stop_cc_timer(trans);
2553 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2554
2555 /* cause */
2556 if (disc->fields & MNCC_F_CAUSE)
2557 encode_cause(msg, 1, &disc->cause);
2558 else
2559 encode_cause(msg, 1, &default_cause);
2560
2561 /* facility */
2562 if (disc->fields & MNCC_F_FACILITY)
2563 encode_facility(msg, 0, &disc->facility);
2564 /* progress */
2565 if (disc->fields & MNCC_F_PROGRESS)
2566 encode_progress(msg, 0, &disc->progress);
2567 /* user-user */
2568 if (disc->fields & MNCC_F_USERUSER)
2569 encode_useruser(msg, 0, &disc->useruser);
2570
2571 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002572 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573
2574 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2575
Harald Welte39e2ead2009-07-23 21:13:03 +02002576 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577}
2578
2579static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2580{
2581 struct gsm48_hdr *gh = msgb_l3(msg);
2582 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2583 struct tlv_parsed tp;
2584 struct gsm_mncc rel;
2585 int rc;
2586
2587 gsm48_stop_cc_timer(trans);
2588
2589 memset(&rel, 0, sizeof(struct gsm_mncc));
2590 rel.callref = trans->callref;
2591 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2592 /* cause */
2593 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2594 rel.fields |= MNCC_F_CAUSE;
2595 decode_cause(&rel.cause,
2596 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2597 }
2598 /* facility */
2599 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2600 rel.fields |= MNCC_F_FACILITY;
2601 decode_facility(&rel.facility,
2602 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2603 }
2604 /* user-user */
2605 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2606 rel.fields |= MNCC_F_USERUSER;
2607 decode_useruser(&rel.useruser,
2608 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2609 }
2610 /* ss-version */
2611 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2612 rel.fields |= MNCC_F_SSVERSION;
2613 decode_ssversion(&rel.ssversion,
2614 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2615 }
2616
Harald Weltedcaf5652009-07-23 18:56:43 +02002617 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002619 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002621 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002622 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002623 GSM48_MT_CC_RELEASE_COMPL);
2624 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 }
2626
2627 new_cc_state(trans, GSM_CSTATE_NULL);
2628
2629 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002630 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631
2632 return rc;
2633}
2634
2635static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *rel = arg;
2638 struct msgb *msg = gsm48_msgb_alloc();
2639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_RELEASE;
2642
2643 trans->callref = 0;
2644
2645 gsm48_stop_cc_timer(trans);
2646 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2647
2648 /* cause */
2649 if (rel->fields & MNCC_F_CAUSE)
2650 encode_cause(msg, 0, &rel->cause);
2651 /* facility */
2652 if (rel->fields & MNCC_F_FACILITY)
2653 encode_facility(msg, 0, &rel->facility);
2654 /* user-user */
2655 if (rel->fields & MNCC_F_USERUSER)
2656 encode_useruser(msg, 0, &rel->useruser);
2657
Harald Weltedcaf5652009-07-23 18:56:43 +02002658 trans->cc.T308_second = 0;
2659 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660
Harald Weltedcaf5652009-07-23 18:56:43 +02002661 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2663
Harald Welte39e2ead2009-07-23 21:13:03 +02002664 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665}
2666
2667static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2668{
2669 struct gsm48_hdr *gh = msgb_l3(msg);
2670 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2671 struct tlv_parsed tp;
2672 struct gsm_mncc rel;
2673 int rc = 0;
2674
2675 gsm48_stop_cc_timer(trans);
2676
2677 memset(&rel, 0, sizeof(struct gsm_mncc));
2678 rel.callref = trans->callref;
2679 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2680 /* cause */
2681 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2682 rel.fields |= MNCC_F_CAUSE;
2683 decode_cause(&rel.cause,
2684 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2685 }
2686 /* facility */
2687 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2688 rel.fields |= MNCC_F_FACILITY;
2689 decode_facility(&rel.facility,
2690 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2691 }
2692 /* user-user */
2693 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2694 rel.fields |= MNCC_F_USERUSER;
2695 decode_useruser(&rel.useruser,
2696 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2697 }
2698 /* ss-version */
2699 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2700 rel.fields |= MNCC_F_SSVERSION;
2701 decode_ssversion(&rel.ssversion,
2702 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2703 }
2704
2705 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002706 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002708 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 MNCC_REJ_IND, &rel);
2710 break;
2711 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002712 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 MNCC_REL_CNF, &rel);
2714 break;
2715 default:
Harald Welte596fed42009-07-23 19:06:52 +02002716 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 MNCC_REL_IND, &rel);
2718 }
2719 }
2720
2721 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002722 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723
2724 return rc;
2725}
2726
2727static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2728{
2729 struct gsm_mncc *rel = arg;
2730 struct msgb *msg = gsm48_msgb_alloc();
2731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2732
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2734
2735 trans->callref = 0;
2736
2737 gsm48_stop_cc_timer(trans);
2738
2739 /* cause */
2740 if (rel->fields & MNCC_F_CAUSE)
2741 encode_cause(msg, 0, &rel->cause);
2742 /* facility */
2743 if (rel->fields & MNCC_F_FACILITY)
2744 encode_facility(msg, 0, &rel->facility);
2745 /* user-user */
2746 if (rel->fields & MNCC_F_USERUSER)
2747 encode_useruser(msg, 0, &rel->useruser);
2748
Harald Weltedcaf5652009-07-23 18:56:43 +02002749 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750
Harald Welte39e2ead2009-07-23 21:13:03 +02002751 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752}
2753
2754static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2755{
2756 struct gsm48_hdr *gh = msgb_l3(msg);
2757 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2758 struct tlv_parsed tp;
2759 struct gsm_mncc fac;
2760
2761 memset(&fac, 0, sizeof(struct gsm_mncc));
2762 fac.callref = trans->callref;
2763 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2764 /* facility */
2765 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2766 fac.fields |= MNCC_F_FACILITY;
2767 decode_facility(&fac.facility,
2768 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2769 }
2770 /* ss-version */
2771 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2772 fac.fields |= MNCC_F_SSVERSION;
2773 decode_ssversion(&fac.ssversion,
2774 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2775 }
2776
Harald Welte596fed42009-07-23 19:06:52 +02002777 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778}
2779
2780static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2781{
2782 struct gsm_mncc *fac = arg;
2783 struct msgb *msg = gsm48_msgb_alloc();
2784 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2785
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 gh->msg_type = GSM48_MT_CC_FACILITY;
2787
2788 /* facility */
2789 encode_facility(msg, 1, &fac->facility);
2790
Harald Welte39e2ead2009-07-23 21:13:03 +02002791 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792}
2793
2794static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2795{
2796 struct gsm_mncc hold;
2797
2798 memset(&hold, 0, sizeof(struct gsm_mncc));
2799 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002800 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801}
2802
2803static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2804{
2805 struct msgb *msg = gsm48_msgb_alloc();
2806 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2807
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2809
Harald Welte39e2ead2009-07-23 21:13:03 +02002810 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811}
2812
2813static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2814{
2815 struct gsm_mncc *hold_rej = 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_HOLD_REJ;
2820
2821 /* cause */
2822 if (hold_rej->fields & MNCC_F_CAUSE)
2823 encode_cause(msg, 1, &hold_rej->cause);
2824 else
2825 encode_cause(msg, 1, &default_cause);
2826
Harald Welte39e2ead2009-07-23 21:13:03 +02002827 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828}
2829
2830static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2831{
2832 struct gsm_mncc retrieve;
2833
2834 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2835 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002836 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2837 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838}
2839
2840static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2841{
2842 struct msgb *msg = gsm48_msgb_alloc();
2843 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2844
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2846
Harald Welte39e2ead2009-07-23 21:13:03 +02002847 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848}
2849
2850static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2851{
2852 struct gsm_mncc *retrieve_rej = arg;
2853 struct msgb *msg = gsm48_msgb_alloc();
2854 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2855
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2857
2858 /* cause */
2859 if (retrieve_rej->fields & MNCC_F_CAUSE)
2860 encode_cause(msg, 1, &retrieve_rej->cause);
2861 else
2862 encode_cause(msg, 1, &default_cause);
2863
Harald Welte39e2ead2009-07-23 21:13:03 +02002864 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865}
2866
2867static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2868{
2869 struct gsm48_hdr *gh = msgb_l3(msg);
2870 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2871 struct tlv_parsed tp;
2872 struct gsm_mncc dtmf;
2873
2874 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2875 dtmf.callref = trans->callref;
2876 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2877 /* keypad facility */
2878 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2879 dtmf.fields |= MNCC_F_KEYPAD;
2880 decode_keypad(&dtmf.keypad,
2881 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2882 }
2883
Harald Welte596fed42009-07-23 19:06:52 +02002884 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885}
2886
2887static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2888{
2889 struct gsm_mncc *dtmf = arg;
2890 struct msgb *msg = gsm48_msgb_alloc();
2891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2892
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2894
2895 /* keypad */
2896 if (dtmf->fields & MNCC_F_KEYPAD)
2897 encode_keypad(msg, dtmf->keypad);
2898
Harald Welte39e2ead2009-07-23 21:13:03 +02002899 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900}
2901
2902static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2903{
2904 struct gsm_mncc *dtmf = arg;
2905 struct msgb *msg = gsm48_msgb_alloc();
2906 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2907
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2909
2910 /* cause */
2911 if (dtmf->fields & MNCC_F_CAUSE)
2912 encode_cause(msg, 1, &dtmf->cause);
2913 else
2914 encode_cause(msg, 1, &default_cause);
2915
Harald Welte39e2ead2009-07-23 21:13:03 +02002916 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917}
2918
2919static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2920{
2921 struct msgb *msg = gsm48_msgb_alloc();
2922 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2923
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2925
Harald Welte39e2ead2009-07-23 21:13:03 +02002926 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927}
2928
2929static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2930{
2931 struct gsm_mncc dtmf;
2932
2933 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2934 dtmf.callref = trans->callref;
2935
Harald Welte596fed42009-07-23 19:06:52 +02002936 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937}
2938
2939static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2940{
2941 struct gsm48_hdr *gh = msgb_l3(msg);
2942 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2943 struct tlv_parsed tp;
2944 struct gsm_mncc modify;
2945
2946 memset(&modify, 0, sizeof(struct gsm_mncc));
2947 modify.callref = trans->callref;
2948 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2949 /* bearer capability */
2950 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2951 modify.fields |= MNCC_F_BEARER_CAP;
2952 decode_bearer_cap(&modify.bearer_cap,
2953 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2954 }
2955
2956 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2957
Harald Welte596fed42009-07-23 19:06:52 +02002958 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959}
2960
2961static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2962{
2963 struct gsm_mncc *modify = arg;
2964 struct msgb *msg = gsm48_msgb_alloc();
2965 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2966
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 gh->msg_type = GSM48_MT_CC_MODIFY;
2968
2969 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2970
2971 /* bearer capability */
2972 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2973
2974 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2975
Harald Welte39e2ead2009-07-23 21:13:03 +02002976 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977}
2978
2979static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2980{
2981 struct gsm48_hdr *gh = msgb_l3(msg);
2982 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2983 struct tlv_parsed tp;
2984 struct gsm_mncc modify;
2985
2986 gsm48_stop_cc_timer(trans);
2987
2988 memset(&modify, 0, sizeof(struct gsm_mncc));
2989 modify.callref = trans->callref;
2990 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2991 /* bearer capability */
2992 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2993 modify.fields |= MNCC_F_BEARER_CAP;
2994 decode_bearer_cap(&modify.bearer_cap,
2995 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2996 }
2997
2998 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2999
Harald Welte596fed42009-07-23 19:06:52 +02003000 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003001}
3002
3003static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3004{
3005 struct gsm_mncc *modify = arg;
3006 struct msgb *msg = gsm48_msgb_alloc();
3007 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3008
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3010
3011 /* bearer capability */
3012 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3013
3014 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3015
Harald Welte39e2ead2009-07-23 21:13:03 +02003016 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017}
3018
3019static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3020{
3021 struct gsm48_hdr *gh = msgb_l3(msg);
3022 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3023 struct tlv_parsed tp;
3024 struct gsm_mncc modify;
3025
3026 gsm48_stop_cc_timer(trans);
3027
3028 memset(&modify, 0, sizeof(struct gsm_mncc));
3029 modify.callref = trans->callref;
3030 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3031 /* bearer capability */
3032 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3033 modify.fields |= GSM48_IE_BEARER_CAP;
3034 decode_bearer_cap(&modify.bearer_cap,
3035 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3036 }
3037 /* cause */
3038 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3039 modify.fields |= MNCC_F_CAUSE;
3040 decode_cause(&modify.cause,
3041 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3042 }
3043
3044 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3045
Harald Welte596fed42009-07-23 19:06:52 +02003046 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047}
3048
3049static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3050{
3051 struct gsm_mncc *modify = arg;
3052 struct msgb *msg = gsm48_msgb_alloc();
3053 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3054
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3056
3057 /* bearer capability */
3058 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3059 /* cause */
3060 encode_cause(msg, 1, &modify->cause);
3061
3062 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3063
Harald Welte39e2ead2009-07-23 21:13:03 +02003064 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065}
3066
3067static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3068{
3069 struct gsm_mncc *notify = arg;
3070 struct msgb *msg = gsm48_msgb_alloc();
3071 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3072
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073 gh->msg_type = GSM48_MT_CC_NOTIFY;
3074
3075 /* notify */
3076 encode_notify(msg, notify->notify);
3077
Harald Welte39e2ead2009-07-23 21:13:03 +02003078 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079}
3080
3081static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3082{
3083 struct gsm48_hdr *gh = msgb_l3(msg);
3084 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3085// struct tlv_parsed tp;
3086 struct gsm_mncc notify;
3087
3088 memset(&notify, 0, sizeof(struct gsm_mncc));
3089 notify.callref = trans->callref;
3090// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3091 if (payload_len >= 1)
3092 decode_notify(&notify.notify, gh->data);
3093
Harald Welte596fed42009-07-23 19:06:52 +02003094 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095}
3096
3097static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3098{
3099 struct gsm_mncc *user = arg;
3100 struct msgb *msg = gsm48_msgb_alloc();
3101 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3102
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103 gh->msg_type = GSM48_MT_CC_USER_INFO;
3104
3105 /* user-user */
3106 if (user->fields & MNCC_F_USERUSER)
3107 encode_useruser(msg, 1, &user->useruser);
3108 /* more data */
3109 if (user->more)
3110 encode_more(msg);
3111
Harald Welte39e2ead2009-07-23 21:13:03 +02003112 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003113}
3114
3115static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3116{
3117 struct gsm48_hdr *gh = msgb_l3(msg);
3118 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3119 struct tlv_parsed tp;
3120 struct gsm_mncc user;
3121
3122 memset(&user, 0, sizeof(struct gsm_mncc));
3123 user.callref = trans->callref;
3124 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3125 /* user-user */
3126 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3127 user.fields |= MNCC_F_USERUSER;
3128 decode_useruser(&user.useruser,
3129 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3130 }
3131 /* more data */
3132 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3133 user.more = 1;
3134
Harald Welte596fed42009-07-23 19:06:52 +02003135 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136}
3137
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003138static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139{
3140 struct gsm_mncc *mode = arg;
3141
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003142 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143}
3144
3145static struct downstate {
3146 u_int32_t states;
3147 int type;
3148 int (*rout) (struct gsm_trans *trans, void *arg);
3149} downstatelist[] = {
3150 /* mobile originating call establishment */
3151 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3152 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3153 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3154 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3155 {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 */
3156 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3157 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3158 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3159 /* mobile terminating call establishment */
3160 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3161 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3162 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3163 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3164 /* signalling during call */
3165 {SBIT(GSM_CSTATE_ACTIVE),
3166 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3167 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3168 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3169 {ALL_STATES,
3170 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3171 {ALL_STATES,
3172 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3173 {ALL_STATES,
3174 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3175 {SBIT(GSM_CSTATE_ACTIVE),
3176 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3177 {SBIT(GSM_CSTATE_ACTIVE),
3178 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3179 {SBIT(GSM_CSTATE_ACTIVE),
3180 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3181 {SBIT(GSM_CSTATE_ACTIVE),
3182 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3185 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3186 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3187 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3188 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3189 {SBIT(GSM_CSTATE_ACTIVE),
3190 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3191 /* clearing */
3192 {SBIT(GSM_CSTATE_INITIATED),
3193 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3194 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3195 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3196 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3197 MNCC_REL_REQ, gsm48_cc_tx_release},
3198 /* special */
3199 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003200 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003201};
3202
3203#define DOWNSLLEN \
3204 (sizeof(downstatelist) / sizeof(struct downstate))
3205
3206
3207int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3208{
Harald Welte1a6f7982009-08-09 18:52:33 +02003209 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003210 struct gsm_trans *trans = NULL, *transt;
3211 struct gsm_subscriber *subscr;
Harald Welte1a6f7982009-08-09 18:52:33 +02003212 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003213 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214 struct gsm_mncc *data = arg, rel;
3215
3216 /* handle special messages */
3217 switch(msg_type) {
3218 case MNCC_BRIDGE:
3219 return tch_bridge(net, arg);
3220 case MNCC_FRAME_DROP:
3221 return tch_recv(net, arg, 0);
3222 case MNCC_FRAME_RECV:
3223 return tch_recv(net, arg, 1);
3224 case GSM_TRAU_FRAME:
3225 return tch_frame(net, arg);
3226 }
3227
3228 memset(&rel, 0, sizeof(struct gsm_mncc));
3229 rel.callref = data->callref;
3230
3231 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003232 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003233
3234 /* Callref unknown */
3235 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003236 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003237 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3238 "Received '%s' from MNCC with "
3239 "unknown callref %d\n", data->called.number,
3240 get_mncc_name(msg_type), data->callref);
3241 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003242 return mncc_release_ind(net, NULL, data->callref,
3243 GSM48_CAUSE_LOC_PRN_S_LU,
3244 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003246 if (!data->called.number[0] && !data->imsi[0]) {
3247 DEBUGP(DCC, "(bts - trx - ts - ti) "
3248 "Received '%s' from MNCC with "
3249 "no number or IMSI\n", get_mncc_name(msg_type));
3250 /* Invalid number */
3251 return mncc_release_ind(net, NULL, data->callref,
3252 GSM48_CAUSE_LOC_PRN_S_LU,
3253 GSM48_CC_CAUSE_INV_NR_FORMAT);
3254 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003256 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003257 subscr = subscr_get_by_extension(net,
3258 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003259 else
Harald Welte9176bd42009-07-23 18:46:00 +02003260 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261 /* If subscriber is not found */
3262 if (!subscr) {
3263 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3264 "Received '%s' from MNCC with "
3265 "unknown subscriber %s\n", data->called.number,
3266 get_mncc_name(msg_type), data->called.number);
3267 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003268 return mncc_release_ind(net, NULL, data->callref,
3269 GSM48_CAUSE_LOC_PRN_S_LU,
3270 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003271 }
3272 /* If subscriber is not "attached" */
3273 if (!subscr->lac) {
3274 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3275 "Received '%s' from MNCC with "
3276 "detached subscriber %s\n", data->called.number,
3277 get_mncc_name(msg_type), data->called.number);
3278 subscr_put(subscr);
3279 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003280 return mncc_release_ind(net, NULL, data->callref,
3281 GSM48_CAUSE_LOC_PRN_S_LU,
3282 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 }
3284 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003285 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3286 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 DEBUGP(DCC, "No memory for trans.\n");
3288 subscr_put(subscr);
3289 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003290 mncc_release_ind(net, NULL, data->callref,
3291 GSM48_CAUSE_LOC_PRN_S_LU,
3292 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003293 return -ENOMEM;
3294 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003296 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 /* If subscriber has no lchan */
3298 if (!lchan) {
3299 /* find transaction with this subscriber already paging */
3300 llist_for_each_entry(transt, &net->trans_list, entry) {
3301 /* Transaction of our lchan? */
3302 if (transt == trans ||
3303 transt->subscr != subscr)
3304 continue;
3305 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3306 "Received '%s' from MNCC with "
3307 "unallocated channel, paging already "
3308 "started.\n", bts->nr,
3309 data->called.number,
3310 get_mncc_name(msg_type));
3311 return 0;
3312 }
3313 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003314 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003315 /* Trigger paging */
3316 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3317 setup_trig_pag_evt, subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003318 return 0;
3319 }
3320 /* Assign lchan */
3321 trans->lchan = lchan;
3322 use_lchan(lchan);
3323 }
3324 lchan = trans->lchan;
3325
3326 /* if paging did not respond yet */
3327 if (!lchan) {
3328 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3329 "Received '%s' from MNCC in paging state\n",
3330 (trans->subscr)?(trans->subscr->extension):"-",
3331 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003332 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3333 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003334 if (msg_type == MNCC_REL_REQ)
3335 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3336 else
3337 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3338 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003339 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003340 return rc;
3341 }
3342
3343 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3344 "Received '%s' from MNCC in state %d (%s)\n",
3345 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3346 trans->transaction_id,
3347 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003348 get_mncc_name(msg_type), trans->cc.state,
3349 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003350
3351 /* Find function for current state and message */
3352 for (i = 0; i < DOWNSLLEN; i++)
3353 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003354 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 break;
3356 if (i == DOWNSLLEN) {
3357 DEBUGP(DCC, "Message unhandled at this state.\n");
3358 return 0;
3359 }
3360
3361 rc = downstatelist[i].rout(trans, arg);
3362
3363 return rc;
3364}
3365
3366
3367static struct datastate {
3368 u_int32_t states;
3369 int type;
3370 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3371} datastatelist[] = {
3372 /* mobile originating call establishment */
3373 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3374 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3375 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3376 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3377 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3378 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3379 /* mobile terminating call establishment */
3380 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3381 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3382 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3383 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3384 {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 */
3385 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3386 /* signalling during call */
3387 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3388 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3389 {SBIT(GSM_CSTATE_ACTIVE),
3390 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3391 {ALL_STATES,
3392 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3393 {ALL_STATES,
3394 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3395 {ALL_STATES,
3396 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3397 {SBIT(GSM_CSTATE_ACTIVE),
3398 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3399 {SBIT(GSM_CSTATE_ACTIVE),
3400 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3401 {SBIT(GSM_CSTATE_ACTIVE),
3402 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3403 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3404 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3405 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3406 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3407 {SBIT(GSM_CSTATE_ACTIVE),
3408 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3409 /* clearing */
3410 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3411 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3412 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3413 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3414 {ALL_STATES, /* 5.4.3.4 */
3415 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3416};
3417
3418#define DATASLLEN \
3419 (sizeof(datastatelist) / sizeof(struct datastate))
3420
Harald Welte4bc90a12008-12-27 16:32:52 +00003421static int gsm0408_rcv_cc(struct msgb *msg)
3422{
3423 struct gsm48_hdr *gh = msgb_l3(msg);
3424 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003425 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003426 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003427 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003428 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003429
Harald Welte4bfdfe72009-06-10 23:11:52 +08003430 if (msg_type & 0x80) {
3431 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3432 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003433 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003434
3435 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003436 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003437
Harald Welte6f5aee02009-07-23 21:21:14 +02003438 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003439 "Received '%s' from MS in state %d (%s)\n",
3440 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3441 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003442 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003443 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003444
3445 /* Create transaction */
3446 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003447 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003448 "creating new trans.\n", transaction_id);
3449 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003450 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3451 transaction_id, new_callref++);
3452 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003453 DEBUGP(DCC, "No memory for trans.\n");
3454 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003455 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003456 GSM48_MT_CC_RELEASE_COMPL);
3457 return -ENOMEM;
3458 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003459 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003460 trans->lchan = lchan;
3461 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003462 }
3463
3464 /* find function for current state and message */
3465 for (i = 0; i < DATASLLEN; i++)
3466 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003467 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003468 break;
3469 if (i == DATASLLEN) {
3470 DEBUGP(DCC, "Message unhandled at this state.\n");
3471 return 0;
3472 }
3473
3474 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003475
3476 return rc;
3477}
3478
Harald Welte52b1f982008-12-23 20:25:15 +00003479/* 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 +02003480int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003481{
3482 struct gsm48_hdr *gh = msgb_l3(msg);
3483 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003484 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003485
3486 switch (pdisc) {
3487 case GSM48_PDISC_CC:
3488 rc = gsm0408_rcv_cc(msg);
3489 break;
3490 case GSM48_PDISC_MM:
3491 rc = gsm0408_rcv_mm(msg);
3492 break;
3493 case GSM48_PDISC_RR:
3494 rc = gsm0408_rcv_rr(msg);
3495 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003496 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003497 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003498 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003499 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003500 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003501 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003502 pdisc);
3503 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003504 case GSM48_PDISC_NC_SS:
3505 rc = handle_rcv_ussd(msg);
3506 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003507 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003508 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003509 pdisc);
3510 break;
3511 }
3512
3513 return rc;
3514}
Harald Welte8470bf22008-12-25 23:28:35 +00003515
Harald Welte4bfdfe72009-06-10 23:11:52 +08003516/* dequeue messages to layer 4 */
3517int bsc_upqueue(struct gsm_network *net)
3518{
3519 struct gsm_mncc *mncc;
3520 struct msgb *msg;
3521 int work = 0;
3522
3523 if (net)
3524 while ((msg = msgb_dequeue(&net->upqueue))) {
3525 mncc = (struct gsm_mncc *)msg->data;
3526 if (net->mncc_recv)
3527 net->mncc_recv(net, mncc->msg_type, mncc);
3528 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003529 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003530 }
3531
3532 return work;
3533}
Harald Weltedcaf5652009-07-23 18:56:43 +02003534
Harald Welte805f6442009-07-28 18:25:29 +02003535/*
3536 * This will be ran by the linker when loading the DSO. We use it to
3537 * do system initialization, e.g. registration of signal handlers.
3538 */
3539static __attribute__((constructor)) void on_dso_load_0408(void)
3540{
Harald Welte805f6442009-07-28 18:25:29 +02003541 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3542 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3543}