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 | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 38 | #include <openbsc/gprs_utils.h> |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 39 | #include "openbsc/gprs_llc.h" |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 40 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 41 | #include <time.h> |
| 42 | |
| 43 | #define GPRS_LLME_CHECK_TICK 30 |
| 44 | |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 45 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 46 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 47 | LLIST_HEAD(sgsn_mm_ctxts); |
| 48 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 49 | LLIST_HEAD(sgsn_apn_ctxts); |
| 50 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 51 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 52 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 53 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 54 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 55 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 56 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 57 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 58 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 59 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 60 | { "suspend", "SUSPEND Count " }, |
| 61 | { "paging.ps", "Paging Packet Switched " }, |
| 62 | { "paging.cs", "Paging Circuit Switched " }, |
| 63 | { "ra_update", "Routing Area Update " }, |
| 64 | }; |
| 65 | |
| 66 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 67 | .group_name_prefix = "sgsn.mmctx", |
| 68 | .group_description = "SGSN MM Context Statistics", |
| 69 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 70 | .ctr_desc = mmctx_ctr_description, |
| 71 | }; |
| 72 | |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 73 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 74 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 75 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 76 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 77 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 78 | }; |
| 79 | |
| 80 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 81 | .group_name_prefix = "sgsn.pdpctx", |
| 82 | .group_description = "SGSN PDP Context Statistics", |
| 83 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 84 | .ctr_desc = pdpctx_ctr_description, |
| 85 | }; |
| 86 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 87 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 88 | const struct gprs_ra_id *id2) |
| 89 | { |
| 90 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 91 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 92 | } |
| 93 | |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 94 | /* See 03.02 Chapter 2.6 */ |
| 95 | static inline uint32_t tlli_foreign(uint32_t tlli) |
| 96 | { |
| 97 | return ((tlli | 0x80000000) & ~0x40000000); |
| 98 | } |
| 99 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 100 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 101 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 102 | const struct gprs_ra_id *raid) |
| 103 | { |
| 104 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 105 | int tlli_type; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 106 | |
| 107 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 108 | if (tlli == ctx->tlli && |
| 109 | ra_id_equals(raid, &ctx->ra)) |
| 110 | return ctx; |
| 111 | } |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 112 | |
| 113 | tlli_type = gprs_tlli_type(tlli); |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 114 | switch (tlli_type) { |
| 115 | case TLLI_LOCAL: |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 116 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 117 | if ((ctx->p_tmsi | 0xC0000000) == tlli || |
| 118 | (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) { |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 119 | ctx->tlli = tlli; |
| 120 | return ctx; |
| 121 | } |
| 122 | } |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 123 | break; |
| 124 | case TLLI_FOREIGN: |
| 125 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 126 | if (tlli == tlli_foreign(ctx->tlli) && |
| 127 | ra_id_equals(raid, &ctx->ra)) |
| 128 | return ctx; |
| 129 | } |
| 130 | break; |
| 131 | default: |
| 132 | break; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 138 | 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] | 139 | { |
| 140 | struct sgsn_mm_ctx *ctx; |
| 141 | |
| 142 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 143 | if (p_tmsi == ctx->p_tmsi || |
| 144 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 145 | return ctx; |
| 146 | } |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 151 | { |
| 152 | struct sgsn_mm_ctx *ctx; |
| 153 | |
| 154 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 155 | if (!strcmp(imsi, ctx->imsi)) |
| 156 | return ctx; |
| 157 | } |
| 158 | return NULL; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /* Allocate a new SGSN MM context */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 163 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 164 | const struct gprs_ra_id *raid) |
| 165 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 166 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 168 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 169 | if (!ctx) |
| 170 | return NULL; |
| 171 | |
| 172 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 173 | ctx->tlli = tlli; |
| 174 | ctx->mm_state = GMM_DEREGISTERED; |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 175 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 176 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 177 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 178 | |
| 179 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 180 | |
| 181 | return ctx; |
| 182 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 183 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 184 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 185 | * in libgtp! */ |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 186 | void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
| 187 | { |
| 188 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 189 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 190 | /* Unlink from global list of MM contexts */ |
| 191 | llist_del(&mm->list); |
| 192 | |
| 193 | /* Free all PDP contexts */ |
| 194 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 195 | sgsn_pdp_ctx_free(pdp); |
| 196 | |
| 197 | rate_ctr_group_free(mm->ctrg); |
| 198 | |
| 199 | talloc_free(mm); |
| 200 | } |
| 201 | |
| 202 | void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm) |
| 203 | { |
| 204 | struct gprs_llc_llme *llme = mm->llme; |
| 205 | uint32_t tlli = mm->tlli; |
| 206 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 207 | |
| 208 | /* delete all existing PDP contexts for this MS */ |
| 209 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) { |
| 210 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 211 | "Dropping PDP context for NSAPI=%u\n", pdp->nsapi); |
| 212 | sgsn_pdp_ctx_terminate(pdp); |
| 213 | } |
| 214 | |
Jacob Erlbeck | ae20b4b | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 215 | if (osmo_timer_pending(&mm->timer)) { |
| 216 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 217 | osmo_timer_del(&mm->timer); |
| 218 | } |
| 219 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 220 | /* Detach from subscriber which is possibly freed then */ |
| 221 | if (mm->subscr) { |
Jacob Erlbeck | 306bb99 | 2015-01-26 13:41:11 +0100 | [diff] [blame] | 222 | struct gsm_subscriber *subscr = subscr_get(mm->subscr); |
Jacob Erlbeck | 3e4e58f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 223 | gprs_subscr_cleanup(subscr); |
Jacob Erlbeck | 37139e5 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 224 | subscr_put(subscr); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 227 | sgsn_mm_ctx_free(mm); |
| 228 | mm = NULL; |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 229 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 230 | /* TLLI unassignment, must be called after sgsn_mm_ctx_free */ |
| 231 | gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 232 | } |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 233 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 234 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 235 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 236 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 237 | uint8_t nsapi) |
| 238 | { |
| 239 | struct sgsn_pdp_ctx *pdp; |
| 240 | |
| 241 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 242 | if (pdp->nsapi == nsapi) |
| 243 | return pdp; |
| 244 | } |
| 245 | return NULL; |
| 246 | } |
| 247 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 248 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 249 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 250 | uint8_t tid) |
| 251 | { |
| 252 | struct sgsn_pdp_ctx *pdp; |
| 253 | |
| 254 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 255 | if (pdp->ti == tid) |
| 256 | return pdp; |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 261 | /* 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] | 262 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 263 | uint8_t nsapi) |
| 264 | { |
| 265 | struct sgsn_pdp_ctx *pdp; |
| 266 | |
| 267 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 268 | if (pdp) |
| 269 | return NULL; |
| 270 | |
| 271 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 272 | if (!pdp) |
| 273 | return NULL; |
| 274 | |
| 275 | pdp->mm = mm; |
| 276 | pdp->nsapi = nsapi; |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 277 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 278 | llist_add(&pdp->list, &mm->pdp_list); |
| 279 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 280 | |
| 281 | return pdp; |
| 282 | } |
| 283 | |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 284 | #include <pdp.h> |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 285 | /* |
| 286 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 287 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 288 | * isn't detached already. |
| 289 | */ |
| 290 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 291 | { |
| 292 | OSMO_ASSERT(pdp->mm != NULL); |
| 293 | |
| 294 | /* There might still be pending callbacks in libgtp. So the parts of |
| 295 | * this object relevant to GTP need to remain intact in this case. */ |
| 296 | |
| 297 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 298 | |
| 299 | /* Force the deactivation of the SNDCP layer */ |
| 300 | sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi); |
| 301 | |
| 302 | /* Detach from MM context */ |
| 303 | llist_del(&pdp->list); |
| 304 | pdp->mm = NULL; |
| 305 | |
| 306 | sgsn_delete_pdp_ctx(pdp); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Don't call this function directly unless you know what you are doing. |
| 311 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 312 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 313 | */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 314 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 315 | { |
Harald Welte | 376d5e5 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 316 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 317 | if (pdp->mm) |
| 318 | llist_del(&pdp->list); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 319 | llist_del(&pdp->g_list); |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 320 | |
| 321 | /* _if_ we still have a library handle, at least set it to NULL |
| 322 | * to avoid any dereferences of the now-deleted PDP context from |
| 323 | * sgsn_libgtp:cb_data_ind() */ |
| 324 | if (pdp->lib) { |
| 325 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 326 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 327 | "has a libgtp handle attached to it, this shouldn't " |
| 328 | "happen!\n"); |
| 329 | osmo_generate_backtrace(); |
| 330 | lib->priv = NULL; |
| 331 | } |
| 332 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 333 | talloc_free(pdp); |
| 334 | } |
| 335 | |
| 336 | /* GGSN contexts */ |
| 337 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 338 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 339 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 340 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 341 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 342 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 343 | if (!ggc) |
| 344 | return NULL; |
| 345 | |
| 346 | ggc->id = id; |
| 347 | ggc->gtp_version = 1; |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 348 | ggc->remote_restart_ctr = -1; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 349 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 350 | ggc->gsn = sgsn->gsn; |
Harald Welte | 119c2ba | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 351 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 352 | |
| 353 | return ggc; |
| 354 | } |
| 355 | |
Jacob Erlbeck | f345612 | 2015-02-03 19:53:15 +0100 | [diff] [blame] | 356 | void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc) |
| 357 | { |
| 358 | llist_del(&ggc->list); |
| 359 | talloc_free(ggc); |
| 360 | } |
| 361 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 362 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 363 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 364 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 365 | |
| 366 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 367 | if (id == ggc->id) |
| 368 | return ggc; |
| 369 | } |
| 370 | return NULL; |
| 371 | } |
| 372 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 373 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 374 | { |
| 375 | struct sgsn_ggsn_ctx *ggc; |
| 376 | |
| 377 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 378 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 379 | return ggc; |
| 380 | } |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 385 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 386 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 387 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 388 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 389 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 390 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 391 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 392 | return ggc; |
| 393 | } |
| 394 | |
| 395 | /* APN contexts */ |
| 396 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 397 | static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 398 | { |
| 399 | struct apn_ctx *actx; |
| 400 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 401 | actx = talloc_zero(tall_bsc_ctx, struct apn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 402 | if (!actx) |
| 403 | return NULL; |
| 404 | actx->name = talloc_strdup(actx, ap_name); |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 405 | actx->imsi_prefix = talloc_strdup(actx, imsi_prefix); |
| 406 | |
| 407 | llist_add_tail(&actx->list, &sgsn_apn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 408 | |
| 409 | return actx; |
| 410 | } |
| 411 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 412 | void sgsn_apn_ctx_free(struct apn_ctx *actx) |
| 413 | { |
| 414 | llist_del(&actx->list); |
| 415 | talloc_free(actx); |
| 416 | } |
| 417 | |
| 418 | struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi) |
| 419 | { |
| 420 | struct apn_ctx *actx; |
| 421 | struct apn_ctx *found_actx = NULL; |
| 422 | size_t imsi_prio = 0; |
| 423 | size_t name_prio = 0; |
| 424 | size_t name_req_len = strlen(name); |
| 425 | |
| 426 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 427 | size_t name_ref_len, imsi_ref_len; |
| 428 | const char *name_ref_start, *name_match_start; |
| 429 | |
| 430 | imsi_ref_len = strlen(actx->imsi_prefix); |
| 431 | if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0) |
| 432 | continue; |
| 433 | |
| 434 | if (imsi_ref_len < imsi_prio) |
| 435 | continue; |
| 436 | |
| 437 | /* IMSI matches */ |
| 438 | |
| 439 | name_ref_start = &actx->name[0]; |
| 440 | if (name_ref_start[0] == '*') { |
| 441 | /* Suffix match */ |
| 442 | name_ref_start += 1; |
| 443 | name_ref_len = strlen(name_ref_start); |
| 444 | if (name_ref_len > name_req_len) |
| 445 | continue; |
| 446 | } else { |
| 447 | name_ref_len = strlen(name_ref_start); |
| 448 | if (name_ref_len != name_req_len) |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | name_match_start = name + (name_req_len - name_ref_len); |
| 453 | if (strcasecmp(name_match_start, name_ref_start) != 0) |
| 454 | continue; |
| 455 | |
| 456 | /* IMSI and name match */ |
| 457 | |
| 458 | if (imsi_ref_len == imsi_prio && name_ref_len < name_prio) |
| 459 | /* Lower priority, skip */ |
| 460 | continue; |
| 461 | |
| 462 | imsi_prio = imsi_ref_len; |
| 463 | name_prio = name_ref_len; |
| 464 | found_actx = actx; |
| 465 | } |
| 466 | return found_actx; |
| 467 | } |
| 468 | |
| 469 | struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 470 | { |
| 471 | struct apn_ctx *actx; |
| 472 | |
| 473 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 474 | if (strcasecmp(name, actx->name) == 0 && |
| 475 | strcasecmp(imsi_prefix, actx->imsi_prefix) == 0) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 476 | return actx; |
| 477 | } |
| 478 | return NULL; |
| 479 | } |
| 480 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 481 | struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 482 | { |
| 483 | struct apn_ctx *actx; |
| 484 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 485 | actx = sgsn_apn_ctx_by_name(name, imsi_prefix); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 486 | if (!actx) |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 487 | actx = sgsn_apn_ctx_alloc(name, imsi_prefix); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 488 | |
| 489 | return actx; |
| 490 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 491 | |
| 492 | uint32_t sgsn_alloc_ptmsi(void) |
| 493 | { |
| 494 | struct sgsn_mm_ctx *mm; |
| 495 | uint32_t ptmsi; |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 496 | int max_retries = 100; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 497 | |
| 498 | restart: |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 499 | ptmsi = rand(); |
| 500 | /* Enforce that the 2 MSB are set without loosing the distance between |
| 501 | * identical values. Since rand() has no duplicate values within a |
| 502 | * period (because the size of the state is the same like the size of |
| 503 | * the random value), this leads to a distance of period/4 when the |
| 504 | * distribution of the 2 MSB is uniform. This approach fails with a |
| 505 | * probability of (3/4)^max_retries, only 1% of the approaches will |
| 506 | * need more than 16 numbers (even distribution assumed). |
| 507 | * |
| 508 | * Alternatively, a freeze list could be used if another PRNG is used |
| 509 | * or when this approach proves to be not sufficient. |
| 510 | */ |
| 511 | if (ptmsi >= 0xC0000000) { |
| 512 | if (!max_retries--) |
| 513 | goto failed; |
| 514 | goto restart; |
| 515 | } |
| 516 | ptmsi |= 0xC0000000; |
| 517 | |
| 518 | if (ptmsi == GSM_RESERVED_TMSI) { |
| 519 | if (!max_retries--) |
| 520 | goto failed; |
| 521 | goto restart; |
| 522 | } |
| 523 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 524 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 525 | if (mm->p_tmsi == ptmsi) { |
| 526 | if (!max_retries--) |
| 527 | goto failed; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 528 | goto restart; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 529 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | return ptmsi; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 533 | |
| 534 | failed: |
| 535 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 536 | return GSM_RESERVED_TMSI; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 537 | } |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 538 | |
| 539 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 540 | { |
| 541 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 542 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 543 | else { |
| 544 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 545 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 546 | "recovery\n"); |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 547 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 548 | sgsn_pdp_ctx_free(pdp); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* 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] | 553 | * otherwise lost state (recovery procedure) */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 554 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 555 | { |
| 556 | struct sgsn_mm_ctx *mm; |
| 557 | int num = 0; |
| 558 | |
| 559 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 560 | struct sgsn_pdp_ctx *pdp; |
| 561 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 562 | if (pdp->ggsn == ggsn) { |
| 563 | drop_one_pdp(pdp); |
| 564 | num++; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return num; |
| 570 | } |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 571 | |
| 572 | int sgsn_force_reattach_oldmsg(struct msgb *oldmsg) |
| 573 | { |
Jacob Erlbeck | abdf02b | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 574 | return gsm0408_gprs_force_reattach_oldmsg(oldmsg); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 577 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 578 | { |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 579 | OSMO_ASSERT(mmctx != NULL); |
Jacob Erlbeck | c939196 | 2014-12-18 09:53:07 +0100 | [diff] [blame] | 580 | LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n"); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 581 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 582 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 583 | } |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 584 | |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 585 | struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx, |
| 586 | struct tlv_parsed *tp, |
| 587 | enum gsm48_gsm_cause *gsm_cause) |
| 588 | { |
| 589 | char req_apn_str[GSM_APN_LENGTH] = {0}; |
| 590 | const struct apn_ctx *apn_ctx = NULL; |
| 591 | const char *selected_apn_str = NULL; |
| 592 | struct sgsn_subscriber_pdp_data *pdp; |
| 593 | struct sgsn_ggsn_ctx *ggsn = NULL; |
| 594 | int allow_any_apn = 0; |
| 595 | |
| 596 | if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) { |
| 597 | if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) { |
| 598 | LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n"); |
| 599 | *gsm_cause = GSM_CAUSE_INV_MAND_INFO; |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | gprs_apn_to_str(req_apn_str, |
| 604 | TLVP_VAL(tp, GSM48_IE_GSM_APN), |
| 605 | TLVP_LEN(tp, GSM48_IE_GSM_APN)); |
| 606 | |
| 607 | if (strcmp(req_apn_str, "*") == 0) |
| 608 | req_apn_str[0] = 0; |
| 609 | } |
| 610 | |
| 611 | if (mmctx->subscr == NULL || |
| 612 | llist_empty(&mmctx->subscr->sgsn_data->pdp_list)) |
| 613 | allow_any_apn = 1; |
| 614 | |
| 615 | if (strlen(req_apn_str) == 0 && !allow_any_apn) { |
| 616 | /* No specific APN requested, check for an APN that is both |
| 617 | * granted and configured */ |
| 618 | |
| 619 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 620 | if (strcmp(pdp->apn_str, "*") == 0) |
| 621 | { |
| 622 | allow_any_apn = 1; |
| 623 | selected_apn_str = ""; |
| 624 | continue; |
| 625 | } |
| 626 | if (!llist_empty(&sgsn_apn_ctxts)) { |
| 627 | apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi); |
| 628 | /* Not configured */ |
| 629 | if (apn_ctx == NULL) |
| 630 | continue; |
| 631 | } |
| 632 | selected_apn_str = pdp->apn_str; |
| 633 | break; |
| 634 | } |
| 635 | } else if (!allow_any_apn) { |
| 636 | /* Check whether the given APN is granted */ |
| 637 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 638 | if (strcmp(pdp->apn_str, "*") == 0) { |
| 639 | selected_apn_str = req_apn_str; |
| 640 | allow_any_apn = 1; |
| 641 | continue; |
| 642 | } |
| 643 | if (strcasecmp(pdp->apn_str, req_apn_str) == 0) { |
| 644 | selected_apn_str = req_apn_str; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } else if (strlen(req_apn_str) != 0) { |
| 649 | /* Any APN is allowed */ |
| 650 | selected_apn_str = req_apn_str; |
| 651 | } else { |
| 652 | /* Prefer the GGSN associated with the wildcard APN */ |
| 653 | selected_apn_str = ""; |
| 654 | } |
| 655 | |
| 656 | if (!allow_any_apn && selected_apn_str == NULL) { |
| 657 | /* Access not granted */ |
| 658 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 659 | "The requested APN '%s' is not allowed\n", |
| 660 | req_apn_str); |
| 661 | *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB; |
| 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | if (apn_ctx == NULL && selected_apn_str) |
| 666 | apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi); |
| 667 | |
| 668 | if (apn_ctx != NULL) { |
| 669 | ggsn = apn_ctx->ggsn; |
| 670 | } else if (llist_empty(&sgsn_apn_ctxts)) { |
| 671 | /* No configuration -> use GGSN 0 */ |
| 672 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 673 | } else if (allow_any_apn && |
| 674 | (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) { |
| 675 | /* No APN given and no default configuration -> Use GGSN 0 */ |
| 676 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 677 | } else { |
| 678 | /* No matching configuration found */ |
| 679 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 680 | "The selected APN '%s' has not been configured\n", |
| 681 | selected_apn_str); |
| 682 | *gsm_cause = GSM_CAUSE_MISSING_APN; |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 687 | "Found GGSN %d for APN '%s' (requested '%s')\n", |
| 688 | ggsn->id, selected_apn_str ? selected_apn_str : "---", |
| 689 | req_apn_str); |
| 690 | |
| 691 | return ggsn; |
| 692 | } |
| 693 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 694 | static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme) |
| 695 | { |
| 696 | struct sgsn_mm_ctx *mmctx = NULL; |
| 697 | |
| 698 | llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) { |
| 699 | if (llme == mmctx->llme) { |
| 700 | gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE); |
| 701 | return; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* No MM context found */ |
| 706 | LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n", |
| 707 | llme->tlli); |
| 708 | gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 709 | } |
| 710 | |
| 711 | static void sgsn_llme_check_cb(void *data_) |
| 712 | { |
| 713 | struct gprs_llc_llme *llme, *llme_tmp; |
| 714 | struct timespec now_tp; |
| 715 | time_t now, age; |
| 716 | time_t max_age = gprs_max_time_to_idle(); |
| 717 | |
| 718 | int rc; |
| 719 | |
| 720 | rc = clock_gettime(CLOCK_MONOTONIC, &now_tp); |
| 721 | OSMO_ASSERT(rc >= 0); |
| 722 | now = now_tp.tv_sec; |
| 723 | |
| 724 | LOGP(DGPRS, LOGL_DEBUG, |
| 725 | "Checking for inactive LLMEs, time = %u\n", (unsigned)now); |
| 726 | |
| 727 | llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) { |
| 728 | if (llme->age_timestamp == GPRS_LLME_RESET_AGE) |
| 729 | llme->age_timestamp = now; |
| 730 | |
| 731 | age = now - llme->age_timestamp; |
| 732 | |
| 733 | if (age > max_age || age < 0) { |
| 734 | LOGP(DGPRS, LOGL_INFO, |
| 735 | "Inactivity timeout for TLLI 0x%08x, age %d\n", |
| 736 | llme->tlli, (int)age); |
| 737 | sgsn_llme_cleanup_free(llme); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 742 | } |
| 743 | |
| 744 | void sgsn_inst_init() |
| 745 | { |
| 746 | sgsn->llme_timer.cb = sgsn_llme_check_cb; |
| 747 | sgsn->llme_timer.data = NULL; |
| 748 | |
| 749 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 750 | } |
| 751 | |