blob: 2b9dcc2383d688785aa94aedd442d036d5cee0cd [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 Weltef1dae192009-12-14 22:24:28 +010035#include <openbsc/bitvec.h>
Harald Welte7584aea2009-02-11 11:44:12 +000036#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Daniel Willmanneea93372009-08-13 03:42:07 +020039#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020050#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020051#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020052#include <openbsc/ussd.h>
Harald Welte52b1f982008-12-23 20:25:15 +000053
Harald Welte0c389302009-06-10 12:08:54 +080054#define GSM_MAX_FACILITY 128
55#define GSM_MAX_SSVERSION 128
56#define GSM_MAX_USERUSER 128
57
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +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 Welte3c7dc6e2009-11-29 19:07:28 +0100168int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +0000169{
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100170 struct gsm48_hdr *gh = msgb_l3(msg);
171 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
172 u_int8_t *data = gh->data;
Harald Weltef1dae192009-12-14 22:24:28 +0100173 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
174 struct bitvec *nbv = &bts->si_common.neigh_list;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100175
176 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
177 return -EINVAL;
Harald Weltef7c43522009-06-09 20:24:21 +0000178
179 if (data[0] & 0x80)
180 rep->flags |= MEAS_REP_F_BA1;
181 if (data[0] & 0x40)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100182 rep->flags |= MEAS_REP_F_UL_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200183 if ((data[1] & 0x40) == 0x00)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100184 rep->flags |= MEAS_REP_F_DL_VALID;
Harald Weltef7c43522009-06-09 20:24:21 +0000185
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100186 rep->dl.full.rx_lev = data[0] & 0x3f;
187 rep->dl.sub.rx_lev = data[1] & 0x3f;
188 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
189 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
190
Harald Weltea2f74b82009-12-16 16:49:21 +0100191 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
Harald Welte51cec5f2009-12-19 16:45:29 +0100192 if (rep->num_cell < 1 || rep->num_cell > 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100193 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000194
195 /* an encoding nightmare in perfection */
196
Harald Weltea2f74b82009-12-16 16:49:21 +0100197 rep->cell[0].rxlev = data[3] & 0x3f;
198 rep->cell[0].arfcn = bitvec_get_nth_set_bit(nbv, data[4] >> 2);
Harald Welte73ddaed2009-12-16 23:29:12 +0100199 rep->cell[0].bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
Harald Weltef7c43522009-06-09 20:24:21 +0000200 if (rep->num_cell < 2)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100201 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000202
Harald Weltea2f74b82009-12-16 16:49:21 +0100203 rep->cell[1].rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
204 rep->cell[1].arfcn = bitvec_get_nth_set_bit(nbv, (data[6] >> 2) & 0x1f);
205 rep->cell[1].bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
Harald Weltef7c43522009-06-09 20:24:21 +0000206 if (rep->num_cell < 3)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100207 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000208
Harald Weltea2f74b82009-12-16 16:49:21 +0100209 rep->cell[2].rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
210 rep->cell[2].arfcn = bitvec_get_nth_set_bit(nbv, (data[8] >> 1) & 0x1f);
211 rep->cell[2].bsic = ((data[8] & 0x01) << 6) | (data[9] >> 3);
Harald Weltef7c43522009-06-09 20:24:21 +0000212 if (rep->num_cell < 4)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100213 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000214
Harald Weltea2f74b82009-12-16 16:49:21 +0100215 rep->cell[3].rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
216 rep->cell[3].arfcn = bitvec_get_nth_set_bit(nbv, data[10] & 0x1f);
217 rep->cell[3].bsic = data[11] >> 2;
Harald Weltef7c43522009-06-09 20:24:21 +0000218 if (rep->num_cell < 5)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100219 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000220
Harald Weltea2f74b82009-12-16 16:49:21 +0100221 rep->cell[4].rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
Harald Weltef1dae192009-12-14 22:24:28 +0100222 rep->cell[4].arfcn = bitvec_get_nth_set_bit(nbv,
Harald Weltea2f74b82009-12-16 16:49:21 +0100223 ((data[12] & 0xf) << 1) | (data[13] >> 7));
224 rep->cell[4].bsic = (data[13] >> 1) & 0x3f;
Harald Weltef7c43522009-06-09 20:24:21 +0000225 if (rep->num_cell < 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100226 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000227
Harald Weltea2f74b82009-12-16 16:49:21 +0100228 rep->cell[5].rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
Harald Weltef1dae192009-12-14 22:24:28 +0100229 rep->cell[5].arfcn = bitvec_get_nth_set_bit(nbv,
Harald Weltea2f74b82009-12-16 16:49:21 +0100230 ((data[14] & 0x07) << 2) | (data[15] >> 6));
231 rep->cell[5].bsic = data[15] & 0x3f;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100232
233 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000234}
235
Holger Freytherd51524f2009-06-09 08:27:07 +0000236int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000237static int gsm48_tx_simple(struct gsm_lchan *lchan,
238 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000239static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000240
Harald Welte52b1f982008-12-23 20:25:15 +0000241struct gsm_lai {
242 u_int16_t mcc;
243 u_int16_t mnc;
244 u_int16_t lac;
245};
246
Harald Welte4bfdfe72009-06-10 23:11:52 +0800247static u_int32_t new_callref = 0x80000001;
248
Holger Freyther73487a22008-12-31 18:53:57 +0000249static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
250 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000251{
252 if (!subscriber)
253 return 0;
254
Holger Freyther73487a22008-12-31 18:53:57 +0000255 /*
256 * Do not send accept yet as more information should arrive. Some
257 * phones will not send us the information and we will have to check
258 * what we want to do with that.
259 */
260 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
261 return 0;
262
Jan Luebbe06513f22009-08-12 12:48:00 +0200263 switch (subscriber->net->auth_policy) {
264 case GSM_AUTH_POLICY_CLOSED:
265 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200266 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200267 if (subscriber->authorized)
268 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200269 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200270 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000271 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200272 default:
273 return 0;
274 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000275}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000276
Holger Freyther73487a22008-12-31 18:53:57 +0000277static void release_loc_updating_req(struct gsm_lchan *lchan)
278{
Harald Welte179f0642008-12-31 23:59:18 +0000279 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000280 return;
281
Harald Welteff117a82009-05-23 05:22:08 +0000282 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200283 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000284 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000285 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000286}
287
288static void allocate_loc_updating_req(struct gsm_lchan *lchan)
289{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000290 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000291 release_loc_updating_req(lchan);
292
Harald Welte470ec292009-06-26 20:25:23 +0200293 lchan->loc_operation = talloc_zero(tall_locop_ctx,
294 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000295}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000296
Holger Freytherd51524f2009-06-09 08:27:07 +0000297static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
298{
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200300 int rc;
301
Holger Freytherd51524f2009-06-09 08:27:07 +0000302 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000303 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200304 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100305 if (lchan->ts->trx->bts->network->send_mm_info) {
306 /* send MM INFO with network name */
307 rc = gsm48_tx_mm_info(msg->lchan);
308 }
Harald Welteb83d9382009-12-12 21:00:48 +0100309
Harald Welteee5ad162009-08-09 19:07:00 +0200310 /* call subscr_update after putting the loc_upd_acc
311 * in the transmit queue, since S_SUBSCR_ATTACHED might
312 * trigger further action like SMS delivery */
313 subscr_update(lchan->subscr, msg->trx->bts,
314 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100315 /* try to close channel ASAP */
316 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200317 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000318 }
319
320 return 0;
321}
322
Holger Freyther7c19f742009-06-06 13:54:35 +0000323static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
324 void *handler_data, void *signal_data)
325{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800326 struct gsm_trans *trans, *temp;
327
Holger Freyther7c19f742009-06-06 13:54:35 +0000328 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
329 return 0;
330
331 /*
332 * Cancel any outstanding location updating request
333 * operation taking place on the lchan.
334 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200335 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200336 if (!lchan)
337 return 0;
338
Holger Freyther7c19f742009-06-06 13:54:35 +0000339 release_loc_updating_req(lchan);
340
Harald Welte4bfdfe72009-06-10 23:11:52 +0800341 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200342 /* FIXME: this is not neccessarily the right thing to do, we should
343 * only set trans->lchan to NULL and wait for another lchan to be
344 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800345 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
346 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200347 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800348 }
349
Holger Freyther7c19f742009-06-06 13:54:35 +0000350 return 0;
351}
352
Harald Welte09e38af2009-02-16 22:52:23 +0000353static const char bcd_num_digits[] = {
354 '0', '1', '2', '3', '4', '5', '6', '7',
355 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
356};
357
Harald Welte0c389302009-06-10 12:08:54 +0800358/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
359int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
360 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000361{
362 u_int8_t in_len = bcd_lv[0];
363 int i;
364
Harald Welte0c389302009-06-10 12:08:54 +0800365 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000366 /* lower nibble */
367 output_len--;
368 if (output_len <= 1)
369 break;
370 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
371
372 /* higher nibble */
373 output_len--;
374 if (output_len <= 1)
375 break;
376 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
377 }
378 if (output_len >= 1)
379 *output++ = '\0';
380
Harald Welte0c389302009-06-10 12:08:54 +0800381 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000382}
383
384/* convert a single ASCII character to call-control BCD */
385static int asc_to_bcd(const char asc)
386{
387 int i;
388
389 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
390 if (bcd_num_digits[i] == asc)
391 return i;
392 }
393 return -EINVAL;
394}
395
Harald Welte0c389302009-06-10 12:08:54 +0800396/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000397int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800398 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000399{
400 int in_len = strlen(input);
401 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800402 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000403
404 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800405 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000406 if (in_len % 2)
407 bcd_lv[0]++;
408
Harald Welte0c389302009-06-10 12:08:54 +0800409 if (bcd_lv[0] > max_len)
410 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000411
412 for (i = 0; i < in_len; i++) {
413 int rc = asc_to_bcd(input[i]);
414 if (rc < 0)
415 return rc;
416 if (i % 2 == 0)
417 *bcd_cur = rc;
418 else
419 *bcd_cur++ |= (rc << 4);
420 }
421 /* append padding nibble in case of odd length */
422 if (i % 2)
423 *bcd_cur++ |= 0xf0;
424
425 /* return how many bytes we used */
426 return (bcd_cur - bcd_lv);
427}
428
Harald Welte0c389302009-06-10 12:08:54 +0800429/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800430static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800431 const u_int8_t *lv)
432{
433 u_int8_t in_len = lv[0];
434 int i, s;
435
436 if (in_len < 1)
437 return -EINVAL;
438
Harald Welte4bfdfe72009-06-10 23:11:52 +0800439 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800440
441 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442 bcap->transfer = lv[1] & 0x07;
443 bcap->mode = (lv[1] & 0x08) >> 3;
444 bcap->coding = (lv[1] & 0x10) >> 4;
445 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800446
Harald Welte09b7e7f2009-12-12 21:36:53 +0100447 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
448 i = 1;
449 s = 0;
450 while(!(lv[i] & 0x80)) {
451 i++; /* octet 3a etc */
452 if (in_len < i)
453 return 0;
454 bcap->speech_ver[s++] = lv[i] & 0x0f;
455 bcap->speech_ver[s] = -1; /* end of list */
456 if (i == 2) /* octet 3a */
457 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
458 if (s == 7) /* maximum speech versions + end of list */
459 return 0;
460 }
461 } else {
462 i = 1;
463 while (!(lv[i] & 0x80)) {
464 i++; /* octet 3a etc */
465 if (in_len < i)
466 return 0;
467 /* ignore them */
468 }
469 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800470 }
471
472 return 0;
473}
474
475/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800476static int encode_bearer_cap(struct msgb *msg, int lv_only,
477 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800478{
479 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100480 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800481
Harald Welte4bfdfe72009-06-10 23:11:52 +0800482 lv[1] = bcap->transfer;
483 lv[1] |= bcap->mode << 3;
484 lv[1] |= bcap->coding << 4;
485 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800486
Harald Welte09b7e7f2009-12-12 21:36:53 +0100487 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
488 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
489 i++; /* octet 3a etc */
490 lv[i] = bcap->speech_ver[s];
491 if (i == 2) /* octet 3a */
492 lv[i] |= bcap->speech_ctm << 5;
493 }
494 lv[i] |= 0x80; /* last IE of octet 3 etc */
495 } else {
496 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800497 }
Harald Welte0c389302009-06-10 12:08:54 +0800498
499 lv[0] = i;
500 if (lv_only)
501 msgb_lv_put(msg, lv[0], lv+1);
502 else
503 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
504
505 return 0;
506}
507
508/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800509static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800510{
511 u_int8_t in_len = lv[0];
512
513 if (in_len < 1)
514 return -EINVAL;
515
516 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800517 ccap->dtmf = lv[1] & 0x01;
518 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800519
520 return 0;
521}
522
523/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800524static int decode_called(struct gsm_mncc_number *called,
525 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800526{
527 u_int8_t in_len = lv[0];
528
529 if (in_len < 1)
530 return -EINVAL;
531
532 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800533 called->plan = lv[1] & 0x0f;
534 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800535
536 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800537 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800538
539 return 0;
540}
541
542/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800543static int encode_called(struct msgb *msg,
544 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800545{
546 u_int8_t lv[18];
547 int ret;
548
549 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800550 lv[1] = called->plan;
551 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800552
553 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800555 if (ret < 0)
556 return ret;
557
558 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
559
560 return 0;
561}
562
563/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800564static int encode_callerid(struct msgb *msg, int ie,
565 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800566{
567 u_int8_t lv[13];
568 int h_len = 1;
569 int ret;
570
571 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572 lv[1] = callerid->plan;
573 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800574
Harald Welte4bfdfe72009-06-10 23:11:52 +0800575 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800576 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800577 lv[2] = callerid->screen;
578 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800579 lv[2] |= 0x80;
580 h_len++;
581 } else
582 lv[1] |= 0x80;
583
584 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800585 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800586 if (ret < 0)
587 return ret;
588
589 msgb_tlv_put(msg, ie, lv[0], lv+1);
590
591 return 0;
592}
593
594/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800595static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800596 const u_int8_t *lv)
597{
598 u_int8_t in_len = lv[0];
599 int i;
600
601 if (in_len < 2)
602 return -EINVAL;
603
Harald Welte4bfdfe72009-06-10 23:11:52 +0800604 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800605
606 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800607 cause->location = lv[1] & 0x0f;
608 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800609
610 i = 1;
611 if (!(lv[i] & 0x80)) {
612 i++; /* octet 3a */
613 if (in_len < i+1)
614 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615 cause->rec = 1;
616 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800617
618 }
619 i++;
620
621 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800623 i++;
624
625 if (in_len < i) /* no diag */
626 return 0;
627
628 if (in_len - (i-1) > 32) /* maximum 32 octets */
629 return 0;
630
631 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800632 memcpy(cause->diag, lv + i, in_len - (i-1));
633 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800634
635 return 0;
636}
637
638/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800639static int encode_cause(struct msgb *msg, int lv_only,
640 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800641{
642 u_int8_t lv[32+4];
643 int i;
644
Harald Welte4bfdfe72009-06-10 23:11:52 +0800645 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800646 return -EINVAL;
647
648 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800649 lv[1] = cause->location;
650 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800651
652 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800653 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800654 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800656 }
657 lv[i] |= 0x80; /* end of octet 3 */
658
659 /* octet 4 */
660 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800662
663 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800664 if (cause->diag_len) {
665 memcpy(lv + i, cause->diag, cause->diag_len);
666 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800667 }
668
669 lv[0] = i;
670 if (lv_only)
671 msgb_lv_put(msg, lv[0], lv+1);
672 else
673 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
674
675 return 0;
676}
677
678/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800679static int encode_calling(struct msgb *msg,
680 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800681{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800683}
684
685/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800686static int encode_connected(struct msgb *msg,
687 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800688{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800689 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800690}
691
692/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800693static int encode_redirecting(struct msgb *msg,
694 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800695{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800696 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800697}
698
699/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800701 const u_int8_t *lv)
702{
703 u_int8_t in_len = lv[0];
704
705 if (in_len < 1)
706 return -EINVAL;
707
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800709 return -EINVAL;
710
Harald Welte4bfdfe72009-06-10 23:11:52 +0800711 memcpy(facility->info, lv+1, in_len);
712 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800713
714 return 0;
715}
716
717/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800718static int encode_facility(struct msgb *msg, int lv_only,
719 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800720{
721 u_int8_t lv[GSM_MAX_FACILITY + 1];
722
Harald Welte4bfdfe72009-06-10 23:11:52 +0800723 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800724 return -EINVAL;
725
Harald Welte4bfdfe72009-06-10 23:11:52 +0800726 memcpy(lv+1, facility->info, facility->len);
727 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800728 if (lv_only)
729 msgb_lv_put(msg, lv[0], lv+1);
730 else
731 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
732
733 return 0;
734}
735
736/* decode 'notify' */
737static int decode_notify(int *notify, const u_int8_t *v)
738{
739 *notify = v[0] & 0x7f;
740
741 return 0;
742}
743
744/* encode 'notify' */
745static int encode_notify(struct msgb *msg, int notify)
746{
747 msgb_v_put(msg, notify | 0x80);
748
749 return 0;
750}
751
752/* encode 'signal' */
753static int encode_signal(struct msgb *msg, int signal)
754{
755 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
756
757 return 0;
758}
759
760/* decode 'keypad' */
761static int decode_keypad(int *keypad, const u_int8_t *lv)
762{
763 u_int8_t in_len = lv[0];
764
765 if (in_len < 1)
766 return -EINVAL;
767
768 *keypad = lv[1] & 0x7f;
769
770 return 0;
771}
772
773/* encode 'keypad' */
774static int encode_keypad(struct msgb *msg, int keypad)
775{
776 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
777
778 return 0;
779}
780
781/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800782static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800783 const u_int8_t *lv)
784{
785 u_int8_t in_len = lv[0];
786
787 if (in_len < 2)
788 return -EINVAL;
789
Harald Welte4bfdfe72009-06-10 23:11:52 +0800790 progress->coding = (lv[1] & 0x60) >> 5;
791 progress->location = lv[1] & 0x0f;
792 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800793
794 return 0;
795}
796
797/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800798static int encode_progress(struct msgb *msg, int lv_only,
799 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800800{
801 u_int8_t lv[3];
802
803 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800804 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
805 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800806 if (lv_only)
807 msgb_lv_put(msg, lv[0], lv+1);
808 else
809 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
810
811 return 0;
812}
813
814/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800815static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800816 const u_int8_t *lv)
817{
818 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800819 char *info = uu->info;
820 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800821 int i;
822
823 if (in_len < 1)
824 return -EINVAL;
825
Harald Welte4bfdfe72009-06-10 23:11:52 +0800826 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800827
828 for (i = 2; i <= in_len; i++) {
829 info_len--;
830 if (info_len <= 1)
831 break;
832 *info++ = lv[i];
833 }
834 if (info_len >= 1)
835 *info++ = '\0';
836
837 return 0;
838}
839
840/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800841static int encode_useruser(struct msgb *msg, int lv_only,
842 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800843{
844 u_int8_t lv[GSM_MAX_USERUSER + 2];
845
Harald Welte4bfdfe72009-06-10 23:11:52 +0800846 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800847 return -EINVAL;
848
Harald Welte4bfdfe72009-06-10 23:11:52 +0800849 lv[0] = 1 + strlen(uu->info);
850 lv[1] = uu->proto;
851 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800852 if (lv_only)
853 msgb_lv_put(msg, lv[0], lv+1);
854 else
855 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
856
857 return 0;
858}
859
860/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800861static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800862 const u_int8_t *lv)
863{
864 u_int8_t in_len = lv[0];
865
Harald Welte4bfdfe72009-06-10 23:11:52 +0800866 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800867 return -EINVAL;
868
Harald Welte4bfdfe72009-06-10 23:11:52 +0800869 memcpy(ssv->info, lv + 1, in_len);
870 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800871
872 return 0;
873}
874
875/* encode 'more data' */
876static int encode_more(struct msgb *msg)
877{
878 u_int8_t *ie;
879
880 ie = msgb_put(msg, 1);
881 ie[0] = GSM48_IE_MORE_DATA;
882
883 return 0;
884}
885
Holger Freyther429e7762008-12-30 13:28:30 +0000886/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000887int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000888{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100889 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000890 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000891 struct gsm48_hdr *gh;
892
Harald Welte8470bf22008-12-25 23:28:35 +0000893 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000894
895 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
896 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000897 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000898 gh->data[0] = cause;
899
Harald Weltedb253af2008-12-30 17:56:55 +0000900 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100901
902 bts->network->stats.loc_upd_resp.reject++;
Harald Weltedb253af2008-12-30 17:56:55 +0000903
Harald Welte39e2ead2009-07-23 21:13:03 +0200904 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000905}
906
907/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000908int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000909{
Harald Welte8470bf22008-12-25 23:28:35 +0000910 struct gsm_bts *bts = lchan->ts->trx->bts;
911 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000912 struct gsm48_hdr *gh;
913 struct gsm48_loc_area_id *lai;
914 u_int8_t *mid;
915
Harald Welte8470bf22008-12-25 23:28:35 +0000916 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000917
918 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
919 gh->proto_discr = GSM48_PDISC_MM;
920 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
921
922 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000923 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000924 bts->network->network_code, bts->location_area_code);
925
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200926 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200927 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000928
929 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
930
Harald Welte24ff6ee2009-12-22 00:41:05 +0100931 bts->network->stats.loc_upd_resp.accept++;
932
Harald Welteb83d9382009-12-12 21:00:48 +0100933 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000934}
935
Harald Weltebf5e8df2009-02-03 12:59:45 +0000936/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000937static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
938{
939 struct msgb *msg = gsm48_msgb_alloc();
940 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000941
Harald Welte231ad4f2008-12-27 11:15:38 +0000942 msg->lchan = lchan;
943
944 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
945 gh->proto_discr = GSM48_PDISC_MM;
946 gh->msg_type = GSM48_MT_MM_ID_REQ;
947 gh->data[0] = id_type;
948
Harald Welte39e2ead2009-07-23 21:13:03 +0200949 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000950}
951
Harald Welte231ad4f2008-12-27 11:15:38 +0000952
Harald Weltebf5e8df2009-02-03 12:59:45 +0000953/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000954static int mm_rx_id_resp(struct msgb *msg)
955{
956 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000957 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200958 struct gsm_bts *bts = lchan->ts->trx->bts;
959 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000960 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200961 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000962
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200963 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000964 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000965 mi_type, mi_string);
966
Harald Welte7659de12009-12-13 12:39:18 +0100967 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
968
Harald Welte75a983f2008-12-27 21:34:06 +0000969 switch (mi_type) {
970 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200971 /* look up subscriber based on IMSI, create if not found */
972 if (!lchan->subscr) {
973 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200974 if (!lchan->subscr)
975 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200976 }
Holger Freyther73487a22008-12-31 18:53:57 +0000977 if (lchan->loc_operation)
978 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000979 break;
980 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000981 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000982 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200983 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000984 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200985 db_sync_equipment(&lchan->subscr->equipment);
986 }
Holger Freyther73487a22008-12-31 18:53:57 +0000987 if (lchan->loc_operation)
988 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000989 break;
990 }
Holger Freyther73487a22008-12-31 18:53:57 +0000991
992 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000993 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000994}
995
Harald Welte255539c2008-12-28 02:26:27 +0000996
997static void loc_upd_rej_cb(void *data)
998{
999 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +01001000 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +00001001
Holger Freyther73487a22008-12-31 18:53:57 +00001002 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +01001003 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001004 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001005}
1006
Holger Freytherb7193e42008-12-29 17:44:08 +00001007static void schedule_reject(struct gsm_lchan *lchan)
1008{
Holger Freyther73487a22008-12-31 18:53:57 +00001009 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1010 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001011 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001012}
1013
Harald Welte2a139372009-02-22 21:14:55 +00001014static const char *lupd_name(u_int8_t type)
1015{
1016 switch (type) {
1017 case GSM48_LUPD_NORMAL:
1018 return "NORMAL";
1019 case GSM48_LUPD_PERIODIC:
1020 return "PEROIDOC";
1021 case GSM48_LUPD_IMSI_ATT:
1022 return "IMSI ATTACH";
1023 default:
1024 return "UNKNOWN";
1025 }
1026}
1027
Harald Weltebf5e8df2009-02-03 12:59:45 +00001028/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001029static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001030{
Harald Welte8470bf22008-12-25 23:28:35 +00001031 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001032 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001033 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001034 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001035 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001036 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001037 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001038 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001039
Harald Welte8470bf22008-12-25 23:28:35 +00001040 lu = (struct gsm48_loc_upd_req *) gh->data;
1041
1042 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001043
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001044 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001045
Harald Weltea0368542009-06-27 02:58:43 +02001046 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001047 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001048
Harald Welte7659de12009-12-13 12:39:18 +01001049 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
1050
Harald Welte24ff6ee2009-12-22 00:41:05 +01001051 switch (lu->type) {
1052 case GSM48_LUPD_NORMAL:
1053 bts->network->stats.loc_upd_type.normal++;
1054 break;
1055 case GSM48_LUPD_IMSI_ATT:
1056 bts->network->stats.loc_upd_type.attach++;
1057 break;
1058 case GSM48_LUPD_PERIODIC:
1059 bts->network->stats.loc_upd_type.periodic++;
1060 break;
1061 }
1062
Holger Freythereaf04692009-06-06 13:54:44 +00001063 /*
1064 * Pseudo Spoof detection: Just drop a second/concurrent
1065 * location updating request.
1066 */
1067 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001069 lchan->loc_operation);
1070 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1071 return 0;
1072 }
1073
Holger Freyther73487a22008-12-31 18:53:57 +00001074 allocate_loc_updating_req(lchan);
1075
Harald Welte52b1f982008-12-23 20:25:15 +00001076 switch (mi_type) {
1077 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001078 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001079 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001080 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001081 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001082
Jan Luebbe370b41d2009-08-12 10:19:34 +02001083 /* look up subscriber based on IMSI, create if not found */
1084 subscr = subscr_get_by_imsi(bts->network, mi_string);
1085 if (!subscr) {
1086 subscr = db_create_subscriber(bts->network, mi_string);
1087 }
Harald Welte4b634542008-12-27 01:55:51 +00001088 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001089 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001090 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001091 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001092 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001093 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001094
Harald Welte52b1f982008-12-23 20:25:15 +00001095 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001096 subscr = subscr_get_by_tmsi(bts->network,
1097 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001098 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001099 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001100 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001101 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001102 }
1103 break;
1104 case GSM_MI_TYPE_IMEI:
1105 case GSM_MI_TYPE_IMEISV:
1106 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001107 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001108 break;
1109 default:
Harald Weltea0368542009-06-27 02:58:43 +02001110 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001111 break;
1112 }
1113
Harald Welte24516ea2009-07-04 10:18:00 +02001114 /* schedule the reject timer */
1115 schedule_reject(lchan);
1116
Harald Welte4bfdfe72009-06-10 23:11:52 +08001117 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001118 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001119 /* FIXME: request id? close channel? */
1120 return -EINVAL;
1121 }
1122
Harald Welte255539c2008-12-28 02:26:27 +00001123 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001124 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001125
Harald Welte24516ea2009-07-04 10:18:00 +02001126 /* check if we can let the subscriber into our network immediately
1127 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001128 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001129}
1130
Harald Welte4bfdfe72009-06-10 23:11:52 +08001131#if 0
1132static u_int8_t to_bcd8(u_int8_t val)
1133{
1134 return ((val / 10) << 4) | (val % 10);
1135}
1136#endif
1137
Harald Weltedb253af2008-12-30 17:56:55 +00001138/* Section 9.2.15a */
1139int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1140{
1141 struct msgb *msg = gsm48_msgb_alloc();
1142 struct gsm48_hdr *gh;
1143 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001144 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001145 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001146#if 0
1147 time_t cur_t;
1148 struct tm* cur_time;
1149 int tz15min;
1150#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001151
1152 msg->lchan = lchan;
1153
1154 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1155 gh->proto_discr = GSM48_PDISC_MM;
1156 gh->msg_type = GSM48_MT_MM_INFO;
1157
1158 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001159#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001160 name_len = strlen(net->name_long);
1161 /* 10.5.3.5a */
1162 ptr8 = msgb_put(msg, 3);
1163 ptr8[0] = GSM48_IE_NAME_LONG;
1164 ptr8[1] = name_len*2 +1;
1165 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1166
1167 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1168 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001169 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001170
1171 /* FIXME: Use Cell Broadcast, not UCS-2, since
1172 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001173#endif
1174 name_len = (strlen(net->name_long)*7)/8;
1175 name_pad = (8 - strlen(net->name_long)*7)%8;
1176 if (name_pad > 0)
1177 name_len++;
1178 /* 10.5.3.5a */
1179 ptr8 = msgb_put(msg, 3);
1180 ptr8[0] = GSM48_IE_NAME_LONG;
1181 ptr8[1] = name_len +1;
1182 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1183
1184 ptr8 = msgb_put(msg, name_len);
1185 gsm_7bit_encode(ptr8, net->name_long);
1186
Harald Weltedb253af2008-12-30 17:56:55 +00001187 }
1188
1189 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001190#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001191 name_len = strlen(net->name_short);
1192 /* 10.5.3.5a */
1193 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001194 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001195 ptr8[1] = name_len*2 + 1;
1196 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1197
Harald Weltee872cb12009-01-01 00:33:37 +00001198 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001199 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001200 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001201#endif
1202 name_len = (strlen(net->name_short)*7)/8;
1203 name_pad = (8 - strlen(net->name_short)*7)%8;
1204 if (name_pad > 0)
1205 name_len++;
1206 /* 10.5.3.5a */
1207 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1208 ptr8[0] = GSM48_IE_NAME_SHORT;
1209 ptr8[1] = name_len +1;
1210 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1211
1212 ptr8 = msgb_put(msg, name_len);
1213 gsm_7bit_encode(ptr8, net->name_short);
1214
Harald Weltedb253af2008-12-30 17:56:55 +00001215 }
1216
1217#if 0
1218 /* Section 10.5.3.9 */
1219 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001220 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001221 ptr8 = msgb_put(msg, 8);
1222 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1223 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1224 ptr8[2] = to_bcd8(cur_time->tm_mon);
1225 ptr8[3] = to_bcd8(cur_time->tm_mday);
1226 ptr8[4] = to_bcd8(cur_time->tm_hour);
1227 ptr8[5] = to_bcd8(cur_time->tm_min);
1228 ptr8[6] = to_bcd8(cur_time->tm_sec);
1229 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1230 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001231 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001232 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001233 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001234#endif
1235
Daniel Willmanneea93372009-08-13 03:42:07 +02001236 DEBUGP(DMM, "-> MM INFO\n");
1237
Harald Welte39e2ead2009-07-23 21:13:03 +02001238 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001239}
1240
Harald Welte7984d5c2009-08-12 22:56:50 +02001241/* Section 9.2.2 */
1242int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1243{
1244 struct msgb *msg = gsm48_msgb_alloc();
1245 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001246 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001247
1248 DEBUGP(DMM, "-> AUTH REQ\n");
1249
1250 msg->lchan = lchan;
1251 gh->proto_discr = GSM48_PDISC_MM;
1252 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1253
Sylvain Munaut849f5542009-09-27 11:10:17 +02001254 /* Key Sequence: FIXME fixed to 0 */
1255 ar->key_seq = 0;
1256
Harald Welte7984d5c2009-08-12 22:56:50 +02001257 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001258 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001259 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001260
1261 return gsm48_sendmsg(msg, NULL);
1262}
1263
1264/* Section 9.2.1 */
1265int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1266{
1267 DEBUGP(DMM, "-> AUTH REJECT\n");
1268 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1269}
1270
Harald Welte4b634542008-12-27 01:55:51 +00001271static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1272{
Harald Welte4b634542008-12-27 01:55:51 +00001273 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001274 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001275}
Harald Welteba4cf162009-01-10 01:49:35 +00001276
1277/* 9.2.6 CM service reject */
1278static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1279 enum gsm48_reject_value value)
1280{
1281 struct msgb *msg = gsm48_msgb_alloc();
1282 struct gsm48_hdr *gh;
1283
1284 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1285
1286 msg->lchan = lchan;
1287 use_lchan(lchan);
1288
1289 gh->proto_discr = GSM48_PDISC_MM;
1290 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1291 gh->data[0] = value;
1292 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1293
Harald Welte39e2ead2009-07-23 21:13:03 +02001294 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001295}
1296
Harald Welte4ed0e922009-01-10 03:17:30 +00001297/*
1298 * Handle CM Service Requests
1299 * a) Verify that the packet is long enough to contain the information
1300 * we require otherwsie reject with INCORRECT_MESSAGE
1301 * b) Try to parse the TMSI. If we do not have one reject
1302 * c) Check that we know the subscriber with the TMSI otherwise reject
1303 * with a HLR cause
1304 * d) Set the subscriber on the gsm_lchan and accept
1305 */
Harald Welte4b634542008-12-27 01:55:51 +00001306static int gsm48_rx_mm_serv_req(struct msgb *msg)
1307{
Harald Welteba4cf162009-01-10 01:49:35 +00001308 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001309 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001310
Harald Welte9176bd42009-07-23 18:46:00 +02001311 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001312 struct gsm_subscriber *subscr;
1313 struct gsm48_hdr *gh = msgb_l3(msg);
1314 struct gsm48_service_request *req =
1315 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001316 /* unfortunately in Phase1 the classmar2 length is variable */
1317 u_int8_t classmark2_len = gh->data[1];
1318 u_int8_t *classmark2 = gh->data+2;
1319 u_int8_t mi_len = *(classmark2 + classmark2_len);
1320 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001321
Harald Weltec9e02182009-05-01 19:07:53 +00001322 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001323 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001324 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001325 return gsm48_tx_mm_serv_rej(msg->lchan,
1326 GSM48_REJECT_INCORRECT_MESSAGE);
1327 }
1328
1329 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001330 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001331 return gsm48_tx_mm_serv_rej(msg->lchan,
1332 GSM48_REJECT_INCORRECT_MESSAGE);
1333 }
1334
Harald Weltec9e02182009-05-01 19:07:53 +00001335 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001336 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001337 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001338 return gsm48_tx_mm_serv_rej(msg->lchan,
1339 GSM48_REJECT_INCORRECT_MESSAGE);
1340 }
1341
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001342 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001343 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001344 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001345
Harald Welte7659de12009-12-13 12:39:18 +01001346 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1347
Harald Welte3ac7f102009-08-10 10:12:45 +02001348 if (is_siemens_bts(bts))
1349 send_siemens_mrpci(msg->lchan, classmark2-1);
1350
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001351 subscr = subscr_get_by_tmsi(bts->network,
1352 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001353
Harald Welte2a139372009-02-22 21:14:55 +00001354 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001355 if (!subscr)
1356 return gsm48_tx_mm_serv_rej(msg->lchan,
1357 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1358
1359 if (!msg->lchan->subscr)
1360 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +01001361 else if (msg->lchan->subscr == subscr)
1362 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1363 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001364 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1365 subscr_put(subscr);
1366 }
1367
Harald Weltec2e302d2009-07-05 14:08:13 +02001368 subscr->equipment.classmark2_len = classmark2_len;
1369 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1370 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001371
Harald Welte4b634542008-12-27 01:55:51 +00001372 return gsm48_tx_mm_serv_ack(msg->lchan);
1373}
1374
Harald Welte2a139372009-02-22 21:14:55 +00001375static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1376{
Harald Welte9176bd42009-07-23 18:46:00 +02001377 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001378 struct gsm48_hdr *gh = msgb_l3(msg);
1379 struct gsm48_imsi_detach_ind *idi =
1380 (struct gsm48_imsi_detach_ind *) gh->data;
1381 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001382 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001384
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001385 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001386 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1387 mi_type, mi_string);
1388
Harald Welte24ff6ee2009-12-22 00:41:05 +01001389 bts->network->stats.loc_upd_type.detach++;
1390
Harald Welte2a139372009-02-22 21:14:55 +00001391 switch (mi_type) {
1392 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001393 subscr = subscr_get_by_tmsi(bts->network,
1394 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001395 break;
1396 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001397 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001398 break;
1399 case GSM_MI_TYPE_IMEI:
1400 case GSM_MI_TYPE_IMEISV:
1401 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001402 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001403 break;
1404 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001405 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001406 break;
1407 }
1408
Holger Freyther4a49e772009-04-12 05:37:29 +00001409 if (subscr) {
1410 subscr_update(subscr, msg->trx->bts,
1411 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001412 DEBUGP(DMM, "Subscriber: %s\n",
1413 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001414
1415 subscr->equipment.classmark1 = idi->classmark1;
1416 db_sync_equipment(&subscr->equipment);
1417
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001418 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001419 } else
Harald Welte2a139372009-02-22 21:14:55 +00001420 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1421
Harald Welte31981a02009-12-20 09:58:40 +01001422 /* FIXME: iterate over all transactions and release them,
1423 * imagine an IMSI DETACH happening during an active call! */
1424
Harald Welteb83d9382009-12-12 21:00:48 +01001425 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +01001426 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +01001427
Harald Welte2a139372009-02-22 21:14:55 +00001428 return 0;
1429}
1430
Harald Welted2a7f5a2009-06-05 20:08:20 +00001431static int gsm48_rx_mm_status(struct msgb *msg)
1432{
1433 struct gsm48_hdr *gh = msgb_l3(msg);
1434
1435 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1436
1437 return 0;
1438}
1439
Harald Weltebf5e8df2009-02-03 12:59:45 +00001440/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001441static int gsm0408_rcv_mm(struct msgb *msg)
1442{
1443 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001444 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001445
1446 switch (gh->msg_type & 0xbf) {
1447 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001448 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001449 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001450 break;
1451 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001452 rc = mm_rx_id_resp(msg);
1453 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001454 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001455 rc = gsm48_rx_mm_serv_req(msg);
1456 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001457 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001458 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001459 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001460 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001461 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1462 msg->lchan->subscr ?
1463 msg->lchan->subscr->imsi :
1464 "unknown subscriber");
1465 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001466 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001467 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1468 break;
1469 case GSM48_MT_MM_CM_REEST_REQ:
1470 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1471 break;
1472 case GSM48_MT_MM_AUTH_RESP:
1473 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001474 break;
1475 default:
1476 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1477 gh->msg_type);
1478 break;
1479 }
1480
1481 return rc;
1482}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001483
Harald Welte2d35ae62009-02-06 12:02:13 +00001484/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001485static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001486{
Harald Welte9176bd42009-07-23 18:46:00 +02001487 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001488 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001489 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001490 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001491 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001493 int rc = 0;
1494
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001495 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001496 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1497 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001498
Harald Weltefe18d8f2009-02-22 21:14:24 +00001499 switch (mi_type) {
1500 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001501 subscr = subscr_get_by_tmsi(bts->network,
1502 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001503 break;
1504 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001505 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001506 break;
1507 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001508
1509 if (!subscr) {
1510 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001511 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001512 return -EINVAL;
1513 }
1514 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001515 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001516
Harald Weltec2e302d2009-07-05 14:08:13 +02001517 subscr->equipment.classmark2_len = *classmark2_lv;
1518 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1519 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001520
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001521 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001522 return rc;
1523}
1524
Harald Weltef7c43522009-06-09 20:24:21 +00001525static int gsm48_rx_rr_classmark(struct msgb *msg)
1526{
1527 struct gsm48_hdr *gh = msgb_l3(msg);
1528 struct gsm_subscriber *subscr = msg->lchan->subscr;
1529 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1530 u_int8_t cm2_len, cm3_len = 0;
1531 u_int8_t *cm2, *cm3 = NULL;
1532
1533 DEBUGP(DRR, "CLASSMARK CHANGE ");
1534
1535 /* classmark 2 */
1536 cm2_len = gh->data[0];
1537 cm2 = &gh->data[1];
1538 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1539
1540 if (payload_len > cm2_len + 1) {
1541 /* we must have a classmark3 */
1542 if (gh->data[cm2_len+1] != 0x20) {
1543 DEBUGPC(DRR, "ERR CM3 TAG\n");
1544 return -EINVAL;
1545 }
1546 if (cm2_len > 3) {
1547 DEBUGPC(DRR, "CM2 too long!\n");
1548 return -EINVAL;
1549 }
1550
1551 cm3_len = gh->data[cm2_len+2];
1552 cm3 = &gh->data[cm2_len+3];
1553 if (cm3_len > 14) {
1554 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1555 return -EINVAL;
1556 }
1557 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1558 }
1559 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001560 subscr->equipment.classmark2_len = cm2_len;
1561 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001562 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001563 subscr->equipment.classmark3_len = cm3_len;
1564 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001565 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001566 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001567 }
1568
Harald Weltef7c43522009-06-09 20:24:21 +00001569 return 0;
1570}
1571
Harald Weltecf5b3592009-05-01 18:28:42 +00001572static int gsm48_rx_rr_status(struct msgb *msg)
1573{
1574 struct gsm48_hdr *gh = msgb_l3(msg);
1575
1576 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1577 rr_cause_name(gh->data[0]));
1578
1579 return 0;
1580}
1581
Harald Weltef7c43522009-06-09 20:24:21 +00001582static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1583{
Harald Welted12b0fd2009-12-15 21:36:05 +01001584 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001585
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001586 /* This shouldn't actually end up here, as RSL treats
1587 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1588 * directly into gsm48_parse_meas_rep */
1589 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001590 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001591
1592 return 0;
1593}
1594
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001595static int gsm48_rx_rr_app_info(struct msgb *msg)
1596{
1597 struct gsm48_hdr *gh = msgb_l3(msg);
1598 u_int8_t apdu_id_flags;
1599 u_int8_t apdu_len;
1600 u_int8_t *apdu_data;
1601
1602 apdu_id_flags = gh->data[0];
1603 apdu_len = gh->data[1];
1604 apdu_data = gh->data+2;
1605
1606 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1607 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1608
Harald Welte (local)026531e2009-08-16 10:40:10 +02001609 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001610}
1611
Harald Welted011e8b2009-11-29 22:45:52 +01001612/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001613static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001614{
1615 struct gsm48_hdr *gh = msgb_l3(msg);
1616
1617 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1618 rr_cause_name(gh->data[0]));
1619
1620 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1621 /* FIXME: release old channel */
1622
1623 return 0;
1624}
1625
1626/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001627static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001628{
1629 struct gsm48_hdr *gh = msgb_l3(msg);
1630
1631 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1632 rr_cause_name(gh->data[0]));
1633
1634 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1635 /* FIXME: release allocated new channel */
1636
1637 return 0;
1638}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001639
Harald Weltebf5e8df2009-02-03 12:59:45 +00001640/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001641static int gsm0408_rcv_rr(struct msgb *msg)
1642{
1643 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001644 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001645
1646 switch (gh->msg_type) {
1647 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001648 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001649 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001650 case GSM48_MT_RR_GPRS_SUSP_REQ:
1651 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1652 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001653 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001654 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001655 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001656 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001657 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001658 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001659 case GSM48_MT_RR_STATUS:
1660 rc = gsm48_rx_rr_status(msg);
1661 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001662 case GSM48_MT_RR_MEAS_REP:
1663 rc = gsm48_rx_rr_meas_rep(msg);
1664 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001665 case GSM48_MT_RR_APP_INFO:
1666 rc = gsm48_rx_rr_app_info(msg);
1667 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001668 case GSM48_MT_RR_CIPH_M_COMPL:
1669 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1670 /* FIXME: check for MI (if any) */
1671 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001672 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001673 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001674 break;
1675 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001676 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001677 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001678 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001679 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001680 gh->msg_type);
1681 break;
1682 }
1683
Harald Welte2d35ae62009-02-06 12:02:13 +00001684 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001685}
1686
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001687int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001688 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001689{
1690 struct msgb *msg = gsm48_msgb_alloc();
1691 struct gsm48_hdr *gh;
1692
1693 msg->lchan = lchan;
1694
1695 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1696 apdu_id, apdu_len);
1697
1698 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1699 gh->proto_discr = GSM48_PDISC_RR;
1700 gh->msg_type = GSM48_MT_RR_APP_INFO;
1701 gh->data[0] = apdu_id;
1702 gh->data[1] = apdu_len;
1703 memcpy(gh->data+2, apdu, apdu_len);
1704
1705 return gsm48_sendmsg(msg, NULL);
1706}
1707
Harald Welte4bc90a12008-12-27 16:32:52 +00001708/* Call Control */
1709
Harald Welte7584aea2009-02-11 11:44:12 +00001710/* The entire call control code is written in accordance with Figure 7.10c
1711 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1712 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1713 * it for voice */
1714
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715static void new_cc_state(struct gsm_trans *trans, int state)
1716{
1717 if (state > 31 || state < 0)
1718 return;
1719
1720 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001721 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722
Harald Weltedcaf5652009-07-23 18:56:43 +02001723 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724}
1725
1726static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001727{
1728 struct msgb *msg = gsm48_msgb_alloc();
1729 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1730 u_int8_t *cause, *call_state;
1731
Harald Welte4bc90a12008-12-27 16:32:52 +00001732 gh->msg_type = GSM48_MT_CC_STATUS;
1733
1734 cause = msgb_put(msg, 3);
1735 cause[0] = 2;
1736 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1737 cause[2] = 0x80 | 30; /* response to status inquiry */
1738
1739 call_state = msgb_put(msg, 1);
1740 call_state[0] = 0xc0 | 0x00;
1741
Harald Welte39e2ead2009-07-23 21:13:03 +02001742 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001743}
1744
Harald Welte6f4b7532008-12-29 00:39:37 +00001745static int gsm48_tx_simple(struct gsm_lchan *lchan,
1746 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001747{
1748 struct msgb *msg = gsm48_msgb_alloc();
1749 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1750
1751 msg->lchan = lchan;
1752
Harald Welte6f4b7532008-12-29 00:39:37 +00001753 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001754 gh->msg_type = msg_type;
1755
Harald Welte39e2ead2009-07-23 21:13:03 +02001756 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001757}
1758
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1760{
Harald Weltedcaf5652009-07-23 18:56:43 +02001761 if (bsc_timer_pending(&trans->cc.timer)) {
1762 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1763 bsc_del_timer(&trans->cc.timer);
1764 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 }
1766}
1767
1768static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1769 int msg_type, struct gsm_mncc *mncc)
1770{
1771 struct msgb *msg;
1772
1773 if (trans)
1774 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001775 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 "Sending '%s' to MNCC.\n",
1777 trans->lchan->ts->trx->bts->nr,
1778 trans->lchan->ts->trx->nr,
1779 trans->lchan->ts->nr, trans->transaction_id,
1780 (trans->subscr)?(trans->subscr->extension):"-",
1781 get_mncc_name(msg_type));
1782 else
1783 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1784 "Sending '%s' to MNCC.\n",
1785 (trans->subscr)?(trans->subscr->extension):"-",
1786 get_mncc_name(msg_type));
1787 else
1788 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1789 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1790
1791 mncc->msg_type = msg_type;
1792
Harald Welte966636f2009-06-26 19:39:35 +02001793 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 if (!msg)
1795 return -ENOMEM;
1796 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1797 msgb_enqueue(&net->upqueue, msg);
1798
1799 return 0;
1800}
1801
1802int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1803 u_int32_t callref, int location, int value)
1804{
1805 struct gsm_mncc rel;
1806
Harald Welte92f70c52009-06-12 01:54:08 +08001807 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001809 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1811}
1812
Harald Weltedcaf5652009-07-23 18:56:43 +02001813/* Call Control Specific transaction release.
1814 * gets called by trans_free, DO NOT CALL YOURSELF! */
1815void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 gsm48_stop_cc_timer(trans);
1818
1819 /* send release to L4, if callref still exists */
1820 if (trans->callref) {
1821 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001822 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001823 GSM48_CAUSE_LOC_PRN_S_LU,
1824 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001826 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001828 if (trans->lchan)
1829 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830}
1831
1832static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1833
Harald Welte09e38af2009-02-16 22:52:23 +00001834/* call-back from paging the B-end of the connection */
1835static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001836 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001837{
Harald Welte7ccf7782009-02-17 01:43:01 +00001838 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839 struct gsm_subscriber *subscr = param;
1840 struct gsm_trans *transt, *tmp;
1841 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001842
Harald Welte09e38af2009-02-16 22:52:23 +00001843 if (hooknum != GSM_HOOK_RR_PAGING)
1844 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845
1846 if (!subscr)
1847 return -EINVAL;
1848 net = subscr->net;
1849 if (!net) {
1850 DEBUGP(DCC, "Error Network not set!\n");
1851 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001852 }
Harald Welte7584aea2009-02-11 11:44:12 +00001853
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 /* check all tranactions (without lchan) for subscriber */
1855 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1856 if (transt->subscr != subscr || transt->lchan)
1857 continue;
1858 switch (event) {
1859 case GSM_PAGING_SUCCEEDED:
1860 if (!lchan) // paranoid
1861 break;
1862 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1863 subscr->extension);
1864 /* Assign lchan */
1865 if (!transt->lchan) {
1866 transt->lchan = lchan;
1867 use_lchan(lchan);
1868 }
1869 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001870 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 break;
1872 case GSM_PAGING_EXPIRED:
1873 DEBUGP(DCC, "Paging subscr %s expired!\n",
1874 subscr->extension);
1875 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001876 mncc_release_ind(transt->subscr->net, transt,
1877 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001878 GSM48_CAUSE_LOC_PRN_S_LU,
1879 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001881 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 break;
1883 }
1884 }
Harald Welte09e38af2009-02-16 22:52:23 +00001885 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001886}
Harald Welte7584aea2009-02-11 11:44:12 +00001887
Harald Welteda7ab742009-12-19 22:23:05 +01001888static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1889
Harald Welte805f6442009-07-28 18:25:29 +02001890/* some other part of the code sends us a signal */
1891static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1892 void *handler_data, void *signal_data)
1893{
1894 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001895 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001896 struct gsm_network *net;
1897 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001898
1899 if (subsys != SS_ABISIP)
1900 return 0;
1901
1902 /* in case we use direct BTS-to-BTS RTP */
1903 if (ipacc_rtp_direct)
1904 return 0;
1905
Harald Welte805f6442009-07-28 18:25:29 +02001906 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001907 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001908 /* check if any transactions on this lchan still have
1909 * a tch_recv_mncc request pending */
1910 net = lchan->ts->trx->bts->network;
1911 llist_for_each_entry(trans, &net->trans_list, entry) {
1912 if (trans->lchan == lchan && trans->tch_recv) {
1913 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1914 tch_recv_mncc(net, trans->callref, 1);
1915 }
1916 }
Harald Welte805f6442009-07-28 18:25:29 +02001917 break;
Harald Welte805f6442009-07-28 18:25:29 +02001918 }
1919
1920 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001921}
1922
Harald Welte49f48b82009-02-17 15:29:33 +00001923/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001924static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001925{
Harald Welte11fa29c2009-02-19 17:24:39 +00001926 struct gsm_bts *bts = lchan->ts->trx->bts;
1927 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001928 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001929
Harald Welte11fa29c2009-02-19 17:24:39 +00001930 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1931 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1932 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1933
1934 if (bts->type != remote_bts->type) {
1935 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1936 return -EINVAL;
1937 }
Harald Welteda7ab742009-12-19 22:23:05 +01001938
1939 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001940 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001941 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001942 if (!ipacc_rtp_direct) {
1943 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001944 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001945 if (rc < 0)
1946 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001947 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001948#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001949
1950 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301951 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1952 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001953 } else {
1954 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301955 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1956 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301957 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001958 if (rc < 0)
1959 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301960 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1961 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301962 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001963 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001964 break;
1965 case GSM_BTS_TYPE_BS11:
1966 trau_mux_map_lchan(lchan, remote_lchan);
1967 break;
1968 default:
1969 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001970 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001971 }
Harald Welte49f48b82009-02-17 15:29:33 +00001972
1973 return 0;
1974}
1975
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976/* bridge channels of two transactions */
1977static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001978{
Harald Weltedcaf5652009-07-23 18:56:43 +02001979 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1980 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001981
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001983 return -EIO;
1984
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985 if (!trans1->lchan || !trans2->lchan)
1986 return -EIO;
1987
1988 /* through-connect channel */
1989 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001990}
1991
Harald Welteda7ab742009-12-19 22:23:05 +01001992/* enable receive of channels to MNCC upqueue */
1993static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994{
1995 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001996 struct gsm_lchan *lchan;
1997 struct gsm_bts *bts;
1998 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001999
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01002001 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 if (!trans)
2003 return -EIO;
2004 if (!trans->lchan)
2005 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01002006 lchan = trans->lchan;
2007 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008
Harald Welteda7ab742009-12-19 22:23:05 +01002009 switch (bts->type) {
2010 case GSM_BTS_TYPE_NANOBTS:
2011 if (ipacc_rtp_direct) {
2012 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
2013 return -EINVAL;
2014 }
2015 /* in case, we don't have a RTP socket yet, we note this
2016 * in the transaction and try later */
2017 if (!lchan->abis_ip.rtp_socket) {
2018 trans->tch_recv = enable;
2019 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2020 return 0;
2021 }
2022 if (enable) {
2023 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01002024 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01002025 if (rc < 0)
2026 return rc;
2027 /* assign socket to application interface */
2028 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2029 net, callref);
2030 } else
2031 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2032 net, 0);
2033 break;
2034 case GSM_BTS_TYPE_BS11:
2035 if (enable)
2036 return trau_recv_lchan(lchan, callref);
2037 return trau_mux_unmap(NULL, callref);
2038 break;
2039 default:
2040 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2041 return -EINVAL;
2042 }
2043
2044 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045}
2046
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2048{
2049 DEBUGP(DCC, "-> STATUS ENQ\n");
2050 return gsm48_cc_tx_status(trans, msg);
2051}
2052
2053static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2054static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2055
2056static void gsm48_cc_timeout(void *arg)
2057{
2058 struct gsm_trans *trans = arg;
2059 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002060 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2061 int mo_location = GSM48_CAUSE_LOC_USER;
2062 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2063 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 struct gsm_mncc mo_rel, l4_rel;
2065
2066 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2067 mo_rel.callref = trans->callref;
2068 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2069 l4_rel.callref = trans->callref;
2070
Harald Weltedcaf5652009-07-23 18:56:43 +02002071 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 case 0x303:
2073 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002074 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 break;
2076 case 0x310:
2077 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002078 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 break;
2080 case 0x313:
2081 disconnect = 1;
2082 /* unknown, did not find it in the specs */
2083 break;
2084 case 0x301:
2085 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002086 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 break;
2088 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002089 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002091 gsm48_cc_tx_release(trans, &trans->cc.msg);
2092 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 break; /* stay in release state */
2094 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002095 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 return;
2097// release = 1;
2098// l4_cause = 14;
2099// break;
2100 case 0x306:
2101 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002102 mo_cause = trans->cc.msg.cause.value;
2103 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 break;
2105 case 0x323:
2106 disconnect = 1;
2107 break;
2108 default:
2109 release = 1;
2110 }
2111
2112 if (release && trans->callref) {
2113 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002114 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 l4_location, l4_cause);
2116 trans->callref = 0;
2117 }
2118
2119 if (disconnect && trans->callref) {
2120 /* process disconnect towards layer 4 */
2121 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002122 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 }
2124
2125 /* process disconnect towards mobile station */
2126 if (disconnect || release) {
2127 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002128 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2129 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2130 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 mo_rel.cause.diag_len = 3;
2132
2133 if (disconnect)
2134 gsm48_cc_tx_disconnect(trans, &mo_rel);
2135 if (release)
2136 gsm48_cc_tx_release(trans, &mo_rel);
2137 }
2138
2139}
2140
2141static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2142 int sec, int micro)
2143{
2144 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002145 trans->cc.timer.cb = gsm48_cc_timeout;
2146 trans->cc.timer.data = trans;
2147 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2148 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149}
2150
2151static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2152{
2153 struct gsm48_hdr *gh = msgb_l3(msg);
2154 u_int8_t msg_type = gh->msg_type & 0xbf;
2155 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2156 struct tlv_parsed tp;
2157 struct gsm_mncc setup;
2158
2159 memset(&setup, 0, sizeof(struct gsm_mncc));
2160 setup.callref = trans->callref;
2161 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2162 /* emergency setup is identified by msg_type */
2163 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2164 setup.emergency = 1;
2165
2166 /* use subscriber as calling party number */
2167 if (trans->subscr) {
2168 setup.fields |= MNCC_F_CALLING;
2169 strncpy(setup.calling.number, trans->subscr->extension,
2170 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002171 strncpy(setup.imsi, trans->subscr->imsi,
2172 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 }
2174 /* bearer capability */
2175 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2176 setup.fields |= MNCC_F_BEARER_CAP;
2177 decode_bearer_cap(&setup.bearer_cap,
2178 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2179 }
2180 /* facility */
2181 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2182 setup.fields |= MNCC_F_FACILITY;
2183 decode_facility(&setup.facility,
2184 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2185 }
2186 /* called party bcd number */
2187 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2188 setup.fields |= MNCC_F_CALLED;
2189 decode_called(&setup.called,
2190 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2191 }
2192 /* user-user */
2193 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2194 setup.fields |= MNCC_F_USERUSER;
2195 decode_useruser(&setup.useruser,
2196 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2197 }
2198 /* ss-version */
2199 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2200 setup.fields |= MNCC_F_SSVERSION;
2201 decode_ssversion(&setup.ssversion,
2202 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2203 }
2204 /* CLIR suppression */
2205 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2206 setup.clir.sup = 1;
2207 /* CLIR invocation */
2208 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2209 setup.clir.inv = 1;
2210 /* cc cap */
2211 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2212 setup.fields |= MNCC_F_CCCAP;
2213 decode_cccap(&setup.cccap,
2214 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2215 }
2216
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 new_cc_state(trans, GSM_CSTATE_INITIATED);
2218
2219 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002220 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221
Harald Welte13cac662009-07-29 12:10:35 +02002222 /* MNCC code will modify the channel asynchronously, we should
2223 * ipaccess-bind only after the modification has been made to the
2224 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 return 0;
2226}
2227
2228static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002229{
2230 struct msgb *msg = gsm48_msgb_alloc();
2231 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002233 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002234
Harald Welte7ccf7782009-02-17 01:43:01 +00002235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002236
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* transaction id must not be assigned */
2238 if (trans->transaction_id != 0xff) { /* unasssigned */
2239 DEBUGP(DCC, "TX Setup with assigned transaction. "
2240 "This is not allowed!\n");
2241 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002242 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002243 GSM48_CAUSE_LOC_PRN_S_LU,
2244 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002246 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 return rc;
2248 }
2249
2250 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002251 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2252 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002254 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002255 GSM48_CAUSE_LOC_PRN_S_LU,
2256 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002258 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 return rc;
2260 }
Harald Welte78283ef2009-07-23 21:36:44 +02002261 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002262
Harald Welte65e74cc2008-12-29 01:55:35 +00002263 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002264
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002266
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 /* bearer capability */
2268 if (setup->fields & MNCC_F_BEARER_CAP)
2269 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2270 /* facility */
2271 if (setup->fields & MNCC_F_FACILITY)
2272 encode_facility(msg, 0, &setup->facility);
2273 /* progress */
2274 if (setup->fields & MNCC_F_PROGRESS)
2275 encode_progress(msg, 0, &setup->progress);
2276 /* calling party BCD number */
2277 if (setup->fields & MNCC_F_CALLING)
2278 encode_calling(msg, &setup->calling);
2279 /* called party BCD number */
2280 if (setup->fields & MNCC_F_CALLED)
2281 encode_called(msg, &setup->called);
2282 /* user-user */
2283 if (setup->fields & MNCC_F_USERUSER)
2284 encode_useruser(msg, 0, &setup->useruser);
2285 /* redirecting party BCD number */
2286 if (setup->fields & MNCC_F_REDIRECTING)
2287 encode_redirecting(msg, &setup->redirecting);
2288 /* signal */
2289 if (setup->fields & MNCC_F_SIGNAL)
2290 encode_signal(msg, setup->signal);
2291
2292 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002293
Harald Welte39e2ead2009-07-23 21:13:03 +02002294 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002295}
2296
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2298{
2299 struct gsm48_hdr *gh = msgb_l3(msg);
2300 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2301 struct tlv_parsed tp;
2302 struct gsm_mncc call_conf;
2303
2304 gsm48_stop_cc_timer(trans);
2305 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2306
2307 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2308 call_conf.callref = trans->callref;
2309 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2310#if 0
2311 /* repeat */
2312 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2313 call_conf.repeat = 1;
2314 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2315 call_conf.repeat = 2;
2316#endif
2317 /* bearer capability */
2318 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2319 call_conf.fields |= MNCC_F_BEARER_CAP;
2320 decode_bearer_cap(&call_conf.bearer_cap,
2321 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2322 }
2323 /* cause */
2324 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2325 call_conf.fields |= MNCC_F_CAUSE;
2326 decode_cause(&call_conf.cause,
2327 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2328 }
2329 /* cc cap */
2330 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2331 call_conf.fields |= MNCC_F_CCCAP;
2332 decode_cccap(&call_conf.cccap,
2333 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2334 }
2335
2336 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2337
Harald Welte596fed42009-07-23 19:06:52 +02002338 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2339 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2343{
2344 struct gsm_mncc *proceeding = arg;
2345 struct msgb *msg = gsm48_msgb_alloc();
2346 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2347
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2349
2350 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2351
2352 /* bearer capability */
2353 if (proceeding->fields & MNCC_F_BEARER_CAP)
2354 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2355 /* facility */
2356 if (proceeding->fields & MNCC_F_FACILITY)
2357 encode_facility(msg, 0, &proceeding->facility);
2358 /* progress */
2359 if (proceeding->fields & MNCC_F_PROGRESS)
2360 encode_progress(msg, 0, &proceeding->progress);
2361
Harald Welte39e2ead2009-07-23 21:13:03 +02002362 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363}
2364
2365static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2366{
2367 struct gsm48_hdr *gh = msgb_l3(msg);
2368 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2369 struct tlv_parsed tp;
2370 struct gsm_mncc alerting;
2371
2372 gsm48_stop_cc_timer(trans);
2373 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2374
2375 memset(&alerting, 0, sizeof(struct gsm_mncc));
2376 alerting.callref = trans->callref;
2377 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2378 /* facility */
2379 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2380 alerting.fields |= MNCC_F_FACILITY;
2381 decode_facility(&alerting.facility,
2382 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2383 }
2384
2385 /* progress */
2386 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2387 alerting.fields |= MNCC_F_PROGRESS;
2388 decode_progress(&alerting.progress,
2389 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2390 }
2391 /* ss-version */
2392 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2393 alerting.fields |= MNCC_F_SSVERSION;
2394 decode_ssversion(&alerting.ssversion,
2395 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2396 }
2397
2398 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2399
Harald Welte596fed42009-07-23 19:06:52 +02002400 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2401 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402}
2403
2404static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2405{
2406 struct gsm_mncc *alerting = arg;
2407 struct msgb *msg = gsm48_msgb_alloc();
2408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2409
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 gh->msg_type = GSM48_MT_CC_ALERTING;
2411
2412 /* facility */
2413 if (alerting->fields & MNCC_F_FACILITY)
2414 encode_facility(msg, 0, &alerting->facility);
2415 /* progress */
2416 if (alerting->fields & MNCC_F_PROGRESS)
2417 encode_progress(msg, 0, &alerting->progress);
2418 /* user-user */
2419 if (alerting->fields & MNCC_F_USERUSER)
2420 encode_useruser(msg, 0, &alerting->useruser);
2421
2422 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2423
Harald Welte39e2ead2009-07-23 21:13:03 +02002424 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *progress = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_PROGRESS;
2434
2435 /* progress */
2436 encode_progress(msg, 1, &progress->progress);
2437 /* user-user */
2438 if (progress->fields & MNCC_F_USERUSER)
2439 encode_useruser(msg, 0, &progress->useruser);
2440
Harald Welte39e2ead2009-07-23 21:13:03 +02002441 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2445{
2446 struct gsm_mncc *connect = arg;
2447 struct msgb *msg = gsm48_msgb_alloc();
2448 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2449
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 gh->msg_type = GSM48_MT_CC_CONNECT;
2451
2452 gsm48_stop_cc_timer(trans);
2453 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2454
2455 /* facility */
2456 if (connect->fields & MNCC_F_FACILITY)
2457 encode_facility(msg, 0, &connect->facility);
2458 /* progress */
2459 if (connect->fields & MNCC_F_PROGRESS)
2460 encode_progress(msg, 0, &connect->progress);
2461 /* connected number */
2462 if (connect->fields & MNCC_F_CONNECTED)
2463 encode_connected(msg, &connect->connected);
2464 /* user-user */
2465 if (connect->fields & MNCC_F_USERUSER)
2466 encode_useruser(msg, 0, &connect->useruser);
2467
2468 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2469
Harald Welte39e2ead2009-07-23 21:13:03 +02002470 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471}
2472
2473static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2474{
2475 struct gsm48_hdr *gh = msgb_l3(msg);
2476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2477 struct tlv_parsed tp;
2478 struct gsm_mncc connect;
2479
2480 gsm48_stop_cc_timer(trans);
2481
2482 memset(&connect, 0, sizeof(struct gsm_mncc));
2483 connect.callref = trans->callref;
2484 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2485 /* use subscriber as connected party number */
2486 if (trans->subscr) {
2487 connect.fields |= MNCC_F_CONNECTED;
2488 strncpy(connect.connected.number, trans->subscr->extension,
2489 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002490 strncpy(connect.imsi, trans->subscr->imsi,
2491 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492 }
2493 /* facility */
2494 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2495 connect.fields |= MNCC_F_FACILITY;
2496 decode_facility(&connect.facility,
2497 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2498 }
2499 /* user-user */
2500 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2501 connect.fields |= MNCC_F_USERUSER;
2502 decode_useruser(&connect.useruser,
2503 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2504 }
2505 /* ss-version */
2506 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2507 connect.fields |= MNCC_F_SSVERSION;
2508 decode_ssversion(&connect.ssversion,
2509 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2510 }
2511
2512 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2513
Harald Welte596fed42009-07-23 19:06:52 +02002514 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515}
2516
2517
2518static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2519{
2520 struct gsm_mncc connect_ack;
2521
2522 gsm48_stop_cc_timer(trans);
2523
2524 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2525
2526 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2527 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002528 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 &connect_ack);
2530}
2531
2532static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2533{
2534 struct msgb *msg = gsm48_msgb_alloc();
2535 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2536
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2538
2539 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2540
Harald Welte39e2ead2009-07-23 21:13:03 +02002541 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542}
2543
2544static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2545{
2546 struct gsm48_hdr *gh = msgb_l3(msg);
2547 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2548 struct tlv_parsed tp;
2549 struct gsm_mncc disc;
2550
2551 gsm48_stop_cc_timer(trans);
2552
2553 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2554
2555 memset(&disc, 0, sizeof(struct gsm_mncc));
2556 disc.callref = trans->callref;
2557 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2558 /* cause */
2559 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2560 disc.fields |= MNCC_F_CAUSE;
2561 decode_cause(&disc.cause,
2562 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2563 }
2564 /* facility */
2565 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2566 disc.fields |= MNCC_F_FACILITY;
2567 decode_facility(&disc.facility,
2568 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2569 }
2570 /* user-user */
2571 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2572 disc.fields |= MNCC_F_USERUSER;
2573 decode_useruser(&disc.useruser,
2574 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2575 }
2576 /* ss-version */
2577 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2578 disc.fields |= MNCC_F_SSVERSION;
2579 decode_ssversion(&disc.ssversion,
2580 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2581 }
2582
Harald Welte596fed42009-07-23 19:06:52 +02002583 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584
2585}
2586
Harald Weltec66b71c2009-06-11 14:23:20 +08002587static struct gsm_mncc_cause default_cause = {
2588 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2589 .coding = 0,
2590 .rec = 0,
2591 .rec_val = 0,
2592 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2593 .diag_len = 0,
2594 .diag = { 0 },
2595};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596
2597static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2598{
2599 struct gsm_mncc *disc = arg;
2600 struct msgb *msg = gsm48_msgb_alloc();
2601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2602
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2604
2605 gsm48_stop_cc_timer(trans);
2606 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2607
2608 /* cause */
2609 if (disc->fields & MNCC_F_CAUSE)
2610 encode_cause(msg, 1, &disc->cause);
2611 else
2612 encode_cause(msg, 1, &default_cause);
2613
2614 /* facility */
2615 if (disc->fields & MNCC_F_FACILITY)
2616 encode_facility(msg, 0, &disc->facility);
2617 /* progress */
2618 if (disc->fields & MNCC_F_PROGRESS)
2619 encode_progress(msg, 0, &disc->progress);
2620 /* user-user */
2621 if (disc->fields & MNCC_F_USERUSER)
2622 encode_useruser(msg, 0, &disc->useruser);
2623
2624 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002625 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626
2627 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2628
Harald Welte39e2ead2009-07-23 21:13:03 +02002629 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2633{
2634 struct gsm48_hdr *gh = msgb_l3(msg);
2635 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2636 struct tlv_parsed tp;
2637 struct gsm_mncc rel;
2638 int rc;
2639
2640 gsm48_stop_cc_timer(trans);
2641
2642 memset(&rel, 0, sizeof(struct gsm_mncc));
2643 rel.callref = trans->callref;
2644 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2645 /* cause */
2646 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2647 rel.fields |= MNCC_F_CAUSE;
2648 decode_cause(&rel.cause,
2649 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2650 }
2651 /* facility */
2652 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2653 rel.fields |= MNCC_F_FACILITY;
2654 decode_facility(&rel.facility,
2655 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2656 }
2657 /* user-user */
2658 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2659 rel.fields |= MNCC_F_USERUSER;
2660 decode_useruser(&rel.useruser,
2661 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2662 }
2663 /* ss-version */
2664 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2665 rel.fields |= MNCC_F_SSVERSION;
2666 decode_ssversion(&rel.ssversion,
2667 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2668 }
2669
Harald Weltedcaf5652009-07-23 18:56:43 +02002670 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002672 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002674 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002675 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002676 GSM48_MT_CC_RELEASE_COMPL);
2677 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 }
2679
2680 new_cc_state(trans, GSM_CSTATE_NULL);
2681
2682 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002683 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684
2685 return rc;
2686}
2687
2688static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2689{
2690 struct gsm_mncc *rel = arg;
2691 struct msgb *msg = gsm48_msgb_alloc();
2692 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2693
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694 gh->msg_type = GSM48_MT_CC_RELEASE;
2695
2696 trans->callref = 0;
2697
2698 gsm48_stop_cc_timer(trans);
2699 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2700
2701 /* cause */
2702 if (rel->fields & MNCC_F_CAUSE)
2703 encode_cause(msg, 0, &rel->cause);
2704 /* facility */
2705 if (rel->fields & MNCC_F_FACILITY)
2706 encode_facility(msg, 0, &rel->facility);
2707 /* user-user */
2708 if (rel->fields & MNCC_F_USERUSER)
2709 encode_useruser(msg, 0, &rel->useruser);
2710
Harald Weltedcaf5652009-07-23 18:56:43 +02002711 trans->cc.T308_second = 0;
2712 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713
Harald Weltedcaf5652009-07-23 18:56:43 +02002714 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2716
Harald Welte39e2ead2009-07-23 21:13:03 +02002717 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2721{
2722 struct gsm48_hdr *gh = msgb_l3(msg);
2723 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2724 struct tlv_parsed tp;
2725 struct gsm_mncc rel;
2726 int rc = 0;
2727
2728 gsm48_stop_cc_timer(trans);
2729
2730 memset(&rel, 0, sizeof(struct gsm_mncc));
2731 rel.callref = trans->callref;
2732 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2733 /* cause */
2734 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2735 rel.fields |= MNCC_F_CAUSE;
2736 decode_cause(&rel.cause,
2737 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2738 }
2739 /* facility */
2740 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2741 rel.fields |= MNCC_F_FACILITY;
2742 decode_facility(&rel.facility,
2743 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2744 }
2745 /* user-user */
2746 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2747 rel.fields |= MNCC_F_USERUSER;
2748 decode_useruser(&rel.useruser,
2749 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2750 }
2751 /* ss-version */
2752 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2753 rel.fields |= MNCC_F_SSVERSION;
2754 decode_ssversion(&rel.ssversion,
2755 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2756 }
2757
2758 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002759 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002761 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 MNCC_REJ_IND, &rel);
2763 break;
2764 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002765 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002767 /* FIXME: in case of multiple calls, we can't simply
2768 * hang up here ! */
2769 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 break;
2771 default:
Harald Welte596fed42009-07-23 19:06:52 +02002772 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773 MNCC_REL_IND, &rel);
2774 }
2775 }
2776
2777 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002778 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779
2780 return rc;
2781}
2782
2783static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2784{
2785 struct gsm_mncc *rel = arg;
2786 struct msgb *msg = gsm48_msgb_alloc();
2787 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2788
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2790
2791 trans->callref = 0;
2792
2793 gsm48_stop_cc_timer(trans);
2794
2795 /* cause */
2796 if (rel->fields & MNCC_F_CAUSE)
2797 encode_cause(msg, 0, &rel->cause);
2798 /* facility */
2799 if (rel->fields & MNCC_F_FACILITY)
2800 encode_facility(msg, 0, &rel->facility);
2801 /* user-user */
2802 if (rel->fields & MNCC_F_USERUSER)
2803 encode_useruser(msg, 0, &rel->useruser);
2804
Harald Weltedcaf5652009-07-23 18:56:43 +02002805 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002806
Harald Welte39e2ead2009-07-23 21:13:03 +02002807 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808}
2809
2810static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2811{
2812 struct gsm48_hdr *gh = msgb_l3(msg);
2813 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2814 struct tlv_parsed tp;
2815 struct gsm_mncc fac;
2816
2817 memset(&fac, 0, sizeof(struct gsm_mncc));
2818 fac.callref = trans->callref;
2819 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2820 /* facility */
2821 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2822 fac.fields |= MNCC_F_FACILITY;
2823 decode_facility(&fac.facility,
2824 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2825 }
2826 /* ss-version */
2827 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2828 fac.fields |= MNCC_F_SSVERSION;
2829 decode_ssversion(&fac.ssversion,
2830 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2831 }
2832
Harald Welte596fed42009-07-23 19:06:52 +02002833 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834}
2835
2836static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2837{
2838 struct gsm_mncc *fac = arg;
2839 struct msgb *msg = gsm48_msgb_alloc();
2840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2841
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 gh->msg_type = GSM48_MT_CC_FACILITY;
2843
2844 /* facility */
2845 encode_facility(msg, 1, &fac->facility);
2846
Harald Welte39e2ead2009-07-23 21:13:03 +02002847 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848}
2849
2850static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2851{
2852 struct gsm_mncc hold;
2853
2854 memset(&hold, 0, sizeof(struct gsm_mncc));
2855 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002856 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857}
2858
2859static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2860{
2861 struct msgb *msg = gsm48_msgb_alloc();
2862 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2863
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2865
Harald Welte39e2ead2009-07-23 21:13:03 +02002866 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867}
2868
2869static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2870{
2871 struct gsm_mncc *hold_rej = arg;
2872 struct msgb *msg = gsm48_msgb_alloc();
2873 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2874
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2876
2877 /* cause */
2878 if (hold_rej->fields & MNCC_F_CAUSE)
2879 encode_cause(msg, 1, &hold_rej->cause);
2880 else
2881 encode_cause(msg, 1, &default_cause);
2882
Harald Welte39e2ead2009-07-23 21:13:03 +02002883 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884}
2885
2886static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2887{
2888 struct gsm_mncc retrieve;
2889
2890 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2891 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002892 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2893 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894}
2895
2896static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2897{
2898 struct msgb *msg = gsm48_msgb_alloc();
2899 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2900
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2902
Harald Welte39e2ead2009-07-23 21:13:03 +02002903 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002904}
2905
2906static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2907{
2908 struct gsm_mncc *retrieve_rej = arg;
2909 struct msgb *msg = gsm48_msgb_alloc();
2910 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2911
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2913
2914 /* cause */
2915 if (retrieve_rej->fields & MNCC_F_CAUSE)
2916 encode_cause(msg, 1, &retrieve_rej->cause);
2917 else
2918 encode_cause(msg, 1, &default_cause);
2919
Harald Welte39e2ead2009-07-23 21:13:03 +02002920 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921}
2922
2923static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2924{
2925 struct gsm48_hdr *gh = msgb_l3(msg);
2926 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2927 struct tlv_parsed tp;
2928 struct gsm_mncc dtmf;
2929
2930 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2931 dtmf.callref = trans->callref;
2932 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2933 /* keypad facility */
2934 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2935 dtmf.fields |= MNCC_F_KEYPAD;
2936 decode_keypad(&dtmf.keypad,
2937 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2938 }
2939
Harald Welte596fed42009-07-23 19:06:52 +02002940 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941}
2942
2943static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2944{
2945 struct gsm_mncc *dtmf = arg;
2946 struct msgb *msg = gsm48_msgb_alloc();
2947 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2948
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2950
2951 /* keypad */
2952 if (dtmf->fields & MNCC_F_KEYPAD)
2953 encode_keypad(msg, dtmf->keypad);
2954
Harald Welte39e2ead2009-07-23 21:13:03 +02002955 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956}
2957
2958static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2959{
2960 struct gsm_mncc *dtmf = arg;
2961 struct msgb *msg = gsm48_msgb_alloc();
2962 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2963
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2965
2966 /* cause */
2967 if (dtmf->fields & MNCC_F_CAUSE)
2968 encode_cause(msg, 1, &dtmf->cause);
2969 else
2970 encode_cause(msg, 1, &default_cause);
2971
Harald Welte39e2ead2009-07-23 21:13:03 +02002972 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973}
2974
2975static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2976{
2977 struct msgb *msg = gsm48_msgb_alloc();
2978 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2979
Harald Welte4bfdfe72009-06-10 23:11:52 +08002980 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2981
Harald Welte39e2ead2009-07-23 21:13:03 +02002982 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983}
2984
2985static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2986{
2987 struct gsm_mncc dtmf;
2988
2989 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2990 dtmf.callref = trans->callref;
2991
Harald Welte596fed42009-07-23 19:06:52 +02002992 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993}
2994
2995static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2996{
2997 struct gsm48_hdr *gh = msgb_l3(msg);
2998 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2999 struct tlv_parsed tp;
3000 struct gsm_mncc modify;
3001
3002 memset(&modify, 0, sizeof(struct gsm_mncc));
3003 modify.callref = trans->callref;
3004 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3005 /* bearer capability */
3006 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3007 modify.fields |= MNCC_F_BEARER_CAP;
3008 decode_bearer_cap(&modify.bearer_cap,
3009 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3010 }
3011
3012 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3013
Harald Welte596fed42009-07-23 19:06:52 +02003014 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015}
3016
3017static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3018{
3019 struct gsm_mncc *modify = arg;
3020 struct msgb *msg = gsm48_msgb_alloc();
3021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3022
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 gh->msg_type = GSM48_MT_CC_MODIFY;
3024
3025 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3026
3027 /* bearer capability */
3028 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3029
3030 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3031
Harald Welte39e2ead2009-07-23 21:13:03 +02003032 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033}
3034
3035static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3036{
3037 struct gsm48_hdr *gh = msgb_l3(msg);
3038 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3039 struct tlv_parsed tp;
3040 struct gsm_mncc modify;
3041
3042 gsm48_stop_cc_timer(trans);
3043
3044 memset(&modify, 0, sizeof(struct gsm_mncc));
3045 modify.callref = trans->callref;
3046 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3047 /* bearer capability */
3048 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3049 modify.fields |= MNCC_F_BEARER_CAP;
3050 decode_bearer_cap(&modify.bearer_cap,
3051 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3052 }
3053
3054 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3055
Harald Welte596fed42009-07-23 19:06:52 +02003056 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057}
3058
3059static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3060{
3061 struct gsm_mncc *modify = arg;
3062 struct msgb *msg = gsm48_msgb_alloc();
3063 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3064
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3066
3067 /* bearer capability */
3068 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3069
3070 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3071
Harald Welte39e2ead2009-07-23 21:13:03 +02003072 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073}
3074
3075static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3076{
3077 struct gsm48_hdr *gh = msgb_l3(msg);
3078 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3079 struct tlv_parsed tp;
3080 struct gsm_mncc modify;
3081
3082 gsm48_stop_cc_timer(trans);
3083
3084 memset(&modify, 0, sizeof(struct gsm_mncc));
3085 modify.callref = trans->callref;
3086 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3087 /* bearer capability */
3088 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3089 modify.fields |= GSM48_IE_BEARER_CAP;
3090 decode_bearer_cap(&modify.bearer_cap,
3091 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3092 }
3093 /* cause */
3094 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3095 modify.fields |= MNCC_F_CAUSE;
3096 decode_cause(&modify.cause,
3097 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3098 }
3099
3100 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3101
Harald Welte596fed42009-07-23 19:06:52 +02003102 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103}
3104
3105static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3106{
3107 struct gsm_mncc *modify = arg;
3108 struct msgb *msg = gsm48_msgb_alloc();
3109 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3110
Harald Welte4bfdfe72009-06-10 23:11:52 +08003111 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3112
3113 /* bearer capability */
3114 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3115 /* cause */
3116 encode_cause(msg, 1, &modify->cause);
3117
3118 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3119
Harald Welte39e2ead2009-07-23 21:13:03 +02003120 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003121}
3122
3123static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3124{
3125 struct gsm_mncc *notify = arg;
3126 struct msgb *msg = gsm48_msgb_alloc();
3127 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3128
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 gh->msg_type = GSM48_MT_CC_NOTIFY;
3130
3131 /* notify */
3132 encode_notify(msg, notify->notify);
3133
Harald Welte39e2ead2009-07-23 21:13:03 +02003134 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003135}
3136
3137static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3138{
3139 struct gsm48_hdr *gh = msgb_l3(msg);
3140 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3141// struct tlv_parsed tp;
3142 struct gsm_mncc notify;
3143
3144 memset(&notify, 0, sizeof(struct gsm_mncc));
3145 notify.callref = trans->callref;
3146// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3147 if (payload_len >= 1)
3148 decode_notify(&notify.notify, gh->data);
3149
Harald Welte596fed42009-07-23 19:06:52 +02003150 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151}
3152
3153static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3154{
3155 struct gsm_mncc *user = arg;
3156 struct msgb *msg = gsm48_msgb_alloc();
3157 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3158
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 gh->msg_type = GSM48_MT_CC_USER_INFO;
3160
3161 /* user-user */
3162 if (user->fields & MNCC_F_USERUSER)
3163 encode_useruser(msg, 1, &user->useruser);
3164 /* more data */
3165 if (user->more)
3166 encode_more(msg);
3167
Harald Welte39e2ead2009-07-23 21:13:03 +02003168 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169}
3170
3171static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3172{
3173 struct gsm48_hdr *gh = msgb_l3(msg);
3174 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3175 struct tlv_parsed tp;
3176 struct gsm_mncc user;
3177
3178 memset(&user, 0, sizeof(struct gsm_mncc));
3179 user.callref = trans->callref;
3180 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3181 /* user-user */
3182 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3183 user.fields |= MNCC_F_USERUSER;
3184 decode_useruser(&user.useruser,
3185 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3186 }
3187 /* more data */
3188 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3189 user.more = 1;
3190
Harald Welte596fed42009-07-23 19:06:52 +02003191 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003192}
3193
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003194static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003195{
3196 struct gsm_mncc *mode = arg;
3197
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003198 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003199}
3200
3201static struct downstate {
3202 u_int32_t states;
3203 int type;
3204 int (*rout) (struct gsm_trans *trans, void *arg);
3205} downstatelist[] = {
3206 /* mobile originating call establishment */
3207 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3208 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3209 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3210 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3211 {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 */
3212 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3213 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3214 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3215 /* mobile terminating call establishment */
3216 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3217 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3218 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3219 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3220 /* signalling during call */
3221 {SBIT(GSM_CSTATE_ACTIVE),
3222 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3223 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3224 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3225 {ALL_STATES,
3226 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3227 {ALL_STATES,
3228 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3229 {ALL_STATES,
3230 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3231 {SBIT(GSM_CSTATE_ACTIVE),
3232 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3233 {SBIT(GSM_CSTATE_ACTIVE),
3234 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3235 {SBIT(GSM_CSTATE_ACTIVE),
3236 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3237 {SBIT(GSM_CSTATE_ACTIVE),
3238 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3239 {SBIT(GSM_CSTATE_ACTIVE),
3240 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3241 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3242 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3243 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3244 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3245 {SBIT(GSM_CSTATE_ACTIVE),
3246 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3247 /* clearing */
3248 {SBIT(GSM_CSTATE_INITIATED),
3249 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3250 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3251 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3252 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3253 MNCC_REL_REQ, gsm48_cc_tx_release},
3254 /* special */
3255 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003256 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257};
3258
3259#define DOWNSLLEN \
3260 (sizeof(downstatelist) / sizeof(struct downstate))
3261
3262
3263int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3264{
Harald Welte1a6f7982009-08-09 18:52:33 +02003265 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003267 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003268 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 struct gsm_mncc *data = arg, rel;
3270
3271 /* handle special messages */
3272 switch(msg_type) {
3273 case MNCC_BRIDGE:
3274 return tch_bridge(net, arg);
3275 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003276 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003277 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003278 return tch_recv_mncc(net, data->callref, 1);
3279 case GSM_TCHF_FRAME:
3280 /* Find callref */
3281 trans = trans_find_by_callref(net, data->callref);
3282 if (!trans)
3283 return -EIO;
3284 if (!trans->lchan)
3285 return 0;
3286 if (trans->lchan->type != GSM_LCHAN_TCH_F)
3287 return 0;
3288 bts = trans->lchan->ts->trx->bts;
3289 switch (bts->type) {
3290 case GSM_BTS_TYPE_NANOBTS:
3291 if (!trans->lchan->abis_ip.rtp_socket)
3292 return 0;
3293 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
3294 case GSM_BTS_TYPE_BS11:
3295 return trau_send_frame(trans->lchan, arg);
3296 default:
3297 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
3298 }
3299 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003300 }
3301
3302 memset(&rel, 0, sizeof(struct gsm_mncc));
3303 rel.callref = data->callref;
3304
3305 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003306 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003307
3308 /* Callref unknown */
3309 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003310 struct gsm_subscriber *subscr;
3311
Harald Welte4a3464c2009-07-04 10:11:24 +02003312 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3314 "Received '%s' from MNCC with "
3315 "unknown callref %d\n", data->called.number,
3316 get_mncc_name(msg_type), data->callref);
3317 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003318 return mncc_release_ind(net, NULL, data->callref,
3319 GSM48_CAUSE_LOC_PRN_S_LU,
3320 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003321 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003322 if (!data->called.number[0] && !data->imsi[0]) {
3323 DEBUGP(DCC, "(bts - trx - ts - ti) "
3324 "Received '%s' from MNCC with "
3325 "no number or IMSI\n", get_mncc_name(msg_type));
3326 /* Invalid number */
3327 return mncc_release_ind(net, NULL, data->callref,
3328 GSM48_CAUSE_LOC_PRN_S_LU,
3329 GSM48_CC_CAUSE_INV_NR_FORMAT);
3330 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003331 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003332 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003333 subscr = subscr_get_by_extension(net,
3334 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003335 else
Harald Welte9176bd42009-07-23 18:46:00 +02003336 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003337 /* If subscriber is not found */
3338 if (!subscr) {
3339 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3340 "Received '%s' from MNCC with "
3341 "unknown subscriber %s\n", data->called.number,
3342 get_mncc_name(msg_type), data->called.number);
3343 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003344 return mncc_release_ind(net, NULL, data->callref,
3345 GSM48_CAUSE_LOC_PRN_S_LU,
3346 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003347 }
3348 /* If subscriber is not "attached" */
3349 if (!subscr->lac) {
3350 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3351 "Received '%s' from MNCC with "
3352 "detached subscriber %s\n", data->called.number,
3353 get_mncc_name(msg_type), data->called.number);
3354 subscr_put(subscr);
3355 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003356 return mncc_release_ind(net, NULL, data->callref,
3357 GSM48_CAUSE_LOC_PRN_S_LU,
3358 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003359 }
3360 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003361 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3362 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003363 DEBUGP(DCC, "No memory for trans.\n");
3364 subscr_put(subscr);
3365 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003366 mncc_release_ind(net, NULL, data->callref,
3367 GSM48_CAUSE_LOC_PRN_S_LU,
3368 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 return -ENOMEM;
3370 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003371 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003372 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003373 /* If subscriber has no lchan */
3374 if (!lchan) {
3375 /* find transaction with this subscriber already paging */
3376 llist_for_each_entry(transt, &net->trans_list, entry) {
3377 /* Transaction of our lchan? */
3378 if (transt == trans ||
3379 transt->subscr != subscr)
3380 continue;
3381 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3382 "Received '%s' from MNCC with "
3383 "unallocated channel, paging already "
3384 "started.\n", bts->nr,
3385 data->called.number,
3386 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003387 subscr_put(subscr);
3388 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003389 return 0;
3390 }
3391 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003392 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003393 /* Trigger paging */
3394 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3395 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003396 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003397 return 0;
3398 }
3399 /* Assign lchan */
3400 trans->lchan = lchan;
3401 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003402 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003403 }
3404 lchan = trans->lchan;
3405
3406 /* if paging did not respond yet */
3407 if (!lchan) {
3408 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3409 "Received '%s' from MNCC in paging state\n",
3410 (trans->subscr)?(trans->subscr->extension):"-",
3411 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003412 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3413 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003414 if (msg_type == MNCC_REL_REQ)
3415 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3416 else
3417 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3418 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003419 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003420 return rc;
3421 }
3422
3423 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3424 "Received '%s' from MNCC in state %d (%s)\n",
3425 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3426 trans->transaction_id,
3427 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003428 get_mncc_name(msg_type), trans->cc.state,
3429 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003430
3431 /* Find function for current state and message */
3432 for (i = 0; i < DOWNSLLEN; i++)
3433 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003434 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003435 break;
3436 if (i == DOWNSLLEN) {
3437 DEBUGP(DCC, "Message unhandled at this state.\n");
3438 return 0;
3439 }
3440
3441 rc = downstatelist[i].rout(trans, arg);
3442
3443 return rc;
3444}
3445
3446
3447static struct datastate {
3448 u_int32_t states;
3449 int type;
3450 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3451} datastatelist[] = {
3452 /* mobile originating call establishment */
3453 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3454 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3455 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3456 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3457 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3458 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3459 /* mobile terminating call establishment */
3460 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3461 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3462 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3463 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3464 {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 */
3465 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3466 /* signalling during call */
3467 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3468 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3469 {SBIT(GSM_CSTATE_ACTIVE),
3470 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3471 {ALL_STATES,
3472 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3473 {ALL_STATES,
3474 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3475 {ALL_STATES,
3476 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3477 {SBIT(GSM_CSTATE_ACTIVE),
3478 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3479 {SBIT(GSM_CSTATE_ACTIVE),
3480 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3481 {SBIT(GSM_CSTATE_ACTIVE),
3482 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3483 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3484 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3485 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3486 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3487 {SBIT(GSM_CSTATE_ACTIVE),
3488 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3489 /* clearing */
3490 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3491 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3492 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3493 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3494 {ALL_STATES, /* 5.4.3.4 */
3495 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3496};
3497
3498#define DATASLLEN \
3499 (sizeof(datastatelist) / sizeof(struct datastate))
3500
Harald Welte4bc90a12008-12-27 16:32:52 +00003501static int gsm0408_rcv_cc(struct msgb *msg)
3502{
3503 struct gsm48_hdr *gh = msgb_l3(msg);
3504 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003505 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003506 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003507 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003508 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003509
Harald Welte4bfdfe72009-06-10 23:11:52 +08003510 if (msg_type & 0x80) {
3511 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3512 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003513 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003514
3515 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003516 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003517
Harald Welte6f5aee02009-07-23 21:21:14 +02003518 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003519 "Received '%s' from MS in state %d (%s)\n",
3520 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3521 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003522 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003523 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003524
3525 /* Create transaction */
3526 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003527 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003528 "creating new trans.\n", transaction_id);
3529 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003530 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3531 transaction_id, new_callref++);
3532 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003533 DEBUGP(DCC, "No memory for trans.\n");
3534 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003535 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003536 GSM48_MT_CC_RELEASE_COMPL);
3537 return -ENOMEM;
3538 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003539 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003540 trans->lchan = lchan;
3541 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003542 }
3543
3544 /* find function for current state and message */
3545 for (i = 0; i < DATASLLEN; i++)
3546 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003547 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003548 break;
3549 if (i == DATASLLEN) {
3550 DEBUGP(DCC, "Message unhandled at this state.\n");
3551 return 0;
3552 }
3553
3554 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003555
3556 return rc;
3557}
3558
Harald Welte52b1f982008-12-23 20:25:15 +00003559/* 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 +02003560int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003561{
3562 struct gsm48_hdr *gh = msgb_l3(msg);
3563 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003564 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003565
3566 switch (pdisc) {
3567 case GSM48_PDISC_CC:
3568 rc = gsm0408_rcv_cc(msg);
3569 break;
3570 case GSM48_PDISC_MM:
3571 rc = gsm0408_rcv_mm(msg);
3572 break;
3573 case GSM48_PDISC_RR:
3574 rc = gsm0408_rcv_rr(msg);
3575 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003576 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003577 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003578 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003579 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003580 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003581 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003582 pdisc);
3583 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003584 case GSM48_PDISC_NC_SS:
3585 rc = handle_rcv_ussd(msg);
3586 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003587 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003588 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003589 pdisc);
3590 break;
3591 }
3592
3593 return rc;
3594}
Harald Welte8470bf22008-12-25 23:28:35 +00003595
Harald Welte4bfdfe72009-06-10 23:11:52 +08003596/* dequeue messages to layer 4 */
3597int bsc_upqueue(struct gsm_network *net)
3598{
3599 struct gsm_mncc *mncc;
3600 struct msgb *msg;
3601 int work = 0;
3602
3603 if (net)
3604 while ((msg = msgb_dequeue(&net->upqueue))) {
3605 mncc = (struct gsm_mncc *)msg->data;
3606 if (net->mncc_recv)
3607 net->mncc_recv(net, mncc->msg_type, mncc);
3608 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003609 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003610 }
3611
3612 return work;
3613}
Harald Weltedcaf5652009-07-23 18:56:43 +02003614
Harald Welte805f6442009-07-28 18:25:29 +02003615/*
3616 * This will be ran by the linker when loading the DSO. We use it to
3617 * do system initialization, e.g. registration of signal handlers.
3618 */
3619static __attribute__((constructor)) void on_dso_load_0408(void)
3620{
Harald Welte805f6442009-07-28 18:25:29 +02003621 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3622 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3623}