blob: 8fbe81097f566b1cd5a93ed638faec0f9f6493d8 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/*
2 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: AGPL-3.0+
6 *
7 * Author: Neels Hofmeyr
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <osmocom/msc/gsm_data.h>
24#include <osmocom/msc/paging.h>
25#include <osmocom/msc/vlr.h>
26#include <osmocom/msc/ran_peer.h>
27#include <osmocom/msc/sgs_iface.h>
28#include <osmocom/msc/signal.h>
29#include <osmocom/msc/msc_a.h>
30#include <osmocom/msc/transaction.h>
31
32#define LOG_PAGING(vsub, paging_request, level, fmt, args ...) \
33 LOGP(DPAG, level, "Paging: %s%s%s: " fmt, \
34 vlr_subscr_name(vsub), paging_request ? " for " : "", paging_request ? (paging_request)->label : "", ## args)
35
36#define VSUB_USE_PAGING "Paging"
37
38const struct value_string paging_cause_names[] = {
39 { PAGING_CAUSE_CALL_CONVERSATIONAL, "CALL_CONVERSATIONAL" },
40 { PAGING_CAUSE_CALL_STREAMING, "CALL_STREAMING" },
41 { PAGING_CAUSE_CALL_INTERACTIVE, "CALL_INTERACTIVE" },
42 { PAGING_CAUSE_CALL_BACKGROUND, "CALL_BACKGROUND" },
43 { PAGING_CAUSE_SIGNALLING_LOW_PRIO, "SIGNALLING_LOW_PRIO" },
44 { PAGING_CAUSE_SIGNALLING_HIGH_PRIO, "SIGNALLING_HIGH_PRIO" },
45 { PAGING_CAUSE_UNSPECIFIED, "UNSPECIFIED" },
46 {}
47};
48
49static void paging_response_timer_cb(void *data)
50{
51 struct vlr_subscr *vsub = data;
Philipp Maier002fb012019-09-24 09:26:47 +020052
53 if (vsub->cs.attached_via_ran == OSMO_RAT_EUTRAN_SGS)
54 sgs_iface_tx_serv_abrt(vsub);
55
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010056 paging_expired(vsub);
57}
58
59/* Execute a paging on the currently active RAN. Returns the number of
60 * delivered paging requests or -EINVAL in case of failure. */
61static int msc_paging_request(struct paging_request *pr, struct vlr_subscr *vsub)
62{
63 struct gsm_network *net = vsub->vlr->user_ctx;
64
65 /* The subscriber was last seen in subscr->lac. Find out which
66 * BSCs/RNCs are responsible and send them a paging request via open
67 * SCCP connections (if any). */
68 switch (vsub->cs.attached_via_ran) {
69 case OSMO_RAT_GERAN_A:
70 return ran_peers_down_paging(net->a.sri, CELL_IDENT_LAC, vsub, pr->cause);
71 case OSMO_RAT_UTRAN_IU:
72 return ran_peers_down_paging(net->iu.sri, CELL_IDENT_LAC, vsub, pr->cause);
73 case OSMO_RAT_EUTRAN_SGS:
74 return sgs_iface_tx_paging(vsub, sgs_serv_ind_from_paging_cause(pr->cause));
75 default:
Vadim Yanitskiy01926fc2019-06-15 23:05:56 +070076 LOG_PAGING(vsub, pr, LOGL_ERROR, "Cannot page, subscriber not attached\n");
Vadim Yanitskiy08553e02019-06-15 23:04:19 +070077 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010078 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010079}
80
81struct paging_request *paging_request_start(struct vlr_subscr *vsub, enum paging_cause cause,
82 paging_cb_t paging_cb, struct gsm_trans *trans,
83 const char *label)
84{
85 int rc;
86 struct paging_request *pr;
Alexander Couzensae167fc2020-09-25 05:25:16 +020087 int paging_response_timer;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010088
Vadim Yanitskiya12ac822019-06-15 23:01:42 +070089 pr = talloc(vsub, struct paging_request);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010090 OSMO_ASSERT(pr);
91 *pr = (struct paging_request){
92 .label = label,
93 .cause = cause,
94 .paging_cb = paging_cb,
95 .trans = trans,
96 };
97
98 if (vsub->cs.is_paging) {
99 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Already paging, not starting another request\n");
100 } else {
101 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Starting paging\n");
102
103 rc = msc_paging_request(pr, vsub);
104 if (rc <= 0) {
105 LOG_PAGING(vsub, pr, LOGL_ERROR, "Starting paging failed (rc=%d)\n", rc);
106 talloc_free(pr);
107 return NULL;
108 }
109
110 /* reduced on the first paging callback */
111 vlr_subscr_get(vsub, VSUB_USE_PAGING);
112 vsub->cs.is_paging = true;
Alexander Couzensae167fc2020-09-25 05:25:16 +0200113 paging_response_timer = osmo_tdef_get(msc_ran_infra[vsub->cs.attached_via_ran].tdefs, -4, OSMO_TDEF_S, 10);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100114 osmo_timer_setup(&vsub->cs.paging_response_timer, paging_response_timer_cb, vsub);
Alexander Couzensae167fc2020-09-25 05:25:16 +0200115 osmo_timer_schedule(&vsub->cs.paging_response_timer, paging_response_timer, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100116 }
117
118 llist_add_tail(&pr->entry, &vsub->cs.requests);
119
120 return pr;
121}
122
123void paging_request_remove(struct paging_request *pr)
124{
125 struct gsm_trans *trans = pr->trans;
126 struct vlr_subscr *vsub = trans ? trans->vsub : NULL;
127 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Removing Paging Request\n");
128
129 if (pr->trans && pr->trans->paging_request == pr)
130 pr->trans->paging_request = NULL;
131
132 llist_del(&pr->entry);
133 talloc_free(pr);
134}
135
136static void paging_concludes(struct vlr_subscr *vsub, struct msc_a *msc_a)
137{
138 struct paging_request *pr, *pr_next;
139 struct paging_signal_data sig_data;
140
Neels Hofmeyrebf55f42020-03-09 21:01:56 +0100141 if (!vsub) {
142 /* A Paging Response has no subscriber. (Related: OS#4449) */
143 return;
144 }
145
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100146 osmo_timer_del(&vsub->cs.paging_response_timer);
147
148 llist_for_each_entry_safe(pr, pr_next, &vsub->cs.requests, entry) {
149 struct gsm_trans *trans = pr->trans;
150 paging_cb_t paging_cb = pr->paging_cb;
151
152 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Paging Response action (%s)%s\n",
153 msc_a ? "success" : "expired",
154 paging_cb ? "" : " (no action defined)");
155
156 /* Remove the paging request before the paging_cb could deallocate e.g. the trans */
157 paging_request_remove(pr);
158 pr = NULL;
159
160 if (paging_cb)
161 paging_cb(msc_a, trans);
162 }
163
164 /* Inform parts of the system we don't know */
165 sig_data = (struct paging_signal_data){
166 .vsub = vsub,
167 .msc_a = msc_a,
168 };
169 osmo_signal_dispatch(SS_PAGING, msc_a ? S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, &sig_data);
170
171 /* balanced with the moment we start paging */
172 if (vsub->cs.is_paging) {
173 vsub->cs.is_paging = false;
174 vlr_subscr_put(vsub, VSUB_USE_PAGING);
175 }
176
177 /* Handling of the paging requests has usually added transactions, which keep the msc_a connection active. If
178 * there are none, then this probably marks release of the connection. */
179 if (msc_a)
180 msc_a_put(msc_a, MSC_A_USE_PAGING_RESPONSE);
181}
182
183void paging_response(struct msc_a *msc_a)
184{
185 paging_concludes(msc_a_vsub(msc_a), msc_a);
186}
187
188void paging_expired(struct vlr_subscr *vsub)
189{
190 paging_concludes(vsub, NULL);
191}