Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS SGSN functionality */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 23 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/talloc.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 28 | #include <osmocom/core/backtrace.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 29 | #include <osmocom/gprs/gprs_ns.h> |
| 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 35 | #include <openbsc/sgsn.h> |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_04_08_gprs.h> |
| 37 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 38 | #include "openbsc/gprs_llc.h" |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 39 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 40 | #include <time.h> |
| 41 | |
| 42 | #define GPRS_LLME_CHECK_TICK 30 |
| 43 | |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 44 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 45 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 46 | LLIST_HEAD(sgsn_mm_ctxts); |
| 47 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 48 | LLIST_HEAD(sgsn_apn_ctxts); |
| 49 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 50 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 51 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 52 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 53 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 54 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 55 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 56 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 57 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 58 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 59 | { "suspend", "SUSPEND Count " }, |
| 60 | { "paging.ps", "Paging Packet Switched " }, |
| 61 | { "paging.cs", "Paging Circuit Switched " }, |
| 62 | { "ra_update", "Routing Area Update " }, |
| 63 | }; |
| 64 | |
| 65 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 66 | .group_name_prefix = "sgsn.mmctx", |
| 67 | .group_description = "SGSN MM Context Statistics", |
| 68 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 69 | .ctr_desc = mmctx_ctr_description, |
| 70 | }; |
| 71 | |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 72 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 73 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 74 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 75 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 76 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 77 | }; |
| 78 | |
| 79 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 80 | .group_name_prefix = "sgsn.pdpctx", |
| 81 | .group_description = "SGSN PDP Context Statistics", |
| 82 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 83 | .ctr_desc = pdpctx_ctr_description, |
| 84 | }; |
| 85 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 86 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 87 | const struct gprs_ra_id *id2) |
| 88 | { |
| 89 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 90 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 91 | } |
| 92 | |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 93 | /* See 03.02 Chapter 2.6 */ |
| 94 | static inline uint32_t tlli_foreign(uint32_t tlli) |
| 95 | { |
| 96 | return ((tlli | 0x80000000) & ~0x40000000); |
| 97 | } |
| 98 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 99 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 100 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 101 | const struct gprs_ra_id *raid) |
| 102 | { |
| 103 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 104 | int tlli_type; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 105 | |
| 106 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 107 | if (tlli == ctx->tlli && |
| 108 | ra_id_equals(raid, &ctx->ra)) |
| 109 | return ctx; |
| 110 | } |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 111 | |
| 112 | tlli_type = gprs_tlli_type(tlli); |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 113 | switch (tlli_type) { |
| 114 | case TLLI_LOCAL: |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 115 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 116 | if ((ctx->p_tmsi | 0xC0000000) == tlli || |
| 117 | (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) { |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 118 | ctx->tlli = tlli; |
| 119 | return ctx; |
| 120 | } |
| 121 | } |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 122 | break; |
| 123 | case TLLI_FOREIGN: |
| 124 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 125 | if (tlli == tlli_foreign(ctx->tlli) && |
| 126 | ra_id_equals(raid, &ctx->ra)) |
| 127 | return ctx; |
| 128 | } |
| 129 | break; |
| 130 | default: |
| 131 | break; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 137 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 138 | { |
| 139 | struct sgsn_mm_ctx *ctx; |
| 140 | |
| 141 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 142 | if (p_tmsi == ctx->p_tmsi || |
| 143 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 144 | return ctx; |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 150 | { |
| 151 | struct sgsn_mm_ctx *ctx; |
| 152 | |
| 153 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 154 | if (!strcmp(imsi, ctx->imsi)) |
| 155 | return ctx; |
| 156 | } |
| 157 | return NULL; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | /* Allocate a new SGSN MM context */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 162 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 163 | const struct gprs_ra_id *raid) |
| 164 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 165 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 166 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 167 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 168 | if (!ctx) |
| 169 | return NULL; |
| 170 | |
| 171 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 172 | ctx->tlli = tlli; |
| 173 | ctx->mm_state = GMM_DEREGISTERED; |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 174 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 175 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 176 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 177 | |
| 178 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 179 | |
| 180 | return ctx; |
| 181 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 182 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 183 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 184 | * in libgtp! */ |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 185 | void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
| 186 | { |
| 187 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 188 | |
Jacob Erlbeck | ae20b4b | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 189 | if (osmo_timer_pending(&mm->timer)) { |
| 190 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 191 | osmo_timer_del(&mm->timer); |
| 192 | } |
| 193 | |
Holger Hans Peter Freyther | e47d4f6 | 2015-01-20 21:14:03 +0100 | [diff] [blame] | 194 | /* Unlink from global list of MM contexts */ |
| 195 | llist_del(&mm->list); |
| 196 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 197 | /* Detach from subscriber which is possibly freed then */ |
| 198 | if (mm->subscr) { |
Jacob Erlbeck | 306bb99 | 2015-01-26 13:41:11 +0100 | [diff] [blame] | 199 | struct gsm_subscriber *subscr = subscr_get(mm->subscr); |
Jacob Erlbeck | 3e4e58f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 200 | gprs_subscr_cleanup(subscr); |
Jacob Erlbeck | 37139e5 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 201 | subscr_put(subscr); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 204 | /* Free all PDP contexts */ |
| 205 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 206 | sgsn_pdp_ctx_free(pdp); |
| 207 | |
| 208 | rate_ctr_group_free(mm->ctrg); |
| 209 | |
| 210 | talloc_free(mm); |
| 211 | } |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 212 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 213 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 214 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 215 | uint8_t nsapi) |
| 216 | { |
| 217 | struct sgsn_pdp_ctx *pdp; |
| 218 | |
| 219 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 220 | if (pdp->nsapi == nsapi) |
| 221 | return pdp; |
| 222 | } |
| 223 | return NULL; |
| 224 | } |
| 225 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 226 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 227 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 228 | uint8_t tid) |
| 229 | { |
| 230 | struct sgsn_pdp_ctx *pdp; |
| 231 | |
| 232 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 233 | if (pdp->ti == tid) |
| 234 | return pdp; |
| 235 | } |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 239 | /* you don't want to use this directly, call sgsn_create_pdp_ctx() */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 240 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 241 | uint8_t nsapi) |
| 242 | { |
| 243 | struct sgsn_pdp_ctx *pdp; |
| 244 | |
| 245 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 246 | if (pdp) |
| 247 | return NULL; |
| 248 | |
| 249 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 250 | if (!pdp) |
| 251 | return NULL; |
| 252 | |
| 253 | pdp->mm = mm; |
| 254 | pdp->nsapi = nsapi; |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 255 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 256 | llist_add(&pdp->list, &mm->pdp_list); |
| 257 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 258 | |
| 259 | return pdp; |
| 260 | } |
| 261 | |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 262 | #include <pdp.h> |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 263 | /* |
| 264 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 265 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 266 | * isn't detached already. |
| 267 | */ |
| 268 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 269 | { |
| 270 | OSMO_ASSERT(pdp->mm != NULL); |
| 271 | |
| 272 | /* There might still be pending callbacks in libgtp. So the parts of |
| 273 | * this object relevant to GTP need to remain intact in this case. */ |
| 274 | |
| 275 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 276 | |
| 277 | /* Force the deactivation of the SNDCP layer */ |
| 278 | sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi); |
| 279 | |
| 280 | /* Detach from MM context */ |
| 281 | llist_del(&pdp->list); |
| 282 | pdp->mm = NULL; |
| 283 | |
| 284 | sgsn_delete_pdp_ctx(pdp); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Don't call this function directly unless you know what you are doing. |
| 289 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 290 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 291 | */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 292 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 293 | { |
Harald Welte | 376d5e5 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 294 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 295 | if (pdp->mm) |
| 296 | llist_del(&pdp->list); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 297 | llist_del(&pdp->g_list); |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 298 | |
| 299 | /* _if_ we still have a library handle, at least set it to NULL |
| 300 | * to avoid any dereferences of the now-deleted PDP context from |
| 301 | * sgsn_libgtp:cb_data_ind() */ |
| 302 | if (pdp->lib) { |
| 303 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 304 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 305 | "has a libgtp handle attached to it, this shouldn't " |
| 306 | "happen!\n"); |
| 307 | osmo_generate_backtrace(); |
| 308 | lib->priv = NULL; |
| 309 | } |
| 310 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 311 | talloc_free(pdp); |
| 312 | } |
| 313 | |
| 314 | /* GGSN contexts */ |
| 315 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 316 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 317 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 318 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 319 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 320 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 321 | if (!ggc) |
| 322 | return NULL; |
| 323 | |
| 324 | ggc->id = id; |
| 325 | ggc->gtp_version = 1; |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 326 | ggc->remote_restart_ctr = -1; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 327 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 328 | ggc->gsn = sgsn->gsn; |
Harald Welte | 119c2ba | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 329 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 330 | |
| 331 | return ggc; |
| 332 | } |
| 333 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 334 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 335 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 336 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 337 | |
| 338 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 339 | if (id == ggc->id) |
| 340 | return ggc; |
| 341 | } |
| 342 | return NULL; |
| 343 | } |
| 344 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 345 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 346 | { |
| 347 | struct sgsn_ggsn_ctx *ggc; |
| 348 | |
| 349 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 350 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 351 | return ggc; |
| 352 | } |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 357 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 358 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 359 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 360 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 361 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 362 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 363 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 364 | return ggc; |
| 365 | } |
| 366 | |
| 367 | /* APN contexts */ |
| 368 | |
| 369 | #if 0 |
| 370 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 371 | { |
| 372 | struct apn_ctx *actx; |
| 373 | |
| 374 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 375 | if (!actx) |
| 376 | return NULL; |
| 377 | actx->name = talloc_strdup(actx, ap_name); |
| 378 | |
| 379 | return actx; |
| 380 | } |
| 381 | |
| 382 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 383 | { |
| 384 | struct apn_ctx *actx; |
| 385 | |
| 386 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 387 | if (!strcmp(name, actx->name)) |
| 388 | return actx; |
| 389 | } |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 394 | { |
| 395 | struct apn_ctx *actx; |
| 396 | |
| 397 | actx = apn_ctx_by_name(name); |
| 398 | if (!actx) |
| 399 | actx = apn_ctx_alloc(name); |
| 400 | |
| 401 | return actx; |
| 402 | } |
| 403 | #endif |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 404 | |
| 405 | uint32_t sgsn_alloc_ptmsi(void) |
| 406 | { |
| 407 | struct sgsn_mm_ctx *mm; |
| 408 | uint32_t ptmsi; |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 409 | int max_retries = 100; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 410 | |
| 411 | restart: |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 412 | ptmsi = rand(); |
| 413 | /* Enforce that the 2 MSB are set without loosing the distance between |
| 414 | * identical values. Since rand() has no duplicate values within a |
| 415 | * period (because the size of the state is the same like the size of |
| 416 | * the random value), this leads to a distance of period/4 when the |
| 417 | * distribution of the 2 MSB is uniform. This approach fails with a |
| 418 | * probability of (3/4)^max_retries, only 1% of the approaches will |
| 419 | * need more than 16 numbers (even distribution assumed). |
| 420 | * |
| 421 | * Alternatively, a freeze list could be used if another PRNG is used |
| 422 | * or when this approach proves to be not sufficient. |
| 423 | */ |
| 424 | if (ptmsi >= 0xC0000000) { |
| 425 | if (!max_retries--) |
| 426 | goto failed; |
| 427 | goto restart; |
| 428 | } |
| 429 | ptmsi |= 0xC0000000; |
| 430 | |
| 431 | if (ptmsi == GSM_RESERVED_TMSI) { |
| 432 | if (!max_retries--) |
| 433 | goto failed; |
| 434 | goto restart; |
| 435 | } |
| 436 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 437 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 438 | if (mm->p_tmsi == ptmsi) { |
| 439 | if (!max_retries--) |
| 440 | goto failed; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 441 | goto restart; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 442 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return ptmsi; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 446 | |
| 447 | failed: |
| 448 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 449 | return GSM_RESERVED_TMSI; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 450 | } |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 451 | |
| 452 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 453 | { |
| 454 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 455 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 456 | else { |
| 457 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 458 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 459 | "recovery\n"); |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 460 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 461 | sgsn_pdp_ctx_free(pdp); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* High-level function to be called in case a GGSN has disappeared or |
Holger Hans Peter Freyther | 19e990d | 2014-10-27 10:24:37 +0100 | [diff] [blame] | 466 | * otherwise lost state (recovery procedure) */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 467 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 468 | { |
| 469 | struct sgsn_mm_ctx *mm; |
| 470 | int num = 0; |
| 471 | |
| 472 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 473 | struct sgsn_pdp_ctx *pdp; |
| 474 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 475 | if (pdp->ggsn == ggsn) { |
| 476 | drop_one_pdp(pdp); |
| 477 | num++; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return num; |
| 483 | } |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 484 | |
| 485 | int sgsn_force_reattach_oldmsg(struct msgb *oldmsg) |
| 486 | { |
Jacob Erlbeck | abdf02b | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 487 | return gsm0408_gprs_force_reattach_oldmsg(oldmsg); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 490 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx, |
| 491 | struct gsm_subscriber *subscr) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 492 | { |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 493 | if (!mmctx && subscr && strlen(subscr->imsi) > 0) { |
| 494 | mmctx = sgsn_mm_ctx_by_imsi(subscr->imsi); |
| 495 | OSMO_ASSERT(!mmctx || !mmctx->subscr || mmctx->subscr == subscr); |
| 496 | } |
| 497 | |
| 498 | if (!mmctx) { |
| 499 | LOGP(DMM, LOGL_INFO, |
| 500 | "Subscriber data update for unregistered MM context, IMSI %s\n", |
| 501 | subscr->imsi); |
| 502 | return; |
| 503 | } |
| 504 | |
Jacob Erlbeck | c939196 | 2014-12-18 09:53:07 +0100 | [diff] [blame] | 505 | LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n"); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 506 | |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 507 | if (!subscr->sgsn_data->mm && !mmctx->subscr) { |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 508 | mmctx->subscr = subscr_get(subscr); |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 509 | mmctx->subscr->sgsn_data->mm = mmctx; |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 510 | } |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 511 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 512 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 513 | } |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 514 | |
| 515 | static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme) |
| 516 | { |
| 517 | struct sgsn_mm_ctx *mmctx = NULL; |
| 518 | |
| 519 | llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) { |
| 520 | if (llme == mmctx->llme) { |
| 521 | gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE); |
| 522 | return; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* No MM context found */ |
| 527 | LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n", |
| 528 | llme->tlli); |
| 529 | gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 530 | } |
| 531 | |
| 532 | static void sgsn_llme_check_cb(void *data_) |
| 533 | { |
| 534 | struct gprs_llc_llme *llme, *llme_tmp; |
| 535 | struct timespec now_tp; |
| 536 | time_t now, age; |
| 537 | time_t max_age = gprs_max_time_to_idle(); |
| 538 | |
| 539 | int rc; |
| 540 | |
| 541 | rc = clock_gettime(CLOCK_MONOTONIC, &now_tp); |
| 542 | OSMO_ASSERT(rc >= 0); |
| 543 | now = now_tp.tv_sec; |
| 544 | |
| 545 | LOGP(DGPRS, LOGL_DEBUG, |
| 546 | "Checking for inactive LLMEs, time = %u\n", (unsigned)now); |
| 547 | |
| 548 | llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) { |
| 549 | if (llme->age_timestamp == GPRS_LLME_RESET_AGE) |
| 550 | llme->age_timestamp = now; |
| 551 | |
| 552 | age = now - llme->age_timestamp; |
| 553 | |
| 554 | if (age > max_age || age < 0) { |
| 555 | LOGP(DGPRS, LOGL_INFO, |
| 556 | "Inactivity timeout for TLLI 0x%08x, age %d\n", |
| 557 | llme->tlli, (int)age); |
| 558 | sgsn_llme_cleanup_free(llme); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 563 | } |
| 564 | |
| 565 | void sgsn_inst_init() |
| 566 | { |
| 567 | sgsn->llme_timer.cb = sgsn_llme_check_cb; |
| 568 | sgsn->llme_timer.data = NULL; |
| 569 | |
| 570 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 571 | } |
| 572 | |