Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS SGSN functionality */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 23 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/talloc.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 28 | #include <osmocom/core/backtrace.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 29 | #include <osmocom/gprs/gprs_ns.h> |
| 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 35 | #include <openbsc/sgsn.h> |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_04_08_gprs.h> |
| 37 | #include <openbsc/gprs_gmm.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 38 | |
| 39 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 40 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 41 | LLIST_HEAD(sgsn_mm_ctxts); |
| 42 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 43 | LLIST_HEAD(sgsn_apn_ctxts); |
| 44 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 45 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 46 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 47 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 48 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 49 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 50 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 51 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 52 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 53 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 54 | { "suspend", "SUSPEND Count " }, |
| 55 | { "paging.ps", "Paging Packet Switched " }, |
| 56 | { "paging.cs", "Paging Circuit Switched " }, |
| 57 | { "ra_update", "Routing Area Update " }, |
| 58 | }; |
| 59 | |
| 60 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 61 | .group_name_prefix = "sgsn.mmctx", |
| 62 | .group_description = "SGSN MM Context Statistics", |
| 63 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 64 | .ctr_desc = mmctx_ctr_description, |
| 65 | }; |
| 66 | |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 67 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 68 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 69 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 70 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 71 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 72 | }; |
| 73 | |
| 74 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 75 | .group_name_prefix = "sgsn.pdpctx", |
| 76 | .group_description = "SGSN PDP Context Statistics", |
| 77 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 78 | .ctr_desc = pdpctx_ctr_description, |
| 79 | }; |
| 80 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 81 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 82 | const struct gprs_ra_id *id2) |
| 83 | { |
| 84 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 85 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 86 | } |
| 87 | |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 88 | /* See 03.02 Chapter 2.6 */ |
| 89 | static inline uint32_t tlli_foreign(uint32_t tlli) |
| 90 | { |
| 91 | return ((tlli | 0x80000000) & ~0x40000000); |
| 92 | } |
| 93 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 94 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 95 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 96 | const struct gprs_ra_id *raid) |
| 97 | { |
| 98 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 99 | int tlli_type; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 100 | |
| 101 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 102 | if (tlli == ctx->tlli && |
| 103 | ra_id_equals(raid, &ctx->ra)) |
| 104 | return ctx; |
| 105 | } |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 106 | |
| 107 | tlli_type = gprs_tlli_type(tlli); |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 108 | switch (tlli_type) { |
| 109 | case TLLI_LOCAL: |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 110 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 111 | if ((ctx->p_tmsi | 0xC0000000) == tlli || |
| 112 | (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) { |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 113 | ctx->tlli = tlli; |
| 114 | return ctx; |
| 115 | } |
| 116 | } |
Harald Welte | f6bd340 | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 117 | break; |
| 118 | case TLLI_FOREIGN: |
| 119 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 120 | if (tlli == tlli_foreign(ctx->tlli) && |
| 121 | ra_id_equals(raid, &ctx->ra)) |
| 122 | return ctx; |
| 123 | } |
| 124 | break; |
| 125 | default: |
| 126 | break; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 129 | return NULL; |
| 130 | } |
| 131 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | struct sgsn_mm_ctx *ctx; |
| 135 | |
| 136 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 137 | if (p_tmsi == ctx->p_tmsi || |
| 138 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 139 | return ctx; |
| 140 | } |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 145 | { |
| 146 | struct sgsn_mm_ctx *ctx; |
| 147 | |
| 148 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 149 | if (!strcmp(imsi, ctx->imsi)) |
| 150 | return ctx; |
| 151 | } |
| 152 | return NULL; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /* Allocate a new SGSN MM context */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 157 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 158 | const struct gprs_ra_id *raid) |
| 159 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 160 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 161 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 162 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 163 | if (!ctx) |
| 164 | return NULL; |
| 165 | |
| 166 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 167 | ctx->tlli = tlli; |
| 168 | ctx->mm_state = GMM_DEREGISTERED; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 169 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 170 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 171 | |
| 172 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 173 | |
| 174 | return ctx; |
| 175 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 176 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 177 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 178 | * in libgtp! */ |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 179 | void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
| 180 | { |
| 181 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 182 | |
Jacob Erlbeck | ae20b4b | 2014-10-20 16:05:55 +0200 | [diff] [blame^] | 183 | if (osmo_timer_pending(&mm->timer)) { |
| 184 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 185 | osmo_timer_del(&mm->timer); |
| 186 | } |
| 187 | |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 188 | /* Unlink from global list of MM contexts */ |
| 189 | llist_del(&mm->list); |
| 190 | |
| 191 | /* Free all PDP contexts */ |
| 192 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 193 | sgsn_pdp_ctx_free(pdp); |
| 194 | |
| 195 | rate_ctr_group_free(mm->ctrg); |
| 196 | |
| 197 | talloc_free(mm); |
| 198 | } |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 199 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 200 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 201 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 202 | uint8_t nsapi) |
| 203 | { |
| 204 | struct sgsn_pdp_ctx *pdp; |
| 205 | |
| 206 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 207 | if (pdp->nsapi == nsapi) |
| 208 | return pdp; |
| 209 | } |
| 210 | return NULL; |
| 211 | } |
| 212 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 213 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 214 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 215 | uint8_t tid) |
| 216 | { |
| 217 | struct sgsn_pdp_ctx *pdp; |
| 218 | |
| 219 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 220 | if (pdp->ti == tid) |
| 221 | return pdp; |
| 222 | } |
| 223 | return NULL; |
| 224 | } |
| 225 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 226 | /* 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] | 227 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 228 | uint8_t nsapi) |
| 229 | { |
| 230 | struct sgsn_pdp_ctx *pdp; |
| 231 | |
| 232 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 233 | if (pdp) |
| 234 | return NULL; |
| 235 | |
| 236 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 237 | if (!pdp) |
| 238 | return NULL; |
| 239 | |
| 240 | pdp->mm = mm; |
| 241 | pdp->nsapi = nsapi; |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 242 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 243 | llist_add(&pdp->list, &mm->pdp_list); |
| 244 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 245 | |
| 246 | return pdp; |
| 247 | } |
| 248 | |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 249 | #include <pdp.h> |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 250 | /* you probably want to call sgsn_delete_pdp_ctx() instead */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 251 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 252 | { |
Harald Welte | 376d5e5 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 253 | rate_ctr_group_free(pdp->ctrg); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 254 | llist_del(&pdp->list); |
| 255 | llist_del(&pdp->g_list); |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 256 | |
| 257 | /* _if_ we still have a library handle, at least set it to NULL |
| 258 | * to avoid any dereferences of the now-deleted PDP context from |
| 259 | * sgsn_libgtp:cb_data_ind() */ |
| 260 | if (pdp->lib) { |
| 261 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 262 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 263 | "has a libgtp handle attached to it, this shouldn't " |
| 264 | "happen!\n"); |
| 265 | osmo_generate_backtrace(); |
| 266 | lib->priv = NULL; |
| 267 | } |
| 268 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 269 | talloc_free(pdp); |
| 270 | } |
| 271 | |
| 272 | /* GGSN contexts */ |
| 273 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 274 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 275 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 276 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 277 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 278 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 279 | if (!ggc) |
| 280 | return NULL; |
| 281 | |
| 282 | ggc->id = id; |
| 283 | ggc->gtp_version = 1; |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 284 | ggc->remote_restart_ctr = -1; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 285 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 286 | ggc->gsn = sgsn->gsn; |
Harald Welte | 119c2ba | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 287 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 288 | |
| 289 | return ggc; |
| 290 | } |
| 291 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 292 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 293 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 294 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 295 | |
| 296 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 297 | if (id == ggc->id) |
| 298 | return ggc; |
| 299 | } |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 303 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 304 | { |
| 305 | struct sgsn_ggsn_ctx *ggc; |
| 306 | |
| 307 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 308 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 309 | return ggc; |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 315 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 316 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 317 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 318 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 319 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 320 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 321 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 322 | return ggc; |
| 323 | } |
| 324 | |
| 325 | /* APN contexts */ |
| 326 | |
| 327 | #if 0 |
| 328 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 329 | { |
| 330 | struct apn_ctx *actx; |
| 331 | |
| 332 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 333 | if (!actx) |
| 334 | return NULL; |
| 335 | actx->name = talloc_strdup(actx, ap_name); |
| 336 | |
| 337 | return actx; |
| 338 | } |
| 339 | |
| 340 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 341 | { |
| 342 | struct apn_ctx *actx; |
| 343 | |
| 344 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 345 | if (!strcmp(name, actx->name)) |
| 346 | return actx; |
| 347 | } |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 352 | { |
| 353 | struct apn_ctx *actx; |
| 354 | |
| 355 | actx = apn_ctx_by_name(name); |
| 356 | if (!actx) |
| 357 | actx = apn_ctx_alloc(name); |
| 358 | |
| 359 | return actx; |
| 360 | } |
| 361 | #endif |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 362 | |
| 363 | uint32_t sgsn_alloc_ptmsi(void) |
| 364 | { |
| 365 | struct sgsn_mm_ctx *mm; |
| 366 | uint32_t ptmsi; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 367 | int max_retries = 23; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 368 | |
| 369 | restart: |
Holger Hans Peter Freyther | b773fbf | 2014-08-05 15:20:23 +0200 | [diff] [blame] | 370 | ptmsi = rand() | 0xC0000000; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 371 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 372 | if (mm->p_tmsi == ptmsi) { |
| 373 | if (!max_retries--) |
| 374 | goto failed; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 375 | goto restart; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 376 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return ptmsi; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 380 | |
| 381 | failed: |
| 382 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 383 | return GSM_RESERVED_TMSI; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 384 | } |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 385 | |
| 386 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 387 | { |
| 388 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 389 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 390 | else { |
| 391 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 392 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 393 | "recovery\n"); |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 394 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 395 | sgsn_pdp_ctx_free(pdp); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* 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] | 400 | * otherwise lost state (recovery procedure) */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 401 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 402 | { |
| 403 | struct sgsn_mm_ctx *mm; |
| 404 | int num = 0; |
| 405 | |
| 406 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 407 | struct sgsn_pdp_ctx *pdp; |
| 408 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 409 | if (pdp->ggsn == ggsn) { |
| 410 | drop_one_pdp(pdp); |
| 411 | num++; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return num; |
| 417 | } |