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