blob: 1db89c964d4a70c99ddf3fa3a5e1efc61dc044ee [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 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
Sylvain Munaut30a15382009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
44#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000045#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000046#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000047#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000048#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000049#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020050#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010051#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010052#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000056
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020057void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010058void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020059
Holger Freytherd51524f2009-06-09 08:27:07 +000060int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000061static int gsm48_tx_simple(struct gsm_lchan *lchan,
62 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010063static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000064
Harald Welte52b1f982008-12-23 20:25:15 +000065struct gsm_lai {
66 u_int16_t mcc;
67 u_int16_t mnc;
68 u_int16_t lac;
69};
70
Harald Welte4bfdfe72009-06-10 23:11:52 +080071static u_int32_t new_callref = 0x80000001;
72
Sylvain Munaut30a15382009-12-24 00:27:26 +010073
74static void release_security_operation(struct gsm_subscriber_connection *conn)
75{
76 if (!conn->sec_operation)
77 return;
78
79 talloc_free(conn->sec_operation);
80 conn->sec_operation = NULL;
81 put_subscr_con(conn);
82}
83
84static void allocate_security_operation(struct gsm_subscriber_connection *conn)
85{
86 use_subscr_con(conn)
87
88 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
89 struct gsm_security_operation);
90}
91
92int gsm48_secure_channel(struct gsm_lchan *lchan, int key_seq,
93 gsm_cbfn *cb, void *cb_data)
94{
95 struct gsm_network *net = lchan->ts->trx->bts->network;
96 struct gsm_subscriber *subscr = lchan->conn.subscr;
97 struct gsm_security_operation *op;
98 struct gsm_auth_tuple atuple;
99 int status = -1, rc;
100
101 /* Check if we _can_ enable encryption. Cases where we can't:
102 * - Encryption disabled in config
103 * - Channel already secured (nothing to do)
104 * - Subscriber equipment doesn't support configured encryption
105 */
106 if (!net->a5_encryption) {
107 status = GSM_SECURITY_NOAVAIL;
108 } else if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
109 DEBUGP(DMM, "Requesting to secure an already secure channel");
110 status = GSM_SECURITY_SUCCEEDED;
111 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
112 net->a5_encryption)) {
113 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
114 status = GSM_SECURITY_NOAVAIL;
115 }
116
117 /* If not done yet, try to get info for this user */
118 if (status < 0) {
119 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
120 if (rc <= 0)
121 status = GSM_SECURITY_NOAVAIL;
122 }
123
124 /* Are we done yet ? */
125 if (status >= 0)
126 return cb ?
127 cb(GSM_HOOK_RR_SECURITY, status, NULL, lchan, cb_data) :
128 0;
129
130 /* Start an operation (can't have more than one pending !!!) */
131 if (lchan->conn.sec_operation)
132 return -EBUSY;
133
134 allocate_security_operation(&lchan->conn);
135 op = lchan->conn.sec_operation;
136 op->cb = cb;
137 op->cb_data = cb_data;
138 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
139
140 /* FIXME: Should start a timer for completion ... */
141
142 /* Then do whatever is needed ... */
143 if (rc == 1) {
144 /* Start authentication */
145 return gsm48_tx_mm_auth_req(lchan, op->atuple.rand, op->atuple.key_seq);
146 } else if (rc == 2) {
147 /* Start ciphering directly */
148 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
149 lchan->encr.key_len = 8;
150 memcpy(lchan->encr.key, op->atuple.kc, 8);
151
152 return gsm48_send_rr_ciph_mode(lchan, 0);
153 }
154
155 return -EINVAL; /* not reached */
156}
157
Holger Freyther73487a22008-12-31 18:53:57 +0000158static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
159 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000160{
161 if (!subscriber)
162 return 0;
163
Holger Freyther73487a22008-12-31 18:53:57 +0000164 /*
165 * Do not send accept yet as more information should arrive. Some
166 * phones will not send us the information and we will have to check
167 * what we want to do with that.
168 */
169 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
170 return 0;
171
Jan Luebbe06513f22009-08-12 12:48:00 +0200172 switch (subscriber->net->auth_policy) {
173 case GSM_AUTH_POLICY_CLOSED:
174 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200175 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200176 if (subscriber->authorized)
177 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200178 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200179 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000180 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200181 default:
182 return 0;
183 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000184}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000185
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100186static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000187{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100188 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000189 return;
190
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100191 bsc_del_timer(&conn->loc_operation->updating_timer);
192 talloc_free(conn->loc_operation);
193 conn->loc_operation = 0;
194 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000195}
196
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100197static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000198{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100199 use_subscr_con(conn)
200 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000201
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100202 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200203 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000204}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000205
Sylvain Munaut267fba02009-12-24 00:28:01 +0100206static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
207 struct msgb *msg, void *data, void *param)
208{
209 struct gsm_lchan *lchan = data;
210 struct gsm_subscriber_connection *conn = &lchan->conn;
211 int rc = 0;
212
213 switch (event) {
214 case GSM_SECURITY_AUTH_FAILED:
215 release_loc_updating_req(conn);
216 break;
217
218 case GSM_SECURITY_NOAVAIL:
219 case GSM_SECURITY_SUCCEEDED:
220 /* We're all good */
221 db_subscriber_alloc_tmsi(conn->subscr);
222 release_loc_updating_req(conn);
223 rc = gsm0408_loc_upd_acc(lchan, conn->subscr->tmsi);
224 if (lchan->ts->trx->bts->network->send_mm_info) {
225 /* send MM INFO with network name */
226 rc = gsm48_tx_mm_info(lchan);
227 }
228
229 /* call subscr_update after putting the loc_upd_acc
230 * in the transmit queue, since S_SUBSCR_ATTACHED might
231 * trigger further action like SMS delivery */
232 subscr_update(conn->subscr, lchan->ts->trx->bts,
233 GSM_SUBSCRIBER_UPDATE_ATTACHED);
234
235 /* try to close channel ASAP */
236 lchan_auto_release(lchan);
237
238 break;
239
240 default:
241 rc = -EINVAL;
242 };
243
244 return rc;
245}
246
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100247static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000248{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100249 if (authorize_subscriber(conn->loc_operation, conn->subscr))
250 return gsm48_secure_channel(conn->lchan,
251 conn->loc_operation->key_seq,
252 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000253 return 0;
254}
255
Holger Freyther7c19f742009-06-06 13:54:35 +0000256static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
257 void *handler_data, void *signal_data)
258{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800259 struct gsm_trans *trans, *temp;
260
Holger Freyther7c19f742009-06-06 13:54:35 +0000261 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
262 return 0;
263
264 /*
265 * Cancel any outstanding location updating request
266 * operation taking place on the lchan.
267 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200268 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200269 if (!lchan)
270 return 0;
271
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100272 release_loc_updating_req(&lchan->conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100273 release_security_operation(&lchan->conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000274
Harald Welte4bfdfe72009-06-10 23:11:52 +0800275 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200276 /* FIXME: this is not neccessarily the right thing to do, we should
277 * only set trans->lchan to NULL and wait for another lchan to be
278 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800279 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100280 if (trans->conn && trans->conn->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200281 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800282 }
283
Holger Freyther7c19f742009-06-06 13:54:35 +0000284 return 0;
285}
286
Holger Freyther429e7762008-12-30 13:28:30 +0000287/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000288int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000289{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100290 struct gsm_subscriber_connection *conn;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100291 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000292 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000293 struct gsm48_hdr *gh;
294
Harald Welte8470bf22008-12-25 23:28:35 +0000295 msg->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100296 conn = &lchan->conn;
Harald Welte52b1f982008-12-23 20:25:15 +0000297
298 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
299 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000300 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000301 gh->data[0] = cause;
302
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100303 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100304 "LAC=%u BTS=%u\n", conn->subscr ?
305 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100306 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100307
Harald Welteffa55a42009-12-22 19:07:32 +0100308 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000309
Harald Welte39e2ead2009-07-23 21:13:03 +0200310 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000311}
312
313/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000314int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000315{
Harald Welte8470bf22008-12-25 23:28:35 +0000316 struct gsm_bts *bts = lchan->ts->trx->bts;
317 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000318 struct gsm48_hdr *gh;
319 struct gsm48_loc_area_id *lai;
320 u_int8_t *mid;
321
Harald Welte8470bf22008-12-25 23:28:35 +0000322 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000323
324 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
325 gh->proto_discr = GSM48_PDISC_MM;
326 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
327
328 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100329 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000330 bts->network->network_code, bts->location_area_code);
331
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200332 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200333 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000334
335 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
336
Harald Welteffa55a42009-12-22 19:07:32 +0100337 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100338
Harald Welteb83d9382009-12-12 21:00:48 +0100339 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000340}
341
Harald Weltebf5e8df2009-02-03 12:59:45 +0000342/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000343static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
344{
345 struct msgb *msg = gsm48_msgb_alloc();
346 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000347
Harald Welte231ad4f2008-12-27 11:15:38 +0000348 msg->lchan = lchan;
349
350 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
351 gh->proto_discr = GSM48_PDISC_MM;
352 gh->msg_type = GSM48_MT_MM_ID_REQ;
353 gh->data[0] = id_type;
354
Harald Welte39e2ead2009-07-23 21:13:03 +0200355 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000356}
357
Harald Welte231ad4f2008-12-27 11:15:38 +0000358
Harald Weltebf5e8df2009-02-03 12:59:45 +0000359/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000360static int mm_rx_id_resp(struct msgb *msg)
361{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100362 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000363 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000364 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200365 struct gsm_bts *bts = lchan->ts->trx->bts;
366 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000367 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200368 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000369
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200370 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000371 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000372 mi_type, mi_string);
373
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100374 conn = &lchan->conn;
375
Harald Welte7659de12009-12-13 12:39:18 +0100376 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
377
Harald Welte75a983f2008-12-27 21:34:06 +0000378 switch (mi_type) {
379 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200380 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100381 if (!conn->subscr) {
382 conn->subscr = subscr_get_by_imsi(net, mi_string);
383 if (!conn->subscr)
384 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200385 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100386 if (conn->loc_operation)
387 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000388 break;
389 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000390 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000391 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100392 if (conn->subscr) {
393 db_subscriber_assoc_imei(conn->subscr, mi_string);
394 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200395 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100396 if (conn->loc_operation)
397 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000398 break;
399 }
Holger Freyther73487a22008-12-31 18:53:57 +0000400
401 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100402 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000403}
404
Harald Welte255539c2008-12-28 02:26:27 +0000405
406static void loc_upd_rej_cb(void *data)
407{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100408 struct gsm_subscriber_connection *conn = data;
409 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100410 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000411
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100412 release_loc_updating_req(conn);
Harald Welte1085c092009-11-18 20:33:19 +0100413 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000414 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000415}
416
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100417static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000418{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100419 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
420 conn->loc_operation->updating_timer.data = conn;
421 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000422}
423
Harald Welte2a139372009-02-22 21:14:55 +0000424static const char *lupd_name(u_int8_t type)
425{
426 switch (type) {
427 case GSM48_LUPD_NORMAL:
428 return "NORMAL";
429 case GSM48_LUPD_PERIODIC:
430 return "PEROIDOC";
431 case GSM48_LUPD_IMSI_ATT:
432 return "IMSI ATTACH";
433 default:
434 return "UNKNOWN";
435 }
436}
437
Harald Weltebf5e8df2009-02-03 12:59:45 +0000438/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000439static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000440{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100441 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000442 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000443 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000445 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200446 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000447 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200448 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000449 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000450
Harald Welte8470bf22008-12-25 23:28:35 +0000451 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100452 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000453
454 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000455
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200456 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000457
Harald Weltea0368542009-06-27 02:58:43 +0200458 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000459 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000460
Harald Welte7659de12009-12-13 12:39:18 +0100461 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
462
Harald Welte24ff6ee2009-12-22 00:41:05 +0100463 switch (lu->type) {
464 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100465 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100466 break;
467 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100468 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100469 break;
470 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100471 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100472 break;
473 }
474
Holger Freythereaf04692009-06-06 13:54:44 +0000475 /*
476 * Pseudo Spoof detection: Just drop a second/concurrent
477 * location updating request.
478 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100479 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200480 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100481 conn->loc_operation);
Holger Freythereaf04692009-06-06 13:54:44 +0000482 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
483 return 0;
484 }
485
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100486 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000487
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200488 conn->loc_operation->key_seq = lu->key_seq;
489
Harald Welte52b1f982008-12-23 20:25:15 +0000490 switch (mi_type) {
491 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200492 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000493 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000494 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100495 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000496
Jan Luebbe370b41d2009-08-12 10:19:34 +0200497 /* look up subscriber based on IMSI, create if not found */
498 subscr = subscr_get_by_imsi(bts->network, mi_string);
499 if (!subscr) {
500 subscr = db_create_subscriber(bts->network, mi_string);
501 }
Harald Welte4b634542008-12-27 01:55:51 +0000502 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000503 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200504 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000505 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200506 subscr = subscr_get_by_tmsi(bts->network,
507 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000508 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000509 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000510 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100511 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000512 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200513 /* we always want the IMEI, too */
514 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
515 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000516 break;
517 case GSM_MI_TYPE_IMEI:
518 case GSM_MI_TYPE_IMEISV:
519 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200520 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000521 break;
522 default:
Harald Weltea0368542009-06-27 02:58:43 +0200523 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000524 break;
525 }
526
Harald Welte24516ea2009-07-04 10:18:00 +0200527 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100528 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200529
Harald Welte4bfdfe72009-06-10 23:11:52 +0800530 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200531 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800532 /* FIXME: request id? close channel? */
533 return -EINVAL;
534 }
535
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100536 conn->subscr = subscr;
537 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000538
Harald Welte24516ea2009-07-04 10:18:00 +0200539 /* check if we can let the subscriber into our network immediately
540 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100541 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000542}
543
Harald Welte4bfdfe72009-06-10 23:11:52 +0800544#if 0
545static u_int8_t to_bcd8(u_int8_t val)
546{
547 return ((val / 10) << 4) | (val % 10);
548}
549#endif
550
Harald Weltedb253af2008-12-30 17:56:55 +0000551/* Section 9.2.15a */
552int gsm48_tx_mm_info(struct gsm_lchan *lchan)
553{
554 struct msgb *msg = gsm48_msgb_alloc();
555 struct gsm48_hdr *gh;
556 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000557 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200558 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800559#if 0
560 time_t cur_t;
561 struct tm* cur_time;
562 int tz15min;
563#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000564
565 msg->lchan = lchan;
566
567 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
568 gh->proto_discr = GSM48_PDISC_MM;
569 gh->msg_type = GSM48_MT_MM_INFO;
570
571 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200572#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000573 name_len = strlen(net->name_long);
574 /* 10.5.3.5a */
575 ptr8 = msgb_put(msg, 3);
576 ptr8[0] = GSM48_IE_NAME_LONG;
577 ptr8[1] = name_len*2 +1;
578 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
579
580 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
581 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000582 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000583
584 /* FIXME: Use Cell Broadcast, not UCS-2, since
585 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200586#endif
587 name_len = (strlen(net->name_long)*7)/8;
588 name_pad = (8 - strlen(net->name_long)*7)%8;
589 if (name_pad > 0)
590 name_len++;
591 /* 10.5.3.5a */
592 ptr8 = msgb_put(msg, 3);
593 ptr8[0] = GSM48_IE_NAME_LONG;
594 ptr8[1] = name_len +1;
595 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
596
597 ptr8 = msgb_put(msg, name_len);
598 gsm_7bit_encode(ptr8, net->name_long);
599
Harald Weltedb253af2008-12-30 17:56:55 +0000600 }
601
602 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200603#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000604 name_len = strlen(net->name_short);
605 /* 10.5.3.5a */
606 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200607 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000608 ptr8[1] = name_len*2 + 1;
609 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
610
Harald Weltee872cb12009-01-01 00:33:37 +0000611 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000612 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000613 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200614#endif
615 name_len = (strlen(net->name_short)*7)/8;
616 name_pad = (8 - strlen(net->name_short)*7)%8;
617 if (name_pad > 0)
618 name_len++;
619 /* 10.5.3.5a */
620 ptr8 = (u_int8_t *) msgb_put(msg, 3);
621 ptr8[0] = GSM48_IE_NAME_SHORT;
622 ptr8[1] = name_len +1;
623 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
624
625 ptr8 = msgb_put(msg, name_len);
626 gsm_7bit_encode(ptr8, net->name_short);
627
Harald Weltedb253af2008-12-30 17:56:55 +0000628 }
629
630#if 0
631 /* Section 10.5.3.9 */
632 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800633 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000634 ptr8 = msgb_put(msg, 8);
635 ptr8[0] = GSM48_IE_NET_TIME_TZ;
636 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
637 ptr8[2] = to_bcd8(cur_time->tm_mon);
638 ptr8[3] = to_bcd8(cur_time->tm_mday);
639 ptr8[4] = to_bcd8(cur_time->tm_hour);
640 ptr8[5] = to_bcd8(cur_time->tm_min);
641 ptr8[6] = to_bcd8(cur_time->tm_sec);
642 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
643 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000645 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800646 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000647#endif
648
Daniel Willmanneea93372009-08-13 03:42:07 +0200649 DEBUGP(DMM, "-> MM INFO\n");
650
Harald Welte39e2ead2009-07-23 21:13:03 +0200651 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000652}
653
Harald Welte7984d5c2009-08-12 22:56:50 +0200654/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100655int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200656{
657 struct msgb *msg = gsm48_msgb_alloc();
658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200659 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200660
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100661 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200662
663 msg->lchan = lchan;
664 gh->proto_discr = GSM48_PDISC_MM;
665 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
666
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100667 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200668
Harald Welte7984d5c2009-08-12 22:56:50 +0200669 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200670 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200671 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200672
673 return gsm48_sendmsg(msg, NULL);
674}
675
676/* Section 9.2.1 */
677int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
678{
679 DEBUGP(DMM, "-> AUTH REJECT\n");
680 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
681}
682
Harald Welte4b634542008-12-27 01:55:51 +0000683static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
684{
Harald Welte4b634542008-12-27 01:55:51 +0000685 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000686 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000687}
Harald Welteba4cf162009-01-10 01:49:35 +0000688
689/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100690static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000691 enum gsm48_reject_value value)
692{
693 struct msgb *msg = gsm48_msgb_alloc();
694 struct gsm48_hdr *gh;
695
696 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
697
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100698 msg->lchan = conn->lchan;
699 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000700
701 gh->proto_discr = GSM48_PDISC_MM;
702 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
703 gh->data[0] = value;
704 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
705
Harald Welte39e2ead2009-07-23 21:13:03 +0200706 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000707}
708
Harald Welte4ed0e922009-01-10 03:17:30 +0000709/*
710 * Handle CM Service Requests
711 * a) Verify that the packet is long enough to contain the information
712 * we require otherwsie reject with INCORRECT_MESSAGE
713 * b) Try to parse the TMSI. If we do not have one reject
714 * c) Check that we know the subscriber with the TMSI otherwise reject
715 * with a HLR cause
716 * d) Set the subscriber on the gsm_lchan and accept
717 */
Harald Welte4b634542008-12-27 01:55:51 +0000718static int gsm48_rx_mm_serv_req(struct msgb *msg)
719{
Harald Welteba4cf162009-01-10 01:49:35 +0000720 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200721 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000722
Harald Welte9176bd42009-07-23 18:46:00 +0200723 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000724 struct gsm_subscriber *subscr;
725 struct gsm48_hdr *gh = msgb_l3(msg);
726 struct gsm48_service_request *req =
727 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800728 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000729 u_int8_t classmark2_len = gh->data[1];
730 u_int8_t *classmark2 = gh->data+2;
731 u_int8_t mi_len = *(classmark2 + classmark2_len);
732 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000733
Harald Weltec9e02182009-05-01 19:07:53 +0000734 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000735 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000736 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100737 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000738 GSM48_REJECT_INCORRECT_MESSAGE);
739 }
740
741 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000742 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100743 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000744 GSM48_REJECT_INCORRECT_MESSAGE);
745 }
746
Harald Weltec9e02182009-05-01 19:07:53 +0000747 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000748 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000749 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100750 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000751 GSM48_REJECT_INCORRECT_MESSAGE);
752 }
753
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200754 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000755 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000756 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000757
Harald Welte7659de12009-12-13 12:39:18 +0100758 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
759
Harald Welte3ac7f102009-08-10 10:12:45 +0200760 if (is_siemens_bts(bts))
761 send_siemens_mrpci(msg->lchan, classmark2-1);
762
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200763 subscr = subscr_get_by_tmsi(bts->network,
764 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000765
Harald Welte2a139372009-02-22 21:14:55 +0000766 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000767 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100768 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000769 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
770
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100771 if (!msg->lchan->conn.subscr)
772 msg->lchan->conn.subscr = subscr;
773 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100774 subscr_put(subscr); /* lchan already has a ref, don't need another one */
775 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000776 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
777 subscr_put(subscr);
778 }
779
Harald Weltec2e302d2009-07-05 14:08:13 +0200780 subscr->equipment.classmark2_len = classmark2_len;
781 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
782 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000783
Harald Welte4b634542008-12-27 01:55:51 +0000784 return gsm48_tx_mm_serv_ack(msg->lchan);
785}
786
Harald Welte2a139372009-02-22 21:14:55 +0000787static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
788{
Harald Welte9176bd42009-07-23 18:46:00 +0200789 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000790 struct gsm48_hdr *gh = msgb_l3(msg);
791 struct gsm48_imsi_detach_ind *idi =
792 (struct gsm48_imsi_detach_ind *) gh->data;
793 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200794 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800795 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000796
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200797 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000798 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
799 mi_type, mi_string);
800
Harald Welteffa55a42009-12-22 19:07:32 +0100801 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100802
Harald Welte2a139372009-02-22 21:14:55 +0000803 switch (mi_type) {
804 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200805 subscr = subscr_get_by_tmsi(bts->network,
806 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000807 break;
808 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200809 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000810 break;
811 case GSM_MI_TYPE_IMEI:
812 case GSM_MI_TYPE_IMEISV:
813 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000814 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000815 break;
816 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000817 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000818 break;
819 }
820
Holger Freyther4a49e772009-04-12 05:37:29 +0000821 if (subscr) {
822 subscr_update(subscr, msg->trx->bts,
823 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100824 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200825
826 subscr->equipment.classmark1 = idi->classmark1;
827 db_sync_equipment(&subscr->equipment);
828
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000829 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000830 } else
Harald Welte2a139372009-02-22 21:14:55 +0000831 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
832
Harald Welte31981a02009-12-20 09:58:40 +0100833 /* FIXME: iterate over all transactions and release them,
834 * imagine an IMSI DETACH happening during an active call! */
835
Harald Welteb83d9382009-12-12 21:00:48 +0100836 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100837 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100838
Harald Welte2a139372009-02-22 21:14:55 +0000839 return 0;
840}
841
Harald Welted2a7f5a2009-06-05 20:08:20 +0000842static int gsm48_rx_mm_status(struct msgb *msg)
843{
844 struct gsm48_hdr *gh = msgb_l3(msg);
845
846 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
847
848 return 0;
849}
850
Sylvain Munaut30a15382009-12-24 00:27:26 +0100851/* Chapter 9.2.3: Authentication Response */
852static int gsm48_rx_mm_auth_resp(struct msgb *msg)
853{
854 struct gsm48_hdr *gh = msgb_l3(msg);
855 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
856 struct gsm_lchan *lchan = msg->lchan;
857 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
858 struct gsm_network *net = lchan->ts->trx->bts->network;
859
860 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s)\n",
861 hexdump(ar->sres, 4));
862
863 /* Safety check */
864 if (!conn->sec_operation) {
865 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
866 return -EIO;
867 }
868
869 /* Validate SRES */
870 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
871 gsm_cbfn *cb = conn->sec_operation->cb;
872 if (cb)
873 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
874 NULL, lchan, conn->sec_operation->cb_data);
875
876 release_security_operation(conn);
877 return gsm48_tx_mm_auth_rej(lchan);
878 }
879
880 /* Start ciphering */
881 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
882 lchan->encr.key_len = 8;
883 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
884
885 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
886}
887
Harald Weltebf5e8df2009-02-03 12:59:45 +0000888/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000889static int gsm0408_rcv_mm(struct msgb *msg)
890{
891 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800892 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000893
894 switch (gh->msg_type & 0xbf) {
895 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200896 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000897 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000898 break;
899 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000900 rc = mm_rx_id_resp(msg);
901 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000902 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000903 rc = gsm48_rx_mm_serv_req(msg);
904 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000905 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000906 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000907 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000908 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000909 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100910 msg->lchan->conn.subscr ?
911 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000912 "unknown subscriber");
913 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000914 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000915 rc = gsm48_rx_mm_imsi_detach_ind(msg);
916 break;
917 case GSM48_MT_MM_CM_REEST_REQ:
918 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
919 break;
920 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100921 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000922 break;
923 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100924 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000925 gh->msg_type);
926 break;
927 }
928
929 return rc;
930}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000931
Harald Welte2d35ae62009-02-06 12:02:13 +0000932/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200933static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000934{
Harald Welte9176bd42009-07-23 18:46:00 +0200935 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000936 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800937 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000938 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200939 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200940 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800941 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000942 int rc = 0;
943
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800944 resp = (struct gsm48_pag_resp *) &gh->data[0];
945 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
946 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000947 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
948 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200949
Harald Weltefe18d8f2009-02-22 21:14:24 +0000950 switch (mi_type) {
951 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200952 subscr = subscr_get_by_tmsi(bts->network,
953 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000954 break;
955 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200956 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000957 break;
958 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000959
960 if (!subscr) {
961 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000962 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000963 return -EINVAL;
964 }
965 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200966 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000967
Harald Weltec2e302d2009-07-05 14:08:13 +0200968 subscr->equipment.classmark2_len = *classmark2_lv;
969 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
970 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000971
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200972 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000973 return rc;
974}
975
Harald Weltef7c43522009-06-09 20:24:21 +0000976static int gsm48_rx_rr_classmark(struct msgb *msg)
977{
978 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100979 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +0000980 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
981 u_int8_t cm2_len, cm3_len = 0;
982 u_int8_t *cm2, *cm3 = NULL;
983
984 DEBUGP(DRR, "CLASSMARK CHANGE ");
985
986 /* classmark 2 */
987 cm2_len = gh->data[0];
988 cm2 = &gh->data[1];
989 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
990
991 if (payload_len > cm2_len + 1) {
992 /* we must have a classmark3 */
993 if (gh->data[cm2_len+1] != 0x20) {
994 DEBUGPC(DRR, "ERR CM3 TAG\n");
995 return -EINVAL;
996 }
997 if (cm2_len > 3) {
998 DEBUGPC(DRR, "CM2 too long!\n");
999 return -EINVAL;
1000 }
1001
1002 cm3_len = gh->data[cm2_len+2];
1003 cm3 = &gh->data[cm2_len+3];
1004 if (cm3_len > 14) {
1005 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1006 return -EINVAL;
1007 }
1008 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1009 }
1010 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001011 subscr->equipment.classmark2_len = cm2_len;
1012 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001013 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001014 subscr->equipment.classmark3_len = cm3_len;
1015 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001016 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001017 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001018 }
1019
Harald Weltef7c43522009-06-09 20:24:21 +00001020 return 0;
1021}
1022
Harald Weltecf5b3592009-05-01 18:28:42 +00001023static int gsm48_rx_rr_status(struct msgb *msg)
1024{
1025 struct gsm48_hdr *gh = msgb_l3(msg);
1026
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001027 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001028 rr_cause_name(gh->data[0]));
1029
1030 return 0;
1031}
1032
Harald Weltef7c43522009-06-09 20:24:21 +00001033static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1034{
Harald Welted12b0fd2009-12-15 21:36:05 +01001035 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001036
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001037 /* This shouldn't actually end up here, as RSL treats
1038 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1039 * directly into gsm48_parse_meas_rep */
1040 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001041 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001042
1043 return 0;
1044}
1045
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001046static int gsm48_rx_rr_app_info(struct msgb *msg)
1047{
1048 struct gsm48_hdr *gh = msgb_l3(msg);
1049 u_int8_t apdu_id_flags;
1050 u_int8_t apdu_len;
1051 u_int8_t *apdu_data;
1052
1053 apdu_id_flags = gh->data[0];
1054 apdu_len = gh->data[1];
1055 apdu_data = gh->data+2;
1056
1057 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1058 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1059
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001060 return db_apdu_blob_store(msg->lchan->conn.subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001061}
1062
Sylvain Munaut30a15382009-12-24 00:27:26 +01001063/* Chapter 9.1.10 Ciphering Mode Complete */
1064static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1065{
1066 struct gsm_lchan *lchan = msg->lchan;
1067 struct gsm_subscriber_connection *conn = &lchan->conn;
1068 gsm_cbfn *cb;
1069 int rc = 0;
1070
1071 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1072
1073 /* Safety check */
1074 if (!conn->sec_operation) {
1075 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1076 return -EIO;
1077 }
1078
1079 /* FIXME: check for MI (if any) */
1080
1081 /* Call back whatever was in progress (if anything) ... */
1082 cb = conn->sec_operation->cb;
1083 if (cb) {
1084 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1085 NULL, lchan, conn->sec_operation->cb_data);
1086 }
1087
1088 /* Complete the operation */
1089 release_security_operation(conn);
1090
1091 return rc;
1092}
1093
Harald Welted011e8b2009-11-29 22:45:52 +01001094/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001095static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001096{
1097 struct gsm48_hdr *gh = msgb_l3(msg);
1098
1099 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1100 rr_cause_name(gh->data[0]));
1101
1102 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1103 /* FIXME: release old channel */
1104
1105 return 0;
1106}
1107
1108/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001109static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001110{
1111 struct gsm48_hdr *gh = msgb_l3(msg);
1112
1113 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1114 rr_cause_name(gh->data[0]));
1115
1116 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1117 /* FIXME: release allocated new channel */
1118
1119 return 0;
1120}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001121
Harald Weltebf5e8df2009-02-03 12:59:45 +00001122/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001123static int gsm0408_rcv_rr(struct msgb *msg)
1124{
1125 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001126 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001127
1128 switch (gh->msg_type) {
1129 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001130 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001131 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001132 case GSM48_MT_RR_GPRS_SUSP_REQ:
1133 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1134 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001135 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001136 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001137 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001138 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001139 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001140 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001141 case GSM48_MT_RR_STATUS:
1142 rc = gsm48_rx_rr_status(msg);
1143 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001144 case GSM48_MT_RR_MEAS_REP:
1145 rc = gsm48_rx_rr_meas_rep(msg);
1146 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001147 case GSM48_MT_RR_APP_INFO:
1148 rc = gsm48_rx_rr_app_info(msg);
1149 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001150 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001151 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001152 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001153 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001154 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001155 break;
1156 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001157 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001158 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001159 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001160 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1161 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001162 break;
1163 }
1164
Harald Welte2d35ae62009-02-06 12:02:13 +00001165 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001166}
1167
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001168int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001169 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001170{
1171 struct msgb *msg = gsm48_msgb_alloc();
1172 struct gsm48_hdr *gh;
1173
1174 msg->lchan = lchan;
1175
1176 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1177 apdu_id, apdu_len);
1178
1179 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1180 gh->proto_discr = GSM48_PDISC_RR;
1181 gh->msg_type = GSM48_MT_RR_APP_INFO;
1182 gh->data[0] = apdu_id;
1183 gh->data[1] = apdu_len;
1184 memcpy(gh->data+2, apdu, apdu_len);
1185
1186 return gsm48_sendmsg(msg, NULL);
1187}
1188
Harald Welte4bc90a12008-12-27 16:32:52 +00001189/* Call Control */
1190
Harald Welte7584aea2009-02-11 11:44:12 +00001191/* The entire call control code is written in accordance with Figure 7.10c
1192 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1193 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1194 * it for voice */
1195
Harald Welte4bfdfe72009-06-10 23:11:52 +08001196static void new_cc_state(struct gsm_trans *trans, int state)
1197{
1198 if (state > 31 || state < 0)
1199 return;
1200
1201 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001202 gsm48_cc_state_name(trans->cc.state),
1203 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001204
Harald Weltedcaf5652009-07-23 18:56:43 +02001205 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001206}
1207
1208static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001209{
1210 struct msgb *msg = gsm48_msgb_alloc();
1211 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1212 u_int8_t *cause, *call_state;
1213
Harald Welte4bc90a12008-12-27 16:32:52 +00001214 gh->msg_type = GSM48_MT_CC_STATUS;
1215
1216 cause = msgb_put(msg, 3);
1217 cause[0] = 2;
1218 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1219 cause[2] = 0x80 | 30; /* response to status inquiry */
1220
1221 call_state = msgb_put(msg, 1);
1222 call_state[0] = 0xc0 | 0x00;
1223
Harald Welte39e2ead2009-07-23 21:13:03 +02001224 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001225}
1226
Harald Welte6f4b7532008-12-29 00:39:37 +00001227static int gsm48_tx_simple(struct gsm_lchan *lchan,
1228 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001229{
1230 struct msgb *msg = gsm48_msgb_alloc();
1231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1232
1233 msg->lchan = lchan;
1234
Harald Welte6f4b7532008-12-29 00:39:37 +00001235 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001236 gh->msg_type = msg_type;
1237
Harald Welte39e2ead2009-07-23 21:13:03 +02001238 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001239}
1240
Harald Welte4bfdfe72009-06-10 23:11:52 +08001241static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1242{
Harald Weltedcaf5652009-07-23 18:56:43 +02001243 if (bsc_timer_pending(&trans->cc.timer)) {
1244 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1245 bsc_del_timer(&trans->cc.timer);
1246 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001247 }
1248}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001249
Harald Welte4bfdfe72009-06-10 23:11:52 +08001250static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1251 int msg_type, struct gsm_mncc *mncc)
1252{
1253 struct msgb *msg;
1254
1255 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001256 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001257 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001258 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001259 trans->conn->lchan->ts->trx->bts->nr,
1260 trans->conn->lchan->ts->trx->nr,
1261 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001262 (trans->subscr)?(trans->subscr->extension):"-",
1263 get_mncc_name(msg_type));
1264 else
1265 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1266 "Sending '%s' to MNCC.\n",
1267 (trans->subscr)?(trans->subscr->extension):"-",
1268 get_mncc_name(msg_type));
1269 else
1270 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1271 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1272
1273 mncc->msg_type = msg_type;
1274
Harald Welte966636f2009-06-26 19:39:35 +02001275 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276 if (!msg)
1277 return -ENOMEM;
1278 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1279 msgb_enqueue(&net->upqueue, msg);
1280
1281 return 0;
1282}
1283
1284int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1285 u_int32_t callref, int location, int value)
1286{
1287 struct gsm_mncc rel;
1288
Harald Welte92f70c52009-06-12 01:54:08 +08001289 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001291 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1293}
1294
Harald Weltedcaf5652009-07-23 18:56:43 +02001295/* Call Control Specific transaction release.
1296 * gets called by trans_free, DO NOT CALL YOURSELF! */
1297void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299 gsm48_stop_cc_timer(trans);
1300
1301 /* send release to L4, if callref still exists */
1302 if (trans->callref) {
1303 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001304 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001305 GSM48_CAUSE_LOC_PRN_S_LU,
1306 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001308 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001310 if (trans->conn)
1311 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001312}
1313
1314static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001315
Harald Welte09e38af2009-02-16 22:52:23 +00001316/* call-back from paging the B-end of the connection */
1317static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001318 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001319{
Harald Welte7ccf7782009-02-17 01:43:01 +00001320 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001321 struct gsm_subscriber *subscr = param;
1322 struct gsm_trans *transt, *tmp;
1323 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001324
Harald Welte09e38af2009-02-16 22:52:23 +00001325 if (hooknum != GSM_HOOK_RR_PAGING)
1326 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001327
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328 if (!subscr)
1329 return -EINVAL;
1330 net = subscr->net;
1331 if (!net) {
1332 DEBUGP(DCC, "Error Network not set!\n");
1333 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001334 }
Harald Welte7584aea2009-02-11 11:44:12 +00001335
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336 /* check all tranactions (without lchan) for subscriber */
1337 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001338 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 continue;
1340 switch (event) {
1341 case GSM_PAGING_SUCCEEDED:
1342 if (!lchan) // paranoid
1343 break;
1344 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1345 subscr->extension);
1346 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001347 if (!transt->conn) {
1348 transt->conn = &lchan->conn;
1349 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 }
1351 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001352 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353 break;
1354 case GSM_PAGING_EXPIRED:
1355 DEBUGP(DCC, "Paging subscr %s expired!\n",
1356 subscr->extension);
1357 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001358 mncc_release_ind(transt->subscr->net, transt,
1359 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001360 GSM48_CAUSE_LOC_PRN_S_LU,
1361 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001363 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 break;
1365 }
1366 }
Harald Welte09e38af2009-02-16 22:52:23 +00001367 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001368}
Harald Welte7584aea2009-02-11 11:44:12 +00001369
Harald Welteda7ab742009-12-19 22:23:05 +01001370static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1371
Harald Welte805f6442009-07-28 18:25:29 +02001372/* some other part of the code sends us a signal */
1373static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1374 void *handler_data, void *signal_data)
1375{
1376 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001377 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001378 struct gsm_network *net;
1379 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001380
1381 if (subsys != SS_ABISIP)
1382 return 0;
1383
1384 /* in case we use direct BTS-to-BTS RTP */
1385 if (ipacc_rtp_direct)
1386 return 0;
1387
Harald Welte805f6442009-07-28 18:25:29 +02001388 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001389 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001390 /* check if any transactions on this lchan still have
1391 * a tch_recv_mncc request pending */
1392 net = lchan->ts->trx->bts->network;
1393 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001394 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001395 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1396 tch_recv_mncc(net, trans->callref, 1);
1397 }
1398 }
Harald Welte805f6442009-07-28 18:25:29 +02001399 break;
Harald Welte805f6442009-07-28 18:25:29 +02001400 }
1401
1402 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001403}
1404
Harald Welte49f48b82009-02-17 15:29:33 +00001405/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001406static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001407{
Harald Welte11fa29c2009-02-19 17:24:39 +00001408 struct gsm_bts *bts = lchan->ts->trx->bts;
1409 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001410 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001411
Harald Welte11fa29c2009-02-19 17:24:39 +00001412 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1413 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1414 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1415
1416 if (bts->type != remote_bts->type) {
1417 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1418 return -EINVAL;
1419 }
Harald Welteda7ab742009-12-19 22:23:05 +01001420
1421 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001422 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001423 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001424 if (!ipacc_rtp_direct) {
1425 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001426 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001427 if (rc < 0)
1428 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001429 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001430#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001431
1432 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301433 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1434 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001435 } else {
1436 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301437 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1438 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301439 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001440 if (rc < 0)
1441 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301442 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1443 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301444 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001445 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001446 break;
1447 case GSM_BTS_TYPE_BS11:
1448 trau_mux_map_lchan(lchan, remote_lchan);
1449 break;
1450 default:
1451 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001452 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001453 }
Harald Welte49f48b82009-02-17 15:29:33 +00001454
1455 return 0;
1456}
1457
Harald Welte4bfdfe72009-06-10 23:11:52 +08001458/* bridge channels of two transactions */
1459static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001460{
Harald Weltedcaf5652009-07-23 18:56:43 +02001461 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1462 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001463
Harald Welte4bfdfe72009-06-10 23:11:52 +08001464 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001465 return -EIO;
1466
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001467 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001468 return -EIO;
1469
1470 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001471 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001472}
1473
Harald Welteda7ab742009-12-19 22:23:05 +01001474/* enable receive of channels to MNCC upqueue */
1475static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001476{
1477 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001478 struct gsm_lchan *lchan;
1479 struct gsm_bts *bts;
1480 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001481
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001483 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001484 if (!trans)
1485 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001486 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001487 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001488 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001489 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001490
Harald Welteda7ab742009-12-19 22:23:05 +01001491 switch (bts->type) {
1492 case GSM_BTS_TYPE_NANOBTS:
1493 if (ipacc_rtp_direct) {
1494 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1495 return -EINVAL;
1496 }
1497 /* in case, we don't have a RTP socket yet, we note this
1498 * in the transaction and try later */
1499 if (!lchan->abis_ip.rtp_socket) {
1500 trans->tch_recv = enable;
1501 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1502 return 0;
1503 }
1504 if (enable) {
1505 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001506 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001507 if (rc < 0)
1508 return rc;
1509 /* assign socket to application interface */
1510 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1511 net, callref);
1512 } else
1513 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1514 net, 0);
1515 break;
1516 case GSM_BTS_TYPE_BS11:
1517 if (enable)
1518 return trau_recv_lchan(lchan, callref);
1519 return trau_mux_unmap(NULL, callref);
1520 break;
1521 default:
1522 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1523 return -EINVAL;
1524 }
1525
1526 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527}
1528
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1530{
1531 DEBUGP(DCC, "-> STATUS ENQ\n");
1532 return gsm48_cc_tx_status(trans, msg);
1533}
1534
1535static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1536static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1537
1538static void gsm48_cc_timeout(void *arg)
1539{
1540 struct gsm_trans *trans = arg;
1541 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001542 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1543 int mo_location = GSM48_CAUSE_LOC_USER;
1544 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1545 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001546 struct gsm_mncc mo_rel, l4_rel;
1547
1548 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1549 mo_rel.callref = trans->callref;
1550 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1551 l4_rel.callref = trans->callref;
1552
Harald Weltedcaf5652009-07-23 18:56:43 +02001553 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 case 0x303:
1555 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001556 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001557 break;
1558 case 0x310:
1559 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001560 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001561 break;
1562 case 0x313:
1563 disconnect = 1;
1564 /* unknown, did not find it in the specs */
1565 break;
1566 case 0x301:
1567 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001568 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 break;
1570 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001571 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001573 gsm48_cc_tx_release(trans, &trans->cc.msg);
1574 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575 break; /* stay in release state */
1576 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001577 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 return;
1579// release = 1;
1580// l4_cause = 14;
1581// break;
1582 case 0x306:
1583 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001584 mo_cause = trans->cc.msg.cause.value;
1585 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 break;
1587 case 0x323:
1588 disconnect = 1;
1589 break;
1590 default:
1591 release = 1;
1592 }
1593
1594 if (release && trans->callref) {
1595 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001596 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 l4_location, l4_cause);
1598 trans->callref = 0;
1599 }
1600
1601 if (disconnect && trans->callref) {
1602 /* process disconnect towards layer 4 */
1603 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001604 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001605 }
1606
1607 /* process disconnect towards mobile station */
1608 if (disconnect || release) {
1609 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001610 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1611 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1612 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001613 mo_rel.cause.diag_len = 3;
1614
1615 if (disconnect)
1616 gsm48_cc_tx_disconnect(trans, &mo_rel);
1617 if (release)
1618 gsm48_cc_tx_release(trans, &mo_rel);
1619 }
1620
1621}
1622
1623static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1624 int sec, int micro)
1625{
1626 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001627 trans->cc.timer.cb = gsm48_cc_timeout;
1628 trans->cc.timer.data = trans;
1629 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1630 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001631}
1632
1633static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1634{
1635 struct gsm48_hdr *gh = msgb_l3(msg);
1636 u_int8_t msg_type = gh->msg_type & 0xbf;
1637 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1638 struct tlv_parsed tp;
1639 struct gsm_mncc setup;
1640
1641 memset(&setup, 0, sizeof(struct gsm_mncc));
1642 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001643 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644 /* emergency setup is identified by msg_type */
1645 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1646 setup.emergency = 1;
1647
1648 /* use subscriber as calling party number */
1649 if (trans->subscr) {
1650 setup.fields |= MNCC_F_CALLING;
1651 strncpy(setup.calling.number, trans->subscr->extension,
1652 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001653 strncpy(setup.imsi, trans->subscr->imsi,
1654 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 }
1656 /* bearer capability */
1657 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1658 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001659 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1661 }
1662 /* facility */
1663 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1664 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001665 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1667 }
1668 /* called party bcd number */
1669 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1670 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001671 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1673 }
1674 /* user-user */
1675 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1676 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001677 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1679 }
1680 /* ss-version */
1681 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1682 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001683 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1685 }
1686 /* CLIR suppression */
1687 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1688 setup.clir.sup = 1;
1689 /* CLIR invocation */
1690 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1691 setup.clir.inv = 1;
1692 /* cc cap */
1693 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1694 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001695 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1697 }
1698
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 new_cc_state(trans, GSM_CSTATE_INITIATED);
1700
Harald Welte (local)47df3992009-12-26 19:45:03 +01001701 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1702 subscr_name(trans->subscr), trans->subscr->extension,
1703 setup.called.number);
1704
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001706 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707
Harald Welte13cac662009-07-29 12:10:35 +02001708 /* MNCC code will modify the channel asynchronously, we should
1709 * ipaccess-bind only after the modification has been made to the
1710 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 return 0;
1712}
1713
1714static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001715{
1716 struct msgb *msg = gsm48_msgb_alloc();
1717 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001719 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001720
Harald Welte7ccf7782009-02-17 01:43:01 +00001721 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001722
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 /* transaction id must not be assigned */
1724 if (trans->transaction_id != 0xff) { /* unasssigned */
1725 DEBUGP(DCC, "TX Setup with assigned transaction. "
1726 "This is not allowed!\n");
1727 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001728 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001729 GSM48_CAUSE_LOC_PRN_S_LU,
1730 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001732 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 return rc;
1734 }
1735
1736 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001737 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1738 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001740 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001741 GSM48_CAUSE_LOC_PRN_S_LU,
1742 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001744 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 return rc;
1746 }
Harald Welte78283ef2009-07-23 21:36:44 +02001747 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001748
Harald Welte65e74cc2008-12-29 01:55:35 +00001749 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001750
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001752
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 /* bearer capability */
1754 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001755 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 /* facility */
1757 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001758 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 /* progress */
1760 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001761 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 /* calling party BCD number */
1763 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001764 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* called party BCD number */
1766 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001767 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 /* user-user */
1769 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001770 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* redirecting party BCD number */
1772 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 /* signal */
1775 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001776 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777
1778 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001779
Harald Welte39e2ead2009-07-23 21:13:03 +02001780 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001781}
1782
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1784{
1785 struct gsm48_hdr *gh = msgb_l3(msg);
1786 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1787 struct tlv_parsed tp;
1788 struct gsm_mncc call_conf;
1789
1790 gsm48_stop_cc_timer(trans);
1791 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1792
1793 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1794 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001795 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796#if 0
1797 /* repeat */
1798 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1799 call_conf.repeat = 1;
1800 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1801 call_conf.repeat = 2;
1802#endif
1803 /* bearer capability */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1805 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1808 }
1809 /* cause */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1811 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1814 }
1815 /* cc cap */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1817 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001818 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1820 }
1821
1822 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1823
Harald Welte596fed42009-07-23 19:06:52 +02001824 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1825 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826}
1827
1828static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1829{
1830 struct gsm_mncc *proceeding = arg;
1831 struct msgb *msg = gsm48_msgb_alloc();
1832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1833
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1835
1836 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1837
1838 /* bearer capability */
1839 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001840 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 /* facility */
1842 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001843 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 /* progress */
1845 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847
Harald Welte39e2ead2009-07-23 21:13:03 +02001848 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849}
1850
1851static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1852{
1853 struct gsm48_hdr *gh = msgb_l3(msg);
1854 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1855 struct tlv_parsed tp;
1856 struct gsm_mncc alerting;
1857
1858 gsm48_stop_cc_timer(trans);
1859 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1860
1861 memset(&alerting, 0, sizeof(struct gsm_mncc));
1862 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001863 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 /* facility */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1866 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1869 }
1870
1871 /* progress */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1873 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1876 }
1877 /* ss-version */
1878 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1879 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1882 }
1883
1884 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1885
Harald Welte596fed42009-07-23 19:06:52 +02001886 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1887 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888}
1889
1890static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1891{
1892 struct gsm_mncc *alerting = arg;
1893 struct msgb *msg = gsm48_msgb_alloc();
1894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_ALERTING;
1897
1898 /* facility */
1899 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001900 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 /* progress */
1902 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001903 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904 /* user-user */
1905 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001906 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907
1908 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1909
Harald Welte39e2ead2009-07-23 21:13:03 +02001910 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911}
1912
1913static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1914{
1915 struct gsm_mncc *progress = arg;
1916 struct msgb *msg = gsm48_msgb_alloc();
1917 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1918
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 gh->msg_type = GSM48_MT_CC_PROGRESS;
1920
1921 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001922 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 /* user-user */
1924 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001925 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926
Harald Welte39e2ead2009-07-23 21:13:03 +02001927 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928}
1929
1930static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1931{
1932 struct gsm_mncc *connect = arg;
1933 struct msgb *msg = gsm48_msgb_alloc();
1934 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1935
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 gh->msg_type = GSM48_MT_CC_CONNECT;
1937
1938 gsm48_stop_cc_timer(trans);
1939 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1940
1941 /* facility */
1942 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944 /* progress */
1945 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001946 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 /* connected number */
1948 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001949 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 /* user-user */
1951 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001952 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953
1954 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1955
Harald Welte39e2ead2009-07-23 21:13:03 +02001956 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957}
1958
1959static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1960{
1961 struct gsm48_hdr *gh = msgb_l3(msg);
1962 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1963 struct tlv_parsed tp;
1964 struct gsm_mncc connect;
1965
1966 gsm48_stop_cc_timer(trans);
1967
1968 memset(&connect, 0, sizeof(struct gsm_mncc));
1969 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001970 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* use subscriber as connected party number */
1972 if (trans->subscr) {
1973 connect.fields |= MNCC_F_CONNECTED;
1974 strncpy(connect.connected.number, trans->subscr->extension,
1975 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001976 strncpy(connect.imsi, trans->subscr->imsi,
1977 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 }
1979 /* facility */
1980 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1981 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1984 }
1985 /* user-user */
1986 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1987 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001988 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1990 }
1991 /* ss-version */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1993 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001994 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1996 }
1997
1998 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1999
Harald Welte596fed42009-07-23 19:06:52 +02002000 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001}
2002
2003
2004static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2005{
2006 struct gsm_mncc connect_ack;
2007
2008 gsm48_stop_cc_timer(trans);
2009
2010 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2011
2012 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2013 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002014 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 &connect_ack);
2016}
2017
2018static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2019{
2020 struct msgb *msg = gsm48_msgb_alloc();
2021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2022
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2024
2025 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2026
Harald Welte39e2ead2009-07-23 21:13:03 +02002027 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028}
2029
2030static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2031{
2032 struct gsm48_hdr *gh = msgb_l3(msg);
2033 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2034 struct tlv_parsed tp;
2035 struct gsm_mncc disc;
2036
2037 gsm48_stop_cc_timer(trans);
2038
2039 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2040
2041 memset(&disc, 0, sizeof(struct gsm_mncc));
2042 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002043 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044 /* cause */
2045 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2046 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002047 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2049 }
2050 /* facility */
2051 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2052 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002053 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2055 }
2056 /* user-user */
2057 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2058 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002059 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2061 }
2062 /* ss-version */
2063 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2064 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002065 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2067 }
2068
Harald Welte596fed42009-07-23 19:06:52 +02002069 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070
2071}
2072
Harald Weltec66b71c2009-06-11 14:23:20 +08002073static struct gsm_mncc_cause default_cause = {
2074 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2075 .coding = 0,
2076 .rec = 0,
2077 .rec_val = 0,
2078 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2079 .diag_len = 0,
2080 .diag = { 0 },
2081};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082
2083static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2084{
2085 struct gsm_mncc *disc = arg;
2086 struct msgb *msg = gsm48_msgb_alloc();
2087 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2088
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2090
2091 gsm48_stop_cc_timer(trans);
2092 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2093
2094 /* cause */
2095 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 else
Harald Welte55c8f352010-03-07 23:40:35 +01002098 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099
2100 /* facility */
2101 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002102 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 /* progress */
2104 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 /* user-user */
2107 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002108 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109
2110 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002111 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112
2113 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2114
Harald Welte39e2ead2009-07-23 21:13:03 +02002115 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116}
2117
2118static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2119{
2120 struct gsm48_hdr *gh = msgb_l3(msg);
2121 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2122 struct tlv_parsed tp;
2123 struct gsm_mncc rel;
2124 int rc;
2125
2126 gsm48_stop_cc_timer(trans);
2127
2128 memset(&rel, 0, sizeof(struct gsm_mncc));
2129 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002130 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 /* cause */
2132 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2133 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002134 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2136 }
2137 /* facility */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2139 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2142 }
2143 /* user-user */
2144 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2145 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002146 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2148 }
2149 /* ss-version */
2150 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2151 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2154 }
2155
Harald Weltedcaf5652009-07-23 18:56:43 +02002156 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002158 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002160 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002161 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002162 GSM48_MT_CC_RELEASE_COMPL);
2163 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 }
2165
2166 new_cc_state(trans, GSM_CSTATE_NULL);
2167
2168 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002169 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170
2171 return rc;
2172}
2173
2174static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2175{
2176 struct gsm_mncc *rel = arg;
2177 struct msgb *msg = gsm48_msgb_alloc();
2178 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2179
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180 gh->msg_type = GSM48_MT_CC_RELEASE;
2181
2182 trans->callref = 0;
2183
2184 gsm48_stop_cc_timer(trans);
2185 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2186
2187 /* cause */
2188 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 /* facility */
2191 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 /* user-user */
2194 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002195 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196
Harald Weltedcaf5652009-07-23 18:56:43 +02002197 trans->cc.T308_second = 0;
2198 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199
Harald Weltedcaf5652009-07-23 18:56:43 +02002200 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2202
Harald Welte39e2ead2009-07-23 21:13:03 +02002203 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204}
2205
2206static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2207{
2208 struct gsm48_hdr *gh = msgb_l3(msg);
2209 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2210 struct tlv_parsed tp;
2211 struct gsm_mncc rel;
2212 int rc = 0;
2213
2214 gsm48_stop_cc_timer(trans);
2215
2216 memset(&rel, 0, sizeof(struct gsm_mncc));
2217 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002218 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 /* cause */
2220 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2221 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2224 }
2225 /* facility */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2227 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2230 }
2231 /* user-user */
2232 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2233 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002234 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2236 }
2237 /* ss-version */
2238 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2239 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002240 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2242 }
2243
2244 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002245 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002247 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 MNCC_REJ_IND, &rel);
2249 break;
2250 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002251 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002253 /* FIXME: in case of multiple calls, we can't simply
2254 * hang up here ! */
2255 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 break;
2257 default:
Harald Welte596fed42009-07-23 19:06:52 +02002258 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 MNCC_REL_IND, &rel);
2260 }
2261 }
2262
2263 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002264 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265
2266 return rc;
2267}
2268
2269static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2270{
2271 struct gsm_mncc *rel = arg;
2272 struct msgb *msg = gsm48_msgb_alloc();
2273 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2274
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2276
2277 trans->callref = 0;
2278
2279 gsm48_stop_cc_timer(trans);
2280
2281 /* cause */
2282 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 /* facility */
2285 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002286 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 /* user-user */
2288 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002289 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290
Harald Weltedcaf5652009-07-23 18:56:43 +02002291 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292
Harald Welte39e2ead2009-07-23 21:13:03 +02002293 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294}
2295
2296static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2297{
2298 struct gsm48_hdr *gh = msgb_l3(msg);
2299 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2300 struct tlv_parsed tp;
2301 struct gsm_mncc fac;
2302
2303 memset(&fac, 0, sizeof(struct gsm_mncc));
2304 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002305 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 /* facility */
2307 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2308 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2311 }
2312 /* ss-version */
2313 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2314 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2317 }
2318
Harald Welte596fed42009-07-23 19:06:52 +02002319 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320}
2321
2322static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2323{
2324 struct gsm_mncc *fac = arg;
2325 struct msgb *msg = gsm48_msgb_alloc();
2326 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2327
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 gh->msg_type = GSM48_MT_CC_FACILITY;
2329
2330 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002331 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332
Harald Welte39e2ead2009-07-23 21:13:03 +02002333 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334}
2335
2336static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2337{
2338 struct gsm_mncc hold;
2339
2340 memset(&hold, 0, sizeof(struct gsm_mncc));
2341 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002342 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343}
2344
2345static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2346{
2347 struct msgb *msg = gsm48_msgb_alloc();
2348 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2349
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2351
Harald Welte39e2ead2009-07-23 21:13:03 +02002352 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353}
2354
2355static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2356{
2357 struct gsm_mncc *hold_rej = arg;
2358 struct msgb *msg = gsm48_msgb_alloc();
2359 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2360
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2362
2363 /* cause */
2364 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 else
Harald Welte55c8f352010-03-07 23:40:35 +01002367 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368
Harald Welte39e2ead2009-07-23 21:13:03 +02002369 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370}
2371
2372static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2373{
2374 struct gsm_mncc retrieve;
2375
2376 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2377 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002378 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2379 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380}
2381
2382static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2383{
2384 struct msgb *msg = gsm48_msgb_alloc();
2385 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2386
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2388
Harald Welte39e2ead2009-07-23 21:13:03 +02002389 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390}
2391
2392static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2393{
2394 struct gsm_mncc *retrieve_rej = arg;
2395 struct msgb *msg = gsm48_msgb_alloc();
2396 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2397
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2399
2400 /* cause */
2401 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002402 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 else
Harald Welte55c8f352010-03-07 23:40:35 +01002404 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405
Harald Welte39e2ead2009-07-23 21:13:03 +02002406 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2410{
2411 struct gsm48_hdr *gh = msgb_l3(msg);
2412 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2413 struct tlv_parsed tp;
2414 struct gsm_mncc dtmf;
2415
2416 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2417 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002418 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 /* keypad facility */
2420 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2421 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002422 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2424 }
2425
Harald Welte596fed42009-07-23 19:06:52 +02002426 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2430{
2431 struct gsm_mncc *dtmf = arg;
2432 struct msgb *msg = gsm48_msgb_alloc();
2433 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2434
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2436
2437 /* keypad */
2438 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440
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_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2445{
2446 struct gsm_mncc *dtmf = 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_START_DTMF_REJ;
2451
2452 /* cause */
2453 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002454 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455 else
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457
Harald Welte39e2ead2009-07-23 21:13:03 +02002458 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459}
2460
2461static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2462{
2463 struct msgb *msg = gsm48_msgb_alloc();
2464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2465
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2467
Harald Welte39e2ead2009-07-23 21:13:03 +02002468 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469}
2470
2471static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2472{
2473 struct gsm_mncc dtmf;
2474
2475 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2476 dtmf.callref = trans->callref;
2477
Harald Welte596fed42009-07-23 19:06:52 +02002478 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479}
2480
2481static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2482{
2483 struct gsm48_hdr *gh = msgb_l3(msg);
2484 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2485 struct tlv_parsed tp;
2486 struct gsm_mncc modify;
2487
2488 memset(&modify, 0, sizeof(struct gsm_mncc));
2489 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002490 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 /* bearer capability */
2492 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2493 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2496 }
2497
2498 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2499
Harald Welte596fed42009-07-23 19:06:52 +02002500 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501}
2502
2503static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2504{
2505 struct gsm_mncc *modify = arg;
2506 struct msgb *msg = gsm48_msgb_alloc();
2507 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2508
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509 gh->msg_type = GSM48_MT_CC_MODIFY;
2510
2511 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2512
2513 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002514 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
2516 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2517
Harald Welte39e2ead2009-07-23 21:13:03 +02002518 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2522{
2523 struct gsm48_hdr *gh = msgb_l3(msg);
2524 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2525 struct tlv_parsed tp;
2526 struct gsm_mncc modify;
2527
2528 gsm48_stop_cc_timer(trans);
2529
2530 memset(&modify, 0, sizeof(struct gsm_mncc));
2531 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002532 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 /* bearer capability */
2534 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2535 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002536 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2538 }
2539
2540 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2541
Harald Welte596fed42009-07-23 19:06:52 +02002542 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2546{
2547 struct gsm_mncc *modify = arg;
2548 struct msgb *msg = gsm48_msgb_alloc();
2549 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2550
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2552
2553 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002554 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555
2556 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2557
Harald Welte39e2ead2009-07-23 21:13:03 +02002558 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2562{
2563 struct gsm48_hdr *gh = msgb_l3(msg);
2564 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2565 struct tlv_parsed tp;
2566 struct gsm_mncc modify;
2567
2568 gsm48_stop_cc_timer(trans);
2569
2570 memset(&modify, 0, sizeof(struct gsm_mncc));
2571 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002572 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 /* bearer capability */
2574 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2575 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002576 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2578 }
2579 /* cause */
2580 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2581 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002582 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2584 }
2585
2586 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2587
Harald Welte596fed42009-07-23 19:06:52 +02002588 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589}
2590
2591static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2592{
2593 struct gsm_mncc *modify = arg;
2594 struct msgb *msg = gsm48_msgb_alloc();
2595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2596
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2598
2599 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002602 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603
2604 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2605
Harald Welte39e2ead2009-07-23 21:13:03 +02002606 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2610{
2611 struct gsm_mncc *notify = arg;
2612 struct msgb *msg = gsm48_msgb_alloc();
2613 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2614
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 gh->msg_type = GSM48_MT_CC_NOTIFY;
2616
2617 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619
Harald Welte39e2ead2009-07-23 21:13:03 +02002620 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621}
2622
2623static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2624{
2625 struct gsm48_hdr *gh = msgb_l3(msg);
2626 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2627// struct tlv_parsed tp;
2628 struct gsm_mncc notify;
2629
2630 memset(&notify, 0, sizeof(struct gsm_mncc));
2631 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002632// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002634 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635
Harald Welte596fed42009-07-23 19:06:52 +02002636 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637}
2638
2639static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2640{
2641 struct gsm_mncc *user = arg;
2642 struct msgb *msg = gsm48_msgb_alloc();
2643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2644
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 gh->msg_type = GSM48_MT_CC_USER_INFO;
2646
2647 /* user-user */
2648 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002649 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650 /* more data */
2651 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002652 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653
Harald Welte39e2ead2009-07-23 21:13:03 +02002654 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655}
2656
2657static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2658{
2659 struct gsm48_hdr *gh = msgb_l3(msg);
2660 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2661 struct tlv_parsed tp;
2662 struct gsm_mncc user;
2663
2664 memset(&user, 0, sizeof(struct gsm_mncc));
2665 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002666 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 /* user-user */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2669 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002670 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2672 }
2673 /* more data */
2674 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2675 user.more = 1;
2676
Harald Welte596fed42009-07-23 19:06:52 +02002677 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678}
2679
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002680static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681{
2682 struct gsm_mncc *mode = arg;
2683
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002684 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685}
2686
2687static struct downstate {
2688 u_int32_t states;
2689 int type;
2690 int (*rout) (struct gsm_trans *trans, void *arg);
2691} downstatelist[] = {
2692 /* mobile originating call establishment */
2693 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2694 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2695 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2696 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2697 {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 */
2698 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2699 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2700 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2701 /* mobile terminating call establishment */
2702 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2703 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2704 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2705 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2706 /* signalling during call */
2707 {SBIT(GSM_CSTATE_ACTIVE),
2708 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2709 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2710 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2711 {ALL_STATES,
2712 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2713 {ALL_STATES,
2714 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2715 {ALL_STATES,
2716 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2717 {SBIT(GSM_CSTATE_ACTIVE),
2718 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2719 {SBIT(GSM_CSTATE_ACTIVE),
2720 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2721 {SBIT(GSM_CSTATE_ACTIVE),
2722 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2723 {SBIT(GSM_CSTATE_ACTIVE),
2724 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2725 {SBIT(GSM_CSTATE_ACTIVE),
2726 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2727 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2728 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2729 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2730 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2731 {SBIT(GSM_CSTATE_ACTIVE),
2732 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2733 /* clearing */
2734 {SBIT(GSM_CSTATE_INITIATED),
2735 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2736 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2737 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2738 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2739 MNCC_REL_REQ, gsm48_cc_tx_release},
2740 /* special */
2741 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002742 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743};
2744
2745#define DOWNSLLEN \
2746 (sizeof(downstatelist) / sizeof(struct downstate))
2747
2748
2749int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2750{
Harald Welte1a6f7982009-08-09 18:52:33 +02002751 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002753 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 struct gsm_mncc *data = arg, rel;
2756
2757 /* handle special messages */
2758 switch(msg_type) {
2759 case MNCC_BRIDGE:
2760 return tch_bridge(net, arg);
2761 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002762 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002764 return tch_recv_mncc(net, data->callref, 1);
2765 case GSM_TCHF_FRAME:
2766 /* Find callref */
2767 trans = trans_find_by_callref(net, data->callref);
2768 if (!trans)
2769 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002770 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002771 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002772 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002773 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002774 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002775 switch (bts->type) {
2776 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002777 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002778 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002779 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002780 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002781 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002782 default:
2783 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2784 }
2785 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 }
2787
2788 memset(&rel, 0, sizeof(struct gsm_mncc));
2789 rel.callref = data->callref;
2790
2791 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002792 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793
2794 /* Callref unknown */
2795 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002796 struct gsm_subscriber *subscr;
2797
Harald Welte4a3464c2009-07-04 10:11:24 +02002798 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002799 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2800 "Received '%s' from MNCC with "
2801 "unknown callref %d\n", data->called.number,
2802 get_mncc_name(msg_type), data->callref);
2803 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002804 return mncc_release_ind(net, NULL, data->callref,
2805 GSM48_CAUSE_LOC_PRN_S_LU,
2806 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002808 if (!data->called.number[0] && !data->imsi[0]) {
2809 DEBUGP(DCC, "(bts - trx - ts - ti) "
2810 "Received '%s' from MNCC with "
2811 "no number or IMSI\n", get_mncc_name(msg_type));
2812 /* Invalid number */
2813 return mncc_release_ind(net, NULL, data->callref,
2814 GSM48_CAUSE_LOC_PRN_S_LU,
2815 GSM48_CC_CAUSE_INV_NR_FORMAT);
2816 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002818 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002819 subscr = subscr_get_by_extension(net,
2820 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002821 else
Harald Welte9176bd42009-07-23 18:46:00 +02002822 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 /* If subscriber is not found */
2824 if (!subscr) {
2825 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2826 "Received '%s' from MNCC with "
2827 "unknown subscriber %s\n", data->called.number,
2828 get_mncc_name(msg_type), data->called.number);
2829 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002830 return mncc_release_ind(net, NULL, data->callref,
2831 GSM48_CAUSE_LOC_PRN_S_LU,
2832 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 }
2834 /* If subscriber is not "attached" */
2835 if (!subscr->lac) {
2836 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2837 "Received '%s' from MNCC with "
2838 "detached subscriber %s\n", data->called.number,
2839 get_mncc_name(msg_type), data->called.number);
2840 subscr_put(subscr);
2841 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002842 return mncc_release_ind(net, NULL, data->callref,
2843 GSM48_CAUSE_LOC_PRN_S_LU,
2844 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845 }
2846 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002847 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2848 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849 DEBUGP(DCC, "No memory for trans.\n");
2850 subscr_put(subscr);
2851 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002852 mncc_release_ind(net, NULL, data->callref,
2853 GSM48_CAUSE_LOC_PRN_S_LU,
2854 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855 return -ENOMEM;
2856 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002858 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002859
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860 /* If subscriber has no lchan */
2861 if (!lchan) {
2862 /* find transaction with this subscriber already paging */
2863 llist_for_each_entry(transt, &net->trans_list, entry) {
2864 /* Transaction of our lchan? */
2865 if (transt == trans ||
2866 transt->subscr != subscr)
2867 continue;
2868 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2869 "Received '%s' from MNCC with "
2870 "unallocated channel, paging already "
2871 "started.\n", bts->nr,
2872 data->called.number,
2873 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002874 subscr_put(subscr);
2875 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 return 0;
2877 }
2878 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002879 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002880 /* Trigger paging */
2881 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2882 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002883 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 return 0;
2885 }
2886 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002887 trans->conn = &lchan->conn;
2888 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002889 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002890 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002891
2892 if (trans->conn)
2893 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894
2895 /* if paging did not respond yet */
2896 if (!lchan) {
2897 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002898 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 (trans->subscr)?(trans->subscr->extension):"-",
2900 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002901 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2902 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903 if (msg_type == MNCC_REL_REQ)
2904 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2905 else
2906 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2907 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002908 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 return rc;
2910 }
2911
2912 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2913 "Received '%s' from MNCC in state %d (%s)\n",
2914 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2915 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002916 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002917 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002918 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919
2920 /* Find function for current state and message */
2921 for (i = 0; i < DOWNSLLEN; i++)
2922 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002923 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924 break;
2925 if (i == DOWNSLLEN) {
2926 DEBUGP(DCC, "Message unhandled at this state.\n");
2927 return 0;
2928 }
2929
2930 rc = downstatelist[i].rout(trans, arg);
2931
2932 return rc;
2933}
2934
2935
2936static struct datastate {
2937 u_int32_t states;
2938 int type;
2939 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2940} datastatelist[] = {
2941 /* mobile originating call establishment */
2942 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2943 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2944 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2945 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2946 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2947 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2948 /* mobile terminating call establishment */
2949 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2950 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2951 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2952 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002953 {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 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2955 /* signalling during call */
2956 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2957 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2958 {SBIT(GSM_CSTATE_ACTIVE),
2959 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2960 {ALL_STATES,
2961 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2962 {ALL_STATES,
2963 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2964 {ALL_STATES,
2965 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2966 {SBIT(GSM_CSTATE_ACTIVE),
2967 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2968 {SBIT(GSM_CSTATE_ACTIVE),
2969 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2970 {SBIT(GSM_CSTATE_ACTIVE),
2971 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2972 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2973 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2974 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2975 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2976 {SBIT(GSM_CSTATE_ACTIVE),
2977 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2978 /* clearing */
2979 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2980 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2981 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2982 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2983 {ALL_STATES, /* 5.4.3.4 */
2984 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2985};
2986
2987#define DATASLLEN \
2988 (sizeof(datastatelist) / sizeof(struct datastate))
2989
Harald Welte4bc90a12008-12-27 16:32:52 +00002990static int gsm0408_rcv_cc(struct msgb *msg)
2991{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002992 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002993 struct gsm48_hdr *gh = msgb_l3(msg);
2994 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002995 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002997 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002998 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002999
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 if (msg_type & 0x80) {
3001 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3002 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003003 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003004
3005 conn = &lchan->conn;
3006
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003008 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003009
Harald Welte6f5aee02009-07-23 21:21:14 +02003010 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 "Received '%s' from MS in state %d (%s)\n",
3012 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003013 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003014 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3015 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016
3017 /* Create transaction */
3018 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003019 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003020 "creating new trans.\n", transaction_id);
3021 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003022 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003023 transaction_id, new_callref++);
3024 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025 DEBUGP(DCC, "No memory for trans.\n");
3026 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003027 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028 GSM48_MT_CC_RELEASE_COMPL);
3029 return -ENOMEM;
3030 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003031 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003032 trans->conn = &lchan->conn;
3033 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034 }
3035
3036 /* find function for current state and message */
3037 for (i = 0; i < DATASLLEN; i++)
3038 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003039 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 break;
3041 if (i == DATASLLEN) {
3042 DEBUGP(DCC, "Message unhandled at this state.\n");
3043 return 0;
3044 }
3045
3046 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003047
3048 return rc;
3049}
3050
Harald Welte52b1f982008-12-23 20:25:15 +00003051/* 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 +02003052int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003053{
3054 struct gsm48_hdr *gh = msgb_l3(msg);
3055 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003056 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003057
3058 if (silent_call_reroute(msg))
3059 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003060
3061 switch (pdisc) {
3062 case GSM48_PDISC_CC:
3063 rc = gsm0408_rcv_cc(msg);
3064 break;
3065 case GSM48_PDISC_MM:
3066 rc = gsm0408_rcv_mm(msg);
3067 break;
3068 case GSM48_PDISC_RR:
3069 rc = gsm0408_rcv_rr(msg);
3070 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003071 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003072 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003073 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003074 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003075 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003076 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3077 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003078 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003079 case GSM48_PDISC_NC_SS:
3080 rc = handle_rcv_ussd(msg);
3081 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003082 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003083 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3084 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003085 break;
3086 }
3087
3088 return rc;
3089}
Harald Welte8470bf22008-12-25 23:28:35 +00003090
Harald Welte805f6442009-07-28 18:25:29 +02003091/*
3092 * This will be ran by the linker when loading the DSO. We use it to
3093 * do system initialization, e.g. registration of signal handlers.
3094 */
3095static __attribute__((constructor)) void on_dso_load_0408(void)
3096{
Harald Welte805f6442009-07-28 18:25:29 +02003097 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3098 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3099}