blob: cd883a3fa223cf02655c86aa409a565cb4212a2a [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 Welte52b1f982008-12-23 20:25:15 +000051
Harald Welte0c389302009-06-10 12:08:54 +080052#define GSM_MAX_FACILITY 128
53#define GSM_MAX_SSVERSION 128
54#define GSM_MAX_USERUSER 128
55
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020057
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020058extern int ipacc_rtp_direct;
Harald Welte805f6442009-07-28 18:25:29 +020059
Harald Welte09e38af2009-02-16 22:52:23 +000060static const struct tlv_definition rsl_att_tlvdef = {
61 .def = {
62 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
63 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
65 [GSM48_IE_UTC] = { TLV_TYPE_TV },
66 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
67 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
68
69 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
70 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
71 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
72 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
73 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
74 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
75 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080076 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000077 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
78 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080079 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
80 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000081 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
82 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
85 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
87 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
88 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
90 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
91 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
92 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
94 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080095 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
96 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000097 /* FIXME: more elements */
98 },
99};
Harald Weltecf5b3592009-05-01 18:28:42 +0000100
101static const char *rr_cause_names[] = {
102 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
103 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
104 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
105 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
106 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
107 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
108 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
109 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
110 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
111 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
112 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
113 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
114 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
115 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
116 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
117 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
118 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
119};
120
Harald Welte4bfdfe72009-06-10 23:11:52 +0800121static const char *cc_state_names[] = {
122 "NULL",
123 "INITIATED",
124 "illegal state 2",
125 "MO_CALL_PROC",
126 "CALL_DELIVERED",
127 "illegal state 5",
128 "CALL_PRESENT",
129 "CALL_RECEIVED",
130 "CONNECT_REQUEST",
131 "MO_TERM_CALL_CONF",
132 "ACTIVE",
133 "DISCONNECT_REQ",
134 "DISCONNECT_IND",
135 "illegal state 13",
136 "illegal state 14",
137 "illegal state 15",
138 "illegal state 16",
139 "illegal state 17",
140 "illegal state 18",
141 "RELEASE_REQ",
142 "illegal state 20",
143 "illegal state 21",
144 "illegal state 22",
145 "illegal state 23",
146 "illegal state 24",
147 "illegal state 25",
148 "MO_ORIG_MODIFY",
149 "MO_TERM_MODIFY",
150 "CONNECT_IND",
151 "illegal state 29",
152 "illegal state 30",
153 "illegal state 31",
154};
155
Harald Weltecf5b3592009-05-01 18:28:42 +0000156static char strbuf[64];
157
158static const char *rr_cause_name(u_int8_t cause)
159{
160 if (cause < ARRAY_SIZE(rr_cause_names) &&
161 rr_cause_names[cause])
162 return rr_cause_names[cause];
163
164 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
165 return strbuf;
166}
167
Harald Weltef7c43522009-06-09 20:24:21 +0000168static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
169 int len)
170{
171 memset(rep, 0, sizeof(*rep));
172
173 if (data[0] & 0x80)
174 rep->flags |= MEAS_REP_F_BA1;
175 if (data[0] & 0x40)
176 rep->flags |= MEAS_REP_F_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200177 if ((data[1] & 0x40) == 0x00)
Harald Weltef7c43522009-06-09 20:24:21 +0000178 rep->flags |= MEAS_REP_F_VALID;
179
180 rep->rxlev_full = data[0] & 0x3f;
181 rep->rxlev_sub = data[1] & 0x3f;
182 rep->rxqual_full = (data[3] >> 4) & 0x7;
183 rep->rxqual_sub = (data[3] >> 1) & 0x7;
184 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
185 if (rep->num_cell < 1)
186 return;
187
188 /* an encoding nightmare in perfection */
189
190 rep->cell[0].rxlev = data[4] & 0x3f;
191 rep->cell[0].bcch_freq = data[5] >> 2;
192 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
193 if (rep->num_cell < 2)
194 return;
195
196 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
197 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
198 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
199 if (rep->num_cell < 3)
200 return;
201
202 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
203 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
204 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
205 if (rep->num_cell < 4)
206 return;
207
208 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
209 rep->cell[3].bcch_freq = data[11] & 0x1f;
210 rep->cell[3].bsic = data[12] >> 2;
211 if (rep->num_cell < 5)
212 return;
213
214 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
215 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
216 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
217 if (rep->num_cell < 6)
218 return;
219
220 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
221 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
222 rep->cell[5].bsic = data[16] & 0x3f;
223}
224
Holger Freytherd51524f2009-06-09 08:27:07 +0000225int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000226static int gsm48_tx_simple(struct gsm_lchan *lchan,
227 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000228static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000229
Harald Welte52b1f982008-12-23 20:25:15 +0000230struct gsm_lai {
231 u_int16_t mcc;
232 u_int16_t mnc;
233 u_int16_t lac;
234};
235
Holger Freythere97f7fb2008-12-31 18:52:11 +0000236static int reject_cause = 0;
237void gsm0408_set_reject_cause(int cause)
238{
239 reject_cause = cause;
240}
241
Harald Welte4bfdfe72009-06-10 23:11:52 +0800242static u_int32_t new_callref = 0x80000001;
243
Holger Freyther73487a22008-12-31 18:53:57 +0000244static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
245 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000246{
247 if (!subscriber)
248 return 0;
249
Holger Freyther73487a22008-12-31 18:53:57 +0000250 /*
251 * Do not send accept yet as more information should arrive. Some
252 * phones will not send us the information and we will have to check
253 * what we want to do with that.
254 */
255 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
256 return 0;
257
Jan Luebbe06513f22009-08-12 12:48:00 +0200258 switch (subscriber->net->auth_policy) {
259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Freyther73487a22008-12-31 18:53:57 +0000272static void release_loc_updating_req(struct gsm_lchan *lchan)
273{
Harald Welte179f0642008-12-31 23:59:18 +0000274 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000275 return;
276
Harald Welteff117a82009-05-23 05:22:08 +0000277 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200278 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000279 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000280 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000281}
282
283static void allocate_loc_updating_req(struct gsm_lchan *lchan)
284{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000285 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000286 release_loc_updating_req(lchan);
287
Harald Welte470ec292009-06-26 20:25:23 +0200288 lchan->loc_operation = talloc_zero(tall_locop_ctx,
289 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000290}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000291
Holger Freytherd51524f2009-06-09 08:27:07 +0000292static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
293{
Holger Freytherd51524f2009-06-09 08:27:07 +0000294 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200295 int rc;
296
Holger Freytherd51524f2009-06-09 08:27:07 +0000297 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000298 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200299 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welteee5ad162009-08-09 19:07:00 +0200300 /* call subscr_update after putting the loc_upd_acc
301 * in the transmit queue, since S_SUBSCR_ATTACHED might
302 * trigger further action like SMS delivery */
303 subscr_update(lchan->subscr, msg->trx->bts,
304 GSM_SUBSCRIBER_UPDATE_ATTACHED);
305 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000306 }
307
308 return 0;
309}
310
Holger Freyther7c19f742009-06-06 13:54:35 +0000311static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
312 void *handler_data, void *signal_data)
313{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800314 struct gsm_trans *trans, *temp;
315
Holger Freyther7c19f742009-06-06 13:54:35 +0000316 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
317 return 0;
318
319 /*
320 * Cancel any outstanding location updating request
321 * operation taking place on the lchan.
322 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200323 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200324 if (!lchan)
325 return 0;
326
Holger Freyther7c19f742009-06-06 13:54:35 +0000327 release_loc_updating_req(lchan);
328
Harald Welte4bfdfe72009-06-10 23:11:52 +0800329 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200330 /* FIXME: this is not neccessarily the right thing to do, we should
331 * only set trans->lchan to NULL and wait for another lchan to be
332 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800333 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
334 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200335 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 }
337
Holger Freyther7c19f742009-06-06 13:54:35 +0000338 return 0;
339}
340
Harald Welte09e38af2009-02-16 22:52:23 +0000341static const char bcd_num_digits[] = {
342 '0', '1', '2', '3', '4', '5', '6', '7',
343 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
344};
345
Harald Welte0c389302009-06-10 12:08:54 +0800346/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
347int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
348 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000349{
350 u_int8_t in_len = bcd_lv[0];
351 int i;
352
Harald Welte0c389302009-06-10 12:08:54 +0800353 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000354 /* lower nibble */
355 output_len--;
356 if (output_len <= 1)
357 break;
358 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
359
360 /* higher nibble */
361 output_len--;
362 if (output_len <= 1)
363 break;
364 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
365 }
366 if (output_len >= 1)
367 *output++ = '\0';
368
Harald Welte0c389302009-06-10 12:08:54 +0800369 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000370}
371
372/* convert a single ASCII character to call-control BCD */
373static int asc_to_bcd(const char asc)
374{
375 int i;
376
377 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
378 if (bcd_num_digits[i] == asc)
379 return i;
380 }
381 return -EINVAL;
382}
383
Harald Welte0c389302009-06-10 12:08:54 +0800384/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000385int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800386 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000387{
388 int in_len = strlen(input);
389 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800390 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000391
392 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800393 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000394 if (in_len % 2)
395 bcd_lv[0]++;
396
Harald Welte0c389302009-06-10 12:08:54 +0800397 if (bcd_lv[0] > max_len)
398 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000399
400 for (i = 0; i < in_len; i++) {
401 int rc = asc_to_bcd(input[i]);
402 if (rc < 0)
403 return rc;
404 if (i % 2 == 0)
405 *bcd_cur = rc;
406 else
407 *bcd_cur++ |= (rc << 4);
408 }
409 /* append padding nibble in case of odd length */
410 if (i % 2)
411 *bcd_cur++ |= 0xf0;
412
413 /* return how many bytes we used */
414 return (bcd_cur - bcd_lv);
415}
416
Harald Welte0c389302009-06-10 12:08:54 +0800417/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800418static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800419 const u_int8_t *lv)
420{
421 u_int8_t in_len = lv[0];
422 int i, s;
423
424 if (in_len < 1)
425 return -EINVAL;
426
Harald Welte4bfdfe72009-06-10 23:11:52 +0800427 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800428
429 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800430 bcap->transfer = lv[1] & 0x07;
431 bcap->mode = (lv[1] & 0x08) >> 3;
432 bcap->coding = (lv[1] & 0x10) >> 4;
433 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800434
435 i = 1;
436 s = 0;
437 while(!(lv[i] & 0x80)) {
438 i++; /* octet 3a etc */
439 if (in_len < i)
440 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800441 bcap->speech_ver[s++] = lv[i] & 0x0f;
442 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800443 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800445 if (s == 7) /* maximum speech versions + end of list */
446 return 0;
447 }
448
449 return 0;
450}
451
452/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800453static int encode_bearer_cap(struct msgb *msg, int lv_only,
454 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800455{
456 u_int8_t lv[32 + 1];
457 int i, s;
458
Harald Welte4bfdfe72009-06-10 23:11:52 +0800459 lv[1] = bcap->transfer;
460 lv[1] |= bcap->mode << 3;
461 lv[1] |= bcap->coding << 4;
462 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800463
464 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800465 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800466 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800467 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800468 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800469 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800470 }
471 lv[i] |= 0x80; /* last IE of octet 3 etc */
472
473 lv[0] = i;
474 if (lv_only)
475 msgb_lv_put(msg, lv[0], lv+1);
476 else
477 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
478
479 return 0;
480}
481
482/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800483static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800484{
485 u_int8_t in_len = lv[0];
486
487 if (in_len < 1)
488 return -EINVAL;
489
490 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800491 ccap->dtmf = lv[1] & 0x01;
492 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800493
494 return 0;
495}
496
497/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800498static int decode_called(struct gsm_mncc_number *called,
499 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800500{
501 u_int8_t in_len = lv[0];
502
503 if (in_len < 1)
504 return -EINVAL;
505
506 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 called->plan = lv[1] & 0x0f;
508 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800509
510 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800511 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800512
513 return 0;
514}
515
516/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800517static int encode_called(struct msgb *msg,
518 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800519{
520 u_int8_t lv[18];
521 int ret;
522
523 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800524 lv[1] = called->plan;
525 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800526
527 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800528 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800529 if (ret < 0)
530 return ret;
531
532 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
533
534 return 0;
535}
536
537/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800538static int encode_callerid(struct msgb *msg, int ie,
539 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800540{
541 u_int8_t lv[13];
542 int h_len = 1;
543 int ret;
544
545 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800546 lv[1] = callerid->plan;
547 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800548
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800550 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 lv[2] = callerid->screen;
552 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800553 lv[2] |= 0x80;
554 h_len++;
555 } else
556 lv[1] |= 0x80;
557
558 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800559 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800560 if (ret < 0)
561 return ret;
562
563 msgb_tlv_put(msg, ie, lv[0], lv+1);
564
565 return 0;
566}
567
568/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800569static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800570 const u_int8_t *lv)
571{
572 u_int8_t in_len = lv[0];
573 int i;
574
575 if (in_len < 2)
576 return -EINVAL;
577
Harald Welte4bfdfe72009-06-10 23:11:52 +0800578 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800579
580 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581 cause->location = lv[1] & 0x0f;
582 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800583
584 i = 1;
585 if (!(lv[i] & 0x80)) {
586 i++; /* octet 3a */
587 if (in_len < i+1)
588 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 cause->rec = 1;
590 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800591
592 }
593 i++;
594
595 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800596 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800597 i++;
598
599 if (in_len < i) /* no diag */
600 return 0;
601
602 if (in_len - (i-1) > 32) /* maximum 32 octets */
603 return 0;
604
605 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800606 memcpy(cause->diag, lv + i, in_len - (i-1));
607 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800608
609 return 0;
610}
611
612/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800613static int encode_cause(struct msgb *msg, int lv_only,
614 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800615{
616 u_int8_t lv[32+4];
617 int i;
618
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800620 return -EINVAL;
621
622 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800623 lv[1] = cause->location;
624 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800625
626 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800628 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800630 }
631 lv[i] |= 0x80; /* end of octet 3 */
632
633 /* octet 4 */
634 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800636
637 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638 if (cause->diag_len) {
639 memcpy(lv + i, cause->diag, cause->diag_len);
640 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800641 }
642
643 lv[0] = i;
644 if (lv_only)
645 msgb_lv_put(msg, lv[0], lv+1);
646 else
647 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
648
649 return 0;
650}
651
652/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800653static int encode_calling(struct msgb *msg,
654 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800655{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800657}
658
659/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800660static int encode_connected(struct msgb *msg,
661 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800662{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800664}
665
666/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800667static int encode_redirecting(struct msgb *msg,
668 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800669{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800671}
672
673/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800675 const u_int8_t *lv)
676{
677 u_int8_t in_len = lv[0];
678
679 if (in_len < 1)
680 return -EINVAL;
681
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800683 return -EINVAL;
684
Harald Welte4bfdfe72009-06-10 23:11:52 +0800685 memcpy(facility->info, lv+1, in_len);
686 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800687
688 return 0;
689}
690
691/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800692static int encode_facility(struct msgb *msg, int lv_only,
693 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800694{
695 u_int8_t lv[GSM_MAX_FACILITY + 1];
696
Harald Welte4bfdfe72009-06-10 23:11:52 +0800697 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800698 return -EINVAL;
699
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700 memcpy(lv+1, facility->info, facility->len);
701 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800702 if (lv_only)
703 msgb_lv_put(msg, lv[0], lv+1);
704 else
705 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
706
707 return 0;
708}
709
710/* decode 'notify' */
711static int decode_notify(int *notify, const u_int8_t *v)
712{
713 *notify = v[0] & 0x7f;
714
715 return 0;
716}
717
718/* encode 'notify' */
719static int encode_notify(struct msgb *msg, int notify)
720{
721 msgb_v_put(msg, notify | 0x80);
722
723 return 0;
724}
725
726/* encode 'signal' */
727static int encode_signal(struct msgb *msg, int signal)
728{
729 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
730
731 return 0;
732}
733
734/* decode 'keypad' */
735static int decode_keypad(int *keypad, const u_int8_t *lv)
736{
737 u_int8_t in_len = lv[0];
738
739 if (in_len < 1)
740 return -EINVAL;
741
742 *keypad = lv[1] & 0x7f;
743
744 return 0;
745}
746
747/* encode 'keypad' */
748static int encode_keypad(struct msgb *msg, int keypad)
749{
750 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
751
752 return 0;
753}
754
755/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800756static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800757 const u_int8_t *lv)
758{
759 u_int8_t in_len = lv[0];
760
761 if (in_len < 2)
762 return -EINVAL;
763
Harald Welte4bfdfe72009-06-10 23:11:52 +0800764 progress->coding = (lv[1] & 0x60) >> 5;
765 progress->location = lv[1] & 0x0f;
766 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800767
768 return 0;
769}
770
771/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800772static int encode_progress(struct msgb *msg, int lv_only,
773 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800774{
775 u_int8_t lv[3];
776
777 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800778 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
779 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800780 if (lv_only)
781 msgb_lv_put(msg, lv[0], lv+1);
782 else
783 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
784
785 return 0;
786}
787
788/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800789static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800790 const u_int8_t *lv)
791{
792 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800793 char *info = uu->info;
794 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800795 int i;
796
797 if (in_len < 1)
798 return -EINVAL;
799
Harald Welte4bfdfe72009-06-10 23:11:52 +0800800 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800801
802 for (i = 2; i <= in_len; i++) {
803 info_len--;
804 if (info_len <= 1)
805 break;
806 *info++ = lv[i];
807 }
808 if (info_len >= 1)
809 *info++ = '\0';
810
811 return 0;
812}
813
814/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800815static int encode_useruser(struct msgb *msg, int lv_only,
816 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800817{
818 u_int8_t lv[GSM_MAX_USERUSER + 2];
819
Harald Welte4bfdfe72009-06-10 23:11:52 +0800820 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800821 return -EINVAL;
822
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823 lv[0] = 1 + strlen(uu->info);
824 lv[1] = uu->proto;
825 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800826 if (lv_only)
827 msgb_lv_put(msg, lv[0], lv+1);
828 else
829 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
830
831 return 0;
832}
833
834/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800835static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800836 const u_int8_t *lv)
837{
838 u_int8_t in_len = lv[0];
839
Harald Welte4bfdfe72009-06-10 23:11:52 +0800840 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800841 return -EINVAL;
842
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843 memcpy(ssv->info, lv + 1, in_len);
844 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800845
846 return 0;
847}
848
849/* encode 'more data' */
850static int encode_more(struct msgb *msg)
851{
852 u_int8_t *ie;
853
854 ie = msgb_put(msg, 1);
855 ie[0] = GSM48_IE_MORE_DATA;
856
857 return 0;
858}
859
Holger Freyther429e7762008-12-30 13:28:30 +0000860/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000861int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000862{
Harald Welte8470bf22008-12-25 23:28:35 +0000863 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000864 struct gsm48_hdr *gh;
865
Harald Welte8470bf22008-12-25 23:28:35 +0000866 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000867
868 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
869 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000870 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000871 gh->data[0] = cause;
872
Harald Weltedb253af2008-12-30 17:56:55 +0000873 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
874
Harald Welte39e2ead2009-07-23 21:13:03 +0200875 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000876}
877
878/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000879int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000880{
Harald Welte8470bf22008-12-25 23:28:35 +0000881 struct gsm_bts *bts = lchan->ts->trx->bts;
882 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000883 struct gsm48_hdr *gh;
884 struct gsm48_loc_area_id *lai;
885 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000886 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000887
Harald Welte8470bf22008-12-25 23:28:35 +0000888 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000889
890 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
891 gh->proto_discr = GSM48_PDISC_MM;
892 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
893
894 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000895 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000896 bts->network->network_code, bts->location_area_code);
897
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200898 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200899 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000900
901 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
902
Harald Welte39e2ead2009-07-23 21:13:03 +0200903 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000904
Harald Welteee5ad162009-08-09 19:07:00 +0200905 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000906 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000907
Holger Freyther07cc8d82008-12-29 06:23:46 +0000908 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000909}
910
Harald Weltebf5e8df2009-02-03 12:59:45 +0000911/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000912static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
913{
914 struct msgb *msg = gsm48_msgb_alloc();
915 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000916
Harald Welte231ad4f2008-12-27 11:15:38 +0000917 msg->lchan = lchan;
918
919 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
920 gh->proto_discr = GSM48_PDISC_MM;
921 gh->msg_type = GSM48_MT_MM_ID_REQ;
922 gh->data[0] = id_type;
923
Harald Welte39e2ead2009-07-23 21:13:03 +0200924 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000925}
926
Harald Welte231ad4f2008-12-27 11:15:38 +0000927
Harald Weltebf5e8df2009-02-03 12:59:45 +0000928/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000929static int mm_rx_id_resp(struct msgb *msg)
930{
931 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000932 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200933 struct gsm_bts *bts = lchan->ts->trx->bts;
934 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000935 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200936 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000937
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200938 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000939 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000940 mi_type, mi_string);
941
Harald Welte75a983f2008-12-27 21:34:06 +0000942 switch (mi_type) {
943 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200944 /* look up subscriber based on IMSI, create if not found */
945 if (!lchan->subscr) {
946 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200947 if (!lchan->subscr)
948 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200949 }
Holger Freyther73487a22008-12-31 18:53:57 +0000950 if (lchan->loc_operation)
951 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000952 break;
953 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000954 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000955 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200956 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000957 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200958 db_sync_equipment(&lchan->subscr->equipment);
959 }
Holger Freyther73487a22008-12-31 18:53:57 +0000960 if (lchan->loc_operation)
961 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000962 break;
963 }
Holger Freyther73487a22008-12-31 18:53:57 +0000964
965 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000966 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000967}
968
Harald Welte255539c2008-12-28 02:26:27 +0000969
970static void loc_upd_rej_cb(void *data)
971{
972 struct gsm_lchan *lchan = data;
973
Holger Freyther73487a22008-12-31 18:53:57 +0000974 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000975 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000976 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000977}
978
Holger Freytherb7193e42008-12-29 17:44:08 +0000979static void schedule_reject(struct gsm_lchan *lchan)
980{
Holger Freyther73487a22008-12-31 18:53:57 +0000981 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
982 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000983 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000984}
985
Harald Welte2a139372009-02-22 21:14:55 +0000986static const char *lupd_name(u_int8_t type)
987{
988 switch (type) {
989 case GSM48_LUPD_NORMAL:
990 return "NORMAL";
991 case GSM48_LUPD_PERIODIC:
992 return "PEROIDOC";
993 case GSM48_LUPD_IMSI_ATT:
994 return "IMSI ATTACH";
995 default:
996 return "UNKNOWN";
997 }
998}
999
Harald Weltebf5e8df2009-02-03 12:59:45 +00001000/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001001static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001002{
Harald Welte8470bf22008-12-25 23:28:35 +00001003 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001004 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001005 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001006 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001007 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001008 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001009 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001010 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001011
Harald Welte8470bf22008-12-25 23:28:35 +00001012 lu = (struct gsm48_loc_upd_req *) gh->data;
1013
1014 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001015
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001016 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001017
Harald Weltea0368542009-06-27 02:58:43 +02001018 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001019 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001020
Holger Freythereaf04692009-06-06 13:54:44 +00001021 /*
1022 * Pseudo Spoof detection: Just drop a second/concurrent
1023 * location updating request.
1024 */
1025 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001026 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001027 lchan->loc_operation);
1028 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1029 return 0;
1030 }
1031
Holger Freyther73487a22008-12-31 18:53:57 +00001032 allocate_loc_updating_req(lchan);
1033
Harald Welte52b1f982008-12-23 20:25:15 +00001034 switch (mi_type) {
1035 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001036 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001037 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001038 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001039 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001040
Jan Luebbe370b41d2009-08-12 10:19:34 +02001041 /* look up subscriber based on IMSI, create if not found */
1042 subscr = subscr_get_by_imsi(bts->network, mi_string);
1043 if (!subscr) {
1044 subscr = db_create_subscriber(bts->network, mi_string);
1045 }
Harald Welte4b634542008-12-27 01:55:51 +00001046 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001047 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001048 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001049 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001050 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001051 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001052
Harald Welte52b1f982008-12-23 20:25:15 +00001053 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001054 subscr = subscr_get_by_tmsi(bts->network,
1055 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001056 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001057 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001058 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001059 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001060 }
1061 break;
1062 case GSM_MI_TYPE_IMEI:
1063 case GSM_MI_TYPE_IMEISV:
1064 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001065 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001066 break;
1067 default:
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001069 break;
1070 }
1071
Harald Welte24516ea2009-07-04 10:18:00 +02001072 /* schedule the reject timer */
1073 schedule_reject(lchan);
1074
Harald Welte4bfdfe72009-06-10 23:11:52 +08001075 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001076 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001077 /* FIXME: request id? close channel? */
1078 return -EINVAL;
1079 }
1080
Harald Welte255539c2008-12-28 02:26:27 +00001081 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001082 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001083
Harald Welte24516ea2009-07-04 10:18:00 +02001084 /* check if we can let the subscriber into our network immediately
1085 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001086 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001087}
1088
Harald Welte13cac662009-07-29 12:10:35 +02001089/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Harald Welte7584aea2009-02-11 11:44:12 +00001090int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1091{
1092 struct msgb *msg = gsm48_msgb_alloc();
1093 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1094 struct gsm48_chan_mode_modify *cmm =
1095 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001096 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001097
Harald Welte4a543e82009-02-28 13:17:55 +00001098 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001099
Harald Welte45b407a2009-05-23 15:51:12 +00001100 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001101 msg->lchan = lchan;
1102 gh->proto_discr = GSM48_PDISC_RR;
1103 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1104
1105 /* fill the channel information element, this code
1106 * should probably be shared with rsl_rx_chan_rqd() */
1107 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001108 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001109 cmm->chan_desc.h0.h = 0;
1110 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1111 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1112 cmm->mode = mode;
1113
Harald Welte39e2ead2009-07-23 21:13:03 +02001114 return gsm48_sendmsg(msg, NULL);
Harald Welte7584aea2009-02-11 11:44:12 +00001115}
1116
Harald Welte4bfdfe72009-06-10 23:11:52 +08001117#if 0
1118static u_int8_t to_bcd8(u_int8_t val)
1119{
1120 return ((val / 10) << 4) | (val % 10);
1121}
1122#endif
1123
Harald Weltedb253af2008-12-30 17:56:55 +00001124/* Section 9.2.15a */
1125int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1126{
1127 struct msgb *msg = gsm48_msgb_alloc();
1128 struct gsm48_hdr *gh;
1129 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001130 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001131 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001132#if 0
1133 time_t cur_t;
1134 struct tm* cur_time;
1135 int tz15min;
1136#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001137
1138 msg->lchan = lchan;
1139
1140 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1141 gh->proto_discr = GSM48_PDISC_MM;
1142 gh->msg_type = GSM48_MT_MM_INFO;
1143
1144 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001145#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001146 name_len = strlen(net->name_long);
1147 /* 10.5.3.5a */
1148 ptr8 = msgb_put(msg, 3);
1149 ptr8[0] = GSM48_IE_NAME_LONG;
1150 ptr8[1] = name_len*2 +1;
1151 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1152
1153 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1154 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001155 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001156
1157 /* FIXME: Use Cell Broadcast, not UCS-2, since
1158 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001159#endif
1160 name_len = (strlen(net->name_long)*7)/8;
1161 name_pad = (8 - strlen(net->name_long)*7)%8;
1162 if (name_pad > 0)
1163 name_len++;
1164 /* 10.5.3.5a */
1165 ptr8 = msgb_put(msg, 3);
1166 ptr8[0] = GSM48_IE_NAME_LONG;
1167 ptr8[1] = name_len +1;
1168 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1169
1170 ptr8 = msgb_put(msg, name_len);
1171 gsm_7bit_encode(ptr8, net->name_long);
1172
Harald Weltedb253af2008-12-30 17:56:55 +00001173 }
1174
1175 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001176#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001177 name_len = strlen(net->name_short);
1178 /* 10.5.3.5a */
1179 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001180 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001181 ptr8[1] = name_len*2 + 1;
1182 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1183
Harald Weltee872cb12009-01-01 00:33:37 +00001184 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001185 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001186 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001187#endif
1188 name_len = (strlen(net->name_short)*7)/8;
1189 name_pad = (8 - strlen(net->name_short)*7)%8;
1190 if (name_pad > 0)
1191 name_len++;
1192 /* 10.5.3.5a */
1193 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1194 ptr8[0] = GSM48_IE_NAME_SHORT;
1195 ptr8[1] = name_len +1;
1196 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1197
1198 ptr8 = msgb_put(msg, name_len);
1199 gsm_7bit_encode(ptr8, net->name_short);
1200
Harald Weltedb253af2008-12-30 17:56:55 +00001201 }
1202
1203#if 0
1204 /* Section 10.5.3.9 */
1205 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001206 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001207 ptr8 = msgb_put(msg, 8);
1208 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1209 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1210 ptr8[2] = to_bcd8(cur_time->tm_mon);
1211 ptr8[3] = to_bcd8(cur_time->tm_mday);
1212 ptr8[4] = to_bcd8(cur_time->tm_hour);
1213 ptr8[5] = to_bcd8(cur_time->tm_min);
1214 ptr8[6] = to_bcd8(cur_time->tm_sec);
1215 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1216 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001217 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001218 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001219 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001220#endif
1221
Daniel Willmanneea93372009-08-13 03:42:07 +02001222 DEBUGP(DMM, "-> MM INFO\n");
1223
Harald Welte39e2ead2009-07-23 21:13:03 +02001224 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001225}
1226
Harald Welte7984d5c2009-08-12 22:56:50 +02001227/* Section 9.2.2 */
1228int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1229{
1230 struct msgb *msg = gsm48_msgb_alloc();
1231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001232 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001233
1234 DEBUGP(DMM, "-> AUTH REQ\n");
1235
1236 msg->lchan = lchan;
1237 gh->proto_discr = GSM48_PDISC_MM;
1238 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1239
Sylvain Munaut849f5542009-09-27 11:10:17 +02001240 /* Key Sequence: FIXME fixed to 0 */
1241 ar->key_seq = 0;
1242
Harald Welte7984d5c2009-08-12 22:56:50 +02001243 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001244 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001245 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001246
1247 return gsm48_sendmsg(msg, NULL);
1248}
1249
1250/* Section 9.2.1 */
1251int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1252{
1253 DEBUGP(DMM, "-> AUTH REJECT\n");
1254 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1255}
1256
Harald Welte4b634542008-12-27 01:55:51 +00001257static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1258{
Harald Welte4b634542008-12-27 01:55:51 +00001259 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001260 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001261}
Harald Welteba4cf162009-01-10 01:49:35 +00001262
1263/* 9.2.6 CM service reject */
1264static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1265 enum gsm48_reject_value value)
1266{
1267 struct msgb *msg = gsm48_msgb_alloc();
1268 struct gsm48_hdr *gh;
1269
1270 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1271
1272 msg->lchan = lchan;
1273 use_lchan(lchan);
1274
1275 gh->proto_discr = GSM48_PDISC_MM;
1276 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1277 gh->data[0] = value;
1278 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1279
Harald Welte39e2ead2009-07-23 21:13:03 +02001280 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001281}
1282
Harald Welte4ed0e922009-01-10 03:17:30 +00001283/*
1284 * Handle CM Service Requests
1285 * a) Verify that the packet is long enough to contain the information
1286 * we require otherwsie reject with INCORRECT_MESSAGE
1287 * b) Try to parse the TMSI. If we do not have one reject
1288 * c) Check that we know the subscriber with the TMSI otherwise reject
1289 * with a HLR cause
1290 * d) Set the subscriber on the gsm_lchan and accept
1291 */
Harald Welte4b634542008-12-27 01:55:51 +00001292static int gsm48_rx_mm_serv_req(struct msgb *msg)
1293{
Harald Welteba4cf162009-01-10 01:49:35 +00001294 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001295 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001296
Harald Welte9176bd42009-07-23 18:46:00 +02001297 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001298 struct gsm_subscriber *subscr;
1299 struct gsm48_hdr *gh = msgb_l3(msg);
1300 struct gsm48_service_request *req =
1301 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001302 /* unfortunately in Phase1 the classmar2 length is variable */
1303 u_int8_t classmark2_len = gh->data[1];
1304 u_int8_t *classmark2 = gh->data+2;
1305 u_int8_t mi_len = *(classmark2 + classmark2_len);
1306 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001307
Harald Weltec9e02182009-05-01 19:07:53 +00001308 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001309 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001310 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001311 return gsm48_tx_mm_serv_rej(msg->lchan,
1312 GSM48_REJECT_INCORRECT_MESSAGE);
1313 }
1314
1315 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001316 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001317 return gsm48_tx_mm_serv_rej(msg->lchan,
1318 GSM48_REJECT_INCORRECT_MESSAGE);
1319 }
1320
Harald Weltec9e02182009-05-01 19:07:53 +00001321 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001322 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001323 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001324 return gsm48_tx_mm_serv_rej(msg->lchan,
1325 GSM48_REJECT_INCORRECT_MESSAGE);
1326 }
1327
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001328 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001329 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001330 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001331
Harald Welte3ac7f102009-08-10 10:12:45 +02001332 if (is_siemens_bts(bts))
1333 send_siemens_mrpci(msg->lchan, classmark2-1);
1334
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001335 subscr = subscr_get_by_tmsi(bts->network,
1336 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001337
Harald Welte2a139372009-02-22 21:14:55 +00001338 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001339 if (!subscr)
1340 return gsm48_tx_mm_serv_rej(msg->lchan,
1341 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1342
1343 if (!msg->lchan->subscr)
1344 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001345 else if (msg->lchan->subscr != subscr) {
1346 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1347 subscr_put(subscr);
1348 }
1349
Harald Weltec2e302d2009-07-05 14:08:13 +02001350 subscr->equipment.classmark2_len = classmark2_len;
1351 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1352 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001353
Harald Welte4b634542008-12-27 01:55:51 +00001354 return gsm48_tx_mm_serv_ack(msg->lchan);
1355}
1356
Harald Welte2a139372009-02-22 21:14:55 +00001357static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1358{
Harald Welte9176bd42009-07-23 18:46:00 +02001359 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001360 struct gsm48_hdr *gh = msgb_l3(msg);
1361 struct gsm48_imsi_detach_ind *idi =
1362 (struct gsm48_imsi_detach_ind *) gh->data;
1363 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001364 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001366
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001367 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001368 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1369 mi_type, mi_string);
1370
1371 switch (mi_type) {
1372 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001373 subscr = subscr_get_by_tmsi(bts->network,
1374 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001375 break;
1376 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001377 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001378 break;
1379 case GSM_MI_TYPE_IMEI:
1380 case GSM_MI_TYPE_IMEISV:
1381 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001382 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001383 break;
1384 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001385 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001386 break;
1387 }
1388
Holger Freyther4a49e772009-04-12 05:37:29 +00001389 if (subscr) {
1390 subscr_update(subscr, msg->trx->bts,
1391 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001392 DEBUGP(DMM, "Subscriber: %s\n",
1393 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001394
1395 subscr->equipment.classmark1 = idi->classmark1;
1396 db_sync_equipment(&subscr->equipment);
1397
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001398 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001399 } else
Harald Welte2a139372009-02-22 21:14:55 +00001400 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1401
Harald Welte2a139372009-02-22 21:14:55 +00001402 return 0;
1403}
1404
Harald Welted2a7f5a2009-06-05 20:08:20 +00001405static int gsm48_rx_mm_status(struct msgb *msg)
1406{
1407 struct gsm48_hdr *gh = msgb_l3(msg);
1408
1409 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1410
1411 return 0;
1412}
1413
Harald Weltebf5e8df2009-02-03 12:59:45 +00001414/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001415static int gsm0408_rcv_mm(struct msgb *msg)
1416{
1417 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001419
1420 switch (gh->msg_type & 0xbf) {
1421 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001422 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001423 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001424 break;
1425 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001426 rc = mm_rx_id_resp(msg);
1427 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001428 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001429 rc = gsm48_rx_mm_serv_req(msg);
1430 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001431 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001432 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001433 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001434 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001435 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1436 msg->lchan->subscr ?
1437 msg->lchan->subscr->imsi :
1438 "unknown subscriber");
1439 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001440 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001441 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1442 break;
1443 case GSM48_MT_MM_CM_REEST_REQ:
1444 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1445 break;
1446 case GSM48_MT_MM_AUTH_RESP:
1447 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001448 break;
1449 default:
1450 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1451 gh->msg_type);
1452 break;
1453 }
1454
1455 return rc;
1456}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001457
Harald Welte2d35ae62009-02-06 12:02:13 +00001458/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001459static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001460{
Harald Welte9176bd42009-07-23 18:46:00 +02001461 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001462 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001463 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001464 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001465 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001466 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001467 int rc = 0;
1468
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001469 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001470 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1471 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001472
Harald Weltefe18d8f2009-02-22 21:14:24 +00001473 switch (mi_type) {
1474 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001475 subscr = subscr_get_by_tmsi(bts->network,
1476 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001477 break;
1478 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001479 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001480 break;
1481 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001482
1483 if (!subscr) {
1484 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001485 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001486 return -EINVAL;
1487 }
1488 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001489 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001490
Harald Weltec2e302d2009-07-05 14:08:13 +02001491 subscr->equipment.classmark2_len = *classmark2_lv;
1492 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1493 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001494
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001495 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001496 return rc;
1497}
1498
Harald Weltef7c43522009-06-09 20:24:21 +00001499static int gsm48_rx_rr_classmark(struct msgb *msg)
1500{
1501 struct gsm48_hdr *gh = msgb_l3(msg);
1502 struct gsm_subscriber *subscr = msg->lchan->subscr;
1503 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1504 u_int8_t cm2_len, cm3_len = 0;
1505 u_int8_t *cm2, *cm3 = NULL;
1506
1507 DEBUGP(DRR, "CLASSMARK CHANGE ");
1508
1509 /* classmark 2 */
1510 cm2_len = gh->data[0];
1511 cm2 = &gh->data[1];
1512 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1513
1514 if (payload_len > cm2_len + 1) {
1515 /* we must have a classmark3 */
1516 if (gh->data[cm2_len+1] != 0x20) {
1517 DEBUGPC(DRR, "ERR CM3 TAG\n");
1518 return -EINVAL;
1519 }
1520 if (cm2_len > 3) {
1521 DEBUGPC(DRR, "CM2 too long!\n");
1522 return -EINVAL;
1523 }
1524
1525 cm3_len = gh->data[cm2_len+2];
1526 cm3 = &gh->data[cm2_len+3];
1527 if (cm3_len > 14) {
1528 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1529 return -EINVAL;
1530 }
1531 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1532 }
1533 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001534 subscr->equipment.classmark2_len = cm2_len;
1535 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001536 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001537 subscr->equipment.classmark3_len = cm3_len;
1538 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001539 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001540 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001541 }
1542
Harald Weltef7c43522009-06-09 20:24:21 +00001543 return 0;
1544}
1545
Harald Weltecf5b3592009-05-01 18:28:42 +00001546static int gsm48_rx_rr_status(struct msgb *msg)
1547{
1548 struct gsm48_hdr *gh = msgb_l3(msg);
1549
1550 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1551 rr_cause_name(gh->data[0]));
1552
1553 return 0;
1554}
1555
Harald Weltef7c43522009-06-09 20:24:21 +00001556static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1557{
1558 struct gsm48_hdr *gh = msgb_l3(msg);
1559 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1560 static struct gsm_meas_rep meas_rep;
1561
Harald Welte10d0e672009-06-27 02:53:10 +02001562 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001563 parse_meas_rep(&meas_rep, gh->data, payload_len);
1564 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001565 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001566 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001567 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001568 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001569 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001570 else
Harald Welte10d0e672009-06-27 02:53:10 +02001571 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001572 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1573 meas_rep.rxqual_sub);
1574
Harald Welte10d0e672009-06-27 02:53:10 +02001575 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001576
1577 /* FIXME: put the results somwhere */
1578
1579 return 0;
1580}
1581
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001582static int gsm48_rx_rr_app_info(struct msgb *msg)
1583{
1584 struct gsm48_hdr *gh = msgb_l3(msg);
1585 u_int8_t apdu_id_flags;
1586 u_int8_t apdu_len;
1587 u_int8_t *apdu_data;
1588
1589 apdu_id_flags = gh->data[0];
1590 apdu_len = gh->data[1];
1591 apdu_data = gh->data+2;
1592
1593 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1594 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1595
Harald Welte (local)026531e2009-08-16 10:40:10 +02001596 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001597}
1598
1599
Harald Weltebf5e8df2009-02-03 12:59:45 +00001600/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001601static int gsm0408_rcv_rr(struct msgb *msg)
1602{
1603 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001604 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001605
1606 switch (gh->msg_type) {
1607 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001608 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001609 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001610 case GSM48_MT_RR_GPRS_SUSP_REQ:
1611 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1612 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001613 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001614 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001615 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001616 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1617 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte13cac662009-07-29 12:10:35 +02001618 /* We've successfully modified the MS side of the channel,
1619 * now go on to modify the BTS side of the channel */
Harald Welte9943c5b2009-07-29 15:41:29 +02001620 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
Harald Welte2c38aa82009-02-18 03:44:24 +00001621 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001622 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001623 case GSM48_MT_RR_STATUS:
1624 rc = gsm48_rx_rr_status(msg);
1625 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001626 case GSM48_MT_RR_MEAS_REP:
1627 rc = gsm48_rx_rr_meas_rep(msg);
1628 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001629 case GSM48_MT_RR_APP_INFO:
1630 rc = gsm48_rx_rr_app_info(msg);
1631 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001632 case GSM48_MT_RR_CIPH_M_COMPL:
1633 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1634 /* FIXME: check for MI (if any) */
1635 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001636 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001637 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001638 gh->msg_type);
1639 break;
1640 }
1641
Harald Welte2d35ae62009-02-06 12:02:13 +00001642 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001643}
1644
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001645int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001646 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001647{
1648 struct msgb *msg = gsm48_msgb_alloc();
1649 struct gsm48_hdr *gh;
1650
1651 msg->lchan = lchan;
1652
1653 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1654 apdu_id, apdu_len);
1655
1656 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1657 gh->proto_discr = GSM48_PDISC_RR;
1658 gh->msg_type = GSM48_MT_RR_APP_INFO;
1659 gh->data[0] = apdu_id;
1660 gh->data[1] = apdu_len;
1661 memcpy(gh->data+2, apdu, apdu_len);
1662
1663 return gsm48_sendmsg(msg, NULL);
1664}
1665
Harald Welte4bc90a12008-12-27 16:32:52 +00001666/* Call Control */
1667
Harald Welte7584aea2009-02-11 11:44:12 +00001668/* The entire call control code is written in accordance with Figure 7.10c
1669 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1670 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1671 * it for voice */
1672
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673static void new_cc_state(struct gsm_trans *trans, int state)
1674{
1675 if (state > 31 || state < 0)
1676 return;
1677
1678 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001679 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680
Harald Weltedcaf5652009-07-23 18:56:43 +02001681 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682}
1683
1684static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001685{
1686 struct msgb *msg = gsm48_msgb_alloc();
1687 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1688 u_int8_t *cause, *call_state;
1689
Harald Welte4bc90a12008-12-27 16:32:52 +00001690 gh->msg_type = GSM48_MT_CC_STATUS;
1691
1692 cause = msgb_put(msg, 3);
1693 cause[0] = 2;
1694 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1695 cause[2] = 0x80 | 30; /* response to status inquiry */
1696
1697 call_state = msgb_put(msg, 1);
1698 call_state[0] = 0xc0 | 0x00;
1699
Harald Welte39e2ead2009-07-23 21:13:03 +02001700 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001701}
1702
Harald Welte6f4b7532008-12-29 00:39:37 +00001703static int gsm48_tx_simple(struct gsm_lchan *lchan,
1704 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001705{
1706 struct msgb *msg = gsm48_msgb_alloc();
1707 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1708
1709 msg->lchan = lchan;
1710
Harald Welte6f4b7532008-12-29 00:39:37 +00001711 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001712 gh->msg_type = msg_type;
1713
Harald Welte39e2ead2009-07-23 21:13:03 +02001714 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001715}
1716
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1718{
Harald Weltedcaf5652009-07-23 18:56:43 +02001719 if (bsc_timer_pending(&trans->cc.timer)) {
1720 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1721 bsc_del_timer(&trans->cc.timer);
1722 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 }
1724}
1725
1726static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1727 int msg_type, struct gsm_mncc *mncc)
1728{
1729 struct msgb *msg;
1730
1731 if (trans)
1732 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001733 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 "Sending '%s' to MNCC.\n",
1735 trans->lchan->ts->trx->bts->nr,
1736 trans->lchan->ts->trx->nr,
1737 trans->lchan->ts->nr, trans->transaction_id,
1738 (trans->subscr)?(trans->subscr->extension):"-",
1739 get_mncc_name(msg_type));
1740 else
1741 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1742 "Sending '%s' to MNCC.\n",
1743 (trans->subscr)?(trans->subscr->extension):"-",
1744 get_mncc_name(msg_type));
1745 else
1746 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1747 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1748
1749 mncc->msg_type = msg_type;
1750
Harald Welte966636f2009-06-26 19:39:35 +02001751 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 if (!msg)
1753 return -ENOMEM;
1754 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1755 msgb_enqueue(&net->upqueue, msg);
1756
1757 return 0;
1758}
1759
1760int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1761 u_int32_t callref, int location, int value)
1762{
1763 struct gsm_mncc rel;
1764
Harald Welte92f70c52009-06-12 01:54:08 +08001765 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001767 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1769}
1770
Harald Weltedcaf5652009-07-23 18:56:43 +02001771/* Call Control Specific transaction release.
1772 * gets called by trans_free, DO NOT CALL YOURSELF! */
1773void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 gsm48_stop_cc_timer(trans);
1776
1777 /* send release to L4, if callref still exists */
1778 if (trans->callref) {
1779 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001780 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001781 GSM48_CAUSE_LOC_PRN_S_LU,
1782 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001786 if (trans->lchan)
1787 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788}
1789
1790static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1791
Harald Welte09e38af2009-02-16 22:52:23 +00001792/* call-back from paging the B-end of the connection */
1793static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001794 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001795{
Harald Welte7ccf7782009-02-17 01:43:01 +00001796 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 struct gsm_subscriber *subscr = param;
1798 struct gsm_trans *transt, *tmp;
1799 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001800
Harald Welte09e38af2009-02-16 22:52:23 +00001801 if (hooknum != GSM_HOOK_RR_PAGING)
1802 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803
1804 if (!subscr)
1805 return -EINVAL;
1806 net = subscr->net;
1807 if (!net) {
1808 DEBUGP(DCC, "Error Network not set!\n");
1809 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001810 }
Harald Welte7584aea2009-02-11 11:44:12 +00001811
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 /* check all tranactions (without lchan) for subscriber */
1813 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1814 if (transt->subscr != subscr || transt->lchan)
1815 continue;
1816 switch (event) {
1817 case GSM_PAGING_SUCCEEDED:
1818 if (!lchan) // paranoid
1819 break;
1820 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1821 subscr->extension);
1822 /* Assign lchan */
1823 if (!transt->lchan) {
1824 transt->lchan = lchan;
1825 use_lchan(lchan);
1826 }
1827 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001828 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 break;
1830 case GSM_PAGING_EXPIRED:
1831 DEBUGP(DCC, "Paging subscr %s expired!\n",
1832 subscr->extension);
1833 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001834 mncc_release_ind(transt->subscr->net, transt,
1835 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001836 GSM48_CAUSE_LOC_PRN_S_LU,
1837 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001839 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001840 break;
1841 }
1842 }
Harald Welte09e38af2009-02-16 22:52:23 +00001843 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001844}
Harald Welte7584aea2009-02-11 11:44:12 +00001845
Harald Welte805f6442009-07-28 18:25:29 +02001846/* some other part of the code sends us a signal */
1847static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1848 void *handler_data, void *signal_data)
1849{
1850 struct gsm_lchan *lchan = signal_data;
1851 struct gsm_bts_trx_ts *ts;
1852 int rc;
1853
1854 if (subsys != SS_ABISIP)
1855 return 0;
1856
1857 /* in case we use direct BTS-to-BTS RTP */
1858 if (ipacc_rtp_direct)
1859 return 0;
1860
1861 ts = lchan->ts;
1862
1863 switch (signal) {
1864 case S_ABISIP_BIND_ACK:
1865 /* the BTS has successfully bound a TCH to a local ip/port,
1866 * which means we can connect our UDP socket to it */
1867 if (ts->abis_ip.rtp_socket) {
1868 rtp_socket_free(ts->abis_ip.rtp_socket);
1869 ts->abis_ip.rtp_socket = NULL;
1870 }
1871
1872 ts->abis_ip.rtp_socket = rtp_socket_create();
1873 if (!ts->abis_ip.rtp_socket)
1874 goto out_err;
1875
1876 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1877 ts->abis_ip.bound_ip,
1878 ts->abis_ip.bound_port);
1879 if (rc < 0)
1880 goto out_err;
1881 break;
1882 case S_ABISIP_DISC_IND:
1883 /* the BTS tells us a RTP stream has been disconnected */
1884 if (ts->abis_ip.rtp_socket) {
1885 rtp_socket_free(ts->abis_ip.rtp_socket);
1886 ts->abis_ip.rtp_socket = NULL;
1887 }
1888 break;
1889 }
1890
1891 return 0;
1892out_err:
1893 /* FIXME: do something */
1894 return 0;
1895}
1896
1897/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1898static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1899{
1900 struct gsm_bts_trx_ts *ts = lchan->ts;
1901 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1902 int rc;
1903
1904 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1905 ntohs(rs->rtp.sin_local.sin_port),
1906 ts->abis_ip.conn_id,
1907 /* FIXME: use RTP payload of bound socket, not BTS*/
1908 ts->abis_ip.rtp_payload2);
1909
1910 return rc;
1911}
1912
Harald Welte49f48b82009-02-17 15:29:33 +00001913/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001914static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001915{
Harald Welte11fa29c2009-02-19 17:24:39 +00001916 struct gsm_bts *bts = lchan->ts->trx->bts;
1917 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001918 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02001919 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001920
Harald Welte11fa29c2009-02-19 17:24:39 +00001921 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1922 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1923 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1924
1925 if (bts->type != remote_bts->type) {
1926 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1927 return -EINVAL;
1928 }
Harald Welte49f48b82009-02-17 15:29:33 +00001929
Harald Welte11fa29c2009-02-19 17:24:39 +00001930 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001931 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001932 if (!ipacc_rtp_direct) {
1933 /* connect the TCH's to our RTP proxy */
1934 rc = ipacc_connect_proxy_bind(lchan);
1935 if (rc < 0)
1936 return rc;
1937 rc = ipacc_connect_proxy_bind(remote_lchan);
1938
1939 /* connect them with each other */
1940 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
1941 remote_lchan->ts->abis_ip.rtp_socket);
1942 } else {
1943 /* directly connect TCH RTP streams to each other */
1944 ts = remote_lchan->ts;
1945 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
1946 ts->abis_ip.bound_port,
1947 lchan->ts->abis_ip.conn_id,
1948 ts->abis_ip.rtp_payload2);
1949 if (rc < 0)
1950 return rc;
1951 ts = lchan->ts;
1952 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
1953 ts->abis_ip.bound_port,
1954 remote_lchan->ts->abis_ip.conn_id,
1955 ts->abis_ip.rtp_payload2);
1956 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001957 break;
1958 case GSM_BTS_TYPE_BS11:
1959 trau_mux_map_lchan(lchan, remote_lchan);
1960 break;
1961 default:
1962 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001963 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001964 break;
1965 }
Harald Welte49f48b82009-02-17 15:29:33 +00001966
1967 return 0;
1968}
1969
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970/* bridge channels of two transactions */
1971static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001972{
Harald Weltedcaf5652009-07-23 18:56:43 +02001973 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1974 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001975
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001977 return -EIO;
1978
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 if (!trans1->lchan || !trans2->lchan)
1980 return -EIO;
1981
1982 /* through-connect channel */
1983 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001984}
1985
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986/* enable receive of channels to upqueue */
1987static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1988{
1989 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00001990
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02001992 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 if (!trans)
1994 return -EIO;
1995 if (!trans->lchan)
1996 return 0;
1997
1998 // todo IPACCESS
1999 if (enable)
2000 return trau_recv_lchan(trans->lchan, data->callref);
2001 return trau_mux_unmap(NULL, data->callref);
2002}
2003
2004/* send a frame to channel */
2005static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2006{
2007 struct gsm_trans *trans;
2008
2009 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002010 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 if (!trans)
2012 return -EIO;
2013 if (!trans->lchan)
2014 return 0;
2015 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2016 trans->lchan->type != GSM_LCHAN_TCH_H)
2017 return 0;
2018
2019 // todo IPACCESS
2020 return trau_send_lchan(trans->lchan,
2021 (struct decoded_trau_frame *)frame->data);
2022}
2023
2024
2025static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2026{
2027 DEBUGP(DCC, "-> STATUS ENQ\n");
2028 return gsm48_cc_tx_status(trans, msg);
2029}
2030
2031static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2032static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2033
2034static void gsm48_cc_timeout(void *arg)
2035{
2036 struct gsm_trans *trans = arg;
2037 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002038 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2039 int mo_location = GSM48_CAUSE_LOC_USER;
2040 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2041 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 struct gsm_mncc mo_rel, l4_rel;
2043
2044 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2045 mo_rel.callref = trans->callref;
2046 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2047 l4_rel.callref = trans->callref;
2048
Harald Weltedcaf5652009-07-23 18:56:43 +02002049 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 case 0x303:
2051 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002052 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 break;
2054 case 0x310:
2055 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002056 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 break;
2058 case 0x313:
2059 disconnect = 1;
2060 /* unknown, did not find it in the specs */
2061 break;
2062 case 0x301:
2063 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002064 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 break;
2066 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002067 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002069 gsm48_cc_tx_release(trans, &trans->cc.msg);
2070 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 break; /* stay in release state */
2072 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002073 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074 return;
2075// release = 1;
2076// l4_cause = 14;
2077// break;
2078 case 0x306:
2079 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002080 mo_cause = trans->cc.msg.cause.value;
2081 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082 break;
2083 case 0x323:
2084 disconnect = 1;
2085 break;
2086 default:
2087 release = 1;
2088 }
2089
2090 if (release && trans->callref) {
2091 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002092 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 l4_location, l4_cause);
2094 trans->callref = 0;
2095 }
2096
2097 if (disconnect && trans->callref) {
2098 /* process disconnect towards layer 4 */
2099 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002100 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 }
2102
2103 /* process disconnect towards mobile station */
2104 if (disconnect || release) {
2105 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002106 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2107 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2108 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 mo_rel.cause.diag_len = 3;
2110
2111 if (disconnect)
2112 gsm48_cc_tx_disconnect(trans, &mo_rel);
2113 if (release)
2114 gsm48_cc_tx_release(trans, &mo_rel);
2115 }
2116
2117}
2118
2119static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2120 int sec, int micro)
2121{
2122 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002123 trans->cc.timer.cb = gsm48_cc_timeout;
2124 trans->cc.timer.data = trans;
2125 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2126 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127}
2128
2129static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2130{
2131 struct gsm48_hdr *gh = msgb_l3(msg);
2132 u_int8_t msg_type = gh->msg_type & 0xbf;
2133 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2134 struct tlv_parsed tp;
2135 struct gsm_mncc setup;
2136
2137 memset(&setup, 0, sizeof(struct gsm_mncc));
2138 setup.callref = trans->callref;
2139 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2140 /* emergency setup is identified by msg_type */
2141 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2142 setup.emergency = 1;
2143
2144 /* use subscriber as calling party number */
2145 if (trans->subscr) {
2146 setup.fields |= MNCC_F_CALLING;
2147 strncpy(setup.calling.number, trans->subscr->extension,
2148 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002149 strncpy(setup.imsi, trans->subscr->imsi,
2150 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 }
2152 /* bearer capability */
2153 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2154 setup.fields |= MNCC_F_BEARER_CAP;
2155 decode_bearer_cap(&setup.bearer_cap,
2156 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2157 }
2158 /* facility */
2159 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2160 setup.fields |= MNCC_F_FACILITY;
2161 decode_facility(&setup.facility,
2162 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2163 }
2164 /* called party bcd number */
2165 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2166 setup.fields |= MNCC_F_CALLED;
2167 decode_called(&setup.called,
2168 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2169 }
2170 /* user-user */
2171 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2172 setup.fields |= MNCC_F_USERUSER;
2173 decode_useruser(&setup.useruser,
2174 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2175 }
2176 /* ss-version */
2177 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2178 setup.fields |= MNCC_F_SSVERSION;
2179 decode_ssversion(&setup.ssversion,
2180 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2181 }
2182 /* CLIR suppression */
2183 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2184 setup.clir.sup = 1;
2185 /* CLIR invocation */
2186 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2187 setup.clir.inv = 1;
2188 /* cc cap */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2190 setup.fields |= MNCC_F_CCCAP;
2191 decode_cccap(&setup.cccap,
2192 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2193 }
2194
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 new_cc_state(trans, GSM_CSTATE_INITIATED);
2196
2197 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002198 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199
Harald Welte13cac662009-07-29 12:10:35 +02002200 /* MNCC code will modify the channel asynchronously, we should
2201 * ipaccess-bind only after the modification has been made to the
2202 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 return 0;
2204}
2205
2206static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002207{
2208 struct msgb *msg = gsm48_msgb_alloc();
2209 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002211 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002212
Harald Welte7ccf7782009-02-17 01:43:01 +00002213 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002214
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 /* transaction id must not be assigned */
2216 if (trans->transaction_id != 0xff) { /* unasssigned */
2217 DEBUGP(DCC, "TX Setup with assigned transaction. "
2218 "This is not allowed!\n");
2219 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002220 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002221 GSM48_CAUSE_LOC_PRN_S_LU,
2222 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002224 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 return rc;
2226 }
2227
2228 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002229 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2230 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002232 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002233 GSM48_CAUSE_LOC_PRN_S_LU,
2234 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002236 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 return rc;
2238 }
Harald Welte78283ef2009-07-23 21:36:44 +02002239 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002240
Harald Welte65e74cc2008-12-29 01:55:35 +00002241 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002242
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002244
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 /* bearer capability */
2246 if (setup->fields & MNCC_F_BEARER_CAP)
2247 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2248 /* facility */
2249 if (setup->fields & MNCC_F_FACILITY)
2250 encode_facility(msg, 0, &setup->facility);
2251 /* progress */
2252 if (setup->fields & MNCC_F_PROGRESS)
2253 encode_progress(msg, 0, &setup->progress);
2254 /* calling party BCD number */
2255 if (setup->fields & MNCC_F_CALLING)
2256 encode_calling(msg, &setup->calling);
2257 /* called party BCD number */
2258 if (setup->fields & MNCC_F_CALLED)
2259 encode_called(msg, &setup->called);
2260 /* user-user */
2261 if (setup->fields & MNCC_F_USERUSER)
2262 encode_useruser(msg, 0, &setup->useruser);
2263 /* redirecting party BCD number */
2264 if (setup->fields & MNCC_F_REDIRECTING)
2265 encode_redirecting(msg, &setup->redirecting);
2266 /* signal */
2267 if (setup->fields & MNCC_F_SIGNAL)
2268 encode_signal(msg, setup->signal);
2269
2270 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002271
Harald Welte39e2ead2009-07-23 21:13:03 +02002272 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002273}
2274
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2276{
2277 struct gsm48_hdr *gh = msgb_l3(msg);
2278 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2279 struct tlv_parsed tp;
2280 struct gsm_mncc call_conf;
2281
2282 gsm48_stop_cc_timer(trans);
2283 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2284
2285 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2286 call_conf.callref = trans->callref;
2287 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2288#if 0
2289 /* repeat */
2290 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2291 call_conf.repeat = 1;
2292 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2293 call_conf.repeat = 2;
2294#endif
2295 /* bearer capability */
2296 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2297 call_conf.fields |= MNCC_F_BEARER_CAP;
2298 decode_bearer_cap(&call_conf.bearer_cap,
2299 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2300 }
2301 /* cause */
2302 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2303 call_conf.fields |= MNCC_F_CAUSE;
2304 decode_cause(&call_conf.cause,
2305 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2306 }
2307 /* cc cap */
2308 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2309 call_conf.fields |= MNCC_F_CCCAP;
2310 decode_cccap(&call_conf.cccap,
2311 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2312 }
2313
2314 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2315
Harald Welte596fed42009-07-23 19:06:52 +02002316 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2317 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318}
2319
2320static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2321{
2322 struct gsm_mncc *proceeding = arg;
2323 struct msgb *msg = gsm48_msgb_alloc();
2324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2325
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2327
2328 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2329
2330 /* bearer capability */
2331 if (proceeding->fields & MNCC_F_BEARER_CAP)
2332 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2333 /* facility */
2334 if (proceeding->fields & MNCC_F_FACILITY)
2335 encode_facility(msg, 0, &proceeding->facility);
2336 /* progress */
2337 if (proceeding->fields & MNCC_F_PROGRESS)
2338 encode_progress(msg, 0, &proceeding->progress);
2339
Harald Welte39e2ead2009-07-23 21:13:03 +02002340 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341}
2342
2343static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2344{
2345 struct gsm48_hdr *gh = msgb_l3(msg);
2346 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2347 struct tlv_parsed tp;
2348 struct gsm_mncc alerting;
2349
2350 gsm48_stop_cc_timer(trans);
2351 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2352
2353 memset(&alerting, 0, sizeof(struct gsm_mncc));
2354 alerting.callref = trans->callref;
2355 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2356 /* facility */
2357 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2358 alerting.fields |= MNCC_F_FACILITY;
2359 decode_facility(&alerting.facility,
2360 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2361 }
2362
2363 /* progress */
2364 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2365 alerting.fields |= MNCC_F_PROGRESS;
2366 decode_progress(&alerting.progress,
2367 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2368 }
2369 /* ss-version */
2370 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2371 alerting.fields |= MNCC_F_SSVERSION;
2372 decode_ssversion(&alerting.ssversion,
2373 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2374 }
2375
2376 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2377
Harald Welte596fed42009-07-23 19:06:52 +02002378 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2379 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380}
2381
2382static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2383{
2384 struct gsm_mncc *alerting = arg;
2385 struct msgb *msg = gsm48_msgb_alloc();
2386 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2387
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 gh->msg_type = GSM48_MT_CC_ALERTING;
2389
2390 /* facility */
2391 if (alerting->fields & MNCC_F_FACILITY)
2392 encode_facility(msg, 0, &alerting->facility);
2393 /* progress */
2394 if (alerting->fields & MNCC_F_PROGRESS)
2395 encode_progress(msg, 0, &alerting->progress);
2396 /* user-user */
2397 if (alerting->fields & MNCC_F_USERUSER)
2398 encode_useruser(msg, 0, &alerting->useruser);
2399
2400 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2401
Harald Welte39e2ead2009-07-23 21:13:03 +02002402 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403}
2404
2405static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2406{
2407 struct gsm_mncc *progress = arg;
2408 struct msgb *msg = gsm48_msgb_alloc();
2409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2410
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 gh->msg_type = GSM48_MT_CC_PROGRESS;
2412
2413 /* progress */
2414 encode_progress(msg, 1, &progress->progress);
2415 /* user-user */
2416 if (progress->fields & MNCC_F_USERUSER)
2417 encode_useruser(msg, 0, &progress->useruser);
2418
Harald Welte39e2ead2009-07-23 21:13:03 +02002419 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420}
2421
2422static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2423{
2424 struct gsm_mncc *connect = arg;
2425 struct msgb *msg = gsm48_msgb_alloc();
2426 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2427
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428 gh->msg_type = GSM48_MT_CC_CONNECT;
2429
2430 gsm48_stop_cc_timer(trans);
2431 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2432
2433 /* facility */
2434 if (connect->fields & MNCC_F_FACILITY)
2435 encode_facility(msg, 0, &connect->facility);
2436 /* progress */
2437 if (connect->fields & MNCC_F_PROGRESS)
2438 encode_progress(msg, 0, &connect->progress);
2439 /* connected number */
2440 if (connect->fields & MNCC_F_CONNECTED)
2441 encode_connected(msg, &connect->connected);
2442 /* user-user */
2443 if (connect->fields & MNCC_F_USERUSER)
2444 encode_useruser(msg, 0, &connect->useruser);
2445
2446 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2447
Harald Welte39e2ead2009-07-23 21:13:03 +02002448 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449}
2450
2451static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2452{
2453 struct gsm48_hdr *gh = msgb_l3(msg);
2454 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2455 struct tlv_parsed tp;
2456 struct gsm_mncc connect;
2457
2458 gsm48_stop_cc_timer(trans);
2459
2460 memset(&connect, 0, sizeof(struct gsm_mncc));
2461 connect.callref = trans->callref;
2462 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2463 /* use subscriber as connected party number */
2464 if (trans->subscr) {
2465 connect.fields |= MNCC_F_CONNECTED;
2466 strncpy(connect.connected.number, trans->subscr->extension,
2467 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002468 strncpy(connect.imsi, trans->subscr->imsi,
2469 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 }
2471 /* facility */
2472 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2473 connect.fields |= MNCC_F_FACILITY;
2474 decode_facility(&connect.facility,
2475 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2476 }
2477 /* user-user */
2478 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2479 connect.fields |= MNCC_F_USERUSER;
2480 decode_useruser(&connect.useruser,
2481 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2482 }
2483 /* ss-version */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2485 connect.fields |= MNCC_F_SSVERSION;
2486 decode_ssversion(&connect.ssversion,
2487 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2488 }
2489
2490 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2491
Harald Welte596fed42009-07-23 19:06:52 +02002492 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495
2496static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2497{
2498 struct gsm_mncc connect_ack;
2499
2500 gsm48_stop_cc_timer(trans);
2501
2502 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2503
2504 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2505 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002506 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 &connect_ack);
2508}
2509
2510static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2511{
2512 struct msgb *msg = gsm48_msgb_alloc();
2513 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2514
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2516
2517 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2518
Harald Welte39e2ead2009-07-23 21:13:03 +02002519 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520}
2521
2522static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2523{
2524 struct gsm48_hdr *gh = msgb_l3(msg);
2525 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2526 struct tlv_parsed tp;
2527 struct gsm_mncc disc;
2528
2529 gsm48_stop_cc_timer(trans);
2530
2531 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2532
2533 memset(&disc, 0, sizeof(struct gsm_mncc));
2534 disc.callref = trans->callref;
2535 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2536 /* cause */
2537 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2538 disc.fields |= MNCC_F_CAUSE;
2539 decode_cause(&disc.cause,
2540 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2541 }
2542 /* facility */
2543 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2544 disc.fields |= MNCC_F_FACILITY;
2545 decode_facility(&disc.facility,
2546 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2547 }
2548 /* user-user */
2549 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2550 disc.fields |= MNCC_F_USERUSER;
2551 decode_useruser(&disc.useruser,
2552 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2553 }
2554 /* ss-version */
2555 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2556 disc.fields |= MNCC_F_SSVERSION;
2557 decode_ssversion(&disc.ssversion,
2558 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2559 }
2560
Harald Welte596fed42009-07-23 19:06:52 +02002561 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562
2563}
2564
Harald Weltec66b71c2009-06-11 14:23:20 +08002565static struct gsm_mncc_cause default_cause = {
2566 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2567 .coding = 0,
2568 .rec = 0,
2569 .rec_val = 0,
2570 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2571 .diag_len = 0,
2572 .diag = { 0 },
2573};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574
2575static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2576{
2577 struct gsm_mncc *disc = arg;
2578 struct msgb *msg = gsm48_msgb_alloc();
2579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2580
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2582
2583 gsm48_stop_cc_timer(trans);
2584 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2585
2586 /* cause */
2587 if (disc->fields & MNCC_F_CAUSE)
2588 encode_cause(msg, 1, &disc->cause);
2589 else
2590 encode_cause(msg, 1, &default_cause);
2591
2592 /* facility */
2593 if (disc->fields & MNCC_F_FACILITY)
2594 encode_facility(msg, 0, &disc->facility);
2595 /* progress */
2596 if (disc->fields & MNCC_F_PROGRESS)
2597 encode_progress(msg, 0, &disc->progress);
2598 /* user-user */
2599 if (disc->fields & MNCC_F_USERUSER)
2600 encode_useruser(msg, 0, &disc->useruser);
2601
2602 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002603 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604
2605 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2606
Harald Welte39e2ead2009-07-23 21:13:03 +02002607 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608}
2609
2610static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2611{
2612 struct gsm48_hdr *gh = msgb_l3(msg);
2613 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2614 struct tlv_parsed tp;
2615 struct gsm_mncc rel;
2616 int rc;
2617
2618 gsm48_stop_cc_timer(trans);
2619
2620 memset(&rel, 0, sizeof(struct gsm_mncc));
2621 rel.callref = trans->callref;
2622 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2623 /* cause */
2624 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2625 rel.fields |= MNCC_F_CAUSE;
2626 decode_cause(&rel.cause,
2627 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2628 }
2629 /* facility */
2630 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2631 rel.fields |= MNCC_F_FACILITY;
2632 decode_facility(&rel.facility,
2633 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2634 }
2635 /* user-user */
2636 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2637 rel.fields |= MNCC_F_USERUSER;
2638 decode_useruser(&rel.useruser,
2639 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2640 }
2641 /* ss-version */
2642 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2643 rel.fields |= MNCC_F_SSVERSION;
2644 decode_ssversion(&rel.ssversion,
2645 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2646 }
2647
Harald Weltedcaf5652009-07-23 18:56:43 +02002648 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002650 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002652 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002653 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002654 GSM48_MT_CC_RELEASE_COMPL);
2655 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 }
2657
2658 new_cc_state(trans, GSM_CSTATE_NULL);
2659
2660 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002661 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662
2663 return rc;
2664}
2665
2666static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2667{
2668 struct gsm_mncc *rel = arg;
2669 struct msgb *msg = gsm48_msgb_alloc();
2670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2671
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 gh->msg_type = GSM48_MT_CC_RELEASE;
2673
2674 trans->callref = 0;
2675
2676 gsm48_stop_cc_timer(trans);
2677 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2678
2679 /* cause */
2680 if (rel->fields & MNCC_F_CAUSE)
2681 encode_cause(msg, 0, &rel->cause);
2682 /* facility */
2683 if (rel->fields & MNCC_F_FACILITY)
2684 encode_facility(msg, 0, &rel->facility);
2685 /* user-user */
2686 if (rel->fields & MNCC_F_USERUSER)
2687 encode_useruser(msg, 0, &rel->useruser);
2688
Harald Weltedcaf5652009-07-23 18:56:43 +02002689 trans->cc.T308_second = 0;
2690 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691
Harald Weltedcaf5652009-07-23 18:56:43 +02002692 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2694
Harald Welte39e2ead2009-07-23 21:13:03 +02002695 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2699{
2700 struct gsm48_hdr *gh = msgb_l3(msg);
2701 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2702 struct tlv_parsed tp;
2703 struct gsm_mncc rel;
2704 int rc = 0;
2705
2706 gsm48_stop_cc_timer(trans);
2707
2708 memset(&rel, 0, sizeof(struct gsm_mncc));
2709 rel.callref = trans->callref;
2710 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2711 /* cause */
2712 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2713 rel.fields |= MNCC_F_CAUSE;
2714 decode_cause(&rel.cause,
2715 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2716 }
2717 /* facility */
2718 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2719 rel.fields |= MNCC_F_FACILITY;
2720 decode_facility(&rel.facility,
2721 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2722 }
2723 /* user-user */
2724 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2725 rel.fields |= MNCC_F_USERUSER;
2726 decode_useruser(&rel.useruser,
2727 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2728 }
2729 /* ss-version */
2730 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2731 rel.fields |= MNCC_F_SSVERSION;
2732 decode_ssversion(&rel.ssversion,
2733 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2734 }
2735
2736 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002737 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002739 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 MNCC_REJ_IND, &rel);
2741 break;
2742 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002743 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 MNCC_REL_CNF, &rel);
2745 break;
2746 default:
Harald Welte596fed42009-07-23 19:06:52 +02002747 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 MNCC_REL_IND, &rel);
2749 }
2750 }
2751
2752 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002753 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754
2755 return rc;
2756}
2757
2758static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2759{
2760 struct gsm_mncc *rel = arg;
2761 struct msgb *msg = gsm48_msgb_alloc();
2762 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2763
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2765
2766 trans->callref = 0;
2767
2768 gsm48_stop_cc_timer(trans);
2769
2770 /* cause */
2771 if (rel->fields & MNCC_F_CAUSE)
2772 encode_cause(msg, 0, &rel->cause);
2773 /* facility */
2774 if (rel->fields & MNCC_F_FACILITY)
2775 encode_facility(msg, 0, &rel->facility);
2776 /* user-user */
2777 if (rel->fields & MNCC_F_USERUSER)
2778 encode_useruser(msg, 0, &rel->useruser);
2779
Harald Weltedcaf5652009-07-23 18:56:43 +02002780 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002781
Harald Welte39e2ead2009-07-23 21:13:03 +02002782 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783}
2784
2785static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2786{
2787 struct gsm48_hdr *gh = msgb_l3(msg);
2788 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2789 struct tlv_parsed tp;
2790 struct gsm_mncc fac;
2791
2792 memset(&fac, 0, sizeof(struct gsm_mncc));
2793 fac.callref = trans->callref;
2794 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2795 /* facility */
2796 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2797 fac.fields |= MNCC_F_FACILITY;
2798 decode_facility(&fac.facility,
2799 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2800 }
2801 /* ss-version */
2802 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2803 fac.fields |= MNCC_F_SSVERSION;
2804 decode_ssversion(&fac.ssversion,
2805 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2806 }
2807
Harald Welte596fed42009-07-23 19:06:52 +02002808 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809}
2810
2811static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2812{
2813 struct gsm_mncc *fac = arg;
2814 struct msgb *msg = gsm48_msgb_alloc();
2815 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2816
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 gh->msg_type = GSM48_MT_CC_FACILITY;
2818
2819 /* facility */
2820 encode_facility(msg, 1, &fac->facility);
2821
Harald Welte39e2ead2009-07-23 21:13:03 +02002822 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823}
2824
2825static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2826{
2827 struct gsm_mncc hold;
2828
2829 memset(&hold, 0, sizeof(struct gsm_mncc));
2830 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002831 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2835{
2836 struct msgb *msg = gsm48_msgb_alloc();
2837 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2838
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2840
Harald Welte39e2ead2009-07-23 21:13:03 +02002841 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842}
2843
2844static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2845{
2846 struct gsm_mncc *hold_rej = arg;
2847 struct msgb *msg = gsm48_msgb_alloc();
2848 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2849
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2851
2852 /* cause */
2853 if (hold_rej->fields & MNCC_F_CAUSE)
2854 encode_cause(msg, 1, &hold_rej->cause);
2855 else
2856 encode_cause(msg, 1, &default_cause);
2857
Harald Welte39e2ead2009-07-23 21:13:03 +02002858 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859}
2860
2861static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2862{
2863 struct gsm_mncc retrieve;
2864
2865 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2866 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002867 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2868 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869}
2870
2871static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2872{
2873 struct msgb *msg = gsm48_msgb_alloc();
2874 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2875
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2877
Harald Welte39e2ead2009-07-23 21:13:03 +02002878 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2882{
2883 struct gsm_mncc *retrieve_rej = arg;
2884 struct msgb *msg = gsm48_msgb_alloc();
2885 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2886
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2888
2889 /* cause */
2890 if (retrieve_rej->fields & MNCC_F_CAUSE)
2891 encode_cause(msg, 1, &retrieve_rej->cause);
2892 else
2893 encode_cause(msg, 1, &default_cause);
2894
Harald Welte39e2ead2009-07-23 21:13:03 +02002895 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896}
2897
2898static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2899{
2900 struct gsm48_hdr *gh = msgb_l3(msg);
2901 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2902 struct tlv_parsed tp;
2903 struct gsm_mncc dtmf;
2904
2905 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2906 dtmf.callref = trans->callref;
2907 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2908 /* keypad facility */
2909 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2910 dtmf.fields |= MNCC_F_KEYPAD;
2911 decode_keypad(&dtmf.keypad,
2912 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2913 }
2914
Harald Welte596fed42009-07-23 19:06:52 +02002915 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916}
2917
2918static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2919{
2920 struct gsm_mncc *dtmf = arg;
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_START_DTMF_ACK;
2925
2926 /* keypad */
2927 if (dtmf->fields & MNCC_F_KEYPAD)
2928 encode_keypad(msg, dtmf->keypad);
2929
Harald Welte39e2ead2009-07-23 21:13:03 +02002930 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002931}
2932
2933static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2934{
2935 struct gsm_mncc *dtmf = arg;
2936 struct msgb *msg = gsm48_msgb_alloc();
2937 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2938
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2940
2941 /* cause */
2942 if (dtmf->fields & MNCC_F_CAUSE)
2943 encode_cause(msg, 1, &dtmf->cause);
2944 else
2945 encode_cause(msg, 1, &default_cause);
2946
Harald Welte39e2ead2009-07-23 21:13:03 +02002947 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948}
2949
2950static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2951{
2952 struct msgb *msg = gsm48_msgb_alloc();
2953 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2954
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2956
Harald Welte39e2ead2009-07-23 21:13:03 +02002957 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958}
2959
2960static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2961{
2962 struct gsm_mncc dtmf;
2963
2964 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2965 dtmf.callref = trans->callref;
2966
Harald Welte596fed42009-07-23 19:06:52 +02002967 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968}
2969
2970static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2971{
2972 struct gsm48_hdr *gh = msgb_l3(msg);
2973 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2974 struct tlv_parsed tp;
2975 struct gsm_mncc modify;
2976
2977 memset(&modify, 0, sizeof(struct gsm_mncc));
2978 modify.callref = trans->callref;
2979 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2980 /* bearer capability */
2981 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2982 modify.fields |= MNCC_F_BEARER_CAP;
2983 decode_bearer_cap(&modify.bearer_cap,
2984 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2985 }
2986
2987 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2988
Harald Welte596fed42009-07-23 19:06:52 +02002989 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990}
2991
2992static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2993{
2994 struct gsm_mncc *modify = arg;
2995 struct msgb *msg = gsm48_msgb_alloc();
2996 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2997
Harald Welte4bfdfe72009-06-10 23:11:52 +08002998 gh->msg_type = GSM48_MT_CC_MODIFY;
2999
3000 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3001
3002 /* bearer capability */
3003 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3004
3005 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3006
Harald Welte39e2ead2009-07-23 21:13:03 +02003007 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008}
3009
3010static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3011{
3012 struct gsm48_hdr *gh = msgb_l3(msg);
3013 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3014 struct tlv_parsed tp;
3015 struct gsm_mncc modify;
3016
3017 gsm48_stop_cc_timer(trans);
3018
3019 memset(&modify, 0, sizeof(struct gsm_mncc));
3020 modify.callref = trans->callref;
3021 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3022 /* bearer capability */
3023 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3024 modify.fields |= MNCC_F_BEARER_CAP;
3025 decode_bearer_cap(&modify.bearer_cap,
3026 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3027 }
3028
3029 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3030
Harald Welte596fed42009-07-23 19:06:52 +02003031 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003032}
3033
3034static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3035{
3036 struct gsm_mncc *modify = arg;
3037 struct msgb *msg = gsm48_msgb_alloc();
3038 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3039
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3041
3042 /* bearer capability */
3043 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3044
3045 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3046
Harald Welte39e2ead2009-07-23 21:13:03 +02003047 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048}
3049
3050static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3051{
3052 struct gsm48_hdr *gh = msgb_l3(msg);
3053 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3054 struct tlv_parsed tp;
3055 struct gsm_mncc modify;
3056
3057 gsm48_stop_cc_timer(trans);
3058
3059 memset(&modify, 0, sizeof(struct gsm_mncc));
3060 modify.callref = trans->callref;
3061 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3062 /* bearer capability */
3063 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3064 modify.fields |= GSM48_IE_BEARER_CAP;
3065 decode_bearer_cap(&modify.bearer_cap,
3066 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3067 }
3068 /* cause */
3069 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3070 modify.fields |= MNCC_F_CAUSE;
3071 decode_cause(&modify.cause,
3072 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3073 }
3074
3075 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3076
Harald Welte596fed42009-07-23 19:06:52 +02003077 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078}
3079
3080static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3081{
3082 struct gsm_mncc *modify = arg;
3083 struct msgb *msg = gsm48_msgb_alloc();
3084 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3085
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3087
3088 /* bearer capability */
3089 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3090 /* cause */
3091 encode_cause(msg, 1, &modify->cause);
3092
3093 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3094
Harald Welte39e2ead2009-07-23 21:13:03 +02003095 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096}
3097
3098static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3099{
3100 struct gsm_mncc *notify = arg;
3101 struct msgb *msg = gsm48_msgb_alloc();
3102 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3103
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104 gh->msg_type = GSM48_MT_CC_NOTIFY;
3105
3106 /* notify */
3107 encode_notify(msg, notify->notify);
3108
Harald Welte39e2ead2009-07-23 21:13:03 +02003109 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110}
3111
3112static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3113{
3114 struct gsm48_hdr *gh = msgb_l3(msg);
3115 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3116// struct tlv_parsed tp;
3117 struct gsm_mncc notify;
3118
3119 memset(&notify, 0, sizeof(struct gsm_mncc));
3120 notify.callref = trans->callref;
3121// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3122 if (payload_len >= 1)
3123 decode_notify(&notify.notify, gh->data);
3124
Harald Welte596fed42009-07-23 19:06:52 +02003125 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003126}
3127
3128static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3129{
3130 struct gsm_mncc *user = arg;
3131 struct msgb *msg = gsm48_msgb_alloc();
3132 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3133
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134 gh->msg_type = GSM48_MT_CC_USER_INFO;
3135
3136 /* user-user */
3137 if (user->fields & MNCC_F_USERUSER)
3138 encode_useruser(msg, 1, &user->useruser);
3139 /* more data */
3140 if (user->more)
3141 encode_more(msg);
3142
Harald Welte39e2ead2009-07-23 21:13:03 +02003143 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144}
3145
3146static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3147{
3148 struct gsm48_hdr *gh = msgb_l3(msg);
3149 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3150 struct tlv_parsed tp;
3151 struct gsm_mncc user;
3152
3153 memset(&user, 0, sizeof(struct gsm_mncc));
3154 user.callref = trans->callref;
3155 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3156 /* user-user */
3157 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3158 user.fields |= MNCC_F_USERUSER;
3159 decode_useruser(&user.useruser,
3160 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3161 }
3162 /* more data */
3163 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3164 user.more = 1;
3165
Harald Welte596fed42009-07-23 19:06:52 +02003166 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167}
3168
3169static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3170{
3171 struct gsm_mncc *mode = arg;
Harald Welte13cac662009-07-29 12:10:35 +02003172 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173
Harald Welte13cac662009-07-29 12:10:35 +02003174 rc = gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3175 if (rc < 0)
3176 return rc;
3177
3178 /* FIXME: we not only need to do this after mode modify, but
3179 * also after channel activation */
3180 if (is_ipaccess_bts(trans->lchan->ts->trx->bts) &&
3181 mode->lchan_mode != GSM48_CMODE_SIGN)
3182 rc = rsl_ipacc_bind(trans->lchan);
3183
3184 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003185}
3186
3187static struct downstate {
3188 u_int32_t states;
3189 int type;
3190 int (*rout) (struct gsm_trans *trans, void *arg);
3191} downstatelist[] = {
3192 /* mobile originating call establishment */
3193 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3194 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3195 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3196 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3197 {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 */
3198 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3199 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3200 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3201 /* mobile terminating call establishment */
3202 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3203 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3204 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3205 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3206 /* signalling during call */
3207 {SBIT(GSM_CSTATE_ACTIVE),
3208 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3209 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3210 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3211 {ALL_STATES,
3212 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3213 {ALL_STATES,
3214 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3215 {ALL_STATES,
3216 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3217 {SBIT(GSM_CSTATE_ACTIVE),
3218 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3219 {SBIT(GSM_CSTATE_ACTIVE),
3220 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3221 {SBIT(GSM_CSTATE_ACTIVE),
3222 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3223 {SBIT(GSM_CSTATE_ACTIVE),
3224 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3225 {SBIT(GSM_CSTATE_ACTIVE),
3226 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3227 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3228 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3229 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3230 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3231 {SBIT(GSM_CSTATE_ACTIVE),
3232 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3233 /* clearing */
3234 {SBIT(GSM_CSTATE_INITIATED),
3235 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3236 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3237 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3238 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3239 MNCC_REL_REQ, gsm48_cc_tx_release},
3240 /* special */
3241 {ALL_STATES,
3242 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3243};
3244
3245#define DOWNSLLEN \
3246 (sizeof(downstatelist) / sizeof(struct downstate))
3247
3248
3249int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3250{
Harald Welte1a6f7982009-08-09 18:52:33 +02003251 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003252 struct gsm_trans *trans = NULL, *transt;
3253 struct gsm_subscriber *subscr;
Harald Welte1a6f7982009-08-09 18:52:33 +02003254 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003256 struct gsm_mncc *data = arg, rel;
3257
3258 /* handle special messages */
3259 switch(msg_type) {
3260 case MNCC_BRIDGE:
3261 return tch_bridge(net, arg);
3262 case MNCC_FRAME_DROP:
3263 return tch_recv(net, arg, 0);
3264 case MNCC_FRAME_RECV:
3265 return tch_recv(net, arg, 1);
3266 case GSM_TRAU_FRAME:
3267 return tch_frame(net, arg);
3268 }
3269
3270 memset(&rel, 0, sizeof(struct gsm_mncc));
3271 rel.callref = data->callref;
3272
3273 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003274 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275
3276 /* Callref unknown */
3277 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003278 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3280 "Received '%s' from MNCC with "
3281 "unknown callref %d\n", data->called.number,
3282 get_mncc_name(msg_type), data->callref);
3283 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003284 return mncc_release_ind(net, NULL, data->callref,
3285 GSM48_CAUSE_LOC_PRN_S_LU,
3286 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003288 if (!data->called.number[0] && !data->imsi[0]) {
3289 DEBUGP(DCC, "(bts - trx - ts - ti) "
3290 "Received '%s' from MNCC with "
3291 "no number or IMSI\n", get_mncc_name(msg_type));
3292 /* Invalid number */
3293 return mncc_release_ind(net, NULL, data->callref,
3294 GSM48_CAUSE_LOC_PRN_S_LU,
3295 GSM48_CC_CAUSE_INV_NR_FORMAT);
3296 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003298 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003299 subscr = subscr_get_by_extension(net,
3300 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003301 else
Harald Welte9176bd42009-07-23 18:46:00 +02003302 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 /* If subscriber is not found */
3304 if (!subscr) {
3305 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3306 "Received '%s' from MNCC with "
3307 "unknown subscriber %s\n", data->called.number,
3308 get_mncc_name(msg_type), data->called.number);
3309 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003310 return mncc_release_ind(net, NULL, data->callref,
3311 GSM48_CAUSE_LOC_PRN_S_LU,
3312 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 }
3314 /* If subscriber is not "attached" */
3315 if (!subscr->lac) {
3316 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3317 "Received '%s' from MNCC with "
3318 "detached subscriber %s\n", data->called.number,
3319 get_mncc_name(msg_type), data->called.number);
3320 subscr_put(subscr);
3321 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003322 return mncc_release_ind(net, NULL, data->callref,
3323 GSM48_CAUSE_LOC_PRN_S_LU,
3324 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003325 }
3326 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003327 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3328 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 DEBUGP(DCC, "No memory for trans.\n");
3330 subscr_put(subscr);
3331 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003332 mncc_release_ind(net, NULL, data->callref,
3333 GSM48_CAUSE_LOC_PRN_S_LU,
3334 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 return -ENOMEM;
3336 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003337 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003338 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003339 /* If subscriber has no lchan */
3340 if (!lchan) {
3341 /* find transaction with this subscriber already paging */
3342 llist_for_each_entry(transt, &net->trans_list, entry) {
3343 /* Transaction of our lchan? */
3344 if (transt == trans ||
3345 transt->subscr != subscr)
3346 continue;
3347 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3348 "Received '%s' from MNCC with "
3349 "unallocated channel, paging already "
3350 "started.\n", bts->nr,
3351 data->called.number,
3352 get_mncc_name(msg_type));
3353 return 0;
3354 }
3355 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003356 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003357 /* Trigger paging */
3358 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3359 setup_trig_pag_evt, subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003360 return 0;
3361 }
3362 /* Assign lchan */
3363 trans->lchan = lchan;
3364 use_lchan(lchan);
3365 }
3366 lchan = trans->lchan;
3367
3368 /* if paging did not respond yet */
3369 if (!lchan) {
3370 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3371 "Received '%s' from MNCC in paging state\n",
3372 (trans->subscr)?(trans->subscr->extension):"-",
3373 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003374 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3375 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003376 if (msg_type == MNCC_REL_REQ)
3377 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3378 else
3379 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3380 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003381 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003382 return rc;
3383 }
3384
3385 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3386 "Received '%s' from MNCC in state %d (%s)\n",
3387 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3388 trans->transaction_id,
3389 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003390 get_mncc_name(msg_type), trans->cc.state,
3391 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003392
3393 /* Find function for current state and message */
3394 for (i = 0; i < DOWNSLLEN; i++)
3395 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003396 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003397 break;
3398 if (i == DOWNSLLEN) {
3399 DEBUGP(DCC, "Message unhandled at this state.\n");
3400 return 0;
3401 }
3402
3403 rc = downstatelist[i].rout(trans, arg);
3404
3405 return rc;
3406}
3407
3408
3409static struct datastate {
3410 u_int32_t states;
3411 int type;
3412 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3413} datastatelist[] = {
3414 /* mobile originating call establishment */
3415 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3416 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3417 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3418 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3419 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3420 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3421 /* mobile terminating call establishment */
3422 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3423 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3424 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3425 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3426 {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 */
3427 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3428 /* signalling during call */
3429 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3430 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3431 {SBIT(GSM_CSTATE_ACTIVE),
3432 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3433 {ALL_STATES,
3434 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3435 {ALL_STATES,
3436 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3437 {ALL_STATES,
3438 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3439 {SBIT(GSM_CSTATE_ACTIVE),
3440 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3441 {SBIT(GSM_CSTATE_ACTIVE),
3442 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3443 {SBIT(GSM_CSTATE_ACTIVE),
3444 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3445 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3446 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3447 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3448 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3449 {SBIT(GSM_CSTATE_ACTIVE),
3450 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3451 /* clearing */
3452 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3453 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3454 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3455 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3456 {ALL_STATES, /* 5.4.3.4 */
3457 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3458};
3459
3460#define DATASLLEN \
3461 (sizeof(datastatelist) / sizeof(struct datastate))
3462
Harald Welte4bc90a12008-12-27 16:32:52 +00003463static int gsm0408_rcv_cc(struct msgb *msg)
3464{
3465 struct gsm48_hdr *gh = msgb_l3(msg);
3466 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003467 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003468 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003469 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003470 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003471
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 if (msg_type & 0x80) {
3473 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3474 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003475 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476
3477 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003478 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003479
Harald Welte6f5aee02009-07-23 21:21:14 +02003480 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003481 "Received '%s' from MS in state %d (%s)\n",
3482 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3483 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003484 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003485 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003486
3487 /* Create transaction */
3488 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003489 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003490 "creating new trans.\n", transaction_id);
3491 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003492 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3493 transaction_id, new_callref++);
3494 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003495 DEBUGP(DCC, "No memory for trans.\n");
3496 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003497 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003498 GSM48_MT_CC_RELEASE_COMPL);
3499 return -ENOMEM;
3500 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003501 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003502 trans->lchan = lchan;
3503 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003504 }
3505
3506 /* find function for current state and message */
3507 for (i = 0; i < DATASLLEN; i++)
3508 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003509 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003510 break;
3511 if (i == DATASLLEN) {
3512 DEBUGP(DCC, "Message unhandled at this state.\n");
3513 return 0;
3514 }
3515
3516 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003517
3518 return rc;
3519}
3520
Harald Welte52b1f982008-12-23 20:25:15 +00003521/* 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 +02003522int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003523{
3524 struct gsm48_hdr *gh = msgb_l3(msg);
3525 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003526 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003527
3528 switch (pdisc) {
3529 case GSM48_PDISC_CC:
3530 rc = gsm0408_rcv_cc(msg);
3531 break;
3532 case GSM48_PDISC_MM:
3533 rc = gsm0408_rcv_mm(msg);
3534 break;
3535 case GSM48_PDISC_RR:
3536 rc = gsm0408_rcv_rr(msg);
3537 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003538 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003539 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003540 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003541 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003542 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003543 case GSM48_PDISC_NC_SS: /* mobile-originated USSD */
3544 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003545 pdisc);
3546 break;
3547 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003548 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003549 pdisc);
3550 break;
3551 }
3552
3553 return rc;
3554}
Harald Welte8470bf22008-12-25 23:28:35 +00003555
Harald Welte4bfdfe72009-06-10 23:11:52 +08003556/* dequeue messages to layer 4 */
3557int bsc_upqueue(struct gsm_network *net)
3558{
3559 struct gsm_mncc *mncc;
3560 struct msgb *msg;
3561 int work = 0;
3562
3563 if (net)
3564 while ((msg = msgb_dequeue(&net->upqueue))) {
3565 mncc = (struct gsm_mncc *)msg->data;
3566 if (net->mncc_recv)
3567 net->mncc_recv(net, mncc->msg_type, mncc);
3568 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003569 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003570 }
3571
3572 return work;
3573}
Harald Weltedcaf5652009-07-23 18:56:43 +02003574
Harald Welte805f6442009-07-28 18:25:29 +02003575/*
3576 * This will be ran by the linker when loading the DSO. We use it to
3577 * do system initialization, e.g. registration of signal handlers.
3578 */
3579static __attribute__((constructor)) void on_dso_load_0408(void)
3580{
Harald Welte805f6442009-07-28 18:25:29 +02003581 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3582 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3583}