Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 1 | /* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame^] | 30 | #include <osmocore/talloc.h> |
| 31 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame^] | 34 | #include <openbsc/paging.h> |
Harald Welte | ea5cf30 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 35 | #include <openbsc/signal.h> |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 36 | #include <openbsc/db.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 37 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame^] | 38 | void *tall_sub_req_ctx; |
| 39 | |
Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 40 | extern struct llist_head *subscr_bsc_active_subscriber(void); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 41 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame^] | 42 | |
| 43 | /* |
| 44 | * Struct for pending channel requests. This is managed in the |
| 45 | * llist_head requests of each subscriber. The reference counting |
| 46 | * should work in such a way that a subscriber with a pending request |
| 47 | * remains in memory. |
| 48 | */ |
| 49 | struct subscr_request { |
| 50 | struct llist_head entry; |
| 51 | |
| 52 | /* back reference */ |
| 53 | struct gsm_subscriber *subscr; |
| 54 | |
| 55 | /* the requested channel type */ |
| 56 | int channel_type; |
| 57 | |
| 58 | /* the callback data */ |
| 59 | gsm_cbfn *cbfn; |
| 60 | void *param; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * We got the channel assigned and can now hand this channel |
| 65 | * over to one of our callbacks. |
| 66 | */ |
| 67 | static int subscr_paging_cb(unsigned int hooknum, unsigned int event, |
| 68 | struct msgb *msg, void *data, void *param) |
| 69 | { |
| 70 | struct subscr_request *request; |
| 71 | struct gsm_subscriber *subscr = (struct gsm_subscriber *)param; |
| 72 | |
| 73 | /* There is no request anymore... */ |
| 74 | if (llist_empty(&subscr->requests)) |
| 75 | return -1; |
| 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 | |
| 89 | subscr_put(subscr); |
| 90 | talloc_free(request); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 95 | { |
| 96 | struct subscr_request *request; |
| 97 | int rc; |
| 98 | |
| 99 | assert(!llist_empty(&subscr->requests)); |
| 100 | |
| 101 | request = (struct subscr_request *)subscr->requests.next; |
| 102 | rc = paging_request(subscr->net, subscr, request->channel_type, |
| 103 | subscr_paging_cb, subscr); |
| 104 | |
| 105 | /* paging failed, quit now */ |
| 106 | if (rc <= 0) { |
| 107 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, |
| 108 | NULL, NULL, subscr); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void subscr_get_channel(struct gsm_subscriber *subscr, |
| 113 | int type, gsm_cbfn *cbfn, void *param) |
| 114 | { |
| 115 | struct subscr_request *request; |
| 116 | |
| 117 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 118 | if (!request) { |
| 119 | if (cbfn) |
| 120 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 121 | NULL, NULL, param); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | memset(request, 0, sizeof(*request)); |
| 126 | request->subscr = subscr_get(subscr); |
| 127 | request->channel_type = type; |
| 128 | request->cbfn = cbfn; |
| 129 | request->param = param; |
| 130 | |
| 131 | /* |
| 132 | * FIXME: We might be able to assign more than one |
| 133 | * channel, e.g. voice and SMS submit at the same |
| 134 | * time. |
| 135 | */ |
| 136 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 137 | /* add to the list, send a request */ |
| 138 | llist_add_tail(&request->entry, &subscr->requests); |
| 139 | subscr_send_paging_request(subscr); |
| 140 | } else { |
| 141 | /* this will be picked up later, from subscr_put_channel */ |
| 142 | llist_add_tail(&request->entry, &subscr->requests); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void subscr_put_channel(struct gsm_subscriber_connection *conn) |
| 147 | { |
| 148 | /* |
| 149 | * FIXME: Continue with other requests now... by checking |
| 150 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 151 | * of the lchan after having asked the next requestee to handle |
| 152 | * the channel. |
| 153 | */ |
| 154 | /* |
| 155 | * FIXME: is the lchan is of a different type we could still |
| 156 | * issue an immediate assignment for another channel and then |
| 157 | * close this one. |
| 158 | */ |
| 159 | /* |
| 160 | * Currently we will drop the last ref of the lchan which |
| 161 | * will result in a channel release on RSL and we will start |
| 162 | * the paging. This should work most of the time as the MS |
| 163 | * will listen to the paging requests before we timeout |
| 164 | */ |
| 165 | |
| 166 | if (conn->subscr && !llist_empty(&conn->subscr->requests)) |
| 167 | subscr_send_paging_request(conn->subscr); |
| 168 | } |
| 169 | |
| 170 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 171 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net, |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 172 | u_int32_t tmsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 173 | { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 174 | char tmsi_string[14]; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 175 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 176 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 177 | /* we might have a record in memory already */ |
Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 178 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 179 | if (tmsi == subscr->tmsi) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 180 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 183 | sprintf(tmsi_string, "%u", tmsi); |
| 184 | return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 187 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net, |
| 188 | const char *imsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 189 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 190 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 191 | |
Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 192 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 193 | if (strcmp(subscr->imsi, imsi) == 0) |
| 194 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 197 | return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 200 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net, |
| 201 | const char *ext) |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 202 | { |
| 203 | struct gsm_subscriber *subscr; |
| 204 | |
Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 205 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 206 | if (strcmp(subscr->extension, ext) == 0) |
| 207 | return subscr_get(subscr); |
| 208 | } |
| 209 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 210 | return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext); |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 213 | struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net, |
| 214 | unsigned long long id) |
| 215 | { |
| 216 | struct gsm_subscriber *subscr; |
| 217 | char buf[32]; |
| 218 | sprintf(buf, "%llu", id); |
| 219 | |
Holger Hans Peter Freyther | 857e5e6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 220 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 221 | if (subscr->id == id) |
| 222 | return subscr_get(subscr); |
| 223 | } |
| 224 | |
| 225 | return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf); |
| 226 | } |
| 227 | |
| 228 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 229 | 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] | 230 | { |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 231 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 232 | switch (reason) { |
| 233 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | dcaf565 | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 234 | s->net = bts->network; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 235 | /* Indicate "attached to LAC" */ |
| 236 | s->lac = bts->location_area_code; |
Harald Welte | 77563da | 2009-12-24 13:48:14 +0100 | [diff] [blame] | 237 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
Harald Welte | 2e6d468 | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 238 | subscr_name(s), s->lac); |
Harald Welte | ea5cf30 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 239 | dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 240 | break; |
| 241 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
Holger Freyther | c3d4b2d | 2009-06-04 14:27:39 +0000 | [diff] [blame] | 242 | /* Only detach if we are currently in this area */ |
| 243 | if (bts->location_area_code == s->lac) |
Holger Hans Peter Freyther | e48b956 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 244 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 2e6d468 | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 245 | LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s)); |
Harald Welte | ea5cf30 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 246 | dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 247 | break; |
| 248 | default: |
| 249 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 250 | reason); |
| 251 | break; |
| 252 | }; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 253 | return db_sync_subscriber(s); |
| 254 | } |
| 255 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 256 | |