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 |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 24 | |
| 25 | #include <osmocore/linuxlist.h> |
| 26 | #include <osmocore/talloc.h> |
| 27 | #include <osmocore/timer.h> |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 28 | #include <osmocore/rate_ctr.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 31 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 32 | #include <openbsc/gprs_ns.h> |
| 33 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 35 | LLIST_HEAD(sgsn_mm_ctxts); |
| 36 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 37 | LLIST_HEAD(sgsn_apn_ctxts); |
| 38 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 39 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 40 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 41 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 42 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 43 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 44 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 45 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 46 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 47 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 48 | { "suspend", "SUSPEND Count " }, |
| 49 | { "paging.ps", "Paging Packet Switched " }, |
| 50 | { "paging.cs", "Paging Circuit Switched " }, |
| 51 | { "ra_update", "Routing Area Update " }, |
| 52 | }; |
| 53 | |
| 54 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 55 | .group_name_prefix = "sgsn.mmctx", |
| 56 | .group_description = "SGSN MM Context Statistics", |
| 57 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 58 | .ctr_desc = mmctx_ctr_description, |
| 59 | }; |
| 60 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 61 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 62 | const struct gprs_ra_id *id2) |
| 63 | { |
| 64 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 65 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 66 | } |
| 67 | |
| 68 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 69 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 70 | const struct gprs_ra_id *raid) |
| 71 | { |
| 72 | struct sgsn_mm_ctx *ctx; |
| 73 | |
| 74 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 75 | if (tlli == ctx->tlli && |
| 76 | ra_id_equals(raid, &ctx->ra)) |
| 77 | return ctx; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | struct sgsn_mm_ctx *ctx; |
| 85 | |
| 86 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 87 | if (p_tmsi == ctx->p_tmsi) |
| 88 | return ctx; |
| 89 | } |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 94 | { |
| 95 | struct sgsn_mm_ctx *ctx; |
| 96 | |
| 97 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 98 | if (!strcmp(imsi, ctx->imsi)) |
| 99 | return ctx; |
| 100 | } |
| 101 | return NULL; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /* Allocate a new SGSN MM context */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 106 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 107 | const struct gprs_ra_id *raid) |
| 108 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 109 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 110 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 111 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 112 | if (!ctx) |
| 113 | return NULL; |
| 114 | |
| 115 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 116 | ctx->tlli = tlli; |
| 117 | ctx->mm_state = GMM_DEREGISTERED; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 118 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 119 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 120 | |
| 121 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 122 | |
| 123 | return ctx; |
| 124 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 125 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 126 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 127 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 128 | uint8_t nsapi) |
| 129 | { |
| 130 | struct sgsn_pdp_ctx *pdp; |
| 131 | |
| 132 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 133 | if (pdp->nsapi == nsapi) |
| 134 | return pdp; |
| 135 | } |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 139 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 140 | uint8_t tid) |
| 141 | { |
| 142 | struct sgsn_pdp_ctx *pdp; |
| 143 | |
| 144 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 145 | if (pdp->ti == tid) |
| 146 | return pdp; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 152 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 153 | uint8_t nsapi) |
| 154 | { |
| 155 | struct sgsn_pdp_ctx *pdp; |
| 156 | |
| 157 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 158 | if (pdp) |
| 159 | return NULL; |
| 160 | |
| 161 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 162 | if (!pdp) |
| 163 | return NULL; |
| 164 | |
| 165 | pdp->mm = mm; |
| 166 | pdp->nsapi = nsapi; |
| 167 | llist_add(&pdp->list, &mm->pdp_list); |
| 168 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 169 | |
| 170 | return pdp; |
| 171 | } |
| 172 | |
| 173 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 174 | { |
| 175 | llist_del(&pdp->list); |
| 176 | llist_del(&pdp->g_list); |
| 177 | talloc_free(pdp); |
| 178 | } |
| 179 | |
| 180 | /* GGSN contexts */ |
| 181 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 182 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 183 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 184 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 185 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 186 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 187 | if (!ggc) |
| 188 | return NULL; |
| 189 | |
| 190 | ggc->id = id; |
| 191 | ggc->gtp_version = 1; |
| 192 | |
| 193 | return ggc; |
| 194 | } |
| 195 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 196 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 197 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 198 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 199 | |
| 200 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 201 | if (id == ggc->id) |
| 202 | return ggc; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 207 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 208 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 209 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 210 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 211 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 212 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame^] | 213 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 214 | return ggc; |
| 215 | } |
| 216 | |
| 217 | /* APN contexts */ |
| 218 | |
| 219 | #if 0 |
| 220 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 221 | { |
| 222 | struct apn_ctx *actx; |
| 223 | |
| 224 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 225 | if (!actx) |
| 226 | return NULL; |
| 227 | actx->name = talloc_strdup(actx, ap_name); |
| 228 | |
| 229 | return actx; |
| 230 | } |
| 231 | |
| 232 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 233 | { |
| 234 | struct apn_ctx *actx; |
| 235 | |
| 236 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 237 | if (!strcmp(name, actx->name)) |
| 238 | return actx; |
| 239 | } |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 244 | { |
| 245 | struct apn_ctx *actx; |
| 246 | |
| 247 | actx = apn_ctx_by_name(name); |
| 248 | if (!actx) |
| 249 | actx = apn_ctx_alloc(name); |
| 250 | |
| 251 | return actx; |
| 252 | } |
| 253 | #endif |