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