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