blob: 5baa0dca0b279fcfeecd15ff3e01359a4f0e46aa [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:
72 break;
73 }
74
75 LOG_PAGING(vsub, pr, LOGL_ERROR, " Cannot page, subscriber not attached\n");
76 return -EINVAL;
77}
78
79struct paging_request *paging_request_start(struct vlr_subscr *vsub, enum paging_cause cause,
80 paging_cb_t paging_cb, struct gsm_trans *trans,
81 const char *label)
82{
83 int rc;
84 struct paging_request *pr;
85 struct gsm_network *net = vsub->vlr->user_ctx;
86
87 pr = talloc_zero(vsub, struct paging_request);
88 OSMO_ASSERT(pr);
89 *pr = (struct paging_request){
90 .label = label,
91 .cause = cause,
92 .paging_cb = paging_cb,
93 .trans = trans,
94 };
95
96 if (vsub->cs.is_paging) {
97 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Already paging, not starting another request\n");
98 } else {
99 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Starting paging\n");
100
101 rc = msc_paging_request(pr, vsub);
102 if (rc <= 0) {
103 LOG_PAGING(vsub, pr, LOGL_ERROR, "Starting paging failed (rc=%d)\n", rc);
104 talloc_free(pr);
105 return NULL;
106 }
107
108 /* reduced on the first paging callback */
109 vlr_subscr_get(vsub, VSUB_USE_PAGING);
110 vsub->cs.is_paging = true;
111 osmo_timer_setup(&vsub->cs.paging_response_timer, paging_response_timer_cb, vsub);
112 osmo_timer_schedule(&vsub->cs.paging_response_timer, net->paging_response_timer, 0);
113 }
114
115 llist_add_tail(&pr->entry, &vsub->cs.requests);
116
117 return pr;
118}
119
120void paging_request_remove(struct paging_request *pr)
121{
122 struct gsm_trans *trans = pr->trans;
123 struct vlr_subscr *vsub = trans ? trans->vsub : NULL;
124 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Removing Paging Request\n");
125
126 if (pr->trans && pr->trans->paging_request == pr)
127 pr->trans->paging_request = NULL;
128
129 llist_del(&pr->entry);
130 talloc_free(pr);
131}
132
133static void paging_concludes(struct vlr_subscr *vsub, struct msc_a *msc_a)
134{
135 struct paging_request *pr, *pr_next;
136 struct paging_signal_data sig_data;
137
138 osmo_timer_del(&vsub->cs.paging_response_timer);
139
140 llist_for_each_entry_safe(pr, pr_next, &vsub->cs.requests, entry) {
141 struct gsm_trans *trans = pr->trans;
142 paging_cb_t paging_cb = pr->paging_cb;
143
144 LOG_PAGING(vsub, pr, LOGL_DEBUG, "Paging Response action (%s)%s\n",
145 msc_a ? "success" : "expired",
146 paging_cb ? "" : " (no action defined)");
147
148 /* Remove the paging request before the paging_cb could deallocate e.g. the trans */
149 paging_request_remove(pr);
150 pr = NULL;
151
152 if (paging_cb)
153 paging_cb(msc_a, trans);
154 }
155
156 /* Inform parts of the system we don't know */
157 sig_data = (struct paging_signal_data){
158 .vsub = vsub,
159 .msc_a = msc_a,
160 };
161 osmo_signal_dispatch(SS_PAGING, msc_a ? S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, &sig_data);
162
163 /* balanced with the moment we start paging */
164 if (vsub->cs.is_paging) {
165 vsub->cs.is_paging = false;
166 vlr_subscr_put(vsub, VSUB_USE_PAGING);
167 }
168
169 /* Handling of the paging requests has usually added transactions, which keep the msc_a connection active. If
170 * there are none, then this probably marks release of the connection. */
171 if (msc_a)
172 msc_a_put(msc_a, MSC_A_USE_PAGING_RESPONSE);
173}
174
175void paging_response(struct msc_a *msc_a)
176{
177 paging_concludes(msc_a_vsub(msc_a), msc_a);
178}
179
180void paging_expired(struct vlr_subscr *vsub)
181{
182 paging_concludes(vsub, NULL);
183}