blob: 14974f338c251778cea3057b48d7298c7f02adb4 [file] [log] [blame]
Harald Weltea1482332009-11-14 10:08:40 +01001/* GSM silent call feature */
2
3/*
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Weltea1482332009-11-14 10:08:40 +010011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Weltea1482332009-11-14 10:08:40 +010017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Weltea1482332009-11-14 10:08:40 +010020 *
21 */
22
23#include <stdlib.h>
24#include <unistd.h>
25#include <errno.h>
26
Sylvain Munaut93558302019-02-14 20:13:08 +010027#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010028#include <osmocom/core/msgb.h>
Philipp Maier92861142019-03-15 09:43:40 +010029#include <osmocom/core/utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/signal.h>
31#include <osmocom/msc/debug.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/gsm_data.h>
33#include <osmocom/msc/gsm_subscriber.h>
Neels Hofmeyrd656dff2018-03-09 14:59:44 +010034#include <osmocom/msc/vlr.h>
Harald Weltea1482332009-11-14 10:08:40 +010035
Sylvain Munaut93558302019-02-14 20:13:08 +010036#include <osmocom/sigtran/sccp_helpers.h>
37
38struct silent_call_data {
39 struct gsm0808_channel_type ct;
40
41 char traffic_ip[INET_ADDRSTRLEN];
42 uint16_t traffic_port;
43
44 void *data;
45
46 struct osmo_timer_list timer;
47 struct ran_conn *conn;
48};
49
50static void timer_cb(void *data)
51{
52 struct silent_call_data *scd = (struct silent_call_data *)data;
53 ran_conn_communicating(scd->conn);
54 talloc_free(scd);
55}
56
Harald Welte51008772009-12-29 11:49:12 +010057/* paging of the requested subscriber has completed */
Harald Weltea1482332009-11-14 10:08:40 +010058static int paging_cb_silent(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +080059 struct msgb *msg, void *_conn, void *_data)
Harald Weltea1482332009-11-14 10:08:40 +010060{
Sylvain Munaut93558302019-02-14 20:13:08 +010061 struct silent_call_data *scd = (struct silent_call_data *)_data;
Neels Hofmeyrc036b792018-11-29 22:37:51 +010062 struct ran_conn *conn = _conn;
Harald Weltea1482332009-11-14 10:08:40 +010063 struct scall_signal_data sigdata;
Sylvain Munaut93558302019-02-14 20:13:08 +010064 struct msgb *msg_ass;
Holger Hans Peter Freythera97152b2010-07-23 19:34:34 +080065 int rc = 0;
Sylvain Munaut93558302019-02-14 20:13:08 +010066 int i;
Harald Weltea1482332009-11-14 10:08:40 +010067
68 if (hooknum != GSM_HOOK_RR_PAGING)
69 return -EINVAL;
70
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +010071 DEBUGP(DLSMS, "paging_cb_silent: ");
Harald Weltea1482332009-11-14 10:08:40 +010072
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +080073 sigdata.conn = conn;
Sylvain Munaut93558302019-02-14 20:13:08 +010074 sigdata.data = scd->data;
Harald Weltea1482332009-11-14 10:08:40 +010075
76 switch (event) {
77 case GSM_PAGING_SUCCEEDED:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020078#if BEFORE_MSCSPLIT
79 /* Re-enable this log output once we can obtain this information via
80 * A-interface, see OS#2391. */
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +010081 DEBUGPC(DLSMS, "success, using Timeslot %u on ARFCN %u\n",
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +080082 conn->lchan->ts->nr, conn->lchan->ts->trx->arfcn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020083#endif
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010084 conn->silent_call = 1;
Sylvain Munaut93558302019-02-14 20:13:08 +010085
86 /* Increment lchan reference count and mark as active*/
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +010087 ran_conn_get(conn, RAN_CONN_USE_SILENT_CALL);
Sylvain Munaut93558302019-02-14 20:13:08 +010088
89 /* Schedule a timer to mark it as active */
90 /* This is a hack we we can't call ran_conn_communicating
91 * from here because we're in the call back context of
92 * a RAN FSM event but before it actually changes its own
93 * state and it's not ready to accept this.
94 * Of all alternatives considered, making the call in an
95 * 'immediate timer' is the least disruptive and least ugly
96 * way to do it I could find.
97 */
98 scd->conn = conn;
99 osmo_timer_setup(&scd->timer, timer_cb, scd);
100 osmo_timer_schedule(&scd->timer, 0, 0);
101
102 /* Manually craft an assignement message with requested mode */
103 if (scd->ct.ch_indctr == GSM0808_CHAN_SPEECH) {
104 struct gsm0808_speech_codec_list scl;
105 union {
106 struct sockaddr_storage st;
107 struct sockaddr_in in;
108 } rtp_addr;
109
110 memset(&rtp_addr, 0, sizeof(rtp_addr));
111 rtp_addr.in.sin_family = AF_INET;
112 rtp_addr.in.sin_port = osmo_htons(scd->traffic_port);
113 rtp_addr.in.sin_addr.s_addr = inet_addr(scd->traffic_ip);
114
115 for (i = 0; i < scd->ct.perm_spch_len; i++)
116 gsm0808_speech_codec_from_chan_type(&scl.codec[i], scd->ct.perm_spch[i]);
117 scl.len = scd->ct.perm_spch_len;
118
119 msg_ass = gsm0808_create_ass(&scd->ct, NULL, &rtp_addr.st, &scl, NULL);
120 } else {
121 msg_ass = gsm0808_create_ass(&scd->ct, NULL, NULL, NULL, NULL);
122 }
123
124 /* Send assignement message, hoping it will work */
125 osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_ass);
126
127 /* Signal completion */
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200128 osmo_signal_dispatch(SS_SCALL, S_SCALL_SUCCESS, &sigdata);
Sylvain Munaut93558302019-02-14 20:13:08 +0100129 return 0;
130
Harald Weltea1482332009-11-14 10:08:40 +0100131 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100132 case GSM_PAGING_BUSY:
Holger Hans Peter Freythereff409492012-11-10 19:46:58 +0100133 DEBUGP(DLSMS, "expired\n");
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200134 osmo_signal_dispatch(SS_SCALL, S_SCALL_EXPIRED, &sigdata);
Harald Weltea1482332009-11-14 10:08:40 +0100135 break;
136 default:
137 rc = -EINVAL;
138 break;
139 }
140
Sylvain Munaut93558302019-02-14 20:13:08 +0100141 talloc_free(scd);
142
Harald Weltea1482332009-11-14 10:08:40 +0100143 return rc;
144}
145
Philipp Maiere0d5caa2017-02-27 16:56:59 +0100146#if 0
Harald Welte51008772009-12-29 11:49:12 +0100147/* receive a layer 3 message from a silent call */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100148int silent_call_rx(struct ran_conn *conn, struct msgb *msg)
Harald Welte51008772009-12-29 11:49:12 +0100149{
150 /* FIXME: do something like sending it through a UDP port */
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100151 LOGP(DLSMS, LOGL_NOTICE, "Discarding L3 message from a silent call.\n");
Harald Welte51008772009-12-29 11:49:12 +0100152 return 0;
153}
154
155struct msg_match {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200156 uint8_t pdisc;
157 uint8_t msg_type;
Harald Welte51008772009-12-29 11:49:12 +0100158};
159
160/* list of messages that are handled inside OpenBSC, even in a silent call */
161static const struct msg_match silent_call_accept[] = {
162 { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST },
163 { GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_REQ },
164};
165
166/* decide if we need to reroute a message as part of a silent call */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100167int silent_call_reroute(struct ran_conn *conn, struct msgb *msg)
Harald Welte51008772009-12-29 11:49:12 +0100168{
169 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100170 uint8_t pdisc = gsm48_hdr_pdisc(gh);
171 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte51008772009-12-29 11:49:12 +0100172 int i;
173
174 /* if we're not part of a silent call, never reroute */
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +0800175 if (!conn->silent_call)
Harald Welte51008772009-12-29 11:49:12 +0100176 return 0;
177
178 /* check if we are a special message that is handled in openbsc */
179 for (i = 0; i < ARRAY_SIZE(silent_call_accept); i++) {
180 if (silent_call_accept[i].pdisc == pdisc &&
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100181 silent_call_accept[i].msg_type == msg_type)
Harald Welte51008772009-12-29 11:49:12 +0100182 return 0;
183 }
184
185 /* otherwise, reroute */
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100186 LOGP(DLSMS, LOGL_INFO, "Rerouting L3 message from a silent call.\n");
Harald Welte51008772009-12-29 11:49:12 +0100187 return 1;
188}
Philipp Maiere0d5caa2017-02-27 16:56:59 +0100189#endif
Harald Welte51008772009-12-29 11:49:12 +0100190
191
192/* initiate a silent call with a given subscriber */
Sylvain Munaut93558302019-02-14 20:13:08 +0100193int gsm_silent_call_start(struct vlr_subscr *vsub,
194 const struct gsm0808_channel_type *ct,
195 const char *traffic_dst_ip, uint16_t traffic_dst_port,
196 void *data)
Harald Weltea1482332009-11-14 10:08:40 +0100197{
Holger Hans Peter Freytherb618c7e2015-08-03 11:21:29 +0200198 struct subscr_request *req;
Sylvain Munaut93558302019-02-14 20:13:08 +0100199 struct silent_call_data *scd;
Harald Weltea1482332009-11-14 10:08:40 +0100200
Sylvain Munaut93558302019-02-14 20:13:08 +0100201 scd = talloc_zero(vsub, struct silent_call_data);
202
203 memcpy(&scd->ct, ct, sizeof(scd->ct));
204
205 if (traffic_dst_ip) {
Philipp Maier92861142019-03-15 09:43:40 +0100206 osmo_strlcpy(scd->traffic_ip, traffic_dst_ip, sizeof(scd->traffic_ip));
Sylvain Munaut93558302019-02-14 20:13:08 +0100207 scd->traffic_port = traffic_dst_port;
208 }
209
210 scd->data = data;
211
212 req = subscr_request_conn(vsub, paging_cb_silent, scd,
Harald Welte0df904d2018-12-03 11:00:04 +0100213 "establish silent call",
214 SGSAP_SERV_IND_CS_CALL);
Sylvain Munaut93558302019-02-14 20:13:08 +0100215 if (!req) {
216 talloc_free(scd);
Neels Hofmeyrd656dff2018-03-09 14:59:44 +0100217 return -ENODEV;
Sylvain Munaut93558302019-02-14 20:13:08 +0100218 }
219
Neels Hofmeyrd656dff2018-03-09 14:59:44 +0100220 return 0;
Harald Weltea1482332009-11-14 10:08:40 +0100221}
222
Harald Welte51008772009-12-29 11:49:12 +0100223/* end a silent call with a given subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +0200224int gsm_silent_call_stop(struct vlr_subscr *vsub)
Harald Weltea1482332009-11-14 10:08:40 +0100225{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100226 struct ran_conn *conn;
Harald Weltea1482332009-11-14 10:08:40 +0100227
Harald Welte2483f1b2016-06-19 18:06:02 +0200228 conn = connection_for_subscr(vsub);
Neels Hofmeyrd656dff2018-03-09 14:59:44 +0100229 if (!conn) {
230 LOGP(DMM, LOGL_ERROR, "%s: Cannot stop silent call, no connection for subscriber\n",
231 vlr_subscr_name(vsub));
232 return -ENODEV;
233 }
Harald Weltea1482332009-11-14 10:08:40 +0100234
Harald Welte83579ca2009-12-29 11:17:18 +0100235 /* did we actually establish a silent call for this guy? */
Neels Hofmeyrd656dff2018-03-09 14:59:44 +0100236 if (!conn->silent_call) {
237 LOGP(DMM, LOGL_ERROR, "%s: Cannot stop silent call, subscriber has no active silent call\n",
238 vlr_subscr_name(vsub));
239 return -ENOENT;
240 }
Harald Welte83579ca2009-12-29 11:17:18 +0100241
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200242#if BEFORE_MSCSPLIT
243 /* Re-enable this log output once we can obtain this information via
244 * A-interface, see OS#2391. */
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100245 DEBUGPC(DLSMS, "Stopping silent call using Timeslot %u on ARFCN %u\n",
246 conn->lchan->ts->nr, conn->lchan->ts->trx->arfcn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200247#endif
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100248
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800249 conn->silent_call = 0;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100250 ran_conn_put(conn, RAN_CONN_USE_SILENT_CALL);
Harald Weltea1482332009-11-14 10:08:40 +0100251
252 return 0;
253}