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