blob: d6fe09fa2e3abda58cd2fe9597130f2fed39f5aa [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{
294 u_int32_t tmsi;
295
296 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200297 int rc;
298
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000300 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
301 release_loc_updating_req(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200302 rc = gsm0408_loc_upd_acc(msg->lchan, tmsi);
303 /* call subscr_update after putting the loc_upd_acc
304 * in the transmit queue, since S_SUBSCR_ATTACHED might
305 * trigger further action like SMS delivery */
306 subscr_update(lchan->subscr, msg->trx->bts,
307 GSM_SUBSCRIBER_UPDATE_ATTACHED);
308 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000309 }
310
311 return 0;
312}
313
Holger Freyther7c19f742009-06-06 13:54:35 +0000314static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
315 void *handler_data, void *signal_data)
316{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800317 struct gsm_trans *trans, *temp;
318
Holger Freyther7c19f742009-06-06 13:54:35 +0000319 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
320 return 0;
321
322 /*
323 * Cancel any outstanding location updating request
324 * operation taking place on the lchan.
325 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200326 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200327 if (!lchan)
328 return 0;
329
Holger Freyther7c19f742009-06-06 13:54:35 +0000330 release_loc_updating_req(lchan);
331
Harald Welte4bfdfe72009-06-10 23:11:52 +0800332 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200333 /* FIXME: this is not neccessarily the right thing to do, we should
334 * only set trans->lchan to NULL and wait for another lchan to be
335 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
337 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200338 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800339 }
340
Holger Freyther7c19f742009-06-06 13:54:35 +0000341 return 0;
342}
343
Harald Welte09e38af2009-02-16 22:52:23 +0000344static const char bcd_num_digits[] = {
345 '0', '1', '2', '3', '4', '5', '6', '7',
346 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
347};
348
Harald Welte0c389302009-06-10 12:08:54 +0800349/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
350int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
351 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000352{
353 u_int8_t in_len = bcd_lv[0];
354 int i;
355
Harald Welte0c389302009-06-10 12:08:54 +0800356 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000357 /* lower nibble */
358 output_len--;
359 if (output_len <= 1)
360 break;
361 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
362
363 /* higher nibble */
364 output_len--;
365 if (output_len <= 1)
366 break;
367 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
368 }
369 if (output_len >= 1)
370 *output++ = '\0';
371
Harald Welte0c389302009-06-10 12:08:54 +0800372 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000373}
374
375/* convert a single ASCII character to call-control BCD */
376static int asc_to_bcd(const char asc)
377{
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
381 if (bcd_num_digits[i] == asc)
382 return i;
383 }
384 return -EINVAL;
385}
386
Harald Welte0c389302009-06-10 12:08:54 +0800387/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000388int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800389 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000390{
391 int in_len = strlen(input);
392 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800393 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000394
395 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800396 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000397 if (in_len % 2)
398 bcd_lv[0]++;
399
Harald Welte0c389302009-06-10 12:08:54 +0800400 if (bcd_lv[0] > max_len)
401 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000402
403 for (i = 0; i < in_len; i++) {
404 int rc = asc_to_bcd(input[i]);
405 if (rc < 0)
406 return rc;
407 if (i % 2 == 0)
408 *bcd_cur = rc;
409 else
410 *bcd_cur++ |= (rc << 4);
411 }
412 /* append padding nibble in case of odd length */
413 if (i % 2)
414 *bcd_cur++ |= 0xf0;
415
416 /* return how many bytes we used */
417 return (bcd_cur - bcd_lv);
418}
419
Harald Welte0c389302009-06-10 12:08:54 +0800420/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800421static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800422 const u_int8_t *lv)
423{
424 u_int8_t in_len = lv[0];
425 int i, s;
426
427 if (in_len < 1)
428 return -EINVAL;
429
Harald Welte4bfdfe72009-06-10 23:11:52 +0800430 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800431
432 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800433 bcap->transfer = lv[1] & 0x07;
434 bcap->mode = (lv[1] & 0x08) >> 3;
435 bcap->coding = (lv[1] & 0x10) >> 4;
436 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800437
438 i = 1;
439 s = 0;
440 while(!(lv[i] & 0x80)) {
441 i++; /* octet 3a etc */
442 if (in_len < i)
443 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 bcap->speech_ver[s++] = lv[i] & 0x0f;
445 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800446 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800448 if (s == 7) /* maximum speech versions + end of list */
449 return 0;
450 }
451
452 return 0;
453}
454
455/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800456static int encode_bearer_cap(struct msgb *msg, int lv_only,
457 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800458{
459 u_int8_t lv[32 + 1];
460 int i, s;
461
Harald Welte4bfdfe72009-06-10 23:11:52 +0800462 lv[1] = bcap->transfer;
463 lv[1] |= bcap->mode << 3;
464 lv[1] |= bcap->coding << 4;
465 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800466
467 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800468 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800469 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800471 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800472 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800473 }
474 lv[i] |= 0x80; /* last IE of octet 3 etc */
475
476 lv[0] = i;
477 if (lv_only)
478 msgb_lv_put(msg, lv[0], lv+1);
479 else
480 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
481
482 return 0;
483}
484
485/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800486static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800487{
488 u_int8_t in_len = lv[0];
489
490 if (in_len < 1)
491 return -EINVAL;
492
493 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800494 ccap->dtmf = lv[1] & 0x01;
495 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800496
497 return 0;
498}
499
500/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800501static int decode_called(struct gsm_mncc_number *called,
502 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800503{
504 u_int8_t in_len = lv[0];
505
506 if (in_len < 1)
507 return -EINVAL;
508
509 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800510 called->plan = lv[1] & 0x0f;
511 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800512
513 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800514 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800515
516 return 0;
517}
518
519/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800520static int encode_called(struct msgb *msg,
521 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800522{
523 u_int8_t lv[18];
524 int ret;
525
526 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800527 lv[1] = called->plan;
528 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800529
530 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800531 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800532 if (ret < 0)
533 return ret;
534
535 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
536
537 return 0;
538}
539
540/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800541static int encode_callerid(struct msgb *msg, int ie,
542 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800543{
544 u_int8_t lv[13];
545 int h_len = 1;
546 int ret;
547
548 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 lv[1] = callerid->plan;
550 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800551
Harald Welte4bfdfe72009-06-10 23:11:52 +0800552 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800553 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554 lv[2] = callerid->screen;
555 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800556 lv[2] |= 0x80;
557 h_len++;
558 } else
559 lv[1] |= 0x80;
560
561 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800562 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800563 if (ret < 0)
564 return ret;
565
566 msgb_tlv_put(msg, ie, lv[0], lv+1);
567
568 return 0;
569}
570
571/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800573 const u_int8_t *lv)
574{
575 u_int8_t in_len = lv[0];
576 int i;
577
578 if (in_len < 2)
579 return -EINVAL;
580
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800582
583 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800584 cause->location = lv[1] & 0x0f;
585 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800586
587 i = 1;
588 if (!(lv[i] & 0x80)) {
589 i++; /* octet 3a */
590 if (in_len < i+1)
591 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592 cause->rec = 1;
593 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800594
595 }
596 i++;
597
598 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800599 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800600 i++;
601
602 if (in_len < i) /* no diag */
603 return 0;
604
605 if (in_len - (i-1) > 32) /* maximum 32 octets */
606 return 0;
607
608 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800609 memcpy(cause->diag, lv + i, in_len - (i-1));
610 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800611
612 return 0;
613}
614
615/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800616static int encode_cause(struct msgb *msg, int lv_only,
617 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800618{
619 u_int8_t lv[32+4];
620 int i;
621
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800623 return -EINVAL;
624
625 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800626 lv[1] = cause->location;
627 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800628
629 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800630 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800631 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800632 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800633 }
634 lv[i] |= 0x80; /* end of octet 3 */
635
636 /* octet 4 */
637 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800639
640 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800641 if (cause->diag_len) {
642 memcpy(lv + i, cause->diag, cause->diag_len);
643 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800644 }
645
646 lv[0] = i;
647 if (lv_only)
648 msgb_lv_put(msg, lv[0], lv+1);
649 else
650 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
651
652 return 0;
653}
654
655/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656static int encode_calling(struct msgb *msg,
657 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800658{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800659 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800660}
661
662/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663static int encode_connected(struct msgb *msg,
664 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800665{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800666 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800667}
668
669/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670static int encode_redirecting(struct msgb *msg,
671 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800672{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800673 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800674}
675
676/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800677static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800678 const u_int8_t *lv)
679{
680 u_int8_t in_len = lv[0];
681
682 if (in_len < 1)
683 return -EINVAL;
684
Harald Welte4bfdfe72009-06-10 23:11:52 +0800685 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800686 return -EINVAL;
687
Harald Welte4bfdfe72009-06-10 23:11:52 +0800688 memcpy(facility->info, lv+1, in_len);
689 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800690
691 return 0;
692}
693
694/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695static int encode_facility(struct msgb *msg, int lv_only,
696 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800697{
698 u_int8_t lv[GSM_MAX_FACILITY + 1];
699
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800701 return -EINVAL;
702
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703 memcpy(lv+1, facility->info, facility->len);
704 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800705 if (lv_only)
706 msgb_lv_put(msg, lv[0], lv+1);
707 else
708 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
709
710 return 0;
711}
712
713/* decode 'notify' */
714static int decode_notify(int *notify, const u_int8_t *v)
715{
716 *notify = v[0] & 0x7f;
717
718 return 0;
719}
720
721/* encode 'notify' */
722static int encode_notify(struct msgb *msg, int notify)
723{
724 msgb_v_put(msg, notify | 0x80);
725
726 return 0;
727}
728
729/* encode 'signal' */
730static int encode_signal(struct msgb *msg, int signal)
731{
732 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
733
734 return 0;
735}
736
737/* decode 'keypad' */
738static int decode_keypad(int *keypad, const u_int8_t *lv)
739{
740 u_int8_t in_len = lv[0];
741
742 if (in_len < 1)
743 return -EINVAL;
744
745 *keypad = lv[1] & 0x7f;
746
747 return 0;
748}
749
750/* encode 'keypad' */
751static int encode_keypad(struct msgb *msg, int keypad)
752{
753 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
754
755 return 0;
756}
757
758/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800759static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800760 const u_int8_t *lv)
761{
762 u_int8_t in_len = lv[0];
763
764 if (in_len < 2)
765 return -EINVAL;
766
Harald Welte4bfdfe72009-06-10 23:11:52 +0800767 progress->coding = (lv[1] & 0x60) >> 5;
768 progress->location = lv[1] & 0x0f;
769 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800770
771 return 0;
772}
773
774/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800775static int encode_progress(struct msgb *msg, int lv_only,
776 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800777{
778 u_int8_t lv[3];
779
780 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800781 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
782 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800783 if (lv_only)
784 msgb_lv_put(msg, lv[0], lv+1);
785 else
786 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
787
788 return 0;
789}
790
791/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800792static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800793 const u_int8_t *lv)
794{
795 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800796 char *info = uu->info;
797 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800798 int i;
799
800 if (in_len < 1)
801 return -EINVAL;
802
Harald Welte4bfdfe72009-06-10 23:11:52 +0800803 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800804
805 for (i = 2; i <= in_len; i++) {
806 info_len--;
807 if (info_len <= 1)
808 break;
809 *info++ = lv[i];
810 }
811 if (info_len >= 1)
812 *info++ = '\0';
813
814 return 0;
815}
816
817/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800818static int encode_useruser(struct msgb *msg, int lv_only,
819 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800820{
821 u_int8_t lv[GSM_MAX_USERUSER + 2];
822
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800824 return -EINVAL;
825
Harald Welte4bfdfe72009-06-10 23:11:52 +0800826 lv[0] = 1 + strlen(uu->info);
827 lv[1] = uu->proto;
828 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800829 if (lv_only)
830 msgb_lv_put(msg, lv[0], lv+1);
831 else
832 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
833
834 return 0;
835}
836
837/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800838static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800839 const u_int8_t *lv)
840{
841 u_int8_t in_len = lv[0];
842
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800844 return -EINVAL;
845
Harald Welte4bfdfe72009-06-10 23:11:52 +0800846 memcpy(ssv->info, lv + 1, in_len);
847 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800848
849 return 0;
850}
851
852/* encode 'more data' */
853static int encode_more(struct msgb *msg)
854{
855 u_int8_t *ie;
856
857 ie = msgb_put(msg, 1);
858 ie[0] = GSM48_IE_MORE_DATA;
859
860 return 0;
861}
862
Holger Freyther429e7762008-12-30 13:28:30 +0000863/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000864int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000865{
Harald Welte8470bf22008-12-25 23:28:35 +0000866 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000867 struct gsm48_hdr *gh;
868
Harald Welte8470bf22008-12-25 23:28:35 +0000869 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000870
871 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
872 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000873 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000874 gh->data[0] = cause;
875
Harald Weltedb253af2008-12-30 17:56:55 +0000876 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
877
Harald Welte39e2ead2009-07-23 21:13:03 +0200878 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000879}
880
881/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000882int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000883{
Harald Welte8470bf22008-12-25 23:28:35 +0000884 struct gsm_bts *bts = lchan->ts->trx->bts;
885 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000886 struct gsm48_hdr *gh;
887 struct gsm48_loc_area_id *lai;
888 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000889 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000890
Harald Welte8470bf22008-12-25 23:28:35 +0000891 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000892
893 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
894 gh->proto_discr = GSM48_PDISC_MM;
895 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
896
897 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000898 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000899 bts->network->network_code, bts->location_area_code);
900
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200901 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200902 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000903
904 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
905
Harald Welte39e2ead2009-07-23 21:13:03 +0200906 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000907
Harald Welteee5ad162009-08-09 19:07:00 +0200908 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000909 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000910
Holger Freyther07cc8d82008-12-29 06:23:46 +0000911 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000912}
913
Harald Weltebf5e8df2009-02-03 12:59:45 +0000914/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000915static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
916{
917 struct msgb *msg = gsm48_msgb_alloc();
918 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000919
Harald Welte231ad4f2008-12-27 11:15:38 +0000920 msg->lchan = lchan;
921
922 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
923 gh->proto_discr = GSM48_PDISC_MM;
924 gh->msg_type = GSM48_MT_MM_ID_REQ;
925 gh->data[0] = id_type;
926
Harald Welte39e2ead2009-07-23 21:13:03 +0200927 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000928}
929
Harald Welte231ad4f2008-12-27 11:15:38 +0000930
Harald Weltebf5e8df2009-02-03 12:59:45 +0000931/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000932static int mm_rx_id_resp(struct msgb *msg)
933{
934 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000935 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200936 struct gsm_bts *bts = lchan->ts->trx->bts;
937 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000938 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200939 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000940
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200941 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000942 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000943 mi_type, mi_string);
944
Harald Welte75a983f2008-12-27 21:34:06 +0000945 switch (mi_type) {
946 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200947 /* look up subscriber based on IMSI, create if not found */
948 if (!lchan->subscr) {
949 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200950 if (!lchan->subscr)
951 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200952 }
Holger Freyther73487a22008-12-31 18:53:57 +0000953 if (lchan->loc_operation)
954 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000955 break;
956 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000957 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000958 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200959 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000960 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200961 db_sync_equipment(&lchan->subscr->equipment);
962 }
Holger Freyther73487a22008-12-31 18:53:57 +0000963 if (lchan->loc_operation)
964 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000965 break;
966 }
Holger Freyther73487a22008-12-31 18:53:57 +0000967
968 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000969 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000970}
971
Harald Welte255539c2008-12-28 02:26:27 +0000972
973static void loc_upd_rej_cb(void *data)
974{
975 struct gsm_lchan *lchan = data;
976
Holger Freyther73487a22008-12-31 18:53:57 +0000977 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000978 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000979 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000980}
981
Holger Freytherb7193e42008-12-29 17:44:08 +0000982static void schedule_reject(struct gsm_lchan *lchan)
983{
Holger Freyther73487a22008-12-31 18:53:57 +0000984 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
985 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000986 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000987}
988
Harald Welte2a139372009-02-22 21:14:55 +0000989static const char *lupd_name(u_int8_t type)
990{
991 switch (type) {
992 case GSM48_LUPD_NORMAL:
993 return "NORMAL";
994 case GSM48_LUPD_PERIODIC:
995 return "PEROIDOC";
996 case GSM48_LUPD_IMSI_ATT:
997 return "IMSI ATTACH";
998 default:
999 return "UNKNOWN";
1000 }
1001}
1002
Harald Weltebf5e8df2009-02-03 12:59:45 +00001003/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001004static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001005{
Harald Welte8470bf22008-12-25 23:28:35 +00001006 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001007 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001008 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001009 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001010 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001011 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001012 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001013 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001014
Harald Welte8470bf22008-12-25 23:28:35 +00001015 lu = (struct gsm48_loc_upd_req *) gh->data;
1016
1017 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001018
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001019 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001020
Harald Weltea0368542009-06-27 02:58:43 +02001021 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001022 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001023
Holger Freythereaf04692009-06-06 13:54:44 +00001024 /*
1025 * Pseudo Spoof detection: Just drop a second/concurrent
1026 * location updating request.
1027 */
1028 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001029 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001030 lchan->loc_operation);
1031 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1032 return 0;
1033 }
1034
Holger Freyther73487a22008-12-31 18:53:57 +00001035 allocate_loc_updating_req(lchan);
1036
Harald Welte52b1f982008-12-23 20:25:15 +00001037 switch (mi_type) {
1038 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001039 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001040 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001041 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001042 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001043
Jan Luebbe370b41d2009-08-12 10:19:34 +02001044 /* look up subscriber based on IMSI, create if not found */
1045 subscr = subscr_get_by_imsi(bts->network, mi_string);
1046 if (!subscr) {
1047 subscr = db_create_subscriber(bts->network, mi_string);
1048 }
Harald Welte4b634542008-12-27 01:55:51 +00001049 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001050 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001051 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001052 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001053 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001054 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001055
Harald Welte52b1f982008-12-23 20:25:15 +00001056 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welte9176bd42009-07-23 18:46:00 +02001057 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001058 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001059 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001060 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001061 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001062 }
1063 break;
1064 case GSM_MI_TYPE_IMEI:
1065 case GSM_MI_TYPE_IMEISV:
1066 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001067 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001068 break;
1069 default:
Harald Weltea0368542009-06-27 02:58:43 +02001070 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001071 break;
1072 }
1073
Harald Welte24516ea2009-07-04 10:18:00 +02001074 /* schedule the reject timer */
1075 schedule_reject(lchan);
1076
Harald Welte4bfdfe72009-06-10 23:11:52 +08001077 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001078 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001079 /* FIXME: request id? close channel? */
1080 return -EINVAL;
1081 }
1082
Harald Welte255539c2008-12-28 02:26:27 +00001083 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001084 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001085
Harald Welte24516ea2009-07-04 10:18:00 +02001086 /* check if we can let the subscriber into our network immediately
1087 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001088 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001089}
1090
Harald Welte13cac662009-07-29 12:10:35 +02001091/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Harald Welte7584aea2009-02-11 11:44:12 +00001092int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1093{
1094 struct msgb *msg = gsm48_msgb_alloc();
1095 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1096 struct gsm48_chan_mode_modify *cmm =
1097 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001098 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001099
Harald Welte4a543e82009-02-28 13:17:55 +00001100 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001101
Harald Welte45b407a2009-05-23 15:51:12 +00001102 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001103 msg->lchan = lchan;
1104 gh->proto_discr = GSM48_PDISC_RR;
1105 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1106
1107 /* fill the channel information element, this code
1108 * should probably be shared with rsl_rx_chan_rqd() */
1109 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001110 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001111 cmm->chan_desc.h0.h = 0;
1112 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1113 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1114 cmm->mode = mode;
1115
Harald Welte39e2ead2009-07-23 21:13:03 +02001116 return gsm48_sendmsg(msg, NULL);
Harald Welte7584aea2009-02-11 11:44:12 +00001117}
1118
Harald Welte4bfdfe72009-06-10 23:11:52 +08001119#if 0
1120static u_int8_t to_bcd8(u_int8_t val)
1121{
1122 return ((val / 10) << 4) | (val % 10);
1123}
1124#endif
1125
Harald Weltedb253af2008-12-30 17:56:55 +00001126/* Section 9.2.15a */
1127int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1128{
1129 struct msgb *msg = gsm48_msgb_alloc();
1130 struct gsm48_hdr *gh;
1131 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001132 u_int8_t *ptr8;
1133 u_int16_t *ptr16;
Daniel Willmanneea93372009-08-13 03:42:07 +02001134 int name_len, name_pad;
Harald Weltedb253af2008-12-30 17:56:55 +00001135 int i;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001136#if 0
1137 time_t cur_t;
1138 struct tm* cur_time;
1139 int tz15min;
1140#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001141
1142 msg->lchan = lchan;
1143
1144 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1145 gh->proto_discr = GSM48_PDISC_MM;
1146 gh->msg_type = GSM48_MT_MM_INFO;
1147
1148 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001149#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001150 name_len = strlen(net->name_long);
1151 /* 10.5.3.5a */
1152 ptr8 = msgb_put(msg, 3);
1153 ptr8[0] = GSM48_IE_NAME_LONG;
1154 ptr8[1] = name_len*2 +1;
1155 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1156
1157 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1158 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001159 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001160
1161 /* FIXME: Use Cell Broadcast, not UCS-2, since
1162 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001163#endif
1164 name_len = (strlen(net->name_long)*7)/8;
1165 name_pad = (8 - strlen(net->name_long)*7)%8;
1166 if (name_pad > 0)
1167 name_len++;
1168 /* 10.5.3.5a */
1169 ptr8 = msgb_put(msg, 3);
1170 ptr8[0] = GSM48_IE_NAME_LONG;
1171 ptr8[1] = name_len +1;
1172 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1173
1174 ptr8 = msgb_put(msg, name_len);
1175 gsm_7bit_encode(ptr8, net->name_long);
1176
Harald Weltedb253af2008-12-30 17:56:55 +00001177 }
1178
1179 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001180#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001181 name_len = strlen(net->name_short);
1182 /* 10.5.3.5a */
1183 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001184 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001185 ptr8[1] = name_len*2 + 1;
1186 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1187
Harald Weltee872cb12009-01-01 00:33:37 +00001188 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001189 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001190 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001191#endif
1192 name_len = (strlen(net->name_short)*7)/8;
1193 name_pad = (8 - strlen(net->name_short)*7)%8;
1194 if (name_pad > 0)
1195 name_len++;
1196 /* 10.5.3.5a */
1197 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1198 ptr8[0] = GSM48_IE_NAME_SHORT;
1199 ptr8[1] = name_len +1;
1200 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1201
1202 ptr8 = msgb_put(msg, name_len);
1203 gsm_7bit_encode(ptr8, net->name_short);
1204
Harald Weltedb253af2008-12-30 17:56:55 +00001205 }
1206
1207#if 0
1208 /* Section 10.5.3.9 */
1209 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001210 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001211 ptr8 = msgb_put(msg, 8);
1212 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1213 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1214 ptr8[2] = to_bcd8(cur_time->tm_mon);
1215 ptr8[3] = to_bcd8(cur_time->tm_mday);
1216 ptr8[4] = to_bcd8(cur_time->tm_hour);
1217 ptr8[5] = to_bcd8(cur_time->tm_min);
1218 ptr8[6] = to_bcd8(cur_time->tm_sec);
1219 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1220 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001221 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001222 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001223 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001224#endif
1225
Daniel Willmanneea93372009-08-13 03:42:07 +02001226 DEBUGP(DMM, "-> MM INFO\n");
1227
Harald Welte39e2ead2009-07-23 21:13:03 +02001228 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001229}
1230
Harald Welte7984d5c2009-08-12 22:56:50 +02001231/* Section 9.2.2 */
1232int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1233{
1234 struct msgb *msg = gsm48_msgb_alloc();
1235 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001236 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001237
1238 DEBUGP(DMM, "-> AUTH REQ\n");
1239
1240 msg->lchan = lchan;
1241 gh->proto_discr = GSM48_PDISC_MM;
1242 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1243
Sylvain Munaut849f5542009-09-27 11:10:17 +02001244 /* Key Sequence: FIXME fixed to 0 */
1245 ar->key_seq = 0;
1246
Harald Welte7984d5c2009-08-12 22:56:50 +02001247 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001248 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001249 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001250
1251 return gsm48_sendmsg(msg, NULL);
1252}
1253
1254/* Section 9.2.1 */
1255int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1256{
1257 DEBUGP(DMM, "-> AUTH REJECT\n");
1258 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1259}
1260
Harald Welte4b634542008-12-27 01:55:51 +00001261static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1262{
Harald Welte4b634542008-12-27 01:55:51 +00001263 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001264 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001265}
Harald Welteba4cf162009-01-10 01:49:35 +00001266
1267/* 9.2.6 CM service reject */
1268static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1269 enum gsm48_reject_value value)
1270{
1271 struct msgb *msg = gsm48_msgb_alloc();
1272 struct gsm48_hdr *gh;
1273
1274 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1275
1276 msg->lchan = lchan;
1277 use_lchan(lchan);
1278
1279 gh->proto_discr = GSM48_PDISC_MM;
1280 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1281 gh->data[0] = value;
1282 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1283
Harald Welte39e2ead2009-07-23 21:13:03 +02001284 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001285}
1286
Harald Welte3ac7f102009-08-10 10:12:45 +02001287static int send_siemens_mrpci(struct gsm_lchan *lchan,
1288 u_int8_t *classmark2_lv)
1289{
1290 struct rsl_mrpci mrpci;
1291
1292 if (classmark2_lv[0] < 2)
1293 return -EINVAL;
1294
1295 mrpci.power_class = classmark2_lv[1] & 0x7;
1296 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
1297 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
1298 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
1299
1300 return rsl_siemens_mrpci(lchan, &mrpci);
1301}
Harald Welte4ed0e922009-01-10 03:17:30 +00001302
1303/*
1304 * Handle CM Service Requests
1305 * a) Verify that the packet is long enough to contain the information
1306 * we require otherwsie reject with INCORRECT_MESSAGE
1307 * b) Try to parse the TMSI. If we do not have one reject
1308 * c) Check that we know the subscriber with the TMSI otherwise reject
1309 * with a HLR cause
1310 * d) Set the subscriber on the gsm_lchan and accept
1311 */
Harald Welte4b634542008-12-27 01:55:51 +00001312static int gsm48_rx_mm_serv_req(struct msgb *msg)
1313{
Harald Welteba4cf162009-01-10 01:49:35 +00001314 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001315 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001316
Harald Welte9176bd42009-07-23 18:46:00 +02001317 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001318 struct gsm_subscriber *subscr;
1319 struct gsm48_hdr *gh = msgb_l3(msg);
1320 struct gsm48_service_request *req =
1321 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001322 /* unfortunately in Phase1 the classmar2 length is variable */
1323 u_int8_t classmark2_len = gh->data[1];
1324 u_int8_t *classmark2 = gh->data+2;
1325 u_int8_t mi_len = *(classmark2 + classmark2_len);
1326 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001327
Harald Weltec9e02182009-05-01 19:07:53 +00001328 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001329 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001330 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001331 return gsm48_tx_mm_serv_rej(msg->lchan,
1332 GSM48_REJECT_INCORRECT_MESSAGE);
1333 }
1334
1335 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001336 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001337 return gsm48_tx_mm_serv_rej(msg->lchan,
1338 GSM48_REJECT_INCORRECT_MESSAGE);
1339 }
1340
Harald Weltec9e02182009-05-01 19:07:53 +00001341 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001342 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001343 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001344 return gsm48_tx_mm_serv_rej(msg->lchan,
1345 GSM48_REJECT_INCORRECT_MESSAGE);
1346 }
1347
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001348 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001349 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001350 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001351
Harald Welte3ac7f102009-08-10 10:12:45 +02001352 if (is_siemens_bts(bts))
1353 send_siemens_mrpci(msg->lchan, classmark2-1);
1354
Harald Welte9176bd42009-07-23 18:46:00 +02001355 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Holger Freythereb443982009-06-04 13:58:42 +00001356
Harald Welte2a139372009-02-22 21:14:55 +00001357 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001358 if (!subscr)
1359 return gsm48_tx_mm_serv_rej(msg->lchan,
1360 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1361
1362 if (!msg->lchan->subscr)
1363 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001364 else if (msg->lchan->subscr != subscr) {
1365 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1366 subscr_put(subscr);
1367 }
1368
Harald Weltec2e302d2009-07-05 14:08:13 +02001369 subscr->equipment.classmark2_len = classmark2_len;
1370 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1371 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001372
Harald Welte4b634542008-12-27 01:55:51 +00001373 return gsm48_tx_mm_serv_ack(msg->lchan);
1374}
1375
Harald Welte2a139372009-02-22 21:14:55 +00001376static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1377{
Harald Welte9176bd42009-07-23 18:46:00 +02001378 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001379 struct gsm48_hdr *gh = msgb_l3(msg);
1380 struct gsm48_imsi_detach_ind *idi =
1381 (struct gsm48_imsi_detach_ind *) gh->data;
1382 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001383 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001384 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001385
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001386 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001387 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1388 mi_type, mi_string);
1389
1390 switch (mi_type) {
1391 case GSM_MI_TYPE_TMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001392 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001393 break;
1394 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001395 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001396 break;
1397 case GSM_MI_TYPE_IMEI:
1398 case GSM_MI_TYPE_IMEISV:
1399 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001400 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001401 break;
1402 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001403 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001404 break;
1405 }
1406
Holger Freyther4a49e772009-04-12 05:37:29 +00001407 if (subscr) {
1408 subscr_update(subscr, msg->trx->bts,
1409 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001410 DEBUGP(DMM, "Subscriber: %s\n",
1411 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001412
1413 subscr->equipment.classmark1 = idi->classmark1;
1414 db_sync_equipment(&subscr->equipment);
1415
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001416 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001417 } else
Harald Welte2a139372009-02-22 21:14:55 +00001418 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1419
Harald Welte2a139372009-02-22 21:14:55 +00001420 return 0;
1421}
1422
Harald Welted2a7f5a2009-06-05 20:08:20 +00001423static int gsm48_rx_mm_status(struct msgb *msg)
1424{
1425 struct gsm48_hdr *gh = msgb_l3(msg);
1426
1427 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1428
1429 return 0;
1430}
1431
Harald Weltebf5e8df2009-02-03 12:59:45 +00001432/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001433static int gsm0408_rcv_mm(struct msgb *msg)
1434{
1435 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001436 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001437
1438 switch (gh->msg_type & 0xbf) {
1439 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001440 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001441 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001442 break;
1443 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001444 rc = mm_rx_id_resp(msg);
1445 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001446 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001447 rc = gsm48_rx_mm_serv_req(msg);
1448 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001449 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001450 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001451 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001452 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001453 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1454 msg->lchan->subscr ?
1455 msg->lchan->subscr->imsi :
1456 "unknown subscriber");
1457 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001458 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001459 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1460 break;
1461 case GSM48_MT_MM_CM_REEST_REQ:
1462 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1463 break;
1464 case GSM48_MT_MM_AUTH_RESP:
1465 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001466 break;
1467 default:
1468 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1469 gh->msg_type);
1470 break;
1471 }
1472
1473 return rc;
1474}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001475
Harald Welte2d35ae62009-02-06 12:02:13 +00001476/* Receive a PAGING RESPONSE message from the MS */
1477static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1478{
Harald Welte9176bd42009-07-23 18:46:00 +02001479 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001480 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001481 u_int8_t *classmark2_lv = gh->data + 1;
1482 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1483 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001484 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001485 struct gsm_subscriber *subscr = NULL;
Harald Welte595ad7b2009-02-16 22:05:44 +00001486 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001487 int rc = 0;
1488
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001489 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001490 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1491 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001492
1493 if (is_siemens_bts(bts))
1494 send_siemens_mrpci(msg->lchan, classmark2_lv);
1495
Harald Weltefe18d8f2009-02-22 21:14:24 +00001496 switch (mi_type) {
1497 case GSM_MI_TYPE_TMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001498 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001499 break;
1500 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001501 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001502 break;
1503 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001504
1505 if (!subscr) {
1506 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001507 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001508 return -EINVAL;
1509 }
1510 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001511 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001512
Harald Weltec2e302d2009-07-05 14:08:13 +02001513 subscr->equipment.classmark2_len = *classmark2_lv;
1514 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1515 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001516
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001517 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001518 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001519 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001520 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1521 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001522 return -EINVAL;
1523 } else {
1524 DEBUGP(DRR, "<- Channel already owned by us\n");
1525 subscr_put(subscr);
1526 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001527 }
1528
Harald Welte595ad7b2009-02-16 22:05:44 +00001529 sig_data.subscr = subscr;
1530 sig_data.bts = msg->lchan->ts->trx->bts;
1531 sig_data.lchan = msg->lchan;
1532
1533 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001534
1535 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001536 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001537
Harald Welte7584aea2009-02-11 11:44:12 +00001538 /* FIXME: somehow signal the completion of the PAGING to
1539 * the entity that requested the paging */
1540
Harald Welte2d35ae62009-02-06 12:02:13 +00001541 return rc;
1542}
1543
Harald Weltef7c43522009-06-09 20:24:21 +00001544static int gsm48_rx_rr_classmark(struct msgb *msg)
1545{
1546 struct gsm48_hdr *gh = msgb_l3(msg);
1547 struct gsm_subscriber *subscr = msg->lchan->subscr;
1548 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1549 u_int8_t cm2_len, cm3_len = 0;
1550 u_int8_t *cm2, *cm3 = NULL;
1551
1552 DEBUGP(DRR, "CLASSMARK CHANGE ");
1553
1554 /* classmark 2 */
1555 cm2_len = gh->data[0];
1556 cm2 = &gh->data[1];
1557 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1558
1559 if (payload_len > cm2_len + 1) {
1560 /* we must have a classmark3 */
1561 if (gh->data[cm2_len+1] != 0x20) {
1562 DEBUGPC(DRR, "ERR CM3 TAG\n");
1563 return -EINVAL;
1564 }
1565 if (cm2_len > 3) {
1566 DEBUGPC(DRR, "CM2 too long!\n");
1567 return -EINVAL;
1568 }
1569
1570 cm3_len = gh->data[cm2_len+2];
1571 cm3 = &gh->data[cm2_len+3];
1572 if (cm3_len > 14) {
1573 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1574 return -EINVAL;
1575 }
1576 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1577 }
1578 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001579 subscr->equipment.classmark2_len = cm2_len;
1580 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001581 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001582 subscr->equipment.classmark3_len = cm3_len;
1583 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001584 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001585 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001586 }
1587
Harald Weltef7c43522009-06-09 20:24:21 +00001588 return 0;
1589}
1590
Harald Weltecf5b3592009-05-01 18:28:42 +00001591static int gsm48_rx_rr_status(struct msgb *msg)
1592{
1593 struct gsm48_hdr *gh = msgb_l3(msg);
1594
1595 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1596 rr_cause_name(gh->data[0]));
1597
1598 return 0;
1599}
1600
Harald Weltef7c43522009-06-09 20:24:21 +00001601static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1602{
1603 struct gsm48_hdr *gh = msgb_l3(msg);
1604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1605 static struct gsm_meas_rep meas_rep;
1606
Harald Welte10d0e672009-06-27 02:53:10 +02001607 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001608 parse_meas_rep(&meas_rep, gh->data, payload_len);
1609 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001610 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001611 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001612 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001613 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001614 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001615 else
Harald Welte10d0e672009-06-27 02:53:10 +02001616 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001617 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1618 meas_rep.rxqual_sub);
1619
Harald Welte10d0e672009-06-27 02:53:10 +02001620 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001621
1622 /* FIXME: put the results somwhere */
1623
1624 return 0;
1625}
1626
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001627static int gsm48_rx_rr_app_info(struct msgb *msg)
1628{
1629 struct gsm48_hdr *gh = msgb_l3(msg);
1630 u_int8_t apdu_id_flags;
1631 u_int8_t apdu_len;
1632 u_int8_t *apdu_data;
1633
1634 apdu_id_flags = gh->data[0];
1635 apdu_len = gh->data[1];
1636 apdu_data = gh->data+2;
1637
1638 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1639 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1640
Harald Welte (local)026531e2009-08-16 10:40:10 +02001641 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001642}
1643
1644
Harald Weltebf5e8df2009-02-03 12:59:45 +00001645/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001646static int gsm0408_rcv_rr(struct msgb *msg)
1647{
1648 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001649 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001650
1651 switch (gh->msg_type) {
1652 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001653 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001654 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001655 case GSM48_MT_RR_GPRS_SUSP_REQ:
1656 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1657 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001658 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001659 rc = gsm48_rr_rx_pag_resp(msg);
1660 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001661 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1662 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte13cac662009-07-29 12:10:35 +02001663 /* We've successfully modified the MS side of the channel,
1664 * now go on to modify the BTS side of the channel */
Harald Welte9943c5b2009-07-29 15:41:29 +02001665 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
Harald Welte2c38aa82009-02-18 03:44:24 +00001666 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001667 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001668 case GSM48_MT_RR_STATUS:
1669 rc = gsm48_rx_rr_status(msg);
1670 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001671 case GSM48_MT_RR_MEAS_REP:
1672 rc = gsm48_rx_rr_meas_rep(msg);
1673 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001674 case GSM48_MT_RR_APP_INFO:
1675 rc = gsm48_rx_rr_app_info(msg);
1676 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001677 case GSM48_MT_RR_CIPH_M_COMPL:
1678 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1679 /* FIXME: check for MI (if any) */
1680 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001681 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001682 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001683 gh->msg_type);
1684 break;
1685 }
1686
Harald Welte2d35ae62009-02-06 12:02:13 +00001687 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001688}
1689
Harald Welte08d91a52009-08-30 15:37:11 +09001690/* Chapter 9.1.9: Ciphering Mode Command */
1691int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan)
1692{
1693 struct msgb *msg = gsm48_msgb_alloc();
1694 struct gsm48_hdr *gh;
1695 u_int8_t ciph_mod_set;
1696
1697 msg->lchan = lchan;
1698
1699 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
1700
1701 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
1702 ciph_mod_set = 0;
1703 else
Sylvain Munaut1dcfc002009-09-27 11:11:41 +02001704 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
Harald Welte08d91a52009-08-30 15:37:11 +09001705
1706 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1707 gh->proto_discr = GSM48_PDISC_RR;
1708 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
1709 gh->data[0] = 0x10 | (ciph_mod_set & 0xf);
1710
1711 return rsl_encryption_cmd(msg);
1712}
Holger Freythere64a7a32009-02-06 21:55:37 +00001713
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001714int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
1715 u_int8_t apdu_len, u_int8_t *apdu)
1716{
1717 struct msgb *msg = gsm48_msgb_alloc();
1718 struct gsm48_hdr *gh;
1719
1720 msg->lchan = lchan;
1721
1722 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1723 apdu_id, apdu_len);
1724
1725 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1726 gh->proto_discr = GSM48_PDISC_RR;
1727 gh->msg_type = GSM48_MT_RR_APP_INFO;
1728 gh->data[0] = apdu_id;
1729 gh->data[1] = apdu_len;
1730 memcpy(gh->data+2, apdu, apdu_len);
1731
1732 return gsm48_sendmsg(msg, NULL);
1733}
1734
Harald Welte4bc90a12008-12-27 16:32:52 +00001735/* Call Control */
1736
Harald Welte7584aea2009-02-11 11:44:12 +00001737/* The entire call control code is written in accordance with Figure 7.10c
1738 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1739 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1740 * it for voice */
1741
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742static void new_cc_state(struct gsm_trans *trans, int state)
1743{
1744 if (state > 31 || state < 0)
1745 return;
1746
1747 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001748 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749
Harald Weltedcaf5652009-07-23 18:56:43 +02001750 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751}
1752
1753static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001754{
1755 struct msgb *msg = gsm48_msgb_alloc();
1756 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1757 u_int8_t *cause, *call_state;
1758
Harald Welte4bc90a12008-12-27 16:32:52 +00001759 gh->msg_type = GSM48_MT_CC_STATUS;
1760
1761 cause = msgb_put(msg, 3);
1762 cause[0] = 2;
1763 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1764 cause[2] = 0x80 | 30; /* response to status inquiry */
1765
1766 call_state = msgb_put(msg, 1);
1767 call_state[0] = 0xc0 | 0x00;
1768
Harald Welte39e2ead2009-07-23 21:13:03 +02001769 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001770}
1771
Harald Welte6f4b7532008-12-29 00:39:37 +00001772static int gsm48_tx_simple(struct gsm_lchan *lchan,
1773 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001774{
1775 struct msgb *msg = gsm48_msgb_alloc();
1776 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1777
1778 msg->lchan = lchan;
1779
Harald Welte6f4b7532008-12-29 00:39:37 +00001780 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001781 gh->msg_type = msg_type;
1782
Harald Welte39e2ead2009-07-23 21:13:03 +02001783 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001784}
1785
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1787{
Harald Weltedcaf5652009-07-23 18:56:43 +02001788 if (bsc_timer_pending(&trans->cc.timer)) {
1789 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1790 bsc_del_timer(&trans->cc.timer);
1791 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 }
1793}
1794
1795static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1796 int msg_type, struct gsm_mncc *mncc)
1797{
1798 struct msgb *msg;
1799
1800 if (trans)
1801 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001802 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 "Sending '%s' to MNCC.\n",
1804 trans->lchan->ts->trx->bts->nr,
1805 trans->lchan->ts->trx->nr,
1806 trans->lchan->ts->nr, trans->transaction_id,
1807 (trans->subscr)?(trans->subscr->extension):"-",
1808 get_mncc_name(msg_type));
1809 else
1810 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1811 "Sending '%s' to MNCC.\n",
1812 (trans->subscr)?(trans->subscr->extension):"-",
1813 get_mncc_name(msg_type));
1814 else
1815 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1816 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1817
1818 mncc->msg_type = msg_type;
1819
Harald Welte966636f2009-06-26 19:39:35 +02001820 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 if (!msg)
1822 return -ENOMEM;
1823 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1824 msgb_enqueue(&net->upqueue, msg);
1825
1826 return 0;
1827}
1828
1829int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1830 u_int32_t callref, int location, int value)
1831{
1832 struct gsm_mncc rel;
1833
Harald Welte92f70c52009-06-12 01:54:08 +08001834 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001836 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1838}
1839
Harald Weltedcaf5652009-07-23 18:56:43 +02001840/* Call Control Specific transaction release.
1841 * gets called by trans_free, DO NOT CALL YOURSELF! */
1842void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 gsm48_stop_cc_timer(trans);
1845
1846 /* send release to L4, if callref still exists */
1847 if (trans->callref) {
1848 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001849 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001850 GSM48_CAUSE_LOC_PRN_S_LU,
1851 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001853 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001855 if (trans->lchan)
1856 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857}
1858
1859static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1860
Harald Welte09e38af2009-02-16 22:52:23 +00001861/* call-back from paging the B-end of the connection */
1862static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001863 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001864{
Harald Welte7ccf7782009-02-17 01:43:01 +00001865 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 struct gsm_subscriber *subscr = param;
1867 struct gsm_trans *transt, *tmp;
1868 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001869
Harald Welte09e38af2009-02-16 22:52:23 +00001870 if (hooknum != GSM_HOOK_RR_PAGING)
1871 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872
1873 if (!subscr)
1874 return -EINVAL;
1875 net = subscr->net;
1876 if (!net) {
1877 DEBUGP(DCC, "Error Network not set!\n");
1878 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001879 }
Harald Welte7584aea2009-02-11 11:44:12 +00001880
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* check all tranactions (without lchan) for subscriber */
1882 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1883 if (transt->subscr != subscr || transt->lchan)
1884 continue;
1885 switch (event) {
1886 case GSM_PAGING_SUCCEEDED:
1887 if (!lchan) // paranoid
1888 break;
1889 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1890 subscr->extension);
1891 /* Assign lchan */
1892 if (!transt->lchan) {
1893 transt->lchan = lchan;
1894 use_lchan(lchan);
1895 }
1896 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001897 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 break;
1899 case GSM_PAGING_EXPIRED:
1900 DEBUGP(DCC, "Paging subscr %s expired!\n",
1901 subscr->extension);
1902 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001903 mncc_release_ind(transt->subscr->net, transt,
1904 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001905 GSM48_CAUSE_LOC_PRN_S_LU,
1906 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001908 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001909 break;
1910 }
1911 }
Harald Welte09e38af2009-02-16 22:52:23 +00001912 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001913}
Harald Welte7584aea2009-02-11 11:44:12 +00001914
Harald Welte805f6442009-07-28 18:25:29 +02001915/* some other part of the code sends us a signal */
1916static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1917 void *handler_data, void *signal_data)
1918{
1919 struct gsm_lchan *lchan = signal_data;
1920 struct gsm_bts_trx_ts *ts;
1921 int rc;
1922
1923 if (subsys != SS_ABISIP)
1924 return 0;
1925
1926 /* in case we use direct BTS-to-BTS RTP */
1927 if (ipacc_rtp_direct)
1928 return 0;
1929
1930 ts = lchan->ts;
1931
1932 switch (signal) {
1933 case S_ABISIP_BIND_ACK:
1934 /* the BTS has successfully bound a TCH to a local ip/port,
1935 * which means we can connect our UDP socket to it */
1936 if (ts->abis_ip.rtp_socket) {
1937 rtp_socket_free(ts->abis_ip.rtp_socket);
1938 ts->abis_ip.rtp_socket = NULL;
1939 }
1940
1941 ts->abis_ip.rtp_socket = rtp_socket_create();
1942 if (!ts->abis_ip.rtp_socket)
1943 goto out_err;
1944
1945 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1946 ts->abis_ip.bound_ip,
1947 ts->abis_ip.bound_port);
1948 if (rc < 0)
1949 goto out_err;
1950 break;
1951 case S_ABISIP_DISC_IND:
1952 /* the BTS tells us a RTP stream has been disconnected */
1953 if (ts->abis_ip.rtp_socket) {
1954 rtp_socket_free(ts->abis_ip.rtp_socket);
1955 ts->abis_ip.rtp_socket = NULL;
1956 }
1957 break;
1958 }
1959
1960 return 0;
1961out_err:
1962 /* FIXME: do something */
1963 return 0;
1964}
1965
1966/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1967static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1968{
1969 struct gsm_bts_trx_ts *ts = lchan->ts;
1970 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1971 int rc;
1972
1973 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1974 ntohs(rs->rtp.sin_local.sin_port),
1975 ts->abis_ip.conn_id,
1976 /* FIXME: use RTP payload of bound socket, not BTS*/
1977 ts->abis_ip.rtp_payload2);
1978
1979 return rc;
1980}
1981
Harald Welte49f48b82009-02-17 15:29:33 +00001982/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001983static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001984{
Harald Welte11fa29c2009-02-19 17:24:39 +00001985 struct gsm_bts *bts = lchan->ts->trx->bts;
1986 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001987 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02001988 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001989
Harald Welte11fa29c2009-02-19 17:24:39 +00001990 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1991 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1992 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1993
1994 if (bts->type != remote_bts->type) {
1995 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1996 return -EINVAL;
1997 }
Harald Welte49f48b82009-02-17 15:29:33 +00001998
Harald Welte11fa29c2009-02-19 17:24:39 +00001999 switch (bts->type) {
2000 case GSM_BTS_TYPE_NANOBTS_900:
2001 case GSM_BTS_TYPE_NANOBTS_1800:
Harald Welte805f6442009-07-28 18:25:29 +02002002 if (!ipacc_rtp_direct) {
2003 /* connect the TCH's to our RTP proxy */
2004 rc = ipacc_connect_proxy_bind(lchan);
2005 if (rc < 0)
2006 return rc;
2007 rc = ipacc_connect_proxy_bind(remote_lchan);
2008
2009 /* connect them with each other */
2010 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
2011 remote_lchan->ts->abis_ip.rtp_socket);
2012 } else {
2013 /* directly connect TCH RTP streams to each other */
2014 ts = remote_lchan->ts;
2015 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
2016 ts->abis_ip.bound_port,
2017 lchan->ts->abis_ip.conn_id,
2018 ts->abis_ip.rtp_payload2);
2019 if (rc < 0)
2020 return rc;
2021 ts = lchan->ts;
2022 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
2023 ts->abis_ip.bound_port,
2024 remote_lchan->ts->abis_ip.conn_id,
2025 ts->abis_ip.rtp_payload2);
2026 }
Harald Welte11fa29c2009-02-19 17:24:39 +00002027 break;
2028 case GSM_BTS_TYPE_BS11:
2029 trau_mux_map_lchan(lchan, remote_lchan);
2030 break;
2031 default:
2032 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02002033 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00002034 break;
2035 }
Harald Welte49f48b82009-02-17 15:29:33 +00002036
2037 return 0;
2038}
2039
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040/* bridge channels of two transactions */
2041static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002042{
Harald Weltedcaf5652009-07-23 18:56:43 +02002043 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
2044 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002045
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002047 return -EIO;
2048
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 if (!trans1->lchan || !trans2->lchan)
2050 return -EIO;
2051
2052 /* through-connect channel */
2053 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002054}
2055
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056/* enable receive of channels to upqueue */
2057static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2058{
2059 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002060
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002062 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 if (!trans)
2064 return -EIO;
2065 if (!trans->lchan)
2066 return 0;
2067
2068 // todo IPACCESS
2069 if (enable)
2070 return trau_recv_lchan(trans->lchan, data->callref);
2071 return trau_mux_unmap(NULL, data->callref);
2072}
2073
2074/* send a frame to channel */
2075static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2076{
2077 struct gsm_trans *trans;
2078
2079 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002080 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 if (!trans)
2082 return -EIO;
2083 if (!trans->lchan)
2084 return 0;
2085 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2086 trans->lchan->type != GSM_LCHAN_TCH_H)
2087 return 0;
2088
2089 // todo IPACCESS
2090 return trau_send_lchan(trans->lchan,
2091 (struct decoded_trau_frame *)frame->data);
2092}
2093
2094
2095static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2096{
2097 DEBUGP(DCC, "-> STATUS ENQ\n");
2098 return gsm48_cc_tx_status(trans, msg);
2099}
2100
2101static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2102static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2103
2104static void gsm48_cc_timeout(void *arg)
2105{
2106 struct gsm_trans *trans = arg;
2107 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002108 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2109 int mo_location = GSM48_CAUSE_LOC_USER;
2110 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2111 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 struct gsm_mncc mo_rel, l4_rel;
2113
2114 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2115 mo_rel.callref = trans->callref;
2116 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2117 l4_rel.callref = trans->callref;
2118
Harald Weltedcaf5652009-07-23 18:56:43 +02002119 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 case 0x303:
2121 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002122 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 break;
2124 case 0x310:
2125 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002126 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 break;
2128 case 0x313:
2129 disconnect = 1;
2130 /* unknown, did not find it in the specs */
2131 break;
2132 case 0x301:
2133 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002134 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 break;
2136 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002137 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002139 gsm48_cc_tx_release(trans, &trans->cc.msg);
2140 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 break; /* stay in release state */
2142 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002143 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 return;
2145// release = 1;
2146// l4_cause = 14;
2147// break;
2148 case 0x306:
2149 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002150 mo_cause = trans->cc.msg.cause.value;
2151 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 break;
2153 case 0x323:
2154 disconnect = 1;
2155 break;
2156 default:
2157 release = 1;
2158 }
2159
2160 if (release && trans->callref) {
2161 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002162 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 l4_location, l4_cause);
2164 trans->callref = 0;
2165 }
2166
2167 if (disconnect && trans->callref) {
2168 /* process disconnect towards layer 4 */
2169 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002170 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 }
2172
2173 /* process disconnect towards mobile station */
2174 if (disconnect || release) {
2175 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002176 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2177 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2178 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 mo_rel.cause.diag_len = 3;
2180
2181 if (disconnect)
2182 gsm48_cc_tx_disconnect(trans, &mo_rel);
2183 if (release)
2184 gsm48_cc_tx_release(trans, &mo_rel);
2185 }
2186
2187}
2188
2189static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2190 int sec, int micro)
2191{
2192 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002193 trans->cc.timer.cb = gsm48_cc_timeout;
2194 trans->cc.timer.data = trans;
2195 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2196 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197}
2198
2199static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2200{
2201 struct gsm48_hdr *gh = msgb_l3(msg);
2202 u_int8_t msg_type = gh->msg_type & 0xbf;
2203 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2204 struct tlv_parsed tp;
2205 struct gsm_mncc setup;
2206
2207 memset(&setup, 0, sizeof(struct gsm_mncc));
2208 setup.callref = trans->callref;
2209 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2210 /* emergency setup is identified by msg_type */
2211 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2212 setup.emergency = 1;
2213
2214 /* use subscriber as calling party number */
2215 if (trans->subscr) {
2216 setup.fields |= MNCC_F_CALLING;
2217 strncpy(setup.calling.number, trans->subscr->extension,
2218 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002219 strncpy(setup.imsi, trans->subscr->imsi,
2220 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 }
2222 /* bearer capability */
2223 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2224 setup.fields |= MNCC_F_BEARER_CAP;
2225 decode_bearer_cap(&setup.bearer_cap,
2226 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2227 }
2228 /* facility */
2229 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2230 setup.fields |= MNCC_F_FACILITY;
2231 decode_facility(&setup.facility,
2232 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2233 }
2234 /* called party bcd number */
2235 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2236 setup.fields |= MNCC_F_CALLED;
2237 decode_called(&setup.called,
2238 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2239 }
2240 /* user-user */
2241 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2242 setup.fields |= MNCC_F_USERUSER;
2243 decode_useruser(&setup.useruser,
2244 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2245 }
2246 /* ss-version */
2247 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2248 setup.fields |= MNCC_F_SSVERSION;
2249 decode_ssversion(&setup.ssversion,
2250 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2251 }
2252 /* CLIR suppression */
2253 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2254 setup.clir.sup = 1;
2255 /* CLIR invocation */
2256 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2257 setup.clir.inv = 1;
2258 /* cc cap */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2260 setup.fields |= MNCC_F_CCCAP;
2261 decode_cccap(&setup.cccap,
2262 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2263 }
2264
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 new_cc_state(trans, GSM_CSTATE_INITIATED);
2266
2267 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002268 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269
Harald Welte13cac662009-07-29 12:10:35 +02002270 /* MNCC code will modify the channel asynchronously, we should
2271 * ipaccess-bind only after the modification has been made to the
2272 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 return 0;
2274}
2275
2276static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002277{
2278 struct msgb *msg = gsm48_msgb_alloc();
2279 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002281 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002282
Harald Welte7ccf7782009-02-17 01:43:01 +00002283 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002284
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 /* transaction id must not be assigned */
2286 if (trans->transaction_id != 0xff) { /* unasssigned */
2287 DEBUGP(DCC, "TX Setup with assigned transaction. "
2288 "This is not allowed!\n");
2289 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002290 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002291 GSM48_CAUSE_LOC_PRN_S_LU,
2292 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002294 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 return rc;
2296 }
2297
2298 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002299 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2300 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002302 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002303 GSM48_CAUSE_LOC_PRN_S_LU,
2304 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002306 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 return rc;
2308 }
Harald Welte78283ef2009-07-23 21:36:44 +02002309 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002310
Harald Welte65e74cc2008-12-29 01:55:35 +00002311 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002312
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002314
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 /* bearer capability */
2316 if (setup->fields & MNCC_F_BEARER_CAP)
2317 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2318 /* facility */
2319 if (setup->fields & MNCC_F_FACILITY)
2320 encode_facility(msg, 0, &setup->facility);
2321 /* progress */
2322 if (setup->fields & MNCC_F_PROGRESS)
2323 encode_progress(msg, 0, &setup->progress);
2324 /* calling party BCD number */
2325 if (setup->fields & MNCC_F_CALLING)
2326 encode_calling(msg, &setup->calling);
2327 /* called party BCD number */
2328 if (setup->fields & MNCC_F_CALLED)
2329 encode_called(msg, &setup->called);
2330 /* user-user */
2331 if (setup->fields & MNCC_F_USERUSER)
2332 encode_useruser(msg, 0, &setup->useruser);
2333 /* redirecting party BCD number */
2334 if (setup->fields & MNCC_F_REDIRECTING)
2335 encode_redirecting(msg, &setup->redirecting);
2336 /* signal */
2337 if (setup->fields & MNCC_F_SIGNAL)
2338 encode_signal(msg, setup->signal);
2339
2340 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002341
Harald Welte39e2ead2009-07-23 21:13:03 +02002342 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002343}
2344
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2346{
2347 struct gsm48_hdr *gh = msgb_l3(msg);
2348 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2349 struct tlv_parsed tp;
2350 struct gsm_mncc call_conf;
2351
2352 gsm48_stop_cc_timer(trans);
2353 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2354
2355 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2356 call_conf.callref = trans->callref;
2357 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2358#if 0
2359 /* repeat */
2360 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2361 call_conf.repeat = 1;
2362 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2363 call_conf.repeat = 2;
2364#endif
2365 /* bearer capability */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2367 call_conf.fields |= MNCC_F_BEARER_CAP;
2368 decode_bearer_cap(&call_conf.bearer_cap,
2369 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2370 }
2371 /* cause */
2372 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2373 call_conf.fields |= MNCC_F_CAUSE;
2374 decode_cause(&call_conf.cause,
2375 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2376 }
2377 /* cc cap */
2378 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2379 call_conf.fields |= MNCC_F_CCCAP;
2380 decode_cccap(&call_conf.cccap,
2381 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2382 }
2383
2384 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2385
Harald Welte596fed42009-07-23 19:06:52 +02002386 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2387 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2391{
2392 struct gsm_mncc *proceeding = arg;
2393 struct msgb *msg = gsm48_msgb_alloc();
2394 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2395
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2397
2398 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2399
2400 /* bearer capability */
2401 if (proceeding->fields & MNCC_F_BEARER_CAP)
2402 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2403 /* facility */
2404 if (proceeding->fields & MNCC_F_FACILITY)
2405 encode_facility(msg, 0, &proceeding->facility);
2406 /* progress */
2407 if (proceeding->fields & MNCC_F_PROGRESS)
2408 encode_progress(msg, 0, &proceeding->progress);
2409
Harald Welte39e2ead2009-07-23 21:13:03 +02002410 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411}
2412
2413static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2414{
2415 struct gsm48_hdr *gh = msgb_l3(msg);
2416 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2417 struct tlv_parsed tp;
2418 struct gsm_mncc alerting;
2419
2420 gsm48_stop_cc_timer(trans);
2421 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2422
2423 memset(&alerting, 0, sizeof(struct gsm_mncc));
2424 alerting.callref = trans->callref;
2425 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2426 /* facility */
2427 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2428 alerting.fields |= MNCC_F_FACILITY;
2429 decode_facility(&alerting.facility,
2430 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2431 }
2432
2433 /* progress */
2434 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2435 alerting.fields |= MNCC_F_PROGRESS;
2436 decode_progress(&alerting.progress,
2437 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2438 }
2439 /* ss-version */
2440 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2441 alerting.fields |= MNCC_F_SSVERSION;
2442 decode_ssversion(&alerting.ssversion,
2443 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2444 }
2445
2446 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2447
Harald Welte596fed42009-07-23 19:06:52 +02002448 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2449 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450}
2451
2452static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2453{
2454 struct gsm_mncc *alerting = arg;
2455 struct msgb *msg = gsm48_msgb_alloc();
2456 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2457
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 gh->msg_type = GSM48_MT_CC_ALERTING;
2459
2460 /* facility */
2461 if (alerting->fields & MNCC_F_FACILITY)
2462 encode_facility(msg, 0, &alerting->facility);
2463 /* progress */
2464 if (alerting->fields & MNCC_F_PROGRESS)
2465 encode_progress(msg, 0, &alerting->progress);
2466 /* user-user */
2467 if (alerting->fields & MNCC_F_USERUSER)
2468 encode_useruser(msg, 0, &alerting->useruser);
2469
2470 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2471
Harald Welte39e2ead2009-07-23 21:13:03 +02002472 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473}
2474
2475static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2476{
2477 struct gsm_mncc *progress = arg;
2478 struct msgb *msg = gsm48_msgb_alloc();
2479 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2480
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 gh->msg_type = GSM48_MT_CC_PROGRESS;
2482
2483 /* progress */
2484 encode_progress(msg, 1, &progress->progress);
2485 /* user-user */
2486 if (progress->fields & MNCC_F_USERUSER)
2487 encode_useruser(msg, 0, &progress->useruser);
2488
Harald Welte39e2ead2009-07-23 21:13:03 +02002489 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490}
2491
2492static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2493{
2494 struct gsm_mncc *connect = arg;
2495 struct msgb *msg = gsm48_msgb_alloc();
2496 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2497
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498 gh->msg_type = GSM48_MT_CC_CONNECT;
2499
2500 gsm48_stop_cc_timer(trans);
2501 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2502
2503 /* facility */
2504 if (connect->fields & MNCC_F_FACILITY)
2505 encode_facility(msg, 0, &connect->facility);
2506 /* progress */
2507 if (connect->fields & MNCC_F_PROGRESS)
2508 encode_progress(msg, 0, &connect->progress);
2509 /* connected number */
2510 if (connect->fields & MNCC_F_CONNECTED)
2511 encode_connected(msg, &connect->connected);
2512 /* user-user */
2513 if (connect->fields & MNCC_F_USERUSER)
2514 encode_useruser(msg, 0, &connect->useruser);
2515
2516 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2517
Harald Welte39e2ead2009-07-23 21:13:03 +02002518 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2522{
2523 struct gsm48_hdr *gh = msgb_l3(msg);
2524 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2525 struct tlv_parsed tp;
2526 struct gsm_mncc connect;
2527
2528 gsm48_stop_cc_timer(trans);
2529
2530 memset(&connect, 0, sizeof(struct gsm_mncc));
2531 connect.callref = trans->callref;
2532 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2533 /* use subscriber as connected party number */
2534 if (trans->subscr) {
2535 connect.fields |= MNCC_F_CONNECTED;
2536 strncpy(connect.connected.number, trans->subscr->extension,
2537 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002538 strncpy(connect.imsi, trans->subscr->imsi,
2539 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 }
2541 /* facility */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2543 connect.fields |= MNCC_F_FACILITY;
2544 decode_facility(&connect.facility,
2545 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2546 }
2547 /* user-user */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2549 connect.fields |= MNCC_F_USERUSER;
2550 decode_useruser(&connect.useruser,
2551 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2552 }
2553 /* ss-version */
2554 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2555 connect.fields |= MNCC_F_SSVERSION;
2556 decode_ssversion(&connect.ssversion,
2557 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2558 }
2559
2560 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2561
Harald Welte596fed42009-07-23 19:06:52 +02002562 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563}
2564
2565
2566static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2567{
2568 struct gsm_mncc connect_ack;
2569
2570 gsm48_stop_cc_timer(trans);
2571
2572 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2573
2574 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2575 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002576 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 &connect_ack);
2578}
2579
2580static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2581{
2582 struct msgb *msg = gsm48_msgb_alloc();
2583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2584
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2586
2587 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2588
Harald Welte39e2ead2009-07-23 21:13:03 +02002589 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590}
2591
2592static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2593{
2594 struct gsm48_hdr *gh = msgb_l3(msg);
2595 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2596 struct tlv_parsed tp;
2597 struct gsm_mncc disc;
2598
2599 gsm48_stop_cc_timer(trans);
2600
2601 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2602
2603 memset(&disc, 0, sizeof(struct gsm_mncc));
2604 disc.callref = trans->callref;
2605 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2606 /* cause */
2607 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2608 disc.fields |= MNCC_F_CAUSE;
2609 decode_cause(&disc.cause,
2610 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2611 }
2612 /* facility */
2613 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2614 disc.fields |= MNCC_F_FACILITY;
2615 decode_facility(&disc.facility,
2616 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2617 }
2618 /* user-user */
2619 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2620 disc.fields |= MNCC_F_USERUSER;
2621 decode_useruser(&disc.useruser,
2622 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2623 }
2624 /* ss-version */
2625 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2626 disc.fields |= MNCC_F_SSVERSION;
2627 decode_ssversion(&disc.ssversion,
2628 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2629 }
2630
Harald Welte596fed42009-07-23 19:06:52 +02002631 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632
2633}
2634
Harald Weltec66b71c2009-06-11 14:23:20 +08002635static struct gsm_mncc_cause default_cause = {
2636 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2637 .coding = 0,
2638 .rec = 0,
2639 .rec_val = 0,
2640 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2641 .diag_len = 0,
2642 .diag = { 0 },
2643};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644
2645static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2646{
2647 struct gsm_mncc *disc = arg;
2648 struct msgb *msg = gsm48_msgb_alloc();
2649 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2650
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2652
2653 gsm48_stop_cc_timer(trans);
2654 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2655
2656 /* cause */
2657 if (disc->fields & MNCC_F_CAUSE)
2658 encode_cause(msg, 1, &disc->cause);
2659 else
2660 encode_cause(msg, 1, &default_cause);
2661
2662 /* facility */
2663 if (disc->fields & MNCC_F_FACILITY)
2664 encode_facility(msg, 0, &disc->facility);
2665 /* progress */
2666 if (disc->fields & MNCC_F_PROGRESS)
2667 encode_progress(msg, 0, &disc->progress);
2668 /* user-user */
2669 if (disc->fields & MNCC_F_USERUSER)
2670 encode_useruser(msg, 0, &disc->useruser);
2671
2672 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002673 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674
2675 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2676
Harald Welte39e2ead2009-07-23 21:13:03 +02002677 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678}
2679
2680static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2681{
2682 struct gsm48_hdr *gh = msgb_l3(msg);
2683 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2684 struct tlv_parsed tp;
2685 struct gsm_mncc rel;
2686 int rc;
2687
2688 gsm48_stop_cc_timer(trans);
2689
2690 memset(&rel, 0, sizeof(struct gsm_mncc));
2691 rel.callref = trans->callref;
2692 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2693 /* cause */
2694 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2695 rel.fields |= MNCC_F_CAUSE;
2696 decode_cause(&rel.cause,
2697 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2698 }
2699 /* facility */
2700 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2701 rel.fields |= MNCC_F_FACILITY;
2702 decode_facility(&rel.facility,
2703 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2704 }
2705 /* user-user */
2706 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2707 rel.fields |= MNCC_F_USERUSER;
2708 decode_useruser(&rel.useruser,
2709 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2710 }
2711 /* ss-version */
2712 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2713 rel.fields |= MNCC_F_SSVERSION;
2714 decode_ssversion(&rel.ssversion,
2715 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2716 }
2717
Harald Weltedcaf5652009-07-23 18:56:43 +02002718 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002720 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002722 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002723 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002724 GSM48_MT_CC_RELEASE_COMPL);
2725 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 }
2727
2728 new_cc_state(trans, GSM_CSTATE_NULL);
2729
2730 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002731 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732
2733 return rc;
2734}
2735
2736static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2737{
2738 struct gsm_mncc *rel = arg;
2739 struct msgb *msg = gsm48_msgb_alloc();
2740 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2741
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 gh->msg_type = GSM48_MT_CC_RELEASE;
2743
2744 trans->callref = 0;
2745
2746 gsm48_stop_cc_timer(trans);
2747 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2748
2749 /* cause */
2750 if (rel->fields & MNCC_F_CAUSE)
2751 encode_cause(msg, 0, &rel->cause);
2752 /* facility */
2753 if (rel->fields & MNCC_F_FACILITY)
2754 encode_facility(msg, 0, &rel->facility);
2755 /* user-user */
2756 if (rel->fields & MNCC_F_USERUSER)
2757 encode_useruser(msg, 0, &rel->useruser);
2758
Harald Weltedcaf5652009-07-23 18:56:43 +02002759 trans->cc.T308_second = 0;
2760 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761
Harald Weltedcaf5652009-07-23 18:56:43 +02002762 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2764
Harald Welte39e2ead2009-07-23 21:13:03 +02002765 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2769{
2770 struct gsm48_hdr *gh = msgb_l3(msg);
2771 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2772 struct tlv_parsed tp;
2773 struct gsm_mncc rel;
2774 int rc = 0;
2775
2776 gsm48_stop_cc_timer(trans);
2777
2778 memset(&rel, 0, sizeof(struct gsm_mncc));
2779 rel.callref = trans->callref;
2780 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2781 /* cause */
2782 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2783 rel.fields |= MNCC_F_CAUSE;
2784 decode_cause(&rel.cause,
2785 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2786 }
2787 /* facility */
2788 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2789 rel.fields |= MNCC_F_FACILITY;
2790 decode_facility(&rel.facility,
2791 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2792 }
2793 /* user-user */
2794 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2795 rel.fields |= MNCC_F_USERUSER;
2796 decode_useruser(&rel.useruser,
2797 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2798 }
2799 /* ss-version */
2800 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2801 rel.fields |= MNCC_F_SSVERSION;
2802 decode_ssversion(&rel.ssversion,
2803 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2804 }
2805
2806 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002807 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002809 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 MNCC_REJ_IND, &rel);
2811 break;
2812 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002813 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814 MNCC_REL_CNF, &rel);
2815 break;
2816 default:
Harald Welte596fed42009-07-23 19:06:52 +02002817 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 MNCC_REL_IND, &rel);
2819 }
2820 }
2821
2822 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002823 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002824
2825 return rc;
2826}
2827
2828static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2829{
2830 struct gsm_mncc *rel = arg;
2831 struct msgb *msg = gsm48_msgb_alloc();
2832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2833
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2835
2836 trans->callref = 0;
2837
2838 gsm48_stop_cc_timer(trans);
2839
2840 /* cause */
2841 if (rel->fields & MNCC_F_CAUSE)
2842 encode_cause(msg, 0, &rel->cause);
2843 /* facility */
2844 if (rel->fields & MNCC_F_FACILITY)
2845 encode_facility(msg, 0, &rel->facility);
2846 /* user-user */
2847 if (rel->fields & MNCC_F_USERUSER)
2848 encode_useruser(msg, 0, &rel->useruser);
2849
Harald Weltedcaf5652009-07-23 18:56:43 +02002850 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851
Harald Welte39e2ead2009-07-23 21:13:03 +02002852 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853}
2854
2855static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2856{
2857 struct gsm48_hdr *gh = msgb_l3(msg);
2858 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2859 struct tlv_parsed tp;
2860 struct gsm_mncc fac;
2861
2862 memset(&fac, 0, sizeof(struct gsm_mncc));
2863 fac.callref = trans->callref;
2864 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2865 /* facility */
2866 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2867 fac.fields |= MNCC_F_FACILITY;
2868 decode_facility(&fac.facility,
2869 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2870 }
2871 /* ss-version */
2872 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2873 fac.fields |= MNCC_F_SSVERSION;
2874 decode_ssversion(&fac.ssversion,
2875 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2876 }
2877
Harald Welte596fed42009-07-23 19:06:52 +02002878 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2882{
2883 struct gsm_mncc *fac = 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_FACILITY;
2888
2889 /* facility */
2890 encode_facility(msg, 1, &fac->facility);
2891
Harald Welte39e2ead2009-07-23 21:13:03 +02002892 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893}
2894
2895static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2896{
2897 struct gsm_mncc hold;
2898
2899 memset(&hold, 0, sizeof(struct gsm_mncc));
2900 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002901 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902}
2903
2904static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2905{
2906 struct msgb *msg = gsm48_msgb_alloc();
2907 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2908
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2910
Harald Welte39e2ead2009-07-23 21:13:03 +02002911 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912}
2913
2914static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2915{
2916 struct gsm_mncc *hold_rej = arg;
2917 struct msgb *msg = gsm48_msgb_alloc();
2918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2919
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2921
2922 /* cause */
2923 if (hold_rej->fields & MNCC_F_CAUSE)
2924 encode_cause(msg, 1, &hold_rej->cause);
2925 else
2926 encode_cause(msg, 1, &default_cause);
2927
Harald Welte39e2ead2009-07-23 21:13:03 +02002928 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929}
2930
2931static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2932{
2933 struct gsm_mncc retrieve;
2934
2935 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2936 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002937 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2938 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939}
2940
2941static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2942{
2943 struct msgb *msg = gsm48_msgb_alloc();
2944 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2945
Harald Welte4bfdfe72009-06-10 23:11:52 +08002946 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2947
Harald Welte39e2ead2009-07-23 21:13:03 +02002948 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949}
2950
2951static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2952{
2953 struct gsm_mncc *retrieve_rej = arg;
2954 struct msgb *msg = gsm48_msgb_alloc();
2955 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2956
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2958
2959 /* cause */
2960 if (retrieve_rej->fields & MNCC_F_CAUSE)
2961 encode_cause(msg, 1, &retrieve_rej->cause);
2962 else
2963 encode_cause(msg, 1, &default_cause);
2964
Harald Welte39e2ead2009-07-23 21:13:03 +02002965 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966}
2967
2968static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2969{
2970 struct gsm48_hdr *gh = msgb_l3(msg);
2971 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2972 struct tlv_parsed tp;
2973 struct gsm_mncc dtmf;
2974
2975 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2976 dtmf.callref = trans->callref;
2977 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2978 /* keypad facility */
2979 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2980 dtmf.fields |= MNCC_F_KEYPAD;
2981 decode_keypad(&dtmf.keypad,
2982 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2983 }
2984
Harald Welte596fed42009-07-23 19:06:52 +02002985 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986}
2987
2988static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2989{
2990 struct gsm_mncc *dtmf = arg;
2991 struct msgb *msg = gsm48_msgb_alloc();
2992 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2993
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2995
2996 /* keypad */
2997 if (dtmf->fields & MNCC_F_KEYPAD)
2998 encode_keypad(msg, dtmf->keypad);
2999
Harald Welte39e2ead2009-07-23 21:13:03 +02003000 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003001}
3002
3003static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3004{
3005 struct gsm_mncc *dtmf = arg;
3006 struct msgb *msg = gsm48_msgb_alloc();
3007 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3008
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3010
3011 /* cause */
3012 if (dtmf->fields & MNCC_F_CAUSE)
3013 encode_cause(msg, 1, &dtmf->cause);
3014 else
3015 encode_cause(msg, 1, &default_cause);
3016
Harald Welte39e2ead2009-07-23 21:13:03 +02003017 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018}
3019
3020static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3021{
3022 struct msgb *msg = gsm48_msgb_alloc();
3023 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3024
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3026
Harald Welte39e2ead2009-07-23 21:13:03 +02003027 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028}
3029
3030static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3031{
3032 struct gsm_mncc dtmf;
3033
3034 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3035 dtmf.callref = trans->callref;
3036
Harald Welte596fed42009-07-23 19:06:52 +02003037 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038}
3039
3040static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3041{
3042 struct gsm48_hdr *gh = msgb_l3(msg);
3043 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3044 struct tlv_parsed tp;
3045 struct gsm_mncc modify;
3046
3047 memset(&modify, 0, sizeof(struct gsm_mncc));
3048 modify.callref = trans->callref;
3049 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3050 /* bearer capability */
3051 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3052 modify.fields |= MNCC_F_BEARER_CAP;
3053 decode_bearer_cap(&modify.bearer_cap,
3054 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3055 }
3056
3057 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3058
Harald Welte596fed42009-07-23 19:06:52 +02003059 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060}
3061
3062static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3063{
3064 struct gsm_mncc *modify = arg;
3065 struct msgb *msg = gsm48_msgb_alloc();
3066 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3067
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 gh->msg_type = GSM48_MT_CC_MODIFY;
3069
3070 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3071
3072 /* bearer capability */
3073 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3074
3075 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3076
Harald Welte39e2ead2009-07-23 21:13:03 +02003077 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078}
3079
3080static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3081{
3082 struct gsm48_hdr *gh = msgb_l3(msg);
3083 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3084 struct tlv_parsed tp;
3085 struct gsm_mncc modify;
3086
3087 gsm48_stop_cc_timer(trans);
3088
3089 memset(&modify, 0, sizeof(struct gsm_mncc));
3090 modify.callref = trans->callref;
3091 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3092 /* bearer capability */
3093 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3094 modify.fields |= MNCC_F_BEARER_CAP;
3095 decode_bearer_cap(&modify.bearer_cap,
3096 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3097 }
3098
3099 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3100
Harald Welte596fed42009-07-23 19:06:52 +02003101 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003102}
3103
3104static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3105{
3106 struct gsm_mncc *modify = arg;
3107 struct msgb *msg = gsm48_msgb_alloc();
3108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3109
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3111
3112 /* bearer capability */
3113 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3114
3115 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3116
Harald Welte39e2ead2009-07-23 21:13:03 +02003117 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003118}
3119
3120static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3121{
3122 struct gsm48_hdr *gh = msgb_l3(msg);
3123 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3124 struct tlv_parsed tp;
3125 struct gsm_mncc modify;
3126
3127 gsm48_stop_cc_timer(trans);
3128
3129 memset(&modify, 0, sizeof(struct gsm_mncc));
3130 modify.callref = trans->callref;
3131 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3132 /* bearer capability */
3133 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3134 modify.fields |= GSM48_IE_BEARER_CAP;
3135 decode_bearer_cap(&modify.bearer_cap,
3136 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3137 }
3138 /* cause */
3139 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3140 modify.fields |= MNCC_F_CAUSE;
3141 decode_cause(&modify.cause,
3142 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3143 }
3144
3145 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3146
Harald Welte596fed42009-07-23 19:06:52 +02003147 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148}
3149
3150static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3151{
3152 struct gsm_mncc *modify = arg;
3153 struct msgb *msg = gsm48_msgb_alloc();
3154 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3155
Harald Welte4bfdfe72009-06-10 23:11:52 +08003156 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3157
3158 /* bearer capability */
3159 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3160 /* cause */
3161 encode_cause(msg, 1, &modify->cause);
3162
3163 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3164
Harald Welte39e2ead2009-07-23 21:13:03 +02003165 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166}
3167
3168static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3169{
3170 struct gsm_mncc *notify = arg;
3171 struct msgb *msg = gsm48_msgb_alloc();
3172 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3173
Harald Welte4bfdfe72009-06-10 23:11:52 +08003174 gh->msg_type = GSM48_MT_CC_NOTIFY;
3175
3176 /* notify */
3177 encode_notify(msg, notify->notify);
3178
Harald Welte39e2ead2009-07-23 21:13:03 +02003179 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180}
3181
3182static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3183{
3184 struct gsm48_hdr *gh = msgb_l3(msg);
3185 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3186// struct tlv_parsed tp;
3187 struct gsm_mncc notify;
3188
3189 memset(&notify, 0, sizeof(struct gsm_mncc));
3190 notify.callref = trans->callref;
3191// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3192 if (payload_len >= 1)
3193 decode_notify(&notify.notify, gh->data);
3194
Harald Welte596fed42009-07-23 19:06:52 +02003195 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003196}
3197
3198static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3199{
3200 struct gsm_mncc *user = arg;
3201 struct msgb *msg = gsm48_msgb_alloc();
3202 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3203
Harald Welte4bfdfe72009-06-10 23:11:52 +08003204 gh->msg_type = GSM48_MT_CC_USER_INFO;
3205
3206 /* user-user */
3207 if (user->fields & MNCC_F_USERUSER)
3208 encode_useruser(msg, 1, &user->useruser);
3209 /* more data */
3210 if (user->more)
3211 encode_more(msg);
3212
Harald Welte39e2ead2009-07-23 21:13:03 +02003213 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214}
3215
3216static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3217{
3218 struct gsm48_hdr *gh = msgb_l3(msg);
3219 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3220 struct tlv_parsed tp;
3221 struct gsm_mncc user;
3222
3223 memset(&user, 0, sizeof(struct gsm_mncc));
3224 user.callref = trans->callref;
3225 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3226 /* user-user */
3227 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3228 user.fields |= MNCC_F_USERUSER;
3229 decode_useruser(&user.useruser,
3230 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3231 }
3232 /* more data */
3233 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3234 user.more = 1;
3235
Harald Welte596fed42009-07-23 19:06:52 +02003236 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003237}
3238
3239static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3240{
3241 struct gsm_mncc *mode = arg;
Harald Welte13cac662009-07-29 12:10:35 +02003242 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243
Harald Welte13cac662009-07-29 12:10:35 +02003244 rc = gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3245 if (rc < 0)
3246 return rc;
3247
3248 /* FIXME: we not only need to do this after mode modify, but
3249 * also after channel activation */
3250 if (is_ipaccess_bts(trans->lchan->ts->trx->bts) &&
3251 mode->lchan_mode != GSM48_CMODE_SIGN)
3252 rc = rsl_ipacc_bind(trans->lchan);
3253
3254 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255}
3256
3257static struct downstate {
3258 u_int32_t states;
3259 int type;
3260 int (*rout) (struct gsm_trans *trans, void *arg);
3261} downstatelist[] = {
3262 /* mobile originating call establishment */
3263 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3264 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3265 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3266 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3267 {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 */
3268 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3269 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3270 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3271 /* mobile terminating call establishment */
3272 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3273 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3274 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3275 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3276 /* signalling during call */
3277 {SBIT(GSM_CSTATE_ACTIVE),
3278 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3279 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3280 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3281 {ALL_STATES,
3282 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3283 {ALL_STATES,
3284 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3285 {ALL_STATES,
3286 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3287 {SBIT(GSM_CSTATE_ACTIVE),
3288 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3289 {SBIT(GSM_CSTATE_ACTIVE),
3290 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3291 {SBIT(GSM_CSTATE_ACTIVE),
3292 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3293 {SBIT(GSM_CSTATE_ACTIVE),
3294 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3295 {SBIT(GSM_CSTATE_ACTIVE),
3296 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3297 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3298 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3299 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3300 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3301 {SBIT(GSM_CSTATE_ACTIVE),
3302 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3303 /* clearing */
3304 {SBIT(GSM_CSTATE_INITIATED),
3305 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3306 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3307 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3308 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3309 MNCC_REL_REQ, gsm48_cc_tx_release},
3310 /* special */
3311 {ALL_STATES,
3312 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3313};
3314
3315#define DOWNSLLEN \
3316 (sizeof(downstatelist) / sizeof(struct downstate))
3317
3318
3319int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3320{
Harald Welte1a6f7982009-08-09 18:52:33 +02003321 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003322 struct gsm_trans *trans = NULL, *transt;
3323 struct gsm_subscriber *subscr;
Harald Welte1a6f7982009-08-09 18:52:33 +02003324 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003325 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003326 struct gsm_mncc *data = arg, rel;
3327
3328 /* handle special messages */
3329 switch(msg_type) {
3330 case MNCC_BRIDGE:
3331 return tch_bridge(net, arg);
3332 case MNCC_FRAME_DROP:
3333 return tch_recv(net, arg, 0);
3334 case MNCC_FRAME_RECV:
3335 return tch_recv(net, arg, 1);
3336 case GSM_TRAU_FRAME:
3337 return tch_frame(net, arg);
3338 }
3339
3340 memset(&rel, 0, sizeof(struct gsm_mncc));
3341 rel.callref = data->callref;
3342
3343 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003344 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003345
3346 /* Callref unknown */
3347 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003348 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003349 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3350 "Received '%s' from MNCC with "
3351 "unknown callref %d\n", data->called.number,
3352 get_mncc_name(msg_type), data->callref);
3353 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003354 return mncc_release_ind(net, NULL, data->callref,
3355 GSM48_CAUSE_LOC_PRN_S_LU,
3356 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003357 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003358 if (!data->called.number[0] && !data->imsi[0]) {
3359 DEBUGP(DCC, "(bts - trx - ts - ti) "
3360 "Received '%s' from MNCC with "
3361 "no number or IMSI\n", get_mncc_name(msg_type));
3362 /* Invalid number */
3363 return mncc_release_ind(net, NULL, data->callref,
3364 GSM48_CAUSE_LOC_PRN_S_LU,
3365 GSM48_CC_CAUSE_INV_NR_FORMAT);
3366 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003367 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003368 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003369 subscr = subscr_get_by_extension(net,
3370 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003371 else
Harald Welte9176bd42009-07-23 18:46:00 +02003372 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003373 /* If subscriber is not found */
3374 if (!subscr) {
3375 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3376 "Received '%s' from MNCC with "
3377 "unknown subscriber %s\n", data->called.number,
3378 get_mncc_name(msg_type), data->called.number);
3379 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003380 return mncc_release_ind(net, NULL, data->callref,
3381 GSM48_CAUSE_LOC_PRN_S_LU,
3382 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003383 }
3384 /* If subscriber is not "attached" */
3385 if (!subscr->lac) {
3386 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3387 "Received '%s' from MNCC with "
3388 "detached subscriber %s\n", data->called.number,
3389 get_mncc_name(msg_type), data->called.number);
3390 subscr_put(subscr);
3391 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003392 return mncc_release_ind(net, NULL, data->callref,
3393 GSM48_CAUSE_LOC_PRN_S_LU,
3394 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003395 }
3396 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003397 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3398 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003399 DEBUGP(DCC, "No memory for trans.\n");
3400 subscr_put(subscr);
3401 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003402 mncc_release_ind(net, NULL, data->callref,
3403 GSM48_CAUSE_LOC_PRN_S_LU,
3404 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003405 return -ENOMEM;
3406 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003407 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003408 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003409 /* If subscriber has no lchan */
3410 if (!lchan) {
3411 /* find transaction with this subscriber already paging */
3412 llist_for_each_entry(transt, &net->trans_list, entry) {
3413 /* Transaction of our lchan? */
3414 if (transt == trans ||
3415 transt->subscr != subscr)
3416 continue;
3417 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3418 "Received '%s' from MNCC with "
3419 "unallocated channel, paging already "
3420 "started.\n", bts->nr,
3421 data->called.number,
3422 get_mncc_name(msg_type));
3423 return 0;
3424 }
3425 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003426 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003427 /* Trigger paging */
3428 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3429 setup_trig_pag_evt, subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003430 return 0;
3431 }
3432 /* Assign lchan */
3433 trans->lchan = lchan;
3434 use_lchan(lchan);
3435 }
3436 lchan = trans->lchan;
3437
3438 /* if paging did not respond yet */
3439 if (!lchan) {
3440 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3441 "Received '%s' from MNCC in paging state\n",
3442 (trans->subscr)?(trans->subscr->extension):"-",
3443 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003444 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3445 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446 if (msg_type == MNCC_REL_REQ)
3447 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3448 else
3449 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3450 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003451 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003452 return rc;
3453 }
3454
3455 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3456 "Received '%s' from MNCC in state %d (%s)\n",
3457 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3458 trans->transaction_id,
3459 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003460 get_mncc_name(msg_type), trans->cc.state,
3461 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003462
3463 /* Find function for current state and message */
3464 for (i = 0; i < DOWNSLLEN; i++)
3465 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003466 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003467 break;
3468 if (i == DOWNSLLEN) {
3469 DEBUGP(DCC, "Message unhandled at this state.\n");
3470 return 0;
3471 }
3472
3473 rc = downstatelist[i].rout(trans, arg);
3474
3475 return rc;
3476}
3477
3478
3479static struct datastate {
3480 u_int32_t states;
3481 int type;
3482 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3483} datastatelist[] = {
3484 /* mobile originating call establishment */
3485 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3486 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3487 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3488 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3489 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3490 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3491 /* mobile terminating call establishment */
3492 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3493 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3494 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3495 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3496 {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 */
3497 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3498 /* signalling during call */
3499 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3500 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3501 {SBIT(GSM_CSTATE_ACTIVE),
3502 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3503 {ALL_STATES,
3504 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3505 {ALL_STATES,
3506 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3507 {ALL_STATES,
3508 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3509 {SBIT(GSM_CSTATE_ACTIVE),
3510 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3511 {SBIT(GSM_CSTATE_ACTIVE),
3512 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3513 {SBIT(GSM_CSTATE_ACTIVE),
3514 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3515 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3516 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3517 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3518 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3519 {SBIT(GSM_CSTATE_ACTIVE),
3520 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3521 /* clearing */
3522 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3523 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3524 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3525 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3526 {ALL_STATES, /* 5.4.3.4 */
3527 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3528};
3529
3530#define DATASLLEN \
3531 (sizeof(datastatelist) / sizeof(struct datastate))
3532
Harald Welte4bc90a12008-12-27 16:32:52 +00003533static int gsm0408_rcv_cc(struct msgb *msg)
3534{
3535 struct gsm48_hdr *gh = msgb_l3(msg);
3536 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003537 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003538 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003539 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003540 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003541
Harald Welte4bfdfe72009-06-10 23:11:52 +08003542 if (msg_type & 0x80) {
3543 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3544 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003545 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003546
3547 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003548 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003549
Harald Welte6f5aee02009-07-23 21:21:14 +02003550 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003551 "Received '%s' from MS in state %d (%s)\n",
3552 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3553 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003554 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003555 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003556
3557 /* Create transaction */
3558 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003559 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003560 "creating new trans.\n", transaction_id);
3561 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003562 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3563 transaction_id, new_callref++);
3564 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003565 DEBUGP(DCC, "No memory for trans.\n");
3566 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003567 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003568 GSM48_MT_CC_RELEASE_COMPL);
3569 return -ENOMEM;
3570 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003571 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003572 trans->lchan = lchan;
3573 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003574 }
3575
3576 /* find function for current state and message */
3577 for (i = 0; i < DATASLLEN; i++)
3578 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003579 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580 break;
3581 if (i == DATASLLEN) {
3582 DEBUGP(DCC, "Message unhandled at this state.\n");
3583 return 0;
3584 }
3585
3586 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003587
3588 return rc;
3589}
3590
Harald Welte52b1f982008-12-23 20:25:15 +00003591/* 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 +02003592int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003593{
3594 struct gsm48_hdr *gh = msgb_l3(msg);
3595 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003596 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003597
3598 switch (pdisc) {
3599 case GSM48_PDISC_CC:
3600 rc = gsm0408_rcv_cc(msg);
3601 break;
3602 case GSM48_PDISC_MM:
3603 rc = gsm0408_rcv_mm(msg);
3604 break;
3605 case GSM48_PDISC_RR:
3606 rc = gsm0408_rcv_rr(msg);
3607 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003608 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003609 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003610 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003611 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003612 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003613 case GSM48_PDISC_NC_SS: /* mobile-originated USSD */
3614 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003615 pdisc);
3616 break;
3617 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003618 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003619 pdisc);
3620 break;
3621 }
3622
3623 return rc;
3624}
Harald Welte8470bf22008-12-25 23:28:35 +00003625
Harald Welte4bfdfe72009-06-10 23:11:52 +08003626/* dequeue messages to layer 4 */
3627int bsc_upqueue(struct gsm_network *net)
3628{
3629 struct gsm_mncc *mncc;
3630 struct msgb *msg;
3631 int work = 0;
3632
3633 if (net)
3634 while ((msg = msgb_dequeue(&net->upqueue))) {
3635 mncc = (struct gsm_mncc *)msg->data;
3636 if (net->mncc_recv)
3637 net->mncc_recv(net, mncc->msg_type, mncc);
3638 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003639 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003640 }
3641
3642 return work;
3643}
Harald Weltedcaf5652009-07-23 18:56:43 +02003644
Harald Welte805f6442009-07-28 18:25:29 +02003645/*
3646 * This will be ran by the linker when loading the DSO. We use it to
3647 * do system initialization, e.g. registration of signal handlers.
3648 */
3649static __attribute__((constructor)) void on_dso_load_0408(void)
3650{
Harald Welte805f6442009-07-28 18:25:29 +02003651 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3652 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3653}