blob: 182b03682a087f012d2f2bf8ae0f7542c050a6e7 [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;
52 paging_expired(vsub);
53}
54
55/* Execute a paging on the currently active RAN. Returns the number of
56 * delivered paging requests or -EINVAL in case of failure. */
57static int msc_paging_request(struct paging_request *pr, struct vlr_subscr *vsub)
58{
59 struct gsm_network *net = vsub->vlr->user_ctx;
60
61 /* The subscriber was last seen in subscr->lac. Find out which
62 * BSCs/RNCs are responsible and send them a paging request via open
63 * SCCP connections (if any). */
64 switch (vsub->cs.attached_via_ran) {
65 case OSMO_RAT_GERAN_A:
66 return ran_peers_down_paging(net->a.sri, CELL_IDENT_LAC, vsub, pr->cause);
67 case OSMO_RAT_UTRAN_IU:
68 return ran_peers_down_paging(net->iu.sri, CELL_IDENT_LAC, vsub, pr->cause);
69 case OSMO_RAT_EUTRAN_SGS:
70 return sgs_iface_tx_paging(vsub, sgs_serv_ind_from_paging_cause(pr->cause));
71 default:
Vadim Yanitskiy01926fc2019-06-15 23:05:56 +070072 LOG_PAGING(vsub, pr, LOGL_ERROR, "Cannot page, subscriber not attached\n");
Vadim Yanitskiy08553e02019-06-15 23:04:19 +070073 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010074 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075}
76
77struct paging_request *paging_request_start(struct vlr_subscr *vsub, enum paging_cause cause,
78 paging_cb_t paging_cb, struct gsm_trans *trans,
79 const char *label)
80{
81 int rc;
82 struct paging_request *pr;
83 struct gsm_network *net = vsub->vlr->user_ctx;
84
Vadim Yanitskiya12ac822019-06-15 23:01:42 +070085 pr = talloc(vsub, struct paging_request);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010086 OSMO_ASSERT(pr);
87 *pr = (struct paging_request){
88 .label = label,
89 .cause = cause,
90 .paging_cb = paging_cb,
91 .trans = trans,
92 };
93
94 if (vsub->cs.is_paging) {
95 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Already paging, not starting another request\n");
96 } else {
97 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Starting paging\n");
98
99 rc = msc_paging_request(pr, vsub);
100 if (rc <= 0) {
101 LOG_PAGING(vsub, pr, LOGL_ERROR, "Starting paging failed (rc=%d)\n", rc);
102 talloc_free(pr);
103 return NULL;
104 }
105
106 /* reduced on the first paging callback */
107 vlr_subscr_get(vsub, VSUB_USE_PAGING);
108 vsub->cs.is_paging = true;
109 osmo_timer_setup(&vsub->cs.paging_response_timer, paging_response_timer_cb, vsub);
110 osmo_timer_schedule(&vsub->cs.paging_response_timer, net->paging_response_timer, 0);
111 }
112
113 llist_add_tail(&pr->entry, &vsub->cs.requests);
114
115 return pr;
116}
117
118void paging_request_remove(struct paging_request *pr)
119{
120 struct gsm_trans *trans = pr->trans;
121 struct vlr_subscr *vsub = trans ? trans->vsub : NULL;
122 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Removing Paging Request\n");
123
124 if (pr->trans && pr->trans->paging_request == pr)
125 pr->trans->paging_request = NULL;
126
127 llist_del(&pr->entry);
128 talloc_free(pr);
129}
130
131static void paging_concludes(struct vlr_subscr *vsub, struct msc_a *msc_a)
132{
133 struct paging_request *pr, *pr_next;
134 struct paging_signal_data sig_data;
135
136 osmo_timer_del(&vsub->cs.paging_response_timer);
137
138 llist_for_each_entry_safe(pr, pr_next, &vsub->cs.requests, entry) {
139 struct gsm_trans *trans = pr->trans;
140 paging_cb_t paging_cb = pr->paging_cb;
141
142 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Paging Response action (%s)%s\n",
143 msc_a ? "success" : "expired",
144 paging_cb ? "" : " (no action defined)");
145
146 /* Remove the paging request before the paging_cb could deallocate e.g. the trans */
147 paging_request_remove(pr);
148 pr = NULL;
149
150 if (paging_cb)
151 paging_cb(msc_a, trans);
152 }
153
154 /* Inform parts of the system we don't know */
155 sig_data = (struct paging_signal_data){
156 .vsub = vsub,
157 .msc_a = msc_a,
158 };
159 osmo_signal_dispatch(SS_PAGING, msc_a ? S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, &sig_data);
160
161 /* balanced with the moment we start paging */
162 if (vsub->cs.is_paging) {
163 vsub->cs.is_paging = false;
164 vlr_subscr_put(vsub, VSUB_USE_PAGING);
165 }
166
167 /* Handling of the paging requests has usually added transactions, which keep the msc_a connection active. If
168 * there are none, then this probably marks release of the connection. */
169 if (msc_a)
170 msc_a_put(msc_a, MSC_A_USE_PAGING_RESPONSE);
171}
172
173void paging_response(struct msc_a *msc_a)
174{
175 paging_concludes(msc_a_vsub(msc_a), msc_a);
176}
177
178void paging_expired(struct vlr_subscr *vsub)
179{
180 paging_concludes(vsub, NULL);
181}