blob: 2ce2d15ff7bcd675b3a3d8d14bf9d164b9ad841f [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 *
7 * 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>
30#include <time.h>
31#include <netinet/in.h>
32
Sylvain Munaut28c47162009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/db.h>
Harald Weltef4625b12010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltef4625b12010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080041#include <openbsc/gsm_subscriber.h>
42#include <openbsc/gsm_04_11.h>
43#include <openbsc/gsm_04_08.h>
44#include <openbsc/abis_rsl.h>
45#include <openbsc/chan_alloc.h>
46#include <openbsc/paging.h>
47#include <openbsc/signal.h>
48#include <openbsc/trau_frame.h>
49#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020050#include <openbsc/rtp_proxy.h>
Harald Weltef4625b12010-02-20 16:24:02 +010051#include <osmocore/talloc.h>
Harald Weltefdc93d92010-03-07 23:40:35 +010052#include <osmocore/gsm48.h>
Harald Weltec2189a62009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Harald Welte59b04682009-06-10 05:40:52 +080056
Harald Welte (local)8751ee92009-08-15 02:30:58 +020057void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010058void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020059
Harald Welte59b04682009-06-10 05:40:52 +080060int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
61static int gsm48_tx_simple(struct gsm_lchan *lchan,
62 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010063static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080064
65struct gsm_lai {
66 u_int16_t mcc;
67 u_int16_t mnc;
68 u_int16_t lac;
69};
70
Harald Welte03740842009-06-10 23:11:52 +080071static u_int32_t new_callref = 0x80000001;
72
Sylvain Munaut28c47162009-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
Harald Welte59b04682009-06-10 05:40:52 +0800158static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
159 struct gsm_subscriber *subscriber)
160{
161 if (!subscriber)
162 return 0;
163
164 /*
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 Luebbe3d6294e2009-08-12 12:48:00 +0200172 switch (subscriber->net->auth_policy) {
173 case GSM_AUTH_POLICY_CLOSED:
174 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200175 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200176 if (subscriber->authorized)
177 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200178 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200179 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800180 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200181 default:
182 return 0;
183 }
Harald Welte59b04682009-06-10 05:40:52 +0800184}
185
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100186static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800187{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100188 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800189 return;
190
Holger Hans Peter Freyther065b8112010-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);
Harald Welte59b04682009-06-10 05:40:52 +0800195}
196
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100197static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800198{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100199 use_subscr_con(conn)
200 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800201
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100202 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200203 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800204}
205
Sylvain Munautcb321fb2009-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 Freyther065b8112010-03-23 06:41:45 +0100247static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800248{
Sylvain Munautcb321fb2009-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);
Harald Welte59b04682009-06-10 05:40:52 +0800253 return 0;
254}
255
256static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
257 void *handler_data, void *signal_data)
258{
Harald Welte03740842009-06-10 23:11:52 +0800259 struct gsm_trans *trans, *temp;
260
Harald Welte59b04682009-06-10 05:40:52 +0800261 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 Welte12560da2009-07-04 09:35:21 +0200268 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Welte1ff81b52009-06-26 20:17:06 +0200269 if (!lchan)
270 return 0;
271
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100272 release_loc_updating_req(&lchan->conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100273 release_security_operation(&lchan->conn);
Harald Welte59b04682009-06-10 05:40:52 +0800274
Harald Welte03740842009-06-10 23:11:52 +0800275 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-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 Welte03740842009-06-10 23:11:52 +0800279 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +0100280 if (trans->conn && trans->conn->lchan == lchan)
Harald Weltec2189a62009-07-23 18:56:43 +0200281 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800282 }
283
Harald Welte59b04682009-06-10 05:40:52 +0800284 return 0;
285}
286
Harald Welte59b04682009-06-10 05:40:52 +0800287/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
288int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
289{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100290 struct gsm_subscriber_connection *conn;
Harald Welte3edc5a92009-12-22 00:41:05 +0100291 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800292 struct msgb *msg = gsm48_msgb_alloc();
293 struct gsm48_hdr *gh;
294
295 msg->lchan = lchan;
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100296 conn = &lchan->conn;
Harald Welte59b04682009-06-10 05:40:52 +0800297
298 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
299 gh->proto_discr = GSM48_PDISC_MM;
300 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
301 gh->data[0] = cause;
302
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100303 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100304 "LAC=%u BTS=%u\n", conn->subscr ?
305 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100306 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100307
Harald Weltebdbb7442009-12-22 19:07:32 +0100308 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Welte59b04682009-06-10 05:40:52 +0800309
Harald Welte36fe2e82009-07-23 21:13:03 +0200310 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800311}
312
313/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
314int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
315{
316 struct gsm_bts *bts = lchan->ts->trx->bts;
317 struct msgb *msg = gsm48_msgb_alloc();
318 struct gsm48_hdr *gh;
319 struct gsm48_loc_area_id *lai;
320 u_int8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800321
322 msg->lchan = lchan;
323
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 Welted8493ac2010-03-04 10:55:40 +0100329 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800330 bts->network->network_code, bts->location_area_code);
331
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200332 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200333 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800334
335 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
336
Harald Weltebdbb7442009-12-22 19:07:32 +0100337 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100338
Harald Welte9ed54ff2009-12-12 21:00:48 +0100339 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800340}
341
Harald Welte59b04682009-06-10 05:40:52 +0800342/* Transmit Chapter 9.2.10 Identity Request */
343static 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;
347
348 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 Welte36fe2e82009-07-23 21:13:03 +0200355 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800356}
357
Harald Welte59b04682009-06-10 05:40:52 +0800358
359/* Parse Chapter 9.2.11 Identity Response */
360static int mm_rx_id_resp(struct msgb *msg)
361{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100362 struct gsm_subscriber_connection *conn;
Harald Welte59b04682009-06-10 05:40:52 +0800363 struct gsm48_hdr *gh = msgb_l3(msg);
364 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200365 struct gsm_bts *bts = lchan->ts->trx->bts;
366 struct gsm_network *net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800367 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200368 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800369
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200370 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800371 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
372 mi_type, mi_string);
373
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100374 conn = &lchan->conn;
375
Harald Welteedf48af2009-12-13 12:39:18 +0100376 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
377
Harald Welte59b04682009-06-10 05:40:52 +0800378 switch (mi_type) {
379 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200380 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-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 Luebbee2974032009-08-12 10:12:52 +0200385 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100386 if (conn->loc_operation)
387 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800388 break;
389 case GSM_MI_TYPE_IMEI:
390 case GSM_MI_TYPE_IMEISV:
391 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-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)ced09ed2009-08-17 09:39:55 +0200395 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100396 if (conn->loc_operation)
397 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800398 break;
399 }
400
401 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100402 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800403}
404
405
406static void loc_upd_rej_cb(void *data)
407{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100408 struct gsm_subscriber_connection *conn = data;
409 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100410 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800411
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100412 release_loc_updating_req(conn);
Harald Welte59936d72009-11-18 20:33:19 +0100413 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Harald Welte59b04682009-06-10 05:40:52 +0800414 lchan_auto_release(lchan);
415}
416
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100417static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800418{
Holger Hans Peter Freyther065b8112010-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);
Harald Welte59b04682009-06-10 05:40:52 +0800422}
423
424static 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 Welte59b04682009-06-10 05:40:52 +0800438/* Chapter 9.2.15: Receive Location Updating Request */
439static int mm_rx_loc_upd_req(struct msgb *msg)
440{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100441 struct gsm_subscriber_connection *conn;
Harald Welte59b04682009-06-10 05:40:52 +0800442 struct gsm48_hdr *gh = msgb_l3(msg);
443 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800444 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800445 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200446 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800447 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200448 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800449 int rc;
450
451 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100452 conn = &lchan->conn;
Harald Welte59b04682009-06-10 05:40:52 +0800453
454 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
455
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200456 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800457
Harald Welte79639662009-06-27 02:58:43 +0200458 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800459 lupd_name(lu->type));
460
Harald Welteedf48af2009-12-13 12:39:18 +0100461 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
462
Harald Welte3edc5a92009-12-22 00:41:05 +0100463 switch (lu->type) {
464 case GSM48_LUPD_NORMAL:
Harald Weltebdbb7442009-12-22 19:07:32 +0100465 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100466 break;
467 case GSM48_LUPD_IMSI_ATT:
Harald Weltebdbb7442009-12-22 19:07:32 +0100468 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100469 break;
470 case GSM48_LUPD_PERIODIC:
Harald Weltebdbb7442009-12-22 19:07:32 +0100471 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100472 break;
473 }
474
Harald Welte59b04682009-06-10 05:40:52 +0800475 /*
476 * Pseudo Spoof detection: Just drop a second/concurrent
477 * location updating request.
478 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100479 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200480 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100481 conn->loc_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800482 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
483 return 0;
484 }
485
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100486 allocate_loc_updating_req(&lchan->conn);
Harald Welte59b04682009-06-10 05:40:52 +0800487
Sylvain Munaut360fc582010-06-10 13:36:59 +0200488 conn->loc_operation->key_seq = lu->key_seq;
489
Harald Welte59b04682009-06-10 05:40:52 +0800490 switch (mi_type) {
491 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200492 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800493 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +0800494 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100495 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800496
Jan Luebbe9bdbd622009-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 Welte59b04682009-06-10 05:40:52 +0800502 break;
503 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200504 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800505 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200506 subscr = subscr_get_by_tmsi(bts->network,
507 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800508 if (!subscr) {
509 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte59b04682009-06-10 05:40:52 +0800510 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100511 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800512 }
Harald Welteca6dfc02010-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 Welte59b04682009-06-10 05:40:52 +0800516 break;
517 case GSM_MI_TYPE_IMEI:
518 case GSM_MI_TYPE_IMEISV:
519 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200520 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800521 break;
522 default:
Harald Welte79639662009-06-27 02:58:43 +0200523 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800524 break;
525 }
526
Harald Welteccd69362009-07-04 10:18:00 +0200527 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100528 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200529
Harald Welte03740842009-06-10 23:11:52 +0800530 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200531 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800532 /* FIXME: request id? close channel? */
533 return -EINVAL;
534 }
535
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100536 conn->subscr = subscr;
537 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800538
Harald Welteccd69362009-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 Freyther065b8112010-03-23 06:41:45 +0100541 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800542}
543
Harald Welte03740842009-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 Welte59b04682009-06-10 05:40:52 +0800551/* 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;
557 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200558 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +0800559#if 0
560 time_t cur_t;
561 struct tm* cur_time;
562 int tz15min;
563#endif
Harald Welte59b04682009-06-10 05:40:52 +0800564
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 Willmannc8188202009-08-13 03:42:07 +0200572#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800573 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++)
582 ptr16[i] = htons(net->name_long[i]);
583
584 /* FIXME: Use Cell Broadcast, not UCS-2, since
585 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-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 Welte59b04682009-06-10 05:40:52 +0800600 }
601
602 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200603#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800604 name_len = strlen(net->name_short);
605 /* 10.5.3.5a */
606 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200607 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800608 ptr8[1] = name_len*2 + 1;
609 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
610
611 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
612 for (i = 0; i < name_len; i++)
613 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-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 Welte59b04682009-06-10 05:40:52 +0800628 }
629
630#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800631 /* Section 10.5.3.9 */
632 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +0800633 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +0800634 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 Welte03740842009-06-10 23:11:52 +0800644 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +0800645 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +0800646 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +0800647#endif
648
Daniel Willmannc8188202009-08-13 03:42:07 +0200649 DEBUGP(DMM, "-> MM INFO\n");
650
Harald Welte36fe2e82009-07-23 21:13:03 +0200651 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800652}
653
Harald Welte36970f62009-08-12 22:56:50 +0200654/* Section 9.2.2 */
Sylvain Munaut0789c592009-12-24 00:23:46 +0100655int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte36970f62009-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 Munaut6de6dc12009-09-27 11:10:17 +0200659 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200660
Sylvain Munautb1a4f282009-12-24 00:24:29 +0100661 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte36970f62009-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 Munaut0789c592009-12-24 00:23:46 +0100667 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200668
Harald Welte36970f62009-08-12 22:56:50 +0200669 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200670 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200671 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-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 Welte59b04682009-06-10 05:40:52 +0800683static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
684{
685 DEBUGP(DMM, "-> CM SERVICE ACK\n");
686 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
687}
688
689/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100690static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800691 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 Freyther065b8112010-03-23 06:41:45 +0100698 msg->lchan = conn->lchan;
699 use_subscr_con(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800700
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 Welte36fe2e82009-07-23 21:13:03 +0200706 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800707}
708
Sylvain Munautbadc7112009-12-24 00:28:46 +0100709static int _gsm48_rx_mm_serv_req_sec_cb(
710 unsigned int hooknum, unsigned int event,
711 struct msgb *msg, void *data, void *param)
712{
713 struct gsm_lchan *lchan = data;
714 int rc = 0;
715
716 switch (event) {
717 case GSM_SECURITY_AUTH_FAILED:
718 /* Nothing to do */
719 break;
720
721 case GSM_SECURITY_NOAVAIL:
722 rc = gsm48_tx_mm_serv_ack(lchan);
723 break;
724
725 case GSM_SECURITY_SUCCEEDED:
726 /* nothing to do. CIPHER MODE COMMAND is
727 * implicit CM SERV ACK */
728 break;
729
730 default:
731 rc = -EINVAL;
732 };
733
734 return rc;
735}
736
Harald Welte59b04682009-06-10 05:40:52 +0800737/*
738 * Handle CM Service Requests
739 * a) Verify that the packet is long enough to contain the information
740 * we require otherwsie reject with INCORRECT_MESSAGE
741 * b) Try to parse the TMSI. If we do not have one reject
742 * c) Check that we know the subscriber with the TMSI otherwise reject
743 * with a HLR cause
744 * d) Set the subscriber on the gsm_lchan and accept
745 */
746static int gsm48_rx_mm_serv_req(struct msgb *msg)
747{
748 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200749 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800750
Harald Welte75350412009-07-23 18:46:00 +0200751 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800752 struct gsm_subscriber *subscr;
753 struct gsm48_hdr *gh = msgb_l3(msg);
754 struct gsm48_service_request *req =
755 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800756 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Welte59b04682009-06-10 05:40:52 +0800757 u_int8_t classmark2_len = gh->data[1];
758 u_int8_t *classmark2 = gh->data+2;
759 u_int8_t mi_len = *(classmark2 + classmark2_len);
760 u_int8_t *mi = (classmark2 + classmark2_len + 1);
761
762 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
763 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
764 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100765 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte59b04682009-06-10 05:40:52 +0800766 GSM48_REJECT_INCORRECT_MESSAGE);
767 }
768
769 if (msg->data_len < req->mi_len + 6) {
770 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100771 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte59b04682009-06-10 05:40:52 +0800772 GSM48_REJECT_INCORRECT_MESSAGE);
773 }
774
775 mi_type = mi[0] & GSM_MI_TYPE_MASK;
776 if (mi_type != GSM_MI_TYPE_TMSI) {
777 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100778 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte59b04682009-06-10 05:40:52 +0800779 GSM48_REJECT_INCORRECT_MESSAGE);
780 }
781
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200782 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800783 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
784 req->cm_service_type, mi_type, mi_string);
785
Harald Welteedf48af2009-12-13 12:39:18 +0100786 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
787
Harald Welte583ceaf2009-08-10 10:12:45 +0200788 if (is_siemens_bts(bts))
789 send_siemens_mrpci(msg->lchan, classmark2-1);
790
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200791 subscr = subscr_get_by_tmsi(bts->network,
792 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800793
794 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
795 if (!subscr)
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100796 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte59b04682009-06-10 05:40:52 +0800797 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
798
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100799 if (!msg->lchan->conn.subscr)
800 msg->lchan->conn.subscr = subscr;
801 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100802 subscr_put(subscr); /* lchan already has a ref, don't need another one */
803 else {
Harald Welte59b04682009-06-10 05:40:52 +0800804 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
805 subscr_put(subscr);
806 }
807
Harald Weltef6845a72009-07-05 14:08:13 +0200808 subscr->equipment.classmark2_len = classmark2_len;
809 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
810 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800811
Sylvain Munautbadc7112009-12-24 00:28:46 +0100812 return gsm48_secure_channel(msg->lchan, req->cipher_key_seq,
813 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800814}
815
816static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
817{
Harald Welte75350412009-07-23 18:46:00 +0200818 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800819 struct gsm48_hdr *gh = msgb_l3(msg);
820 struct gsm48_imsi_detach_ind *idi =
821 (struct gsm48_imsi_detach_ind *) gh->data;
822 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200823 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800824 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800825
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200826 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800827 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
828 mi_type, mi_string);
829
Harald Weltebdbb7442009-12-22 19:07:32 +0100830 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100831
Harald Welte59b04682009-06-10 05:40:52 +0800832 switch (mi_type) {
833 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200834 subscr = subscr_get_by_tmsi(bts->network,
835 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800836 break;
837 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200838 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800839 break;
840 case GSM_MI_TYPE_IMEI:
841 case GSM_MI_TYPE_IMEISV:
842 /* no sim card... FIXME: what to do ? */
843 DEBUGPC(DMM, "unimplemented mobile identity type\n");
844 break;
845 default:
846 DEBUGPC(DMM, "unknown mobile identity type\n");
847 break;
848 }
849
850 if (subscr) {
851 subscr_update(subscr, msg->trx->bts,
852 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100853 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200854
855 subscr->equipment.classmark1 = idi->classmark1;
856 db_sync_equipment(&subscr->equipment);
857
Harald Welte59b04682009-06-10 05:40:52 +0800858 subscr_put(subscr);
859 } else
860 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
861
Harald Welte43250dd2009-12-20 09:58:40 +0100862 /* FIXME: iterate over all transactions and release them,
863 * imagine an IMSI DETACH happening during an active call! */
864
Harald Welte9ed54ff2009-12-12 21:00:48 +0100865 /* subscriber is detached: should we release lchan? */
Harald Welte92d88912009-12-21 13:30:17 +0100866 lchan_auto_release(msg->lchan);
Harald Welte9ed54ff2009-12-12 21:00:48 +0100867
Harald Welte59b04682009-06-10 05:40:52 +0800868 return 0;
869}
870
871static int gsm48_rx_mm_status(struct msgb *msg)
872{
873 struct gsm48_hdr *gh = msgb_l3(msg);
874
875 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
876
877 return 0;
878}
879
Sylvain Munaut28c47162009-12-24 00:27:26 +0100880/* Chapter 9.2.3: Authentication Response */
881static int gsm48_rx_mm_auth_resp(struct msgb *msg)
882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
885 struct gsm_lchan *lchan = msg->lchan;
886 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
887 struct gsm_network *net = lchan->ts->trx->bts->network;
888
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200889 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut28c47162009-12-24 00:27:26 +0100890 hexdump(ar->sres, 4));
891
892 /* Safety check */
893 if (!conn->sec_operation) {
894 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
895 return -EIO;
896 }
897
898 /* Validate SRES */
899 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
900 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200901
902 DEBUGPC(DMM, "Invalid (expected %s)\n",
903 hexdump(conn->sec_operation->atuple.sres, 4));
904
Sylvain Munaut28c47162009-12-24 00:27:26 +0100905 if (cb)
906 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
907 NULL, lchan, conn->sec_operation->cb_data);
908
909 release_security_operation(conn);
910 return gsm48_tx_mm_auth_rej(lchan);
911 }
912
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200913 DEBUGPC(DMM, "OK\n");
914
Sylvain Munaut28c47162009-12-24 00:27:26 +0100915 /* Start ciphering */
916 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
917 lchan->encr.key_len = 8;
918 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
919
920 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
921}
922
Harald Welte59b04682009-06-10 05:40:52 +0800923/* Receive a GSM 04.08 Mobility Management (MM) message */
924static int gsm0408_rcv_mm(struct msgb *msg)
925{
926 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800927 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800928
929 switch (gh->msg_type & 0xbf) {
930 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200931 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte59b04682009-06-10 05:40:52 +0800932 rc = mm_rx_loc_upd_req(msg);
933 break;
934 case GSM48_MT_MM_ID_RESP:
935 rc = mm_rx_id_resp(msg);
936 break;
937 case GSM48_MT_MM_CM_SERV_REQ:
938 rc = gsm48_rx_mm_serv_req(msg);
939 break;
940 case GSM48_MT_MM_STATUS:
941 rc = gsm48_rx_mm_status(msg);
942 break;
943 case GSM48_MT_MM_TMSI_REALL_COMPL:
944 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100945 msg->lchan->conn.subscr ?
946 subscr_name(msg->lchan->conn.subscr) :
Harald Welte59b04682009-06-10 05:40:52 +0800947 "unknown subscriber");
948 break;
949 case GSM48_MT_MM_IMSI_DETACH_IND:
950 rc = gsm48_rx_mm_imsi_detach_ind(msg);
951 break;
952 case GSM48_MT_MM_CM_REEST_REQ:
953 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
954 break;
955 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut28c47162009-12-24 00:27:26 +0100956 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800957 break;
958 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +0100959 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +0800960 gh->msg_type);
961 break;
962 }
963
964 return rc;
965}
966
967/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +0200968static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800969{
Harald Welte75350412009-07-23 18:46:00 +0200970 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800971 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +0800972 struct gsm48_pag_resp *resp;
Harald Welte59b04682009-06-10 05:40:52 +0800973 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +0200974 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200975 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800976 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800977 int rc = 0;
978
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +0800979 resp = (struct gsm48_pag_resp *) &gh->data[0];
980 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
981 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +0800982 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
983 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +0200984
Harald Welte59b04682009-06-10 05:40:52 +0800985 switch (mi_type) {
986 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200987 subscr = subscr_get_by_tmsi(bts->network,
988 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800989 break;
990 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200991 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800992 break;
993 }
994
995 if (!subscr) {
996 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
997 /* FIXME: request id? close channel? */
998 return -EINVAL;
999 }
1000 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001001 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001002
Harald Weltef6845a72009-07-05 14:08:13 +02001003 subscr->equipment.classmark2_len = *classmark2_lv;
1004 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1005 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001006
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001007 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001008 return rc;
1009}
1010
1011static int gsm48_rx_rr_classmark(struct msgb *msg)
1012{
1013 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01001014 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Welte59b04682009-06-10 05:40:52 +08001015 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1016 u_int8_t cm2_len, cm3_len = 0;
1017 u_int8_t *cm2, *cm3 = NULL;
1018
1019 DEBUGP(DRR, "CLASSMARK CHANGE ");
1020
1021 /* classmark 2 */
1022 cm2_len = gh->data[0];
1023 cm2 = &gh->data[1];
1024 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1025
1026 if (payload_len > cm2_len + 1) {
1027 /* we must have a classmark3 */
1028 if (gh->data[cm2_len+1] != 0x20) {
1029 DEBUGPC(DRR, "ERR CM3 TAG\n");
1030 return -EINVAL;
1031 }
1032 if (cm2_len > 3) {
1033 DEBUGPC(DRR, "CM2 too long!\n");
1034 return -EINVAL;
1035 }
1036
1037 cm3_len = gh->data[cm2_len+2];
1038 cm3 = &gh->data[cm2_len+3];
1039 if (cm3_len > 14) {
1040 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1041 return -EINVAL;
1042 }
1043 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1044 }
1045 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001046 subscr->equipment.classmark2_len = cm2_len;
1047 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001048 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001049 subscr->equipment.classmark3_len = cm3_len;
1050 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001051 }
Harald Weltef6845a72009-07-05 14:08:13 +02001052 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001053 }
1054
Harald Welte59b04682009-06-10 05:40:52 +08001055 return 0;
1056}
1057
1058static int gsm48_rx_rr_status(struct msgb *msg)
1059{
1060 struct gsm48_hdr *gh = msgb_l3(msg);
1061
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001062 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Welte59b04682009-06-10 05:40:52 +08001063 rr_cause_name(gh->data[0]));
1064
1065 return 0;
1066}
1067
1068static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1069{
Harald Weltef9476812009-12-15 21:36:05 +01001070 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001071
Harald Weltec20bd1d2009-11-29 19:07:28 +01001072 /* This shouldn't actually end up here, as RSL treats
1073 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1074 * directly into gsm48_parse_meas_rep */
1075 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Weltef9476812009-12-15 21:36:05 +01001076 gsm48_parse_meas_rep(meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001077
1078 return 0;
1079}
1080
Harald Welte (local)b5103922009-08-15 23:32:44 +02001081static int gsm48_rx_rr_app_info(struct msgb *msg)
1082{
1083 struct gsm48_hdr *gh = msgb_l3(msg);
1084 u_int8_t apdu_id_flags;
1085 u_int8_t apdu_len;
1086 u_int8_t *apdu_data;
1087
1088 apdu_id_flags = gh->data[0];
1089 apdu_len = gh->data[1];
1090 apdu_data = gh->data+2;
1091
1092 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1093 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1094
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01001095 return db_apdu_blob_store(msg->lchan->conn.subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001096}
1097
Sylvain Munaut28c47162009-12-24 00:27:26 +01001098/* Chapter 9.1.10 Ciphering Mode Complete */
1099static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1100{
1101 struct gsm_lchan *lchan = msg->lchan;
1102 struct gsm_subscriber_connection *conn = &lchan->conn;
1103 gsm_cbfn *cb;
1104 int rc = 0;
1105
1106 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1107
1108 /* Safety check */
1109 if (!conn->sec_operation) {
1110 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1111 return -EIO;
1112 }
1113
1114 /* FIXME: check for MI (if any) */
1115
1116 /* Call back whatever was in progress (if anything) ... */
1117 cb = conn->sec_operation->cb;
1118 if (cb) {
1119 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1120 NULL, lchan, conn->sec_operation->cb_data);
1121 }
1122
1123 /* Complete the operation */
1124 release_security_operation(conn);
1125
1126 return rc;
1127}
1128
Harald Welte6720a432009-11-29 22:45:52 +01001129/* Chapter 9.1.16 Handover complete */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001130static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
1133
1134 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1135 rr_cause_name(gh->data[0]));
1136
1137 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1138 /* FIXME: release old channel */
1139
1140 return 0;
1141}
1142
1143/* Chapter 9.1.17 Handover Failure */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001144static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001145{
1146 struct gsm48_hdr *gh = msgb_l3(msg);
1147
1148 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1149 rr_cause_name(gh->data[0]));
1150
1151 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1152 /* FIXME: release allocated new channel */
1153
1154 return 0;
1155}
Harald Welte (local)b5103922009-08-15 23:32:44 +02001156
Harald Welte59b04682009-06-10 05:40:52 +08001157/* Receive a GSM 04.08 Radio Resource (RR) message */
1158static int gsm0408_rcv_rr(struct msgb *msg)
1159{
1160 struct gsm48_hdr *gh = msgb_l3(msg);
1161 int rc = 0;
1162
1163 switch (gh->msg_type) {
1164 case GSM48_MT_RR_CLSM_CHG:
1165 rc = gsm48_rx_rr_classmark(msg);
1166 break;
1167 case GSM48_MT_RR_GPRS_SUSP_REQ:
1168 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1169 break;
1170 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +02001171 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001172 break;
1173 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freyther9e6d6ce2009-10-22 15:23:11 +02001174 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001175 break;
1176 case GSM48_MT_RR_STATUS:
1177 rc = gsm48_rx_rr_status(msg);
1178 break;
1179 case GSM48_MT_RR_MEAS_REP:
1180 rc = gsm48_rx_rr_meas_rep(msg);
1181 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001182 case GSM48_MT_RR_APP_INFO:
1183 rc = gsm48_rx_rr_app_info(msg);
1184 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001185 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut28c47162009-12-24 00:27:26 +01001186 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welted2dd9de2009-08-30 15:37:11 +09001187 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001188 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001189 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001190 break;
1191 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001192 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001193 break;
Harald Welte59b04682009-06-10 05:40:52 +08001194 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001195 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1196 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001197 break;
1198 }
1199
1200 return rc;
1201}
1202
Harald Welte (local)b5103922009-08-15 23:32:44 +02001203int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001204 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001205{
1206 struct msgb *msg = gsm48_msgb_alloc();
1207 struct gsm48_hdr *gh;
1208
1209 msg->lchan = lchan;
1210
1211 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1212 apdu_id, apdu_len);
1213
1214 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1215 gh->proto_discr = GSM48_PDISC_RR;
1216 gh->msg_type = GSM48_MT_RR_APP_INFO;
1217 gh->data[0] = apdu_id;
1218 gh->data[1] = apdu_len;
1219 memcpy(gh->data+2, apdu, apdu_len);
1220
1221 return gsm48_sendmsg(msg, NULL);
1222}
1223
Harald Welte59b04682009-06-10 05:40:52 +08001224/* Call Control */
1225
1226/* The entire call control code is written in accordance with Figure 7.10c
1227 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1228 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1229 * it for voice */
1230
Harald Welte03740842009-06-10 23:11:52 +08001231static void new_cc_state(struct gsm_trans *trans, int state)
1232{
1233 if (state > 31 || state < 0)
1234 return;
1235
1236 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001237 gsm48_cc_state_name(trans->cc.state),
1238 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001239
Harald Weltec2189a62009-07-23 18:56:43 +02001240 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001241}
1242
1243static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001244{
1245 struct msgb *msg = gsm48_msgb_alloc();
1246 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1247 u_int8_t *cause, *call_state;
1248
Harald Welte59b04682009-06-10 05:40:52 +08001249 gh->msg_type = GSM48_MT_CC_STATUS;
1250
1251 cause = msgb_put(msg, 3);
1252 cause[0] = 2;
1253 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1254 cause[2] = 0x80 | 30; /* response to status inquiry */
1255
1256 call_state = msgb_put(msg, 1);
1257 call_state[0] = 0xc0 | 0x00;
1258
Harald Welte36fe2e82009-07-23 21:13:03 +02001259 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001260}
1261
1262static int gsm48_tx_simple(struct gsm_lchan *lchan,
1263 u_int8_t pdisc, u_int8_t msg_type)
1264{
1265 struct msgb *msg = gsm48_msgb_alloc();
1266 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1267
1268 msg->lchan = lchan;
1269
1270 gh->proto_discr = pdisc;
1271 gh->msg_type = msg_type;
1272
Harald Welte36fe2e82009-07-23 21:13:03 +02001273 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001274}
1275
Harald Welte03740842009-06-10 23:11:52 +08001276static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1277{
Harald Weltec2189a62009-07-23 18:56:43 +02001278 if (bsc_timer_pending(&trans->cc.timer)) {
1279 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1280 bsc_del_timer(&trans->cc.timer);
1281 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001282 }
1283}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001284
Harald Welte03740842009-06-10 23:11:52 +08001285static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1286 int msg_type, struct gsm_mncc *mncc)
1287{
1288 struct msgb *msg;
1289
1290 if (trans)
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001291 if (trans->conn)
Harald Welte4861c822009-07-23 21:21:14 +02001292 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001293 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001294 trans->conn->lchan->ts->trx->bts->nr,
1295 trans->conn->lchan->ts->trx->nr,
1296 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001297 (trans->subscr)?(trans->subscr->extension):"-",
1298 get_mncc_name(msg_type));
1299 else
1300 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1301 "Sending '%s' to MNCC.\n",
1302 (trans->subscr)?(trans->subscr->extension):"-",
1303 get_mncc_name(msg_type));
1304 else
1305 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1306 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1307
1308 mncc->msg_type = msg_type;
1309
Harald Welte9cfc9352009-06-26 19:39:35 +02001310 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001311 if (!msg)
1312 return -ENOMEM;
1313 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1314 msgb_enqueue(&net->upqueue, msg);
1315
1316 return 0;
1317}
1318
1319int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1320 u_int32_t callref, int location, int value)
1321{
1322 struct gsm_mncc rel;
1323
Harald Weltecb0595f2009-06-12 01:54:08 +08001324 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001325 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001326 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001327 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1328}
1329
Harald Weltec2189a62009-07-23 18:56:43 +02001330/* Call Control Specific transaction release.
1331 * gets called by trans_free, DO NOT CALL YOURSELF! */
1332void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001333{
Harald Welte03740842009-06-10 23:11:52 +08001334 gsm48_stop_cc_timer(trans);
1335
1336 /* send release to L4, if callref still exists */
1337 if (trans->callref) {
1338 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001339 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001340 GSM48_CAUSE_LOC_PRN_S_LU,
1341 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001342 }
Harald Weltec2189a62009-07-23 18:56:43 +02001343 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001344 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001345 if (trans->conn)
1346 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001347}
1348
1349static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001350
Harald Welte59b04682009-06-10 05:40:52 +08001351/* call-back from paging the B-end of the connection */
1352static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
1353 struct msgb *msg, void *_lchan, void *param)
1354{
1355 struct gsm_lchan *lchan = _lchan;
Harald Welte03740842009-06-10 23:11:52 +08001356 struct gsm_subscriber *subscr = param;
1357 struct gsm_trans *transt, *tmp;
1358 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001359
Harald Welte59b04682009-06-10 05:40:52 +08001360 if (hooknum != GSM_HOOK_RR_PAGING)
1361 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001362
Harald Welte03740842009-06-10 23:11:52 +08001363 if (!subscr)
1364 return -EINVAL;
1365 net = subscr->net;
1366 if (!net) {
1367 DEBUGP(DCC, "Error Network not set!\n");
1368 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001369 }
1370
Harald Welte03740842009-06-10 23:11:52 +08001371 /* check all tranactions (without lchan) for subscriber */
1372 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001373 if (transt->subscr != subscr || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001374 continue;
1375 switch (event) {
1376 case GSM_PAGING_SUCCEEDED:
1377 if (!lchan) // paranoid
1378 break;
1379 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1380 subscr->extension);
1381 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001382 if (!transt->conn) {
1383 transt->conn = &lchan->conn;
1384 use_subscr_con(transt->conn);
Harald Welte03740842009-06-10 23:11:52 +08001385 }
1386 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001387 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001388 break;
1389 case GSM_PAGING_EXPIRED:
1390 DEBUGP(DCC, "Paging subscr %s expired!\n",
1391 subscr->extension);
1392 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001393 mncc_release_ind(transt->subscr->net, transt,
1394 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001395 GSM48_CAUSE_LOC_PRN_S_LU,
1396 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001397 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001398 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001399 break;
1400 }
1401 }
Harald Welte59b04682009-06-10 05:40:52 +08001402 return 0;
1403}
1404
Harald Welte3971ad52009-12-19 22:23:05 +01001405static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1406
Harald Welte3c062072009-07-28 18:25:29 +02001407/* some other part of the code sends us a signal */
1408static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1409 void *handler_data, void *signal_data)
1410{
1411 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001412 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001413 struct gsm_network *net;
1414 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001415
1416 if (subsys != SS_ABISIP)
1417 return 0;
1418
1419 /* in case we use direct BTS-to-BTS RTP */
1420 if (ipacc_rtp_direct)
1421 return 0;
1422
Harald Welte3c062072009-07-28 18:25:29 +02001423 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001424 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001425 /* check if any transactions on this lchan still have
1426 * a tch_recv_mncc request pending */
1427 net = lchan->ts->trx->bts->network;
1428 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001429 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001430 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1431 tch_recv_mncc(net, trans->callref, 1);
1432 }
1433 }
Harald Welte3c062072009-07-28 18:25:29 +02001434 break;
Harald Welte3c062072009-07-28 18:25:29 +02001435 }
1436
1437 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001438}
1439
Harald Welte59b04682009-06-10 05:40:52 +08001440/* map two ipaccess RTP streams onto each other */
1441static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1442{
1443 struct gsm_bts *bts = lchan->ts->trx->bts;
1444 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001445 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001446
1447 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1448 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1449 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1450
1451 if (bts->type != remote_bts->type) {
1452 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1453 return -EINVAL;
1454 }
Harald Welte3971ad52009-12-19 22:23:05 +01001455
1456 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001457 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001458 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001459 if (!ipacc_rtp_direct) {
1460 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001461 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001462 if (rc < 0)
1463 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001464 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001465#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001466
1467 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301468 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1469 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001470 } else {
1471 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301472 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1473 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301474 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001475 if (rc < 0)
1476 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301477 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1478 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301479 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001480 }
Harald Welte59b04682009-06-10 05:40:52 +08001481 break;
1482 case GSM_BTS_TYPE_BS11:
1483 trau_mux_map_lchan(lchan, remote_lchan);
1484 break;
1485 default:
1486 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001487 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001488 }
1489
1490 return 0;
1491}
1492
Harald Welte03740842009-06-10 23:11:52 +08001493/* bridge channels of two transactions */
1494static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001495{
Harald Weltec2189a62009-07-23 18:56:43 +02001496 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1497 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001498
Harald Welte03740842009-06-10 23:11:52 +08001499 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001500 return -EIO;
1501
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001502 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001503 return -EIO;
1504
1505 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001506 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001507}
1508
Harald Welte3971ad52009-12-19 22:23:05 +01001509/* enable receive of channels to MNCC upqueue */
1510static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001511{
1512 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001513 struct gsm_lchan *lchan;
1514 struct gsm_bts *bts;
1515 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001516
Harald Welte03740842009-06-10 23:11:52 +08001517 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001518 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001519 if (!trans)
1520 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001521 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001522 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001523 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001524 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001525
Harald Welte3971ad52009-12-19 22:23:05 +01001526 switch (bts->type) {
1527 case GSM_BTS_TYPE_NANOBTS:
1528 if (ipacc_rtp_direct) {
1529 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1530 return -EINVAL;
1531 }
1532 /* in case, we don't have a RTP socket yet, we note this
1533 * in the transaction and try later */
1534 if (!lchan->abis_ip.rtp_socket) {
1535 trans->tch_recv = enable;
1536 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1537 return 0;
1538 }
1539 if (enable) {
1540 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001541 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001542 if (rc < 0)
1543 return rc;
1544 /* assign socket to application interface */
1545 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1546 net, callref);
1547 } else
1548 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1549 net, 0);
1550 break;
1551 case GSM_BTS_TYPE_BS11:
1552 if (enable)
1553 return trau_recv_lchan(lchan, callref);
1554 return trau_mux_unmap(NULL, callref);
1555 break;
1556 default:
1557 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1558 return -EINVAL;
1559 }
1560
1561 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001562}
1563
Harald Welte03740842009-06-10 23:11:52 +08001564static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1565{
1566 DEBUGP(DCC, "-> STATUS ENQ\n");
1567 return gsm48_cc_tx_status(trans, msg);
1568}
1569
1570static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1571static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1572
1573static void gsm48_cc_timeout(void *arg)
1574{
1575 struct gsm_trans *trans = arg;
1576 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001577 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1578 int mo_location = GSM48_CAUSE_LOC_USER;
1579 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1580 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001581 struct gsm_mncc mo_rel, l4_rel;
1582
1583 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1584 mo_rel.callref = trans->callref;
1585 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1586 l4_rel.callref = trans->callref;
1587
Harald Weltec2189a62009-07-23 18:56:43 +02001588 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001589 case 0x303:
1590 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001591 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001592 break;
1593 case 0x310:
1594 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001595 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001596 break;
1597 case 0x313:
1598 disconnect = 1;
1599 /* unknown, did not find it in the specs */
1600 break;
1601 case 0x301:
1602 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001603 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001604 break;
1605 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001606 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001607 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001608 gsm48_cc_tx_release(trans, &trans->cc.msg);
1609 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001610 break; /* stay in release state */
1611 }
Harald Weltec2189a62009-07-23 18:56:43 +02001612 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001613 return;
1614// release = 1;
1615// l4_cause = 14;
1616// break;
1617 case 0x306:
1618 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001619 mo_cause = trans->cc.msg.cause.value;
1620 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001621 break;
1622 case 0x323:
1623 disconnect = 1;
1624 break;
1625 default:
1626 release = 1;
1627 }
1628
1629 if (release && trans->callref) {
1630 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001631 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001632 l4_location, l4_cause);
1633 trans->callref = 0;
1634 }
1635
1636 if (disconnect && trans->callref) {
1637 /* process disconnect towards layer 4 */
1638 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001639 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001640 }
1641
1642 /* process disconnect towards mobile station */
1643 if (disconnect || release) {
1644 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001645 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1646 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1647 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001648 mo_rel.cause.diag_len = 3;
1649
1650 if (disconnect)
1651 gsm48_cc_tx_disconnect(trans, &mo_rel);
1652 if (release)
1653 gsm48_cc_tx_release(trans, &mo_rel);
1654 }
1655
1656}
1657
1658static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1659 int sec, int micro)
1660{
1661 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001662 trans->cc.timer.cb = gsm48_cc_timeout;
1663 trans->cc.timer.data = trans;
1664 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1665 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001666}
1667
1668static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1669{
1670 struct gsm48_hdr *gh = msgb_l3(msg);
1671 u_int8_t msg_type = gh->msg_type & 0xbf;
1672 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1673 struct tlv_parsed tp;
1674 struct gsm_mncc setup;
1675
1676 memset(&setup, 0, sizeof(struct gsm_mncc));
1677 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001678 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001679 /* emergency setup is identified by msg_type */
1680 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1681 setup.emergency = 1;
1682
1683 /* use subscriber as calling party number */
1684 if (trans->subscr) {
1685 setup.fields |= MNCC_F_CALLING;
1686 strncpy(setup.calling.number, trans->subscr->extension,
1687 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001688 strncpy(setup.imsi, trans->subscr->imsi,
1689 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001690 }
1691 /* bearer capability */
1692 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1693 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001694 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001695 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1696 }
1697 /* facility */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1699 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001700 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001701 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1702 }
1703 /* called party bcd number */
1704 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1705 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001706 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001707 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1708 }
1709 /* user-user */
1710 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1711 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001712 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001713 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1714 }
1715 /* ss-version */
1716 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1717 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001718 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001719 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1720 }
1721 /* CLIR suppression */
1722 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1723 setup.clir.sup = 1;
1724 /* CLIR invocation */
1725 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1726 setup.clir.inv = 1;
1727 /* cc cap */
1728 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1729 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001730 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001731 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1732 }
1733
Harald Welte03740842009-06-10 23:11:52 +08001734 new_cc_state(trans, GSM_CSTATE_INITIATED);
1735
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001736 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1737 subscr_name(trans->subscr), trans->subscr->extension,
1738 setup.called.number);
1739
Harald Welte03740842009-06-10 23:11:52 +08001740 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001741 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001742
Harald Welteca745e22009-07-29 12:10:35 +02001743 /* MNCC code will modify the channel asynchronously, we should
1744 * ipaccess-bind only after the modification has been made to the
1745 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001746 return 0;
1747}
1748
1749static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001750{
1751 struct msgb *msg = gsm48_msgb_alloc();
1752 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001753 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001754 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001755
1756 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1757
Harald Welte03740842009-06-10 23:11:52 +08001758 /* transaction id must not be assigned */
1759 if (trans->transaction_id != 0xff) { /* unasssigned */
1760 DEBUGP(DCC, "TX Setup with assigned transaction. "
1761 "This is not allowed!\n");
1762 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001763 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001764 GSM48_CAUSE_LOC_PRN_S_LU,
1765 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001766 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001767 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001768 return rc;
1769 }
1770
1771 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001772 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1773 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001774 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001775 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001776 GSM48_CAUSE_LOC_PRN_S_LU,
1777 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001778 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001779 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001780 return rc;
1781 }
Harald Welte165fc332009-07-23 21:36:44 +02001782 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001783
Harald Welte59b04682009-06-10 05:40:52 +08001784 gh->msg_type = GSM48_MT_CC_SETUP;
1785
Harald Welte03740842009-06-10 23:11:52 +08001786 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001787
Harald Welte03740842009-06-10 23:11:52 +08001788 /* bearer capability */
1789 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001790 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001791 /* facility */
1792 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001793 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001794 /* progress */
1795 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001796 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001797 /* calling party BCD number */
1798 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001799 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001800 /* called party BCD number */
1801 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001802 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001803 /* user-user */
1804 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001805 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001806 /* redirecting party BCD number */
1807 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001808 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001809 /* signal */
1810 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001811 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001812
1813 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001814
Harald Welte36fe2e82009-07-23 21:13:03 +02001815 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001816}
1817
Harald Welte03740842009-06-10 23:11:52 +08001818static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1819{
1820 struct gsm48_hdr *gh = msgb_l3(msg);
1821 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1822 struct tlv_parsed tp;
1823 struct gsm_mncc call_conf;
1824
1825 gsm48_stop_cc_timer(trans);
1826 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1827
1828 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1829 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001830 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001831#if 0
1832 /* repeat */
1833 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1834 call_conf.repeat = 1;
1835 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1836 call_conf.repeat = 2;
1837#endif
1838 /* bearer capability */
1839 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1840 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001841 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001842 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1843 }
1844 /* cause */
1845 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1846 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001847 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001848 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1849 }
1850 /* cc cap */
1851 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1852 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001853 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001854 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1855 }
1856
1857 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1858
Harald Welte0abe5a62009-07-23 19:06:52 +02001859 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1860 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001861}
1862
1863static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1864{
1865 struct gsm_mncc *proceeding = arg;
1866 struct msgb *msg = gsm48_msgb_alloc();
1867 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1868
Harald Welte03740842009-06-10 23:11:52 +08001869 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1870
1871 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1872
1873 /* bearer capability */
1874 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001875 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001876 /* facility */
1877 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001878 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001879 /* progress */
1880 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001881 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001882
Harald Welte36fe2e82009-07-23 21:13:03 +02001883 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001884}
1885
1886static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1887{
1888 struct gsm48_hdr *gh = msgb_l3(msg);
1889 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1890 struct tlv_parsed tp;
1891 struct gsm_mncc alerting;
1892
1893 gsm48_stop_cc_timer(trans);
1894 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1895
1896 memset(&alerting, 0, sizeof(struct gsm_mncc));
1897 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001898 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001899 /* facility */
1900 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1901 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001902 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001903 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1904 }
1905
1906 /* progress */
1907 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1908 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001909 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001910 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1911 }
1912 /* ss-version */
1913 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1914 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001915 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001916 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1917 }
1918
1919 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1920
Harald Welte0abe5a62009-07-23 19:06:52 +02001921 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1922 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001923}
1924
1925static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1926{
1927 struct gsm_mncc *alerting = arg;
1928 struct msgb *msg = gsm48_msgb_alloc();
1929 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1930
Harald Welte03740842009-06-10 23:11:52 +08001931 gh->msg_type = GSM48_MT_CC_ALERTING;
1932
1933 /* facility */
1934 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001935 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001936 /* progress */
1937 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001938 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001939 /* user-user */
1940 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001941 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001942
1943 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1944
Harald Welte36fe2e82009-07-23 21:13:03 +02001945 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001946}
1947
1948static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1949{
1950 struct gsm_mncc *progress = arg;
1951 struct msgb *msg = gsm48_msgb_alloc();
1952 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1953
Harald Welte03740842009-06-10 23:11:52 +08001954 gh->msg_type = GSM48_MT_CC_PROGRESS;
1955
1956 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01001957 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08001958 /* user-user */
1959 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001960 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001961
Harald Welte36fe2e82009-07-23 21:13:03 +02001962 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001963}
1964
1965static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1966{
1967 struct gsm_mncc *connect = arg;
1968 struct msgb *msg = gsm48_msgb_alloc();
1969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1970
Harald Welte03740842009-06-10 23:11:52 +08001971 gh->msg_type = GSM48_MT_CC_CONNECT;
1972
1973 gsm48_stop_cc_timer(trans);
1974 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1975
1976 /* facility */
1977 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001978 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08001979 /* progress */
1980 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001981 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08001982 /* connected number */
1983 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001984 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08001985 /* user-user */
1986 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001987 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001988
1989 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1990
Harald Welte36fe2e82009-07-23 21:13:03 +02001991 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001992}
1993
1994static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1995{
1996 struct gsm48_hdr *gh = msgb_l3(msg);
1997 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1998 struct tlv_parsed tp;
1999 struct gsm_mncc connect;
2000
2001 gsm48_stop_cc_timer(trans);
2002
2003 memset(&connect, 0, sizeof(struct gsm_mncc));
2004 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002005 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002006 /* use subscriber as connected party number */
2007 if (trans->subscr) {
2008 connect.fields |= MNCC_F_CONNECTED;
2009 strncpy(connect.connected.number, trans->subscr->extension,
2010 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002011 strncpy(connect.imsi, trans->subscr->imsi,
2012 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002013 }
2014 /* facility */
2015 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2016 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002017 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002018 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2019 }
2020 /* user-user */
2021 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2022 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002023 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002024 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2025 }
2026 /* ss-version */
2027 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2028 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002029 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002030 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2031 }
2032
2033 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2034
Harald Welte0abe5a62009-07-23 19:06:52 +02002035 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002036}
2037
2038
2039static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2040{
2041 struct gsm_mncc connect_ack;
2042
2043 gsm48_stop_cc_timer(trans);
2044
2045 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2046
2047 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2048 connect_ack.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002049 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002050 &connect_ack);
2051}
2052
2053static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2054{
2055 struct msgb *msg = gsm48_msgb_alloc();
2056 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2057
Harald Welte03740842009-06-10 23:11:52 +08002058 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2059
2060 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2061
Harald Welte36fe2e82009-07-23 21:13:03 +02002062 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002063}
2064
2065static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2066{
2067 struct gsm48_hdr *gh = msgb_l3(msg);
2068 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2069 struct tlv_parsed tp;
2070 struct gsm_mncc disc;
2071
2072 gsm48_stop_cc_timer(trans);
2073
2074 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2075
2076 memset(&disc, 0, sizeof(struct gsm_mncc));
2077 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002078 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002079 /* cause */
2080 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2081 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002082 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002083 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2084 }
2085 /* facility */
2086 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2087 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002088 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002089 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2090 }
2091 /* user-user */
2092 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2093 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002094 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002095 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2096 }
2097 /* ss-version */
2098 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2099 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002100 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002101 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2102 }
2103
Harald Welte0abe5a62009-07-23 19:06:52 +02002104 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002105
2106}
2107
Harald Weltebbc636a2009-06-11 14:23:20 +08002108static struct gsm_mncc_cause default_cause = {
2109 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2110 .coding = 0,
2111 .rec = 0,
2112 .rec_val = 0,
2113 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2114 .diag_len = 0,
2115 .diag = { 0 },
2116};
Harald Welte03740842009-06-10 23:11:52 +08002117
2118static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2119{
2120 struct gsm_mncc *disc = arg;
2121 struct msgb *msg = gsm48_msgb_alloc();
2122 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2123
Harald Welte03740842009-06-10 23:11:52 +08002124 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2125
2126 gsm48_stop_cc_timer(trans);
2127 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2128
2129 /* cause */
2130 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002131 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002132 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002133 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002134
2135 /* facility */
2136 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002137 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002138 /* progress */
2139 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002140 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002141 /* user-user */
2142 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002143 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002144
2145 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002146 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002147
2148 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2149
Harald Welte36fe2e82009-07-23 21:13:03 +02002150 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002151}
2152
2153static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2154{
2155 struct gsm48_hdr *gh = msgb_l3(msg);
2156 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2157 struct tlv_parsed tp;
2158 struct gsm_mncc rel;
2159 int rc;
2160
2161 gsm48_stop_cc_timer(trans);
2162
2163 memset(&rel, 0, sizeof(struct gsm_mncc));
2164 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002165 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002166 /* cause */
2167 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2168 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002169 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002170 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2171 }
2172 /* facility */
2173 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2174 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002175 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002176 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2177 }
2178 /* user-user */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2180 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002181 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002182 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2183 }
2184 /* ss-version */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2186 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002187 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2189 }
2190
Harald Weltec2189a62009-07-23 18:56:43 +02002191 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002192 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002193 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002194 } else {
Harald Welte0abe5a62009-07-23 19:06:52 +02002195 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02002196 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002197 GSM48_MT_CC_RELEASE_COMPL);
2198 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002199 }
2200
2201 new_cc_state(trans, GSM_CSTATE_NULL);
2202
2203 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002204 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002205
2206 return rc;
2207}
2208
2209static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2210{
2211 struct gsm_mncc *rel = arg;
2212 struct msgb *msg = gsm48_msgb_alloc();
2213 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2214
Harald Welte03740842009-06-10 23:11:52 +08002215 gh->msg_type = GSM48_MT_CC_RELEASE;
2216
2217 trans->callref = 0;
2218
2219 gsm48_stop_cc_timer(trans);
2220 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2221
2222 /* cause */
2223 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002224 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002225 /* facility */
2226 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002227 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002228 /* user-user */
2229 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002230 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002231
Harald Weltec2189a62009-07-23 18:56:43 +02002232 trans->cc.T308_second = 0;
2233 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002234
Harald Weltec2189a62009-07-23 18:56:43 +02002235 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002236 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2237
Harald Welte36fe2e82009-07-23 21:13:03 +02002238 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002239}
2240
2241static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2242{
2243 struct gsm48_hdr *gh = msgb_l3(msg);
2244 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2245 struct tlv_parsed tp;
2246 struct gsm_mncc rel;
2247 int rc = 0;
2248
2249 gsm48_stop_cc_timer(trans);
2250
2251 memset(&rel, 0, sizeof(struct gsm_mncc));
2252 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002253 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002254 /* cause */
2255 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2256 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002257 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002258 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2259 }
2260 /* facility */
2261 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2262 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002263 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002264 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2265 }
2266 /* user-user */
2267 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2268 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002269 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002270 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2271 }
2272 /* ss-version */
2273 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2274 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002275 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002276 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2277 }
2278
2279 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002280 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002281 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002282 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002283 MNCC_REJ_IND, &rel);
2284 break;
2285 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002286 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002287 MNCC_REL_CNF, &rel);
Harald Welte9ed54ff2009-12-12 21:00:48 +01002288 /* FIXME: in case of multiple calls, we can't simply
2289 * hang up here ! */
2290 lchan_auto_release(msg->lchan);
Harald Welte03740842009-06-10 23:11:52 +08002291 break;
2292 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002293 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002294 MNCC_REL_IND, &rel);
2295 }
2296 }
2297
2298 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002299 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002300
2301 return rc;
2302}
2303
2304static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2305{
2306 struct gsm_mncc *rel = arg;
2307 struct msgb *msg = gsm48_msgb_alloc();
2308 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2309
Harald Welte03740842009-06-10 23:11:52 +08002310 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2311
2312 trans->callref = 0;
2313
2314 gsm48_stop_cc_timer(trans);
2315
2316 /* cause */
2317 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002318 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002319 /* facility */
2320 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002321 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002322 /* user-user */
2323 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002324 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002325
Harald Weltec2189a62009-07-23 18:56:43 +02002326 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002327
Harald Welte36fe2e82009-07-23 21:13:03 +02002328 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002329}
2330
2331static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2332{
2333 struct gsm48_hdr *gh = msgb_l3(msg);
2334 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2335 struct tlv_parsed tp;
2336 struct gsm_mncc fac;
2337
2338 memset(&fac, 0, sizeof(struct gsm_mncc));
2339 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002340 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002341 /* facility */
2342 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2343 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002344 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002345 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2346 }
2347 /* ss-version */
2348 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2349 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002350 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002351 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2352 }
2353
Harald Welte0abe5a62009-07-23 19:06:52 +02002354 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002355}
2356
2357static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2358{
2359 struct gsm_mncc *fac = arg;
2360 struct msgb *msg = gsm48_msgb_alloc();
2361 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2362
Harald Welte03740842009-06-10 23:11:52 +08002363 gh->msg_type = GSM48_MT_CC_FACILITY;
2364
2365 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002366 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002367
Harald Welte36fe2e82009-07-23 21:13:03 +02002368 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002369}
2370
2371static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2372{
2373 struct gsm_mncc hold;
2374
2375 memset(&hold, 0, sizeof(struct gsm_mncc));
2376 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002377 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002378}
2379
2380static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2381{
2382 struct msgb *msg = gsm48_msgb_alloc();
2383 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2384
Harald Welte03740842009-06-10 23:11:52 +08002385 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2386
Harald Welte36fe2e82009-07-23 21:13:03 +02002387 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2391{
2392 struct gsm_mncc *hold_rej = arg;
2393 struct msgb *msg = gsm48_msgb_alloc();
2394 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2395
Harald Welte03740842009-06-10 23:11:52 +08002396 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2397
2398 /* cause */
2399 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002400 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002401 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002402 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002403
Harald Welte36fe2e82009-07-23 21:13:03 +02002404 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2408{
2409 struct gsm_mncc retrieve;
2410
2411 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2412 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002413 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2414 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2418{
2419 struct msgb *msg = gsm48_msgb_alloc();
2420 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2421
Harald Welte03740842009-06-10 23:11:52 +08002422 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2423
Harald Welte36fe2e82009-07-23 21:13:03 +02002424 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *retrieve_rej = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte03740842009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2434
2435 /* cause */
2436 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002437 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002438 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002439 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002440
Harald Welte36fe2e82009-07-23 21:13:03 +02002441 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2445{
2446 struct gsm48_hdr *gh = msgb_l3(msg);
2447 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2448 struct tlv_parsed tp;
2449 struct gsm_mncc dtmf;
2450
2451 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2452 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002453 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002454 /* keypad facility */
2455 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2456 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002457 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002458 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2459 }
2460
Harald Welte0abe5a62009-07-23 19:06:52 +02002461 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002462}
2463
2464static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2465{
2466 struct gsm_mncc *dtmf = arg;
2467 struct msgb *msg = gsm48_msgb_alloc();
2468 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2469
Harald Welte03740842009-06-10 23:11:52 +08002470 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2471
2472 /* keypad */
2473 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002474 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002475
Harald Welte36fe2e82009-07-23 21:13:03 +02002476 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002477}
2478
2479static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2480{
2481 struct gsm_mncc *dtmf = arg;
2482 struct msgb *msg = gsm48_msgb_alloc();
2483 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2484
Harald Welte03740842009-06-10 23:11:52 +08002485 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2486
2487 /* cause */
2488 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002489 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002490 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002491 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002492
Harald Welte36fe2e82009-07-23 21:13:03 +02002493 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2497{
2498 struct msgb *msg = gsm48_msgb_alloc();
2499 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2500
Harald Welte03740842009-06-10 23:11:52 +08002501 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2502
Harald Welte36fe2e82009-07-23 21:13:03 +02002503 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2507{
2508 struct gsm_mncc dtmf;
2509
2510 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2511 dtmf.callref = trans->callref;
2512
Harald Welte0abe5a62009-07-23 19:06:52 +02002513 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2517{
2518 struct gsm48_hdr *gh = msgb_l3(msg);
2519 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2520 struct tlv_parsed tp;
2521 struct gsm_mncc modify;
2522
2523 memset(&modify, 0, sizeof(struct gsm_mncc));
2524 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002525 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002526 /* bearer capability */
2527 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2528 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002529 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002530 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2531 }
2532
2533 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2534
Harald Welte0abe5a62009-07-23 19:06:52 +02002535 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2539{
2540 struct gsm_mncc *modify = arg;
2541 struct msgb *msg = gsm48_msgb_alloc();
2542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2543
Harald Welte03740842009-06-10 23:11:52 +08002544 gh->msg_type = GSM48_MT_CC_MODIFY;
2545
2546 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2547
2548 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002549 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002550
2551 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2552
Harald Welte36fe2e82009-07-23 21:13:03 +02002553 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002554}
2555
2556static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2557{
2558 struct gsm48_hdr *gh = msgb_l3(msg);
2559 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2560 struct tlv_parsed tp;
2561 struct gsm_mncc modify;
2562
2563 gsm48_stop_cc_timer(trans);
2564
2565 memset(&modify, 0, sizeof(struct gsm_mncc));
2566 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002567 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002568 /* bearer capability */
2569 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2570 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002571 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002572 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2573 }
2574
2575 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2576
Harald Welte0abe5a62009-07-23 19:06:52 +02002577 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002578}
2579
2580static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2581{
2582 struct gsm_mncc *modify = arg;
2583 struct msgb *msg = gsm48_msgb_alloc();
2584 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2585
Harald Welte03740842009-06-10 23:11:52 +08002586 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2587
2588 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002589 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002590
2591 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2592
Harald Welte36fe2e82009-07-23 21:13:03 +02002593 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002594}
2595
2596static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2597{
2598 struct gsm48_hdr *gh = msgb_l3(msg);
2599 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2600 struct tlv_parsed tp;
2601 struct gsm_mncc modify;
2602
2603 gsm48_stop_cc_timer(trans);
2604
2605 memset(&modify, 0, sizeof(struct gsm_mncc));
2606 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002607 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002608 /* bearer capability */
2609 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2610 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002611 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002612 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2613 }
2614 /* cause */
2615 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2616 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002617 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002618 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2619 }
2620
2621 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2622
Harald Welte0abe5a62009-07-23 19:06:52 +02002623 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002624}
2625
2626static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2627{
2628 struct gsm_mncc *modify = arg;
2629 struct msgb *msg = gsm48_msgb_alloc();
2630 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2631
Harald Welte03740842009-06-10 23:11:52 +08002632 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2633
2634 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002635 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002636 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002637 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002638
2639 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2640
Harald Welte36fe2e82009-07-23 21:13:03 +02002641 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002642}
2643
2644static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2645{
2646 struct gsm_mncc *notify = arg;
2647 struct msgb *msg = gsm48_msgb_alloc();
2648 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2649
Harald Welte03740842009-06-10 23:11:52 +08002650 gh->msg_type = GSM48_MT_CC_NOTIFY;
2651
2652 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002653 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002654
Harald Welte36fe2e82009-07-23 21:13:03 +02002655 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2659{
2660 struct gsm48_hdr *gh = msgb_l3(msg);
2661 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2662// struct tlv_parsed tp;
2663 struct gsm_mncc notify;
2664
2665 memset(&notify, 0, sizeof(struct gsm_mncc));
2666 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002667// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002668 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002669 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002670
Harald Welte0abe5a62009-07-23 19:06:52 +02002671 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002672}
2673
2674static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2675{
2676 struct gsm_mncc *user = arg;
2677 struct msgb *msg = gsm48_msgb_alloc();
2678 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2679
Harald Welte03740842009-06-10 23:11:52 +08002680 gh->msg_type = GSM48_MT_CC_USER_INFO;
2681
2682 /* user-user */
2683 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002684 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002685 /* more data */
2686 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002687 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002688
Harald Welte36fe2e82009-07-23 21:13:03 +02002689 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002690}
2691
2692static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2693{
2694 struct gsm48_hdr *gh = msgb_l3(msg);
2695 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2696 struct tlv_parsed tp;
2697 struct gsm_mncc user;
2698
2699 memset(&user, 0, sizeof(struct gsm_mncc));
2700 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002701 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002702 /* user-user */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2704 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002705 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002706 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2707 }
2708 /* more data */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2710 user.more = 1;
2711
Harald Welte0abe5a62009-07-23 19:06:52 +02002712 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002713}
2714
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002715static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002716{
2717 struct gsm_mncc *mode = arg;
2718
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002719 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002720}
2721
2722static struct downstate {
2723 u_int32_t states;
2724 int type;
2725 int (*rout) (struct gsm_trans *trans, void *arg);
2726} downstatelist[] = {
2727 /* mobile originating call establishment */
2728 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2729 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2730 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2731 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2732 {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 */
2733 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2734 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2735 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2736 /* mobile terminating call establishment */
2737 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2738 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2739 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2740 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2741 /* signalling during call */
2742 {SBIT(GSM_CSTATE_ACTIVE),
2743 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2744 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2745 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2746 {ALL_STATES,
2747 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2748 {ALL_STATES,
2749 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2750 {ALL_STATES,
2751 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2752 {SBIT(GSM_CSTATE_ACTIVE),
2753 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2754 {SBIT(GSM_CSTATE_ACTIVE),
2755 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2756 {SBIT(GSM_CSTATE_ACTIVE),
2757 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2758 {SBIT(GSM_CSTATE_ACTIVE),
2759 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2760 {SBIT(GSM_CSTATE_ACTIVE),
2761 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2762 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2763 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2764 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2765 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2766 {SBIT(GSM_CSTATE_ACTIVE),
2767 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2768 /* clearing */
2769 {SBIT(GSM_CSTATE_INITIATED),
2770 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2771 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2772 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2773 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2774 MNCC_REL_REQ, gsm48_cc_tx_release},
2775 /* special */
2776 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002777 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002778};
2779
2780#define DOWNSLLEN \
2781 (sizeof(downstatelist) / sizeof(struct downstate))
2782
2783
2784int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2785{
Harald Welteaa60edb2009-08-09 18:52:33 +02002786 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002787 struct gsm_trans *trans = NULL, *transt;
Harald Welteaa60edb2009-08-09 18:52:33 +02002788 struct gsm_lchan *lchan = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002789 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002790 struct gsm_mncc *data = arg, rel;
2791
2792 /* handle special messages */
2793 switch(msg_type) {
2794 case MNCC_BRIDGE:
2795 return tch_bridge(net, arg);
2796 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002797 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002798 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002799 return tch_recv_mncc(net, data->callref, 1);
2800 case GSM_TCHF_FRAME:
2801 /* Find callref */
2802 trans = trans_find_by_callref(net, data->callref);
2803 if (!trans)
2804 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002805 if (!trans->conn)
Harald Welte3971ad52009-12-19 22:23:05 +01002806 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002807 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welte3971ad52009-12-19 22:23:05 +01002808 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002809 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002810 switch (bts->type) {
2811 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002812 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welte3971ad52009-12-19 22:23:05 +01002813 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002814 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002815 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002816 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002817 default:
2818 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2819 }
2820 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002821 }
2822
2823 memset(&rel, 0, sizeof(struct gsm_mncc));
2824 rel.callref = data->callref;
2825
2826 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002827 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002828
2829 /* Callref unknown */
2830 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002831 struct gsm_subscriber *subscr;
2832
Harald Welte6e1536e2009-07-04 10:11:24 +02002833 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002834 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2835 "Received '%s' from MNCC with "
2836 "unknown callref %d\n", data->called.number,
2837 get_mncc_name(msg_type), data->callref);
2838 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002839 return mncc_release_ind(net, NULL, data->callref,
2840 GSM48_CAUSE_LOC_PRN_S_LU,
2841 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002842 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002843 if (!data->called.number[0] && !data->imsi[0]) {
2844 DEBUGP(DCC, "(bts - trx - ts - ti) "
2845 "Received '%s' from MNCC with "
2846 "no number or IMSI\n", get_mncc_name(msg_type));
2847 /* Invalid number */
2848 return mncc_release_ind(net, NULL, data->callref,
2849 GSM48_CAUSE_LOC_PRN_S_LU,
2850 GSM48_CC_CAUSE_INV_NR_FORMAT);
2851 }
Harald Welte03740842009-06-10 23:11:52 +08002852 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002853 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002854 subscr = subscr_get_by_extension(net,
2855 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002856 else
Harald Welte75350412009-07-23 18:46:00 +02002857 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002858 /* If subscriber is not found */
2859 if (!subscr) {
2860 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2861 "Received '%s' from MNCC with "
2862 "unknown subscriber %s\n", data->called.number,
2863 get_mncc_name(msg_type), data->called.number);
2864 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002865 return mncc_release_ind(net, NULL, data->callref,
2866 GSM48_CAUSE_LOC_PRN_S_LU,
2867 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002868 }
2869 /* If subscriber is not "attached" */
2870 if (!subscr->lac) {
2871 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2872 "Received '%s' from MNCC with "
2873 "detached subscriber %s\n", data->called.number,
2874 get_mncc_name(msg_type), data->called.number);
2875 subscr_put(subscr);
2876 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002877 return mncc_release_ind(net, NULL, data->callref,
2878 GSM48_CAUSE_LOC_PRN_S_LU,
2879 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002880 }
2881 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002882 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2883 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002884 DEBUGP(DCC, "No memory for trans.\n");
2885 subscr_put(subscr);
2886 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002887 mncc_release_ind(net, NULL, data->callref,
2888 GSM48_CAUSE_LOC_PRN_S_LU,
2889 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002890 return -ENOMEM;
2891 }
Harald Welte03740842009-06-10 23:11:52 +08002892 /* Find lchan */
Harald Welteaa60edb2009-08-09 18:52:33 +02002893 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002894
Harald Welte03740842009-06-10 23:11:52 +08002895 /* If subscriber has no lchan */
2896 if (!lchan) {
2897 /* find transaction with this subscriber already paging */
2898 llist_for_each_entry(transt, &net->trans_list, entry) {
2899 /* Transaction of our lchan? */
2900 if (transt == trans ||
2901 transt->subscr != subscr)
2902 continue;
2903 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2904 "Received '%s' from MNCC with "
2905 "unallocated channel, paging already "
2906 "started.\n", bts->nr,
2907 data->called.number,
2908 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002909 subscr_put(subscr);
2910 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002911 return 0;
2912 }
2913 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002914 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte2a3a81b2009-08-01 19:31:47 +02002915 /* Trigger paging */
2916 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2917 setup_trig_pag_evt, subscr);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002918 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002919 return 0;
2920 }
2921 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002922 trans->conn = &lchan->conn;
2923 use_subscr_con(trans->conn);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002924 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002925 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002926
2927 if (trans->conn)
2928 lchan = trans->conn->lchan;
Harald Welte03740842009-06-10 23:11:52 +08002929
2930 /* if paging did not respond yet */
2931 if (!lchan) {
2932 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002933 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08002934 (trans->subscr)?(trans->subscr->extension):"-",
2935 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08002936 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2937 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08002938 if (msg_type == MNCC_REL_REQ)
2939 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2940 else
2941 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2942 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002943 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002944 return rc;
2945 }
2946
2947 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2948 "Received '%s' from MNCC in state %d (%s)\n",
2949 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2950 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002951 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02002952 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08002953 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08002954
2955 /* Find function for current state and message */
2956 for (i = 0; i < DOWNSLLEN; i++)
2957 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02002958 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08002959 break;
2960 if (i == DOWNSLLEN) {
2961 DEBUGP(DCC, "Message unhandled at this state.\n");
2962 return 0;
2963 }
2964
2965 rc = downstatelist[i].rout(trans, arg);
2966
2967 return rc;
2968}
2969
2970
2971static struct datastate {
2972 u_int32_t states;
2973 int type;
2974 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2975} datastatelist[] = {
2976 /* mobile originating call establishment */
2977 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2978 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2979 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2980 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2981 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2982 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2983 /* mobile terminating call establishment */
2984 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2985 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2986 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2987 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002988 {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 Welte03740842009-06-10 23:11:52 +08002989 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2990 /* signalling during call */
2991 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2992 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2993 {SBIT(GSM_CSTATE_ACTIVE),
2994 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2995 {ALL_STATES,
2996 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2997 {ALL_STATES,
2998 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2999 {ALL_STATES,
3000 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3001 {SBIT(GSM_CSTATE_ACTIVE),
3002 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3003 {SBIT(GSM_CSTATE_ACTIVE),
3004 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3005 {SBIT(GSM_CSTATE_ACTIVE),
3006 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3007 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3008 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3009 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3010 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3011 {SBIT(GSM_CSTATE_ACTIVE),
3012 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3013 /* clearing */
3014 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3015 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3016 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3017 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3018 {ALL_STATES, /* 5.4.3.4 */
3019 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3020};
3021
3022#define DATASLLEN \
3023 (sizeof(datastatelist) / sizeof(struct datastate))
3024
Harald Welte59b04682009-06-10 05:40:52 +08003025static int gsm0408_rcv_cc(struct msgb *msg)
3026{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003027 struct gsm_subscriber_connection *conn;
Harald Welte59b04682009-06-10 05:40:52 +08003028 struct gsm48_hdr *gh = msgb_l3(msg);
3029 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003030 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte03740842009-06-10 23:11:52 +08003031 struct gsm_lchan *lchan = msg->lchan;
Harald Weltec2189a62009-07-23 18:56:43 +02003032 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003033 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003034
Harald Welte03740842009-06-10 23:11:52 +08003035 if (msg_type & 0x80) {
3036 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3037 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003038 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003039
3040 conn = &lchan->conn;
3041
Harald Welte03740842009-06-10 23:11:52 +08003042 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003043 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003044
Harald Welte4861c822009-07-23 21:21:14 +02003045 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003046 "Received '%s' from MS in state %d (%s)\n",
3047 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003048 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003049 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3050 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003051
3052 /* Create transaction */
3053 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003054 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003055 "creating new trans.\n", transaction_id);
3056 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003057 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003058 transaction_id, new_callref++);
3059 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003060 DEBUGP(DCC, "No memory for trans.\n");
3061 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02003062 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003063 GSM48_MT_CC_RELEASE_COMPL);
3064 return -ENOMEM;
3065 }
Harald Welte03740842009-06-10 23:11:52 +08003066 /* Assign transaction */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003067 trans->conn = &lchan->conn;
3068 use_subscr_con(trans->conn);
Harald Welte03740842009-06-10 23:11:52 +08003069 }
3070
3071 /* find function for current state and message */
3072 for (i = 0; i < DATASLLEN; i++)
3073 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003074 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003075 break;
3076 if (i == DATASLLEN) {
3077 DEBUGP(DCC, "Message unhandled at this state.\n");
3078 return 0;
3079 }
3080
3081 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003082
3083 return rc;
3084}
3085
3086/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)64994ce2009-08-14 11:41:12 +02003087int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte59b04682009-06-10 05:40:52 +08003088{
3089 struct gsm48_hdr *gh = msgb_l3(msg);
3090 u_int8_t pdisc = gh->proto_discr & 0x0f;
3091 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003092
3093 if (silent_call_reroute(msg))
3094 return silent_call_rx(msg);
Harald Welte59b04682009-06-10 05:40:52 +08003095
3096 switch (pdisc) {
3097 case GSM48_PDISC_CC:
3098 rc = gsm0408_rcv_cc(msg);
3099 break;
3100 case GSM48_PDISC_MM:
3101 rc = gsm0408_rcv_mm(msg);
3102 break;
3103 case GSM48_PDISC_RR:
3104 rc = gsm0408_rcv_rr(msg);
3105 break;
3106 case GSM48_PDISC_SMS:
Harald Welte (local)64994ce2009-08-14 11:41:12 +02003107 rc = gsm0411_rcv_sms(msg, link_id);
Harald Welte59b04682009-06-10 05:40:52 +08003108 break;
3109 case GSM48_PDISC_MM_GPRS:
3110 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003111 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3112 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003113 break;
Harald Welte03115042009-10-16 08:32:58 +02003114 case GSM48_PDISC_NC_SS:
3115 rc = handle_rcv_ussd(msg);
3116 break;
Harald Welte59b04682009-06-10 05:40:52 +08003117 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003118 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3119 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003120 break;
3121 }
3122
3123 return rc;
3124}
3125
Harald Welte3c062072009-07-28 18:25:29 +02003126/*
3127 * This will be ran by the linker when loading the DSO. We use it to
3128 * do system initialization, e.g. registration of signal handlers.
3129 */
3130static __attribute__((constructor)) void on_dso_load_0408(void)
3131{
Harald Welte3c062072009-07-28 18:25:29 +02003132 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3133 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3134}