Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (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 |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 28 | #include <assert.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 29 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 30 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 31 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 32 | #include <openbsc/paging.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 34 | #include <openbsc/paging.h> |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 35 | #include <openbsc/signal.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/db.h> |
| 37 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 38 | LLIST_HEAD(active_subscribers); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 39 | static void *tall_subscr_ctx; |
| 40 | static void *tall_sub_req_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Struct for pending channel requests. This is managed in the |
| 44 | * llist_head requests of each subscriber. The reference counting |
| 45 | * should work in such a way that a subscriber with a pending request |
| 46 | * remains in memory. |
| 47 | */ |
| 48 | struct subscr_request { |
| 49 | struct llist_head entry; |
| 50 | |
| 51 | /* back reference */ |
| 52 | struct gsm_subscriber *subscr; |
| 53 | |
| 54 | /* the requested channel type */ |
| 55 | int channel_type; |
| 56 | |
| 57 | /* the bts we have decided to use */ |
| 58 | struct gsm_network *network; |
| 59 | |
| 60 | /* the callback data */ |
| 61 | gsm_cbfn *cbfn; |
| 62 | void *param; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * We got the channel assigned and can now hand this channel |
| 67 | * over to one of our callbacks. |
| 68 | */ |
| 69 | static int subscr_paging_cb(unsigned int hooknum, unsigned int event, |
| 70 | struct msgb *msg, void *data, void *param) |
| 71 | { |
| 72 | struct subscr_request *request; |
| 73 | struct gsm_subscriber *subscr = (struct gsm_subscriber *)param; |
| 74 | |
| 75 | assert(!llist_empty(&subscr->requests)); |
| 76 | |
| 77 | /* |
| 78 | * FIXME: What to do with paging requests coming during |
| 79 | * this callback? We must be sure to not start paging when |
| 80 | * we have an active connection to a subscriber and to make |
| 81 | * the subscr_put_channel work as required... |
| 82 | */ |
| 83 | request = (struct subscr_request *)subscr->requests.next; |
| 84 | llist_del(&request->entry); |
| 85 | subscr->in_callback = 1; |
| 86 | request->cbfn(hooknum, event, msg, data, request->param); |
| 87 | subscr->in_callback = 0; |
| 88 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 89 | talloc_free(request); |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 94 | { |
| 95 | struct subscr_request *request; |
| 96 | assert(!llist_empty(&subscr->requests)); |
| 97 | |
| 98 | request = (struct subscr_request *)subscr->requests.next; |
| 99 | paging_request(request->network, subscr, request->channel_type, |
| 100 | subscr_paging_cb, subscr); |
| 101 | } |
| 102 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 103 | struct gsm_subscriber *subscr_alloc(void) |
| 104 | { |
| 105 | struct gsm_subscriber *s; |
| 106 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 107 | s = talloc(tall_subscr_ctx, struct gsm_subscriber); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 108 | if (!s) |
| 109 | return NULL; |
| 110 | |
| 111 | memset(s, 0, sizeof(*s)); |
| 112 | llist_add_tail(&s->entry, &active_subscribers); |
| 113 | s->use_count = 1; |
| 114 | |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 115 | INIT_LLIST_HEAD(&s->requests); |
| 116 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 117 | return s; |
| 118 | } |
| 119 | |
| 120 | static void subscr_free(struct gsm_subscriber *subscr) |
| 121 | { |
| 122 | llist_del(&subscr->entry); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 123 | talloc_free(subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 126 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net, |
| 127 | const char *tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 128 | { |
| 129 | struct gsm_subscriber *subscr; |
| 130 | |
| 131 | /* we might have a record in memory already */ |
| 132 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 133 | if (strcmp(subscr->tmsi, tmsi) == 0) |
| 134 | return subscr_get(subscr); |
| 135 | } |
| 136 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 137 | return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 138 | } |
| 139 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 140 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net, |
| 141 | const char *imsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 142 | { |
| 143 | struct gsm_subscriber *subscr; |
| 144 | |
| 145 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 146 | if (strcmp(subscr->imsi, imsi) == 0) |
| 147 | return subscr_get(subscr); |
| 148 | } |
| 149 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 150 | return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 153 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net, |
| 154 | const char *ext) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 155 | { |
| 156 | struct gsm_subscriber *subscr; |
| 157 | |
| 158 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 159 | if (strcmp(subscr->extension, ext) == 0) |
| 160 | return subscr_get(subscr); |
| 161 | } |
| 162 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 163 | return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 166 | struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net, |
| 167 | unsigned long long id) |
| 168 | { |
| 169 | struct gsm_subscriber *subscr; |
| 170 | char buf[32]; |
| 171 | sprintf(buf, "%llu", id); |
| 172 | |
| 173 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 174 | if (subscr->id == id) |
| 175 | return subscr_get(subscr); |
| 176 | } |
| 177 | |
| 178 | return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf); |
| 179 | } |
| 180 | |
| 181 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 182 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
| 183 | { |
| 184 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 185 | switch (reason) { |
| 186 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | c2189a6 | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 187 | s->net = bts->network; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 188 | /* Indicate "attached to LAC" */ |
| 189 | s->lac = bts->location_area_code; |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 190 | dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 191 | break; |
| 192 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
| 193 | /* Only detach if we are currently in this area */ |
| 194 | if (bts->location_area_code == s->lac) |
| 195 | s->lac = 0; |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 196 | dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 197 | break; |
| 198 | default: |
| 199 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 200 | reason); |
| 201 | break; |
| 202 | }; |
| 203 | return db_sync_subscriber(s); |
| 204 | } |
| 205 | |
| 206 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 207 | { |
| 208 | subscr->use_count++; |
| 209 | DEBUGP(DCC, "subscr %s usage increases usage to: %d\n", |
| 210 | subscr->extension, subscr->use_count); |
| 211 | return subscr; |
| 212 | } |
| 213 | |
| 214 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 215 | { |
| 216 | subscr->use_count--; |
| 217 | DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n", |
| 218 | subscr->extension, subscr->use_count); |
| 219 | if (subscr->use_count <= 0) |
| 220 | subscr_free(subscr); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 224 | void subscr_get_channel(struct gsm_subscriber *subscr, |
| 225 | struct gsm_network *network, int type, |
| 226 | gsm_cbfn *cbfn, void *param) |
| 227 | { |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 228 | struct subscr_request *request; |
| 229 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 230 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 231 | if (!request) { |
| 232 | if (cbfn) |
| 233 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 234 | NULL, NULL, param); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | memset(request, 0, sizeof(*request)); |
| 239 | request->network = network; |
| 240 | request->subscr = subscr; |
| 241 | request->channel_type = type; |
| 242 | request->cbfn = cbfn; |
| 243 | request->param = param; |
| 244 | |
| 245 | /* |
| 246 | * FIXME: We might be able to assign more than one |
| 247 | * channel, e.g. voice and SMS submit at the same |
| 248 | * time. |
| 249 | */ |
| 250 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 251 | /* add to the list, send a request */ |
| 252 | llist_add_tail(&request->entry, &subscr->requests); |
| 253 | subscr_send_paging_request(subscr); |
| 254 | } else { |
| 255 | /* this will be picked up later, from subscr_put_channel */ |
| 256 | llist_add_tail(&request->entry, &subscr->requests); |
| 257 | } |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 260 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 261 | { |
| 262 | /* |
| 263 | * FIXME: Continue with other requests now... by checking |
| 264 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 265 | * of the lchan after having asked the next requestee to handle |
| 266 | * the channel. |
| 267 | */ |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 268 | /* |
| 269 | * FIXME: is the lchan is of a different type we could still |
| 270 | * issue an immediate assignment for another channel and then |
| 271 | * close this one. |
| 272 | */ |
| 273 | /* |
| 274 | * Currently we will drop the last ref of the lchan which |
| 275 | * will result in a channel release on RSL and we will start |
| 276 | * the paging. This should work most of the time as the MS |
| 277 | * will listen to the paging requests before we timeout |
| 278 | */ |
| 279 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 280 | put_lchan(lchan); |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 281 | |
| 282 | if (lchan->subscr && !llist_empty(&lchan->subscr->requests)) |
| 283 | subscr_send_paging_request(lchan->subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 284 | } |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 285 | |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 286 | |
| 287 | static __attribute__((constructor)) void on_dso_load_subscr(void) |
| 288 | { |
| 289 | tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1, "subscriber"); |
| 290 | |
| 291 | tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 292 | "subscr_request"); |
| 293 | } |