blob: c444fe058198daa02c583e68801177fe8d243975 [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>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010031
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +010032#include <osmocom/vty/vty.h>
33
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010035#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000036#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010037#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020038#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000039#include <openbsc/db.h>
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +020040#include <openbsc/chan_alloc.h>
Harald Welte52b1f982008-12-23 20:25:15 +000041
Sylvain Munaut1e245502010-12-01 21:07:29 +010042void *tall_sub_req_ctx;
43
Jan Luebbe6e300682012-12-25 18:58:34 +010044extern struct llist_head *subscr_bsc_active_subscribers(void);
Harald Welte75a983f2008-12-27 21:34:06 +000045
Sylvain Munaut5a86e062010-12-01 22:38:03 +010046int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
47 gsm_cbfn *cb, void *cb_data);
48
Sylvain Munaut1e245502010-12-01 21:07:29 +010049
50/*
51 * Struct for pending channel requests. This is managed in the
52 * llist_head requests of each subscriber. The reference counting
53 * should work in such a way that a subscriber with a pending request
54 * remains in memory.
55 */
56struct subscr_request {
57 struct llist_head entry;
58
59 /* back reference */
60 struct gsm_subscriber *subscr;
61
62 /* the requested channel type */
63 int channel_type;
64
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010065 /* what did we do */
66 int state;
67
Sylvain Munaut1e245502010-12-01 21:07:29 +010068 /* the callback data */
69 gsm_cbfn *cbfn;
70 void *param;
71};
72
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010073enum {
74 REQ_STATE_INITIAL,
75 REQ_STATE_QUEUED,
76 REQ_STATE_PAGED,
77 REQ_STATE_FAILED_START,
78 REQ_STATE_DISPATCHED,
79};
80
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010081static struct gsm_subscriber *get_subscriber(struct gsm_subscriber_group *sgrp,
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +020082 int type, const char *ident)
83{
84 struct gsm_subscriber *subscr = db_get_subscriber(type, ident);
85 if (subscr)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010086 subscr->group = sgrp;
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +020087 return subscr;
88}
89
Sylvain Munaut1e245502010-12-01 21:07:29 +010090/*
91 * We got the channel assigned and can now hand this channel
92 * over to one of our callbacks.
93 */
Sylvain Munaut5a86e062010-12-01 22:38:03 +010094static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
95 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010096{
97 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010098 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010099 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +0100100 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100101
102 /* There is no request anymore... */
103 if (llist_empty(&subscr->requests))
104 return -1;
105
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +0100106 /* Dispatch signal */
107 sig_data.subscr = subscr;
108 sig_data.bts = conn ? conn->bts : NULL;
109 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100110 sig_data.paging_result = event;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200111 osmo_signal_dispatch(
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +0100112 SS_PAGING,
113 event == GSM_PAGING_SUCCEEDED ?
114 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
115 &sig_data
116 );
117
Sylvain Munaut1e245502010-12-01 21:07:29 +0100118 /*
119 * FIXME: What to do with paging requests coming during
120 * this callback? We must be sure to not start paging when
121 * we have an active connection to a subscriber and to make
122 * the subscr_put_channel work as required...
123 */
124 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100125 request->state = REQ_STATE_DISPATCHED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100126 llist_del(&request->entry);
127 subscr->in_callback = 1;
128 request->cbfn(hooknum, event, msg, data, request->param);
129 subscr->in_callback = 0;
130
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100131 if (event != GSM_PAGING_SUCCEEDED) {
132 /*
133 * This is a workaround for a bigger issue. We have
134 * issued paging that might involve multiple BTSes
135 * and one of them have failed now. We will stop the
136 * other paging requests as well as the next timeout
137 * would work on the next paging request and the queue
138 * will do bad things. This should be fixed by counting
139 * the outstanding results.
140 */
141 paging_request_stop(NULL, subscr, NULL, NULL);
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100142 subscr_put_channel(subscr);
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100143 }
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100144
Sylvain Munaut1e245502010-12-01 21:07:29 +0100145 subscr_put(subscr);
146 talloc_free(request);
147 return 0;
148}
149
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100150static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
151 struct msgb *msg, void *data, void *param)
152{
153 int rc;
154
155 switch (event) {
156 case GSM_SECURITY_AUTH_FAILED:
157 /* Dispatch as paging failure */
158 rc = subscr_paging_dispatch(
159 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
160 msg, data, param);
161 break;
162
163 case GSM_SECURITY_NOAVAIL:
164 case GSM_SECURITY_SUCCEEDED:
165 /* Dispatch as paging failure */
166 rc = subscr_paging_dispatch(
167 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
168 msg, data, param);
169 break;
170
171 default:
172 rc = -EINVAL;
173 }
174
175 return rc;
176}
177
178static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
179 struct msgb *msg, void *data, void *param)
180{
181 struct gsm_subscriber_connection *conn = data;
182 struct gsm48_hdr *gh;
183 struct gsm48_pag_resp *pr;
184
185 /* Other cases mean problem, dispatch direclty */
186 if (event != GSM_PAGING_SUCCEEDED)
187 return subscr_paging_dispatch(hooknum, event, msg, data, param);
188
189 /* Get paging response */
190 gh = msgb_l3(msg);
191 pr = (struct gsm48_pag_resp *)gh->data;
192
193 /* We _really_ have a channel, secure it now ! */
194 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
195}
196
197
Sylvain Munaut1e245502010-12-01 21:07:29 +0100198static void subscr_send_paging_request(struct gsm_subscriber *subscr)
199{
200 struct subscr_request *request;
201 int rc;
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100202 struct gsm_network *net = subscr->group->net;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100203
204 assert(!llist_empty(&subscr->requests));
205
206 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100207 request->state = REQ_STATE_PAGED;
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100208 rc = paging_request(net, subscr, request->channel_type,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100209 subscr_paging_cb, subscr);
210
211 /* paging failed, quit now */
212 if (rc <= 0) {
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100213 request->state = REQ_STATE_FAILED_START;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100214 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100215 NULL, NULL, subscr);
216 }
217}
218
219void subscr_get_channel(struct gsm_subscriber *subscr,
220 int type, gsm_cbfn *cbfn, void *param)
221{
222 struct subscr_request *request;
223
224 request = talloc(tall_sub_req_ctx, struct subscr_request);
225 if (!request) {
226 if (cbfn)
227 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
228 NULL, NULL, param);
229 return;
230 }
231
232 memset(request, 0, sizeof(*request));
233 request->subscr = subscr_get(subscr);
234 request->channel_type = type;
235 request->cbfn = cbfn;
236 request->param = param;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100237 request->state = REQ_STATE_INITIAL;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100238
239 /*
240 * FIXME: We might be able to assign more than one
241 * channel, e.g. voice and SMS submit at the same
242 * time.
243 */
244 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
245 /* add to the list, send a request */
246 llist_add_tail(&request->entry, &subscr->requests);
247 subscr_send_paging_request(subscr);
248 } else {
249 /* this will be picked up later, from subscr_put_channel */
250 llist_add_tail(&request->entry, &subscr->requests);
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100251 request->state = REQ_STATE_QUEUED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100252 }
253}
254
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100255void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100256{
257 /*
258 * FIXME: Continue with other requests now... by checking
259 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
260 * of the lchan after having asked the next requestee to handle
261 * the channel.
262 */
263 /*
264 * FIXME: is the lchan is of a different type we could still
265 * issue an immediate assignment for another channel and then
266 * close this one.
267 */
268 /*
269 * Currently we will drop the last ref of the lchan which
270 * will result in a channel release on RSL and we will start
271 * the paging. This should work most of the time as the MS
272 * will listen to the paging requests before we timeout
273 */
274
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100275 if (subscr && !llist_empty(&subscr->requests))
276 subscr_send_paging_request(subscr);
Sylvain Munaut1e245502010-12-01 21:07:29 +0100277}
278
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100279struct gsm_subscriber *subscr_create_subscriber(struct gsm_subscriber_group *sgrp,
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200280 const char *imsi)
281{
282 struct gsm_subscriber *subscr = db_create_subscriber(imsi);
283 if (subscr)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100284 subscr->group = sgrp;
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200285 return subscr;
286}
Sylvain Munaut1e245502010-12-01 21:07:29 +0100287
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100288struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_subscriber_group *sgrp,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200289 uint32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000290{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200291 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000292 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000293
Holger Freyther12aa50d2009-01-01 18:02:05 +0000294 /* we might have a record in memory already */
Jan Luebbe6e300682012-12-25 18:58:34 +0100295 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200296 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000297 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000298 }
299
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200300 sprintf(tmsi_string, "%u", tmsi);
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100301 return get_subscriber(sgrp, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000302}
303
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100304struct gsm_subscriber *subscr_get_by_imsi(struct gsm_subscriber_group *sgrp,
Harald Welte9176bd42009-07-23 18:46:00 +0200305 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000306{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000307 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000308
Jan Luebbe6e300682012-12-25 18:58:34 +0100309 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000310 if (strcmp(subscr->imsi, imsi) == 0)
311 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000312 }
313
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100314 return get_subscriber(sgrp, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000315}
316
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100317struct gsm_subscriber *subscr_get_by_extension(struct gsm_subscriber_group *sgrp,
Harald Welte9176bd42009-07-23 18:46:00 +0200318 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000319{
320 struct gsm_subscriber *subscr;
321
Jan Luebbe6e300682012-12-25 18:58:34 +0100322 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000323 if (strcmp(subscr->extension, ext) == 0)
324 return subscr_get(subscr);
325 }
326
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100327 return get_subscriber(sgrp, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000328}
329
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100330struct gsm_subscriber *subscr_get_by_id(struct gsm_subscriber_group *sgrp,
Harald Welte76042182009-08-08 16:03:15 +0200331 unsigned long long id)
332{
333 struct gsm_subscriber *subscr;
334 char buf[32];
335 sprintf(buf, "%llu", id);
336
Jan Luebbe6e300682012-12-25 18:58:34 +0100337 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200338 if (subscr->id == id)
339 return subscr_get(subscr);
340 }
341
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100342 return get_subscriber(sgrp, GSM_SUBSCRIBER_ID, buf);
Harald Welte76042182009-08-08 16:03:15 +0200343}
344
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200345int subscr_update_expire_lu(struct gsm_subscriber *s, struct gsm_bts *bts)
346{
347 int rc;
348
349 /* Table 10.5.33: The T3212 timeout value field is coded as the
350 * binary representation of the timeout value for
351 * periodic updating in decihours. Mark the subscriber as
352 * inactive if it missed two consecutive location updates.
353 * Timeout is twice the t3212 value plus one minute */
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200354
355 /* Is expiration handling enabled? */
356 if (bts->si_common.chan_desc.t3212 == 0)
357 s->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION;
358 else
359 s->expire_lu = time(NULL) +
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200360 (bts->si_common.chan_desc.t3212 * 60 * 6 * 2) + 60;
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200361
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200362 rc = db_sync_subscriber(s);
363 db_subscriber_update(s);
364 return rc;
365}
Harald Welte76042182009-08-08 16:03:15 +0200366
Holger Freyther4a49e772009-04-12 05:37:29 +0000367int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000368{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100369 int rc;
370
Holger Freyther4a49e772009-04-12 05:37:29 +0000371 /* FIXME: Migrate pending requests from one BSC to another */
372 switch (reason) {
373 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100374 s->group = bts->network->subscr_group;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000375 /* Indicate "attached to LAC" */
376 s->lac = bts->location_area_code;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100377
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200378 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
379 subscr_name(s), s->lac);
380
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200381 /*
382 * The below will set a new expire_lu but as a side-effect
383 * the new lac will be saved in the database.
384 */
385 rc = subscr_update_expire_lu(s, bts);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200386 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000387 break;
388 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000389 /* Only detach if we are currently in this area */
390 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200391 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100392 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100393 rc = db_sync_subscriber(s);
394 db_subscriber_update(s);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200395 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000396 break;
397 default:
398 fprintf(stderr, "subscr_update with unknown reason: %d\n",
399 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100400 rc = db_sync_subscriber(s);
401 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000402 break;
403 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100404
405 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000406}
407
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100408void subscr_update_from_db(struct gsm_subscriber *sub)
409{
410 db_subscriber_update(sub);
411}
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100412
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100413static void subscr_expire_callback(void *data, long long unsigned int id)
414{
415 struct gsm_network *net = data;
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100416 struct gsm_subscriber *s = subscr_get_by_id(net->subscr_group, id);
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200417 struct gsm_subscriber_connection *conn = connection_for_subscr(s);
418
419 /*
420 * The subscriber is active and the phone stopped the timer. As
421 * we don't want to periodically update the database for active
422 * subscribers we will just do it when the subscriber was selected
423 * for expiration. This way on the next around another subscriber
424 * will be selected.
425 */
426 if (conn && conn->expire_timer_stopped) {
427 LOGP(DMM, LOGL_DEBUG, "Not expiring subscriber %s (ID %llu)\n",
428 subscr_name(s), id);
429 subscr_update_expire_lu(s, conn->bts);
430 return;
431 }
432
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100433
Holger Hans Peter Freyther81cff912013-07-04 20:22:27 +0200434 LOGP(DMM, LOGL_NOTICE, "Expiring inactive subscriber %s (ID %llu)\n",
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100435 subscr_name(s), id);
436 s->lac = GSM_LAC_RESERVED_DETACHED;
437 db_sync_subscriber(s);
438
439 subscr_put(s);
440}
441
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100442void subscr_expire(struct gsm_subscriber_group *sgrp)
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100443{
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100444 db_subscriber_expire(sgrp->net, subscr_expire_callback);
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100445}
446
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100447int subscr_pending_requests(struct gsm_subscriber *sub)
448{
449 struct subscr_request *req;
450 int pending = 0;
451
452 llist_for_each_entry(req, &sub->requests, entry)
453 pending += 1;
454
455 return pending;
456}
Holger Hans Peter Freytherfc857412010-12-28 22:21:55 +0100457
458int subscr_pending_clear(struct gsm_subscriber *sub)
459{
460 int deleted = 0;
461 struct subscr_request *req, *tmp;
462
463 llist_for_each_entry_safe(req, tmp, &sub->requests, entry) {
464 subscr_put(req->subscr);
465 llist_del(&req->entry);
466 talloc_free(req);
467 deleted += 1;
468 }
469
470 return deleted;
471}
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100472
473int subscr_pending_dump(struct gsm_subscriber *sub, struct vty *vty)
474{
475 struct subscr_request *req;
476
477 vty_out(vty, "Pending Requests for Subscriber %llu.%s", sub->id, VTY_NEWLINE);
478 llist_for_each_entry(req, &sub->requests, entry) {
479 vty_out(vty, "Channel type: %d State: %d Sub: %llu.%s",
480 req->channel_type, req->state, req->subscr->id, VTY_NEWLINE);
481 }
482
483 return 0;
484}
Holger Hans Peter Freyther451eb292010-12-29 13:45:49 +0100485
486int subscr_pending_kick(struct gsm_subscriber *sub)
487{
488 subscr_put_channel(sub);
489 return 0;
490}