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