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