Harald Welte | 75bb820 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 75bb820 | 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 | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 28 | #include <osmocore/rate_ctr.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 31 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 32 | #include <openbsc/gprs_ns.h> |
| 33 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | |
Harald Welte | c1f6bfe | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 39 | |
Harald Welte | 8a035af | 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 | 75bb820 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 69 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 75bb820 | 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 | d85d9a9 | 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 | 75bb820 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 106 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 107 | const struct gprs_ra_id *raid) |
| 108 | { |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 109 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 110 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 111 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 75bb820 | 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 | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 118 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | ded83ec | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 119 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 75bb820 | 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 | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 125 | |
| 126 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 127 | uint8_t nsapi) |
| 128 | { |
| 129 | struct sgsn_pdp_ctx *pdp; |
| 130 | |
| 131 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 132 | if (pdp->nsapi == nsapi) |
| 133 | return pdp; |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 139 | uint8_t nsapi) |
| 140 | { |
| 141 | struct sgsn_pdp_ctx *pdp; |
| 142 | |
| 143 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 144 | if (pdp) |
| 145 | return NULL; |
| 146 | |
| 147 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 148 | if (!pdp) |
| 149 | return NULL; |
| 150 | |
| 151 | pdp->mm = mm; |
| 152 | pdp->nsapi = nsapi; |
| 153 | llist_add(&pdp->list, &mm->pdp_list); |
| 154 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 155 | |
| 156 | return pdp; |
| 157 | } |
| 158 | |
| 159 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 160 | { |
| 161 | llist_del(&pdp->list); |
| 162 | llist_del(&pdp->g_list); |
| 163 | talloc_free(pdp); |
| 164 | } |
| 165 | |
| 166 | /* GGSN contexts */ |
| 167 | |
| 168 | struct ggsn_ctx *ggsn_ctx_alloc(uint32_t id) |
| 169 | { |
| 170 | struct ggsn_ctx *ggc; |
| 171 | |
| 172 | ggc = talloc_zero(tall_bsc_ctx, struct ggsn_ctx); |
| 173 | if (!ggc) |
| 174 | return NULL; |
| 175 | |
| 176 | ggc->id = id; |
| 177 | ggc->gtp_version = 1; |
| 178 | |
| 179 | return ggc; |
| 180 | } |
| 181 | |
| 182 | struct ggsn_ctx *ggsn_ctx_by_id(uint32_t id) |
| 183 | { |
| 184 | struct ggsn_ctx *ggc; |
| 185 | |
| 186 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 187 | if (id == ggc->id) |
| 188 | return ggc; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | struct ggsn_ctx *ggsn_ctx_find_alloc(uint32_t id) |
| 194 | { |
| 195 | struct ggsn_ctx *ggc; |
| 196 | |
| 197 | ggc = ggsn_ctx_by_id(id); |
| 198 | if (!ggc) |
| 199 | ggc = ggsn_ctx_alloc(id); |
| 200 | return ggc; |
| 201 | } |
| 202 | |
| 203 | /* APN contexts */ |
| 204 | |
| 205 | #if 0 |
| 206 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 207 | { |
| 208 | struct apn_ctx *actx; |
| 209 | |
| 210 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 211 | if (!actx) |
| 212 | return NULL; |
| 213 | actx->name = talloc_strdup(actx, ap_name); |
| 214 | |
| 215 | return actx; |
| 216 | } |
| 217 | |
| 218 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 219 | { |
| 220 | struct apn_ctx *actx; |
| 221 | |
| 222 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 223 | if (!strcmp(name, actx->name)) |
| 224 | return actx; |
| 225 | } |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 230 | { |
| 231 | struct apn_ctx *actx; |
| 232 | |
| 233 | actx = apn_ctx_by_name(name); |
| 234 | if (!actx) |
| 235 | actx = apn_ctx_alloc(name); |
| 236 | |
| 237 | return actx; |
| 238 | } |
| 239 | #endif |