Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | /* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 5 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 28 | #include <assert.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 29 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 30 | #include <openbsc/talloc.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 31 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | 04866d4 | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 32 | #include <openbsc/paging.h> |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 34 | #include <openbsc/paging.h> |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 35 | #include <openbsc/db.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 36 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 37 | LLIST_HEAD(active_subscribers); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 38 | static void *tall_subscr_ctx; |
| 39 | static void *tall_sub_req_ctx; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 40 | |
Holger Freyther | 85a7b36 | 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 | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 88 | talloc_free(request); |
Holger Freyther | 85a7b36 | 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 | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 102 | struct gsm_subscriber *subscr_alloc(void) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 103 | { |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 104 | struct gsm_subscriber *s; |
| 105 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 106 | if (!tall_subscr_ctx) |
| 107 | tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 108 | "subscriber"); |
| 109 | |
| 110 | s = talloc(tall_subscr_ctx, struct gsm_subscriber); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 111 | if (!s) |
| 112 | return NULL; |
| 113 | |
| 114 | memset(s, 0, sizeof(*s)); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 115 | llist_add_tail(&s->entry, &active_subscribers); |
| 116 | s->use_count = 1; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 117 | |
Holger Freyther | 04866d4 | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 118 | INIT_LLIST_HEAD(&s->requests); |
| 119 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 120 | return s; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 121 | } |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 122 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 123 | static void subscr_free(struct gsm_subscriber *subscr) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 124 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 125 | llist_del(&subscr->entry); |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 126 | talloc_free(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Holger Freyther | a471a41 | 2009-01-04 03:47:05 +0000 | [diff] [blame] | 129 | struct gsm_subscriber *subscr_get_by_tmsi(const char *tmsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 130 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 131 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 132 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 133 | /* we might have a record in memory already */ |
| 134 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 135 | if (strcmp(subscr->tmsi, tmsi) == 0) |
| 136 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 139 | return db_get_subscriber(GSM_SUBSCRIBER_TMSI, tmsi); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Holger Freyther | a471a41 | 2009-01-04 03:47:05 +0000 | [diff] [blame] | 142 | struct gsm_subscriber *subscr_get_by_imsi(const char *imsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 143 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 144 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 145 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 146 | llist_for_each_entry(subscr, &active_subscribers, entry) { |
| 147 | if (strcmp(subscr->imsi, imsi) == 0) |
| 148 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 151 | return db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 154 | struct gsm_subscriber *subscr_get_by_extension(const char *ext) |
| 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 | |
| 163 | return db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, ext); |
| 164 | } |
| 165 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 166 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 167 | { |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 168 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 169 | switch (reason) { |
| 170 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | aa0b29c | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 171 | s->net = bts->network; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 172 | /* Indicate "attached to LAC" */ |
| 173 | s->lac = bts->location_area_code; |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 174 | break; |
| 175 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
Holger Freyther | c3d4b2d | 2009-06-04 14:27:39 +0000 | [diff] [blame] | 176 | /* Only detach if we are currently in this area */ |
| 177 | if (bts->location_area_code == s->lac) |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 178 | s->lac = 0; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 179 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 180 | break; |
| 181 | default: |
| 182 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 183 | reason); |
| 184 | break; |
| 185 | }; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 186 | return db_sync_subscriber(s); |
| 187 | } |
| 188 | |
| 189 | struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr) |
| 190 | { |
| 191 | subscr->use_count++; |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 192 | DEBUGP(DCC, "subscr %s usage increases usage to: %d\n", |
| 193 | subscr->extension, subscr->use_count); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 194 | return subscr; |
| 195 | } |
| 196 | |
| 197 | struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr) |
| 198 | { |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 199 | subscr->use_count--; |
| 200 | DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n", |
| 201 | subscr->extension, subscr->use_count); |
| 202 | if (subscr->use_count <= 0) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 203 | subscr_free(subscr); |
| 204 | return NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 205 | } |
Holger Freyther | a1f92f0 | 2009-04-12 05:37:52 +0000 | [diff] [blame] | 206 | |
Holger Freyther | 04866d4 | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 207 | void subscr_get_channel(struct gsm_subscriber *subscr, |
| 208 | struct gsm_network *network, int type, |
| 209 | gsm_cbfn *cbfn, void *param) |
| 210 | { |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 211 | struct subscr_request *request; |
| 212 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 213 | if (!tall_sub_req_ctx) |
| 214 | tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 215 | "subscr_request"); |
| 216 | |
| 217 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 218 | if (!request) { |
| 219 | if (cbfn) |
| 220 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 221 | NULL, NULL, param); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | memset(request, 0, sizeof(*request)); |
| 226 | request->network = network; |
| 227 | request->subscr = subscr; |
| 228 | request->channel_type = type; |
| 229 | request->cbfn = cbfn; |
| 230 | request->param = param; |
| 231 | |
| 232 | /* |
| 233 | * FIXME: We might be able to assign more than one |
| 234 | * channel, e.g. voice and SMS submit at the same |
| 235 | * time. |
| 236 | */ |
| 237 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 238 | /* add to the list, send a request */ |
| 239 | llist_add_tail(&request->entry, &subscr->requests); |
| 240 | subscr_send_paging_request(subscr); |
| 241 | } else { |
| 242 | /* this will be picked up later, from subscr_put_channel */ |
| 243 | llist_add_tail(&request->entry, &subscr->requests); |
| 244 | } |
Holger Freyther | 04866d4 | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Holger Freyther | a1f92f0 | 2009-04-12 05:37:52 +0000 | [diff] [blame] | 247 | void subscr_put_channel(struct gsm_lchan *lchan) |
| 248 | { |
| 249 | /* |
| 250 | * FIXME: Continue with other requests now... by checking |
| 251 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 252 | * of the lchan after having asked the next requestee to handle |
| 253 | * the channel. |
| 254 | */ |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 255 | /* |
| 256 | * FIXME: is the lchan is of a different type we could still |
| 257 | * issue an immediate assignment for another channel and then |
| 258 | * close this one. |
| 259 | */ |
| 260 | /* |
| 261 | * Currently we will drop the last ref of the lchan which |
| 262 | * will result in a channel release on RSL and we will start |
| 263 | * the paging. This should work most of the time as the MS |
| 264 | * will listen to the paging requests before we timeout |
| 265 | */ |
| 266 | |
Holger Freyther | a1f92f0 | 2009-04-12 05:37:52 +0000 | [diff] [blame] | 267 | put_lchan(lchan); |
Holger Freyther | 85a7b36 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 268 | |
| 269 | if (lchan->subscr && !llist_empty(&lchan->subscr->requests)) |
| 270 | subscr_send_paging_request(lchan->subscr); |
Holger Freyther | a1f92f0 | 2009-04-12 05:37:52 +0000 | [diff] [blame] | 271 | } |
Holger Freyther | 04866d4 | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 272 | |