blob: 73361a169acd82813a475db05f6c15583301e6f9 [file] [log] [blame]
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +02001/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
Harald Welte52b1f982008-12-23 20:25:15 +00002
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02004 * (C) 2009,2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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 Welte52b1f982008-12-23 20:25:15 +000011 * (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 Welte52b1f982008-12-23 20:25:15 +000017 *
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 Welte52b1f982008-12-23 20:25:15 +000020 *
21 */
22
Harald Welte75a983f2008-12-27 21:34:06 +000023#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020027#include <assert.h>
Jan Luebbebfbdeec2012-12-27 00:27:16 +010028#include <time.h>
Maxe6052c42016-06-30 10:25:49 +020029#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000030
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/talloc.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010032
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +010033#include <osmocom/vty/vty.h>
34
Harald Welte8470bf22008-12-25 23:28:35 +000035#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010036#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000037#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010038#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020039#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000040#include <openbsc/db.h>
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +020041#include <openbsc/chan_alloc.h>
Harald Welte7b423ed2016-06-19 18:06:02 +020042#include <openbsc/vlr.h>
Neels Hofmeyra1756f32016-05-20 21:59:55 +020043#include <openbsc/iu.h>
44#include <openbsc/osmo_msc.h>
45#include <openbsc/msc_ifaces.h>
Philipp Maier4b60d072017-04-09 12:32:51 +020046#include <openbsc/a_iface.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte7b423ed2016-06-19 18:06:02 +020048int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
49 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010050{
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +020051 struct subscr_request *request, *tmp;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010052 struct gsm_subscriber_connection *conn = data;
Harald Welte7b423ed2016-06-19 18:06:02 +020053 struct vlr_subscr *vsub = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010054 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010055
Neels Hofmeyra1756f32016-05-20 21:59:55 +020056 OSMO_ASSERT(vsub);
57 OSMO_ASSERT(hooknum == GSM_HOOK_RR_PAGING);
58 OSMO_ASSERT(!(conn && (conn->vsub != vsub)));
59 OSMO_ASSERT(!((event == GSM_PAGING_SUCCEEDED) && !conn));
Sylvain Munaut1e245502010-12-01 21:07:29 +010060
Neels Hofmeyra1756f32016-05-20 21:59:55 +020061 LOGP(DPAG, LOGL_DEBUG, "Paging %s for %s (event=%d)\n",
62 event == GSM_PAGING_SUCCEEDED ? "success" : "failure",
63 vlr_subscr_name(vsub), event);
64
65 if (!vsub->cs.is_paging) {
66 LOGP(DPAG, LOGL_ERROR,
67 "Paging Response received for subscriber"
68 " that is not paging.\n");
69 return -EINVAL;
70 }
71
72 if (event == GSM_PAGING_SUCCEEDED)
73 msc_stop_paging(vsub);
Holger Hans Peter Freyther778c7d12015-08-03 11:18:23 +020074
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +020075 /* Inform parts of the system we don't know */
Neels Hofmeyra1756f32016-05-20 21:59:55 +020076 sig_data.vsub = vsub;
77 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +010078 sig_data.paging_result = event;
Neels Hofmeyra1756f32016-05-20 21:59:55 +020079 osmo_signal_dispatch(SS_PAGING,
80 event == GSM_PAGING_SUCCEEDED ?
81 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
82 &sig_data);
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010083
Harald Welte7b423ed2016-06-19 18:06:02 +020084 llist_for_each_entry_safe(request, tmp, &vsub->cs.requests, entry) {
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +020085 llist_del(&request->entry);
Neels Hofmeyra1756f32016-05-20 21:59:55 +020086 if (request->cbfn) {
87 LOGP(DPAG, LOGL_DEBUG, "Calling paging cbfn.\n");
88 request->cbfn(hooknum, event, msg, data, request->param);
89 } else
90 LOGP(DPAG, LOGL_DEBUG, "Paging without action.\n");
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +020091 talloc_free(request);
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +010092 }
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +010093
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +020094 /* balanced with the moment we start paging */
Harald Welte7b423ed2016-06-19 18:06:02 +020095 vsub->cs.is_paging = false;
96 vlr_subscr_put(vsub);
Sylvain Munaut1e245502010-12-01 21:07:29 +010097 return 0;
98}
99
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200100int msc_paging_request(struct vlr_subscr *vsub)
101{
102 /* The subscriber was last seen in subscr->lac. Find out which
103 * BSCs/RNCs are responsible and send them a paging request via open
104 * SCCP connections (if any). */
105 /* TODO Implementing only RNC paging, since this is code on the iu branch.
106 * Need to add BSC paging at some point. */
107 switch (vsub->cs.attached_via_ran) {
108 case RAN_GERAN_A:
Philipp Maier4b60d072017-04-09 12:32:51 +0200109 return a_iface_tx_paging(vsub->imsi, vsub->tmsi, vsub->lac);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200110 case RAN_UTRAN_IU:
111 return iu_page_cs(vsub->imsi,
112 vsub->tmsi == GSM_RESERVED_TMSI?
113 NULL : &vsub->tmsi,
114 vsub->lac);
115 default:
116 break;
117 }
118
119 LOGP(DPAG, LOGL_ERROR, "%s: Cannot page, subscriber not attached\n",
120 vlr_subscr_name(vsub));
121 return -EINVAL;
122}
123
124/*! \brief Start a paging request for vsub, call cbfn(param) when done.
125 * \param vsub subscriber to page.
126 * \param cbfn function to call when the conn is established.
127 * \param param caller defined param to pass to cbfn().
128 * \param label human readable label of the request kind used for logging.
129 */
130struct subscr_request *subscr_request_conn(struct vlr_subscr *vsub,
131 gsm_cbfn *cbfn, void *param,
132 const char *label)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100133{
Sylvain Munaut1e245502010-12-01 21:07:29 +0100134 int rc;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100135 struct subscr_request *request;
136
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200137 /* Start paging.. we know it is async so we can do it before */
Harald Welte7b423ed2016-06-19 18:06:02 +0200138 if (!vsub->cs.is_paging) {
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200139 LOGP(DMM, LOGL_DEBUG, "Subscriber %s not paged yet, start paging.\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200140 vlr_subscr_name(vsub));
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200141 rc = msc_paging_request(vsub);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200142 if (rc <= 0) {
143 LOGP(DMM, LOGL_ERROR, "Subscriber %s paging failed: %d\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200144 vlr_subscr_name(vsub), rc);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200145 return NULL;
146 }
147 /* reduced on the first paging callback */
Harald Welte7b423ed2016-06-19 18:06:02 +0200148 vlr_subscr_get(vsub);
149 vsub->cs.is_paging = true;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200150 } else {
151 LOGP(DMM, LOGL_DEBUG, "Subscriber %s already paged.\n",
152 vlr_subscr_name(vsub));
Sylvain Munaut1e245502010-12-01 21:07:29 +0100153 }
154
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200155 /* TODO: Stop paging in case of memory allocation failure */
Harald Welte7b423ed2016-06-19 18:06:02 +0200156 request = talloc_zero(vsub, struct subscr_request);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200157 if (!request)
158 return NULL;
159
Sylvain Munaut1e245502010-12-01 21:07:29 +0100160 request->cbfn = cbfn;
161 request->param = param;
Harald Welte7b423ed2016-06-19 18:06:02 +0200162 llist_add_tail(&request->entry, &vsub->cs.requests);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200163 return request;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100164}
165
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200166void subscr_remove_request(struct subscr_request *request)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100167{
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +0200168 llist_del(&request->entry);
169 talloc_free(request);
Sylvain Munaut1e245502010-12-01 21:07:29 +0100170}
171
Harald Welte7b423ed2016-06-19 18:06:02 +0200172struct gsm_subscriber_connection *connection_for_subscr(struct vlr_subscr *vsub)
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200173{
Harald Welte7b423ed2016-06-19 18:06:02 +0200174 struct gsm_network *net = vsub->vlr->user_ctx;
Neels Hofmeyr1a606442016-05-10 13:56:43 +0200175 struct gsm_subscriber_connection *conn;
176
177 llist_for_each_entry(conn, &net->subscr_conns, entry) {
Harald Welte7b423ed2016-06-19 18:06:02 +0200178 if (conn->vsub == vsub)
Neels Hofmeyr1a606442016-05-10 13:56:43 +0200179 return conn;
180 }
181
182 return NULL;
183}