blob: f5fb16c5a6cecd7368dee56ce51fa26b1926e355 [file] [log] [blame]
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02001/* The concept of a subscriber as seen by the BSC */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther108d58a2010-06-30 09:22:31 +08004 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther51808442010-10-06 20:37:09 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <assert.h>
30
Harald Weltef4625b12010-02-20 16:24:02 +010031#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020032#include <openbsc/gsm_subscriber.h>
33#include <openbsc/paging.h>
34#include <openbsc/debug.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020035
36LLIST_HEAD(active_subscribers);
37void *tall_subscr_ctx;
38void *tall_sub_req_ctx;
39
40/* for the gsm_subscriber.c */
41struct llist_head *subscr_bsc_active_subscriber(void)
42{
43 return &active_subscribers;
44}
45
46/*
47 * Struct for pending channel requests. This is managed in the
48 * llist_head requests of each subscriber. The reference counting
49 * should work in such a way that a subscriber with a pending request
50 * remains in memory.
51 */
52struct subscr_request {
53 struct llist_head entry;
54
55 /* back reference */
56 struct gsm_subscriber *subscr;
57
58 /* the requested channel type */
59 int channel_type;
60
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020061 /* the callback data */
62 gsm_cbfn *cbfn;
63 void *param;
64};
65
66/*
67 * We got the channel assigned and can now hand this channel
68 * over to one of our callbacks.
69 */
70static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
71 struct msgb *msg, void *data, void *param)
72{
73 struct subscr_request *request;
74 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
75
Holger Hans Peter Freytheraa62bb92009-08-22 11:18:13 +020076 /* There is no request anymore... */
77 if (llist_empty(&subscr->requests))
78 return -1;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020079
80 /*
81 * FIXME: What to do with paging requests coming during
82 * this callback? We must be sure to not start paging when
83 * we have an active connection to a subscriber and to make
84 * the subscr_put_channel work as required...
85 */
86 request = (struct subscr_request *)subscr->requests.next;
87 llist_del(&request->entry);
88 subscr->in_callback = 1;
89 request->cbfn(hooknum, event, msg, data, request->param);
90 subscr->in_callback = 0;
91
Holger Hans Peter Freyther108d58a2010-06-30 09:22:31 +080092 subscr_put(subscr);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020093 talloc_free(request);
94 return 0;
95}
96
97static void subscr_send_paging_request(struct gsm_subscriber *subscr)
98{
99 struct subscr_request *request;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +0200100 int rc;
101
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200102 assert(!llist_empty(&subscr->requests));
103
104 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +0200105 rc = paging_request(subscr->net, subscr, request->channel_type,
106 subscr_paging_cb, subscr);
107
108 /* paging failed, quit now */
109 if (rc <= 0) {
110 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
Holger Hans Peter Freyther2b2362f2010-06-16 15:17:59 +0800111 NULL, NULL, subscr);
Holger Hans Peter Freytheraca08d92009-08-21 02:35:28 +0200112 }
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200113}
114
Holger Hans Peter Freyther3ea77a52010-06-30 12:56:41 +0800115char *subscr_name(struct gsm_subscriber *subscr)
116{
117 if (strlen(subscr->name))
118 return subscr->name;
119
120 return subscr->imsi;
121}
122
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200123struct gsm_subscriber *subscr_alloc(void)
124{
125 struct gsm_subscriber *s;
126
Holger Hans Peter Freythere4533482009-10-29 02:29:45 +0100127 s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200128 if (!s)
129 return NULL;
130
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200131 llist_add_tail(&s->entry, &active_subscribers);
132 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200133 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200134
135 INIT_LLIST_HEAD(&s->requests);
136
137 return s;
138}
139
140static void subscr_free(struct gsm_subscriber *subscr)
141{
142 llist_del(&subscr->entry);
143 talloc_free(subscr);
144}
145
146struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
147{
148 subscr->use_count++;
Harald Welte57fde552009-12-24 11:46:44 +0100149 DEBUGP(DREF, "subscr %s usage increases usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200150 subscr->extension, subscr->use_count);
151 return subscr;
152}
153
154struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
155{
156 subscr->use_count--;
Harald Welte57fde552009-12-24 11:46:44 +0100157 DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200158 subscr->extension, subscr->use_count);
159 if (subscr->use_count <= 0)
160 subscr_free(subscr);
161 return NULL;
162}
163
164void subscr_get_channel(struct gsm_subscriber *subscr,
Holger Hans Peter Freyther35672842009-08-20 13:33:51 +0200165 int type, gsm_cbfn *cbfn, void *param)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200166{
167 struct subscr_request *request;
168
169 request = talloc(tall_sub_req_ctx, struct subscr_request);
170 if (!request) {
171 if (cbfn)
172 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
173 NULL, NULL, param);
174 return;
175 }
176
177 memset(request, 0, sizeof(*request));
Holger Hans Peter Freyther108d58a2010-06-30 09:22:31 +0800178 request->subscr = subscr_get(subscr);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200179 request->channel_type = type;
180 request->cbfn = cbfn;
181 request->param = param;
182
183 /*
184 * FIXME: We might be able to assign more than one
185 * channel, e.g. voice and SMS submit at the same
186 * time.
187 */
188 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
189 /* add to the list, send a request */
190 llist_add_tail(&request->entry, &subscr->requests);
191 subscr_send_paging_request(subscr);
192 } else {
193 /* this will be picked up later, from subscr_put_channel */
194 llist_add_tail(&request->entry, &subscr->requests);
195 }
196}
197
Holger Hans Peter Freyther5e58a0e2010-06-16 14:02:41 +0800198void subscr_put_channel(struct gsm_subscriber_connection *conn)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200199{
200 /*
201 * FIXME: Continue with other requests now... by checking
202 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
203 * of the lchan after having asked the next requestee to handle
204 * the channel.
205 */
206 /*
207 * FIXME: is the lchan is of a different type we could still
208 * issue an immediate assignment for another channel and then
209 * close this one.
210 */
211 /*
212 * Currently we will drop the last ref of the lchan which
213 * will result in a channel release on RSL and we will start
214 * the paging. This should work most of the time as the MS
215 * will listen to the paging requests before we timeout
216 */
217
Holger Hans Peter Freyther5e58a0e2010-06-16 14:02:41 +0800218 if (conn->subscr && !llist_empty(&conn->subscr->requests))
219 subscr_send_paging_request(conn->subscr);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200220}
221
Holger Hans Peter Freytherdcec9aa2010-11-15 09:15:01 +0100222struct gsm_subscriber *subscr_get_or_create(struct gsm_network *net,
223 const char *imsi)
224{
225 struct gsm_subscriber *subscr;
226
227 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
228 if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net)
229 return subscr_get(subscr);
230 }
231
232 subscr = subscr_alloc();
233 if (!subscr)
234 return NULL;
235
236 strcpy(subscr->imsi, imsi);
237 subscr->net = net;
238 return subscr;
239}