blob: 7d2e8981b54f92a4cff6d22d1eebadac2c6621df [file] [log] [blame]
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001/* Implementation for MSC decisions which interface to send messages out on. */
2
3/* (C) 2016 by sysmocom s.m.f.c GmbH <info@sysmocom.de>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <osmocom/core/logging.h>
22
23#include <openbsc/debug.h>
24#include <openbsc/gsm_data.h>
25#include <openbsc/msc_ifaces.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020026#include <openbsc/iu.h>
27#include <openbsc/gsm_subscriber.h>
28#include <openbsc/transaction.h>
29#include <openbsc/mgcp.h>
30#include <openbsc/mgcpgw_client.h>
31#include <openbsc/vlr.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020032#include <openbsc/a_iface.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020033
34#include "../../bscconfig.h"
35
36#ifdef BUILD_IU
37extern struct msgb *ranap_new_msg_rab_assign_voice(uint8_t rab_id,
38 uint32_t rtp_ip,
39 uint16_t rtp_port,
40 bool use_x213_nsap);
41#endif /* BUILD_IU */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020042
43static int msc_tx(struct gsm_subscriber_connection *conn, struct msgb *msg)
44{
Philipp Maierfbf66102017-04-09 12:32:51 +020045 if (!conn)
46 return -EINVAL;
47 if (!msg)
48 return -EINVAL;
49
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020050 DEBUGP(DMSC, "msc_tx %u bytes to %s via %s\n",
51 msg->len, vlr_subscr_name(conn->vsub),
52 ran_type_name(conn->via_ran));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020053 switch (conn->via_ran) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020054 case RAN_GERAN_A:
55 msg->dst = conn;
Philipp Maierfbf66102017-04-09 12:32:51 +020056 return a_iface_tx_dtap(msg);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020057
58 case RAN_UTRAN_IU:
59 msg->dst = conn->iu.ue_ctx;
60 return iu_tx(msg, 0);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020061
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020062 default:
63 LOGP(DMSC, LOGL_ERROR,
64 "msc_tx(): conn->via_ran invalid (%d)\n",
65 conn->via_ran);
66 return -1;
67 }
68}
69
70
71int msc_tx_dtap(struct gsm_subscriber_connection *conn,
72 struct msgb *msg)
73{
74 return msc_tx(conn, msg);
75}
76
77
78/* 9.2.5 CM service accept */
79int msc_gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
80{
Philipp Maierfbf66102017-04-09 12:32:51 +020081 struct msgb *msg;
82 struct gsm48_hdr *gh;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020083
Philipp Maierfbf66102017-04-09 12:32:51 +020084 if (!conn)
85 return -EINVAL;
86
87 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACC");
88
89 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020090 gh->proto_discr = GSM48_PDISC_MM;
91 gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
92
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020093 DEBUGP(DMM, "-> CM SERVICE ACCEPT %s\n",
94 vlr_subscr_name(conn->vsub));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020095
96 return msc_tx_dtap(conn, msg);
97}
98
99/* 9.2.6 CM service reject */
100int msc_gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
101 enum gsm48_reject_value value)
102{
103 struct msgb *msg;
Philipp Maierfbf66102017-04-09 12:32:51 +0200104
105 if (!conn)
106 return -EINVAL;
107
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200108 conn->received_cm_service_request = false;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200109
110 msg = gsm48_create_mm_serv_rej(value);
111 if (!msg) {
112 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
113 return -1;
114 }
115
116 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
117
118 return msc_tx_dtap(conn, msg);
119}
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200120
121int msc_tx_common_id(struct gsm_subscriber_connection *conn)
122{
Philipp Maierfbf66102017-04-09 12:32:51 +0200123 if (!conn)
124 return -EINVAL;
125
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200126 /* Common ID is only sent over IuCS */
127 if (conn->via_ran != RAN_UTRAN_IU) {
128 LOGP(DMM, LOGL_INFO,
129 "%s: Asked to transmit Common ID, but skipping"
130 " because this is not on UTRAN\n",
131 vlr_subscr_name(conn->vsub));
132 return 0;
133 }
134
135 DEBUGP(DIUCS, "%s: tx CommonID %s\n",
136 vlr_subscr_name(conn->vsub), conn->vsub->imsi);
137 return iu_tx_common_id(conn->iu.ue_ctx, conn->vsub->imsi);
138}
139
Philipp Maierfbf66102017-04-09 12:32:51 +0200140static int iu_rab_act_cs(struct ue_conn_ctx *uectx, uint8_t rab_id,
141 uint32_t rtp_ip, uint16_t rtp_port)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200142{
Philipp Maierfbf66102017-04-09 12:32:51 +0200143#ifdef BUILD_IU
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200144 struct msgb *msg;
145 bool use_x213_nsap;
146 uint32_t conn_id = uectx->conn_id;
147
148 use_x213_nsap = (uectx->rab_assign_addr_enc == NSAP_ADDR_ENC_X213);
149
150 LOGP(DIUCS, LOGL_DEBUG, "Assigning RAB: conn_id=%u, rab_id=%d,"
151 " rtp=%x:%u, use_x213_nsap=%d\n", conn_id, rab_id, rtp_ip,
152 rtp_port, use_x213_nsap);
153
154 msg = ranap_new_msg_rab_assign_voice(rab_id, rtp_ip, rtp_port,
155 use_x213_nsap);
156 msg->l2h = msg->data;
157
158 if (iu_rab_act(uectx, msg))
159 LOGP(DIUCS, LOGL_ERROR, "Failed to send RAB Assignment:"
160 " conn_id=%d rab_id=%d rtp=%x:%u\n",
161 conn_id, rab_id, rtp_ip, rtp_port);
Philipp Maierfbf66102017-04-09 12:32:51 +0200162 return 0;
163#else
164 LOGP(DMSC, LOGL_ERROR, "Cannot send Iu RAB Assignment: built without Iu support\n");
165 return -ENOTSUP;
166#endif
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200167}
168
169static void mgcp_response_rab_act_cs_crcx(struct mgcp_response *r, void *priv)
170{
171 struct gsm_trans *trans = priv;
172 struct gsm_subscriber_connection *conn = trans->conn;
173 struct ue_conn_ctx *uectx = conn->iu.ue_ctx;
174 uint32_t rtp_ip;
175 int rc;
176
177 if (r->head.response_code != 200) {
178 LOGP(DMGCP, LOGL_ERROR,
179 "MGCPGW response yields error: %d %s\n",
180 r->head.response_code, r->head.comment);
181 goto rab_act_cs_error;
182 }
183
184 rc = mgcp_response_parse_params(r);
185 if (rc) {
186 LOGP(DMGCP, LOGL_ERROR,
187 "Cannot parse MGCP response, for %s\n",
188 vlr_subscr_name(trans->vsub));
189 goto rab_act_cs_error;
190 }
191
Philipp Maierfbf66102017-04-09 12:32:51 +0200192 conn->rtp.port_cn = r->audio_port;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200193
194 rtp_ip = mgcpgw_client_remote_addr_n(conn->network->mgcpgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +0200195
196 if (trans->conn->via_ran == RAN_UTRAN_IU) {
197 /* Assign a voice channel via RANAP on 3G */
198 if (iu_rab_act_cs(uectx, conn->iu.rab_id, rtp_ip, conn->rtp.port_subscr))
199 goto rab_act_cs_error;
200 } else if (trans->conn->via_ran == RAN_GERAN_A) {
201 /* Assign a voice channel via A on 2G */
202 if (a_iface_tx_assignment(trans))
203 goto rab_act_cs_error;
204 } else
205 goto rab_act_cs_error;
206
207 /* Respond back to MNCC (if requested) */
208 if (trans->tch_rtp_create) {
209 if (gsm48_tch_rtp_create(trans))
210 goto rab_act_cs_error;
211 }
212 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200213
214rab_act_cs_error:
215 /* FIXME abort call, invalidate conn, ... */
Philipp Maierfbf66102017-04-09 12:32:51 +0200216 LOGP(DMSC, LOGL_ERROR, "%s: failure during assignment\n",
217 vlr_subscr_name(trans->vsub));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200218 return;
219}
220
Philipp Maierfbf66102017-04-09 12:32:51 +0200221int msc_call_assignment(struct gsm_trans *trans)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200222{
Philipp Maierfbf66102017-04-09 12:32:51 +0200223 struct gsm_subscriber_connection *conn;
224 struct mgcpgw_client *mgcp;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200225 struct msgb *msg;
Philipp Maierfbf66102017-04-09 12:32:51 +0200226 uint16_t bts_base;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200227
Philipp Maierfbf66102017-04-09 12:32:51 +0200228 if (!trans)
229 return -EINVAL;
230 if (!trans->conn)
231 return -EINVAL;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200232
Philipp Maierfbf66102017-04-09 12:32:51 +0200233 conn = trans->conn;
234 mgcp = conn->network->mgcpgw.client;
235
236#ifdef BUILD_IU
237 /* FIXME: HACK. where to scope the RAB Id? At the conn / subscriber / ue_conn_ctx? */
238 static uint8_t next_iu_rab_id = 1;
239 if (conn->via_ran == RAN_UTRAN_IU)
240 conn->iu.rab_id = next_iu_rab_id ++;
241#endif
242
243 conn->rtp.mgcp_rtp_endpoint =
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200244 mgcpgw_client_next_endpoint(conn->network->mgcpgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +0200245
246 /* This will calculate the port we assign to the BTS via AoIP
247 * assignment command (or rab-assignment on 3G) The BTS will send
248 * its RTP traffic to that port on the MGCPGW side. The MGCPGW only
249 * gets the endpoint ID via the CRCX. It will do the same calculation
250 * on his side too to get knowledge of the rtp port. */
251 bts_base = mgcp->actual.bts_base;
252 conn->rtp.port_subscr = bts_base + 2 * conn->rtp.mgcp_rtp_endpoint;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253
254 /* Establish the RTP stream first as looping back to the originator.
255 * The MDCX will patch through to the counterpart. TODO: play a ring
256 * tone instead. */
Philipp Maierfbf66102017-04-09 12:32:51 +0200257 msg = mgcp_msg_crcx(mgcp, conn->rtp.mgcp_rtp_endpoint,
258 conn->rtp.mgcp_rtp_endpoint, MGCP_CONN_LOOPBACK);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200259 return mgcpgw_client_tx(mgcp, msg, mgcp_response_rab_act_cs_crcx, trans);
260}
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200261
262static void mgcp_response_bridge_mdcx(struct mgcp_response *r, void *priv);
263
264static void mgcp_bridge(struct gsm_trans *from, struct gsm_trans *to,
265 enum bridge_state state,
266 enum mgcp_connection_mode mode)
267{
268 struct gsm_subscriber_connection *conn1 = from->conn;
269 struct gsm_subscriber_connection *conn2 = to->conn;
270 struct mgcpgw_client *mgcp = conn1->network->mgcpgw.client;
271 const char *ip;
272 struct msgb *msg;
273
274 OSMO_ASSERT(mgcp);
275
276 from->bridge.peer = to;
277 from->bridge.state = state;
278
279 /* Loop back to the same MGCP GW */
280 ip = mgcpgw_client_remote_addr_str(mgcp);
281
282 msg = mgcp_msg_mdcx(mgcp,
Philipp Maierfbf66102017-04-09 12:32:51 +0200283 conn1->rtp.mgcp_rtp_endpoint,
284 ip, conn2->rtp.port_cn,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200285 mode);
286 if (mgcpgw_client_tx(mgcp, msg, mgcp_response_bridge_mdcx, from))
287 LOGP(DMGCP, LOGL_ERROR,
288 "Failed to send MDCX message for %s\n",
289 vlr_subscr_name(from->vsub));
290}
291
292static void mgcp_response_bridge_mdcx(struct mgcp_response *r, void *priv)
293{
294 struct gsm_trans *trans = priv;
295 struct gsm_trans *peer = trans->bridge.peer;
296
297 switch (trans->bridge.state) {
298 case BRIDGE_STATE_LOOPBACK_PENDING:
299 trans->bridge.state = BRIDGE_STATE_LOOPBACK_ESTABLISHED;
300
301 switch (peer->bridge.state) {
302 case BRIDGE_STATE_LOOPBACK_PENDING:
303 /* Wait until the other is done as well. */
304 return;
305 case BRIDGE_STATE_LOOPBACK_ESTABLISHED:
306 /* Now that both are in loopback, switch both to
307 * forwarding. */
308 mgcp_bridge(trans, peer, BRIDGE_STATE_BRIDGE_PENDING,
309 MGCP_CONN_RECV_SEND);
310 mgcp_bridge(peer, trans, BRIDGE_STATE_BRIDGE_PENDING,
311 MGCP_CONN_RECV_SEND);
312 break;
313 default:
314 LOGP(DMGCP, LOGL_ERROR,
315 "Unexpected bridge state: %d for %s\n",
316 trans->bridge.state, vlr_subscr_name(trans->vsub));
317 break;
318 }
319 break;
320
321 case BRIDGE_STATE_BRIDGE_PENDING:
322 trans->bridge.state = BRIDGE_STATE_BRIDGE_ESTABLISHED;
323 break;
324
325 default:
326 LOGP(DMGCP, LOGL_ERROR,
327 "Unexpected bridge state: %d for %s\n",
328 trans->bridge.state, vlr_subscr_name(trans->vsub));
329 break;
330 }
331}
332
Philipp Maierfbf66102017-04-09 12:32:51 +0200333int msc_call_connect(struct gsm_trans *trans, uint16_t port, uint32_t ip)
334{
335 /* With this function we inform the MGCP-GW where (ip/port) it
336 * has to send its outgoing voic traffic. The receiving end will
337 * usually be a PBX (e.g. Asterisk). The IP-Address we tell, will
338 * not only be used to direct the traffic, it will also be used
339 * as a filter to make sure only RTP packets from the right
340 * remote end will reach the BSS. This is also the reason why
341 * inbound audio will not work until this step is performed */
342
343 /* NOTE: This function is used when msc_call_bridge(), is not
344 * applicable. This is usually the case when an external MNCC
345 * is in use */
346
347 struct gsm_subscriber_connection *conn;
348 struct mgcpgw_client *mgcp;
349 struct msgb *msg;
350
351 if (!trans)
352 return -EINVAL;
353 if (!trans->conn)
354 return -EINVAL;
355 if (!trans->conn->network)
356 return -EINVAL;
357 if (!trans->conn->network->mgcpgw.client)
358 return -EINVAL;
359
360 mgcp = trans->conn->network->mgcpgw.client;
361
362 struct in_addr ip_addr;
363 ip_addr.s_addr = ntohl(ip);
364
365 conn = trans->conn;
366
367 msg = mgcp_msg_mdcx(mgcp,
368 conn->rtp.mgcp_rtp_endpoint,
369 inet_ntoa(ip_addr), port, MGCP_CONN_RECV_SEND);
370 if (mgcpgw_client_tx(mgcp, msg, NULL, trans))
371 LOGP(DMGCP, LOGL_ERROR,
372 "Failed to send MDCX message for %s\n",
373 vlr_subscr_name(trans->vsub));
374
375 return 0;
376}
377
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200378int msc_call_bridge(struct gsm_trans *trans1, struct gsm_trans *trans2)
379{
Philipp Maierfbf66102017-04-09 12:32:51 +0200380 if (!trans1)
381 return -EINVAL;
382 if (!trans2)
383 return -EINVAL;
384
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200385 /* First setup as loopback and configure the counterparts' endpoints,
386 * so that when transmission starts the originating addresses are
387 * already known to be valid. The mgcp callback will continue. */
388 mgcp_bridge(trans1, trans2, BRIDGE_STATE_LOOPBACK_PENDING,
389 MGCP_CONN_LOOPBACK);
390 mgcp_bridge(trans2, trans1, BRIDGE_STATE_LOOPBACK_PENDING,
391 MGCP_CONN_LOOPBACK);
392
393 return 0;
394}
Philipp Maierfbf66102017-04-09 12:32:51 +0200395
396void msc_call_release(struct gsm_trans *trans)
397{
398 struct msgb *msg;
399 struct gsm_subscriber_connection *conn;
400 struct mgcpgw_client *mgcp;
401
402 if (!trans)
403 return;
404 if (!trans->conn)
405 return;
406 if (!trans->conn->network)
407 return;
408
409 conn = trans->conn;
410 mgcp = conn->network->mgcpgw.client;
411
412 /* Send DLCX */
413 msg = mgcp_msg_dlcx(mgcp, conn->rtp.mgcp_rtp_endpoint,
414 conn->rtp.mgcp_rtp_endpoint);
415 if (mgcpgw_client_tx(mgcp, msg, NULL, NULL))
416 LOGP(DMGCP, LOGL_ERROR,
417 "Failed to send DLCX message for %s\n",
418 vlr_subscr_name(trans->vsub));
419
420 /* Release endpoint id */
421 mgcpgw_client_release_endpoint(conn->rtp.mgcp_rtp_endpoint, mgcp);
422}