blob: 52b1c0ff0a83e8ad4cecc9c42147e9f0906b383a [file] [log] [blame]
Pau Espin Pedrol05190c32023-01-05 20:13:13 +01001/* PDP context 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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
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/>.
19 *
20 */
21
22#include <stdint.h>
23
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>
28#include <osmocom/core/stats.h>
29
30#include <osmocom/sgsn/pdpctx.h>
31#include <osmocom/sgsn/gprs_sgsn.h>
32#include <osmocom/sgsn/sgsn.h>
33#include <osmocom/sgsn/debug.h>
34#include <osmocom/sgsn/signal.h>
35#include <osmocom/sgsn/gtp_ggsn.h>
36#include <osmocom/sgsn/gprs_llc_xid.h>
37#include <osmocom/sgsn/gprs_sndcp.h>
38#include <osmocom/sgsn/gprs_llc.h>
39#include <osmocom/sgsn/gprs_sm.h>
40#include <osmocom/sgsn/gtp.h>
41
42LLIST_HEAD(sgsn_pdp_ctxts);
43
44static const struct rate_ctr_desc pdpctx_ctr_description[] = {
45 { "udata:packets:in", "User Data Messages ( In)" },
46 { "udata:packets:out", "User Data Messages (Out)" },
47 { "udata:bytes:in", "User Data Bytes ( In)" },
48 { "udata:bytes:out", "User Data Bytes (Out)" },
49};
50
51static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
52 .group_name_prefix = "sgsn:pdpctx",
53 .group_description = "SGSN PDP Context Statistics",
54 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
55 .ctr_desc = pdpctx_ctr_description,
56 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
57};
58
59/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
60struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
61 struct sgsn_ggsn_ctx *ggsn,
62 uint8_t nsapi)
63{
64 struct sgsn_pdp_ctx *pdp;
65
66 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
67 if (pdp)
68 return NULL;
69
70 pdp = talloc_zero(tall_sgsn_ctx, struct sgsn_pdp_ctx);
71 if (!pdp)
72 return NULL;
73
74 pdp->mm = mm;
75 pdp->ggsn = ggsn;
76 pdp->nsapi = nsapi;
77 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
78 if (!pdp->ctrg) {
79 LOGPDPCTXP(LOGL_ERROR, pdp, "Error allocation counter group\n");
80 talloc_free(pdp);
81 return NULL;
82 }
83 llist_add(&pdp->list, &mm->pdp_list);
84 sgsn_ggsn_ctx_add_pdp(pdp->ggsn, pdp);
85 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
86
87 return pdp;
88}
89
90/*
91 * This function will not trigger any GSM DEACT PDP ACK messages, so you
92 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
93 * isn't detached already.
94 */
95void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
96{
97 struct sgsn_signal_data sig_data;
98
99 OSMO_ASSERT(pdp->mm != NULL);
100
101 /* There might still be pending callbacks in libgtp. So the parts of
102 * this object relevant to GTP need to remain intact in this case. */
103
104 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
105
106 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
107 /* Force the deactivation of the SNDCP layer */
108 if (pdp->mm->gb.llme)
109 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
110 }
111
112 memset(&sig_data, 0, sizeof(sig_data));
113 sig_data.pdp = pdp;
114 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
115
116 /* Detach from MM context */
117 pdp_ctx_detach_mm_ctx(pdp);
118 if (pdp->ggsn)
119 sgsn_delete_pdp_ctx(pdp);
120}
121
122/*
123 * Don't call this function directly unless you know what you are doing.
124 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
125 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
126 */
127void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
128{
129 struct sgsn_signal_data sig_data;
130
131 memset(&sig_data, 0, sizeof(sig_data));
132 sig_data.pdp = pdp;
133 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
134
135 if (osmo_timer_pending(&pdp->timer)) {
136 LOGPDPCTXP(LOGL_ERROR, pdp, "Freeing PDP ctx with timer %u pending\n", pdp->T);
137 osmo_timer_del(&pdp->timer);
138 }
139
140 rate_ctr_group_free(pdp->ctrg);
141 if (pdp->mm)
142 llist_del(&pdp->list);
143 if (pdp->ggsn)
144 sgsn_ggsn_ctx_remove_pdp(pdp->ggsn, pdp);
145 llist_del(&pdp->g_list);
146
147 /* _if_ we still have a library handle, at least set it to NULL
148 * to avoid any dereferences of the now-deleted PDP context from
149 * sgsn_libgtp:cb_data_ind() */
150 if (pdp->lib) {
151 struct pdp_t *lib = pdp->lib;
152 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
153 "has a libgtp handle attached to it, this shouldn't "
154 "happen!\n");
155 osmo_generate_backtrace();
156 lib->priv = NULL;
157 }
158
159 talloc_free(pdp);
160}