blob: 32fb8e49d16fc5da8ac8d710e1d72b29f18a9266 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte9f1f3ad2010-05-02 12:56:57 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltec2e8cc42010-05-31 20:23:38 +02005 * (C) 2010 by On-Waves
Harald Welte9b455bf2010-03-14 15:45:01 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080028#include <errno.h>
29
30#include <netinet/in.h>
Harald Welte421cba42010-05-02 23:11:50 +020031#include <arpa/inet.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080032
33#include <openbsc/db.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/msgb.h>
Harald Welted36ff762011-03-23 18:26:56 +010035#include <osmocom/gsm/tlv.h>
36#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/signal.h>
38#include <osmocom/core/talloc.h>
39#include <osmocom/core/rate_ctr.h>
Harald Welted6057092010-05-02 12:57:45 +020040
Harald Welteea34a4e2012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_bssgp.h>
42
Harald Welte9b455bf2010-03-14 15:45:01 +080043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080045#include <openbsc/gsm_subscriber.h>
46#include <openbsc/gsm_04_08.h>
47#include <openbsc/gsm_04_08_gprs.h>
48#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080049#include <openbsc/transaction.h>
50#include <openbsc/gprs_llc.h>
51#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020052#include <openbsc/gprs_gmm.h>
Harald Welted193cb32010-05-17 22:58:03 +020053#include <openbsc/sgsn.h>
54
Harald Welte6abf94e2010-05-18 10:35:06 +020055#include <pdp.h>
56
Harald Weltec2e8cc42010-05-31 20:23:38 +020057#define PTMSI_ALLOC
58
Harald Welte649e1ff2013-07-21 17:41:46 +080059/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020060#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
61#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
62#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
63#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
64
Harald Welte649e1ff2013-07-21 17:41:46 +080065/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020066#define GSM0408_T3313_SECS 30 /* waiting for paging response */
67#define GSM0408_T3314_SECS 44 /* force to STBY on expiry */
68#define GSM0408_T3316_SECS 44
69
Harald Weltea9b473a2010-12-24 21:13:26 +010070/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
71#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
72#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
73#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
74#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
75
Harald Welted193cb32010-05-17 22:58:03 +020076extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080077
Harald Welte588d44e2010-06-09 10:28:29 +020078static const struct tlv_definition gsm48_gmm_att_tlvdef = {
79 .def = {
Harald Welte4b6ac1e2010-07-03 11:09:06 +020080 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welte588d44e2010-06-09 10:28:29 +020081 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
82 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020083 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
84 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
85 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welte588d44e2010-06-09 10:28:29 +020086 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020087 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welte588d44e2010-06-09 10:28:29 +020088 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
89 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
90 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
91 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
92 },
93};
94
95static const struct tlv_definition gsm48_sm_att_tlvdef = {
96 .def = {
97 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
98 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
99 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
100 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
101 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
102 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200103 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
104 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welte588d44e2010-06-09 10:28:29 +0200105 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
106 },
107};
108
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100109static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx);
110
Harald Welte2720e732010-05-17 00:44:57 +0200111/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800112
Harald Weltec2e8cc42010-05-31 20:23:38 +0200113static void mmctx_timer_cb(void *_mm);
114
115static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
116 unsigned int seconds)
117{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200118 if (osmo_timer_pending(&mm->timer))
Harald Weltec2e8cc42010-05-31 20:23:38 +0200119 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
120 "timer %u pending\n", T, mm->T);
121 mm->T = T;
122 mm->num_T_exp = 0;
123
124 /* FIXME: we should do this only once ? */
125 mm->timer.data = mm;
126 mm->timer.cb = &mmctx_timer_cb;
127
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200128 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200129}
130
131static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
132{
133 if (mm->T != T)
134 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
135 "%u is running\n", T, mm->T);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200136 osmo_timer_del(&mm->timer);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200137}
138
Harald Welte9b455bf2010-03-14 15:45:01 +0800139/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200140static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200141 struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800142{
Harald Welte8acd88f2010-05-18 10:57:45 +0200143 if (mm)
144 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
145
Harald Welte11d7c102010-05-02 11:54:55 +0200146 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte56a01452010-05-31 22:12:30 +0200147 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800148}
149
Harald Welte11d7c102010-05-02 11:54:55 +0200150/* copy identifiers from old message to new message, this
151 * is required so lower layers can route it correctly */
152static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
153{
154 msgb_tlli(msg) = msgb_tlli(old);
155 msgb_bvci(msg) = msgb_bvci(old);
156 msgb_nsei(msg) = msgb_nsei(old);
157}
158
Harald Welte6abf94e2010-05-18 10:35:06 +0200159/* Store BVCI/NSEI in MM context */
160static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
161{
162 mm->bvci = msgb_bvci(msg);
163 mm->nsei = msgb_nsei(msg);
164}
165
166/* Store BVCI/NSEI in MM context */
167static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
168{
169 msgb_tlli(msg) = mm->tlli;
170 msgb_bvci(msg) = mm->bvci;
171 msgb_nsei(msg) = mm->nsei;
172}
173
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200174static void delete_pdp_contexts(struct sgsn_mm_ctx *ctx, const char *log_text)
175{
176 struct sgsn_pdp_ctx *pdp, *pdp2;
177
178 /* delete all existing PDP contexts for this MS */
179 llist_for_each_entry_safe(pdp, pdp2, &ctx->pdp_list, list) {
180 LOGMMCTXP(LOGL_NOTICE, ctx,
181 "Dropping PDP context for NSAPI=%u due to %s\n",
182 pdp->nsapi, log_text);
183 sgsn_pdp_ctx_terminate(pdp);
184 }
185}
186
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200187static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text)
188{
189 struct gprs_llc_llme *llme = ctx->llme;
190 uint32_t tlli = ctx->tlli;
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200191
192 /* Mark MM state as deregistered */
193 ctx->mm_state = GMM_DEREGISTERED;
194
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200195 delete_pdp_contexts(ctx, log_text);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200196
197 sgsn_mm_ctx_free(ctx);
198 ctx = NULL;
199
200 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
201 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
202}
203
Harald Weltef54e7e22010-06-09 10:51:23 +0200204/* Chapter 9.4.18 */
205static int _tx_status(struct msgb *msg, uint8_t cause,
206 struct sgsn_mm_ctx *mmctx, int sm)
207{
208 struct gsm48_hdr *gh;
209
210 /* MMCTX might be NULL! */
211
212 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100213 get_value_string(gsm48_gmm_cause_names, cause));
Harald Weltef54e7e22010-06-09 10:51:23 +0200214
215 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
216 if (sm) {
217 gh->proto_discr = GSM48_PDISC_SM_GPRS;
218 gh->msg_type = GSM48_MT_GSM_STATUS;
219 } else {
220 gh->proto_discr = GSM48_PDISC_MM_GPRS;
221 gh->msg_type = GSM48_MT_GMM_STATUS;
222 }
223 gh->data[0] = cause;
224
225 return gsm48_gmm_sendmsg(msg, 0, mmctx);
226}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100227
Harald Weltef54e7e22010-06-09 10:51:23 +0200228static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
229{
230 struct msgb *msg = gsm48_msgb_alloc();
231
232 mmctx2msgid(msg, mmctx);
233 return _tx_status(msg, cause, mmctx, 0);
Harald Weltef54e7e22010-06-09 10:51:23 +0200234}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100235
Harald Weltef54e7e22010-06-09 10:51:23 +0200236static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
237{
238 struct msgb *msg = gsm48_msgb_alloc();
239
240 mmctx2msgid(msg, mmctx);
241 return _tx_status(msg, cause, mmctx, 1);
Harald Weltef54e7e22010-06-09 10:51:23 +0200242}
243
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200244static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause,
245 struct sgsn_mm_ctx *mmctx)
246{
247 struct gsm48_hdr *gh;
248
249 /* MMCTX might be NULL! */
250
251 DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n",
252 get_value_string(gprs_det_t_mt_strs, detach_type),
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100253 get_value_string(gsm48_gmm_cause_names, cause));
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200254
255 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
256
257 gh->proto_discr = GSM48_PDISC_MM_GPRS;
258 gh->msg_type = GSM48_MT_GMM_DETACH_REQ;
259 gh->data[0] = detach_type & 0x07;
260
261 msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause);
262
263 return gsm48_gmm_sendmsg(msg, 0, mmctx);
264}
265
266static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx,
267 uint8_t detach_type, uint8_t cause)
268{
269 struct msgb *msg = gsm48_msgb_alloc();
270
271 mmctx2msgid(msg, mmctx);
272 return _tx_detach_req(msg, detach_type, cause, mmctx);
273}
274
275static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg,
276 uint8_t detach_type, uint8_t cause)
277{
278 struct msgb *msg = gsm48_msgb_alloc();
279
280 gmm_copy_id(msg, oldmsg);
281 return _tx_detach_req(msg, detach_type, cause, NULL);
282}
Harald Weltef54e7e22010-06-09 10:51:23 +0200283
Harald Welte86fda902010-05-03 10:54:35 +0200284static struct gsm48_qos default_qos = {
285 .delay_class = 4, /* best effort */
286 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
287 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
288 .preced_class = GSM48_QOS_PC_NORMAL,
289 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
290 .traf_class = GSM48_QOS_TC_INTERACTIVE,
291 .deliv_order = GSM48_QOS_DO_UNORDERED,
292 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
293 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
294 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
295 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
296 .resid_ber = GSM48_QOS_RBER_5e_2,
297 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
298 .handling_prio = 3,
299 .xfer_delay = 0x10, /* 200ms */
300 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
301 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
302 .sig_ind = 0, /* not optimised for signalling */
303 .max_bitrate_down_ext = 0, /* use octet 9 */
304 .guar_bitrate_down_ext = 0, /* use octet 13 */
305};
306
Harald Welte9b455bf2010-03-14 15:45:01 +0800307/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200308static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800309{
310 struct msgb *msg = gsm48_msgb_alloc();
311 struct gsm48_hdr *gh;
312 struct gsm48_attach_ack *aa;
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200313 uint8_t *mid;
314#if 0
315 uint8_t *ptsig;
316#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800317
Daniel Willmann46553142014-09-03 17:46:44 +0200318 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800319
Harald Welte6463c072010-05-18 17:04:55 +0200320 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800321
322 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
323 gh->proto_discr = GSM48_PDISC_MM_GPRS;
324 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
325
326 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
327 aa->force_stby = 0; /* not indicated */
328 aa->att_result = 1; /* GPRS only */
329 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
330 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200331 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800332
Harald Welteab1d5622010-05-18 19:58:38 +0200333#if 0
Harald Welte6463c072010-05-18 17:04:55 +0200334 /* Optional: P-TMSI signature */
335 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
336 ptsig = msgb_put(msg, 3);
337 ptsig[0] = mm->p_tmsi_sig >> 16;
338 ptsig[1] = mm->p_tmsi_sig >> 8;
339 ptsig[2] = mm->p_tmsi_sig & 0xff;
340
341 /* Optional: Negotiated Ready timer value */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200342#endif
Harald Welte6463c072010-05-18 17:04:55 +0200343
Harald Weltec2e8cc42010-05-31 20:23:38 +0200344#ifdef PTMSI_ALLOC
Harald Welte6463c072010-05-18 17:04:55 +0200345 /* Optional: Allocated P-TMSI */
Harald Welte6463c072010-05-18 17:04:55 +0200346 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
347 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteab1d5622010-05-18 19:58:38 +0200348 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
349#endif
Harald Weltec2e8cc42010-05-31 20:23:38 +0200350
Harald Welte6463c072010-05-18 17:04:55 +0200351 /* Optional: MS-identity (combined attach) */
352 /* Optional: GMM cause (partial attach result for combined attach) */
353
Harald Welte56a01452010-05-31 22:12:30 +0200354 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800355}
356
357/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200358static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800359{
Harald Welte9b455bf2010-03-14 15:45:01 +0800360 struct gsm48_hdr *gh;
361
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100362 LOGP(DMM, LOGL_NOTICE, "<- GPRS ATTACH REJECT: %s\n", get_value_string(gsm48_gmm_cause_names, gmm_cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800363
Harald Welte9b455bf2010-03-14 15:45:01 +0800364 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
365 gh->proto_discr = GSM48_PDISC_MM_GPRS;
366 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
367 gh->data[0] = gmm_cause;
368
Harald Welte8acd88f2010-05-18 10:57:45 +0200369 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800370}
Harald Welte6463c072010-05-18 17:04:55 +0200371static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
372 uint8_t gmm_cause)
373{
374 struct msgb *msg = gsm48_msgb_alloc();
375 gmm_copy_id(msg, old_msg);
376 return _tx_gmm_att_rej(msg, gmm_cause);
377}
378static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
379 uint8_t gmm_cause)
380{
381 struct msgb *msg = gsm48_msgb_alloc();
382 mmctx2msgid(msg, mm);
383 return _tx_gmm_att_rej(msg, gmm_cause);
384}
Harald Welte9b455bf2010-03-14 15:45:01 +0800385
Harald Welte2720e732010-05-17 00:44:57 +0200386/* Chapter 9.4.6.2 Detach accept */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200387static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby,
388 struct sgsn_mm_ctx *mm)
Harald Welte2720e732010-05-17 00:44:57 +0200389{
Harald Welte2720e732010-05-17 00:44:57 +0200390 struct gsm48_hdr *gh;
391
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200392 /* MMCTX might be NULL! */
Harald Welte2720e732010-05-17 00:44:57 +0200393
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200394 DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby);
Harald Welte6463c072010-05-18 17:04:55 +0200395
Harald Welte2720e732010-05-17 00:44:57 +0200396 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
397 gh->proto_discr = GSM48_PDISC_MM_GPRS;
398 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
399 gh->data[0] = force_stby;
400
Harald Welte56a01452010-05-31 22:12:30 +0200401 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200402}
403
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200404static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
405{
406 struct msgb *msg = gsm48_msgb_alloc();
407
408 mmctx2msgid(msg, mm);
409 return _tx_detach_ack(msg, force_stby, mm);
410}
411
412static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby)
413{
414 struct msgb *msg = gsm48_msgb_alloc();
415
416 gmm_copy_id(msg, oldmsg);
417 return _tx_detach_ack(msg, force_stby, NULL);
418}
419
Harald Welte9b455bf2010-03-14 15:45:01 +0800420/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200421static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800422{
423 struct msgb *msg = gsm48_msgb_alloc();
424 struct gsm48_hdr *gh;
425
Daniel Willmann46553142014-09-03 17:46:44 +0200426 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800427
Harald Welte6463c072010-05-18 17:04:55 +0200428 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800429
430 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
431 gh->proto_discr = GSM48_PDISC_MM_GPRS;
432 gh->msg_type = GSM48_MT_GMM_ID_REQ;
433 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
434 gh->data[0] = id_type & 0xf;
435
Harald Weltedce12552010-05-31 22:31:04 +0200436 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800437}
438
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200439/* Section 9.4.9: Authentication and Ciphering Request */
440static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
441 uint8_t key_seq, uint8_t algo)
442{
443 struct msgb *msg = gsm48_msgb_alloc();
444 struct gsm48_hdr *gh;
445 struct gsm48_auth_ciph_req *acreq;
446 uint8_t *m_rand, *m_cksn;
447
Daniel Willmann46553142014-09-03 17:46:44 +0200448 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200449 osmo_hexdump(rand, 16));
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200450
451 mmctx2msgid(msg, mm);
452
453 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
454 gh->proto_discr = GSM48_PDISC_MM_GPRS;
455 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
456
457 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
458 acreq->ciph_alg = algo & 0xf;
459 acreq->imeisv_req = 0x1;
460 acreq->force_stby = 0x0;
461 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
462
463 /* Only if authentication is requested we need to set RAND + CKSN */
464 if (rand) {
465 m_rand = msgb_put(msg, 16+1);
466 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
467 memcpy(m_rand+1, rand, 16);
468
Jacob Erlbeck65d82732014-12-01 10:36:17 +0100469 m_cksn = msgb_put(msg, 1);
470 m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200471 }
472
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200473 /* FIXME: make sure we don't send any other messages to the MS */
474
475 return gsm48_gmm_sendmsg(msg, 1, mm);
476}
477
478/* Section 9.4.11: Authentication and Ciphering Reject */
479static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
480{
481 struct msgb *msg = gsm48_msgb_alloc();
482 struct gsm48_hdr *gh;
483
Daniel Willmann46553142014-09-03 17:46:44 +0200484 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200485
486 mmctx2msgid(msg, mm);
487
488 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
489 gh->proto_discr = GSM48_PDISC_MM_GPRS;
490 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
491
492 return gsm48_gmm_sendmsg(msg, 0, mm);
493}
494
495/* Section 9.4.10: Authentication and Ciphering Response */
496static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
497 struct msgb *msg)
498{
499 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
500 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
501 struct tlv_parsed tp;
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100502 struct gsm_auth_tuple *at;
503 int rc;
504
505 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n");
506
507 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
508 LOGMMCTXP(LOGL_NOTICE, ctx,
509 "Unexpected Auth & Ciph Response (ignored)\n");
510 return 0;
511 }
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200512
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100513 /* Stop T3360 */
514 mmctx_timer_stop(ctx, 3360);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200515
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200516 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200517 (msg->data + msg->len) - acr->data, 0, 0);
518
519 /* FIXME: compare ac_ref? */
520
521 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
522 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100523 /* TODO: missing mandatory IE, return STATUS or REJ? */
524 LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n");
525 return -EINVAL;
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200526 }
527
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100528 /* Compare SRES with what we expected */
529 LOGMMCTXP(LOGL_DEBUG, ctx, "checking received auth info, SRES = %s\n",
530 osmo_hexdump(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES),
531 TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES)));
532
533 at = &ctx->auth_triplet;
534
535 if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES) != sizeof(at->sres) ||
536 memcmp(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), at->sres,
537 sizeof(at->sres)) != 0) {
538
539 LOGMMCTXP(LOGL_NOTICE, ctx, "Received SRES doesn't match\n");
540 rc = gsm48_tx_gmm_auth_ciph_rej(ctx);
541 mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT");
542 return rc;
543 }
544
545 ctx->is_authenticated = 1;
546
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200547 /* FIXME: enable LLC cipheirng */
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100548
549 /* Check if we can let the mobile station enter */
550 return gsm48_gmm_authorize(ctx);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200551}
552
Harald Welte9b455bf2010-03-14 15:45:01 +0800553/* Check if we can already authorize a subscriber */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100554static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800555{
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100556 /* Request IMSI and IMEI from the MS if they are unknown */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200557 if (!strlen(ctx->imei)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200558 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
559 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200560 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200561 }
Harald Weltec2e8cc42010-05-31 20:23:38 +0200562 if (!strlen(ctx->imsi)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200563 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
564 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200565 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200566 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800567
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100568 /* All information required for authentication is available */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100569 ctx->t3370_id_type = GSM_MI_TYPE_NONE;
570
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200571 if (ctx->auth_state == SGSN_AUTH_UNKNOWN) {
572 /* Request authorization, this leads to a call to
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100573 * sgsn_auth_update which in turn calls
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200574 * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */
575
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100576 sgsn_auth_request(ctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200577 /* Note that gsm48_gmm_authorize can be called recursively via
578 * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED
579 */
580 return 0;
581 }
582
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100583 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) {
584 struct gsm_auth_tuple *at = &ctx->auth_triplet;
585
586 mmctx_timer_start(ctx, 3360, GSM0408_T3360_SECS);
587 return gsm48_tx_gmm_auth_ciph_req(ctx, at->rand, at->key_seq,
588 GPRS_ALGO_GEA0);
589 }
590
591 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated &&
592 ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
593 /* Check again for authorization */
594 sgsn_auth_request(ctx);
595 return 0;
596 }
597
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200598 if (ctx->auth_state != SGSN_AUTH_ACCEPTED) {
599 LOGMMCTXP(LOGL_NOTICE, ctx,
600 "authorization is denied, aborting procedure\n");
601 return -EACCES;
Jacob Erlbeck0c06f982014-10-29 22:12:20 +0100602 }
603
604 /* The MS is authorized */
605
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100606 switch (ctx->pending_req) {
607 case 0:
608 LOGMMCTXP(LOGL_INFO, ctx,
609 "no pending request, authorization completed\n");
610 break;
611 case GSM48_MT_GMM_ATTACH_REQ:
612#ifdef PTMSI_ALLOC
613 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
614 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
615 ctx->t3350_mode = GMM_T3350_MODE_ATT;
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100616#else
617 ctx->mm_state = GMM_REGISTERED_NORMAL;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100618#endif
619
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100620 return gsm48_tx_gmm_att_ack(ctx);
621 default:
622 LOGMMCTXP(LOGL_ERROR, ctx,
623 "only Attach Request is supported yet, "
624 "got request type %u\n", ctx->pending_req);
625 break;
626 }
627
Harald Welte9b455bf2010-03-14 15:45:01 +0800628 return 0;
629}
630
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100631void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx)
632{
633 ctx->is_authenticated = 0;
634
635 gsm48_gmm_authorize(ctx);
636}
637
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200638void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)
639{
640 switch (ctx->mm_state) {
641 case GMM_COMMON_PROC_INIT:
642 LOGP(DMM, LOGL_NOTICE,
643 "Authorized, continuing procedure, IMSI=%s\n",
644 ctx->imsi);
645 /* Continue with the authorization */
646 gsm48_gmm_authorize(ctx);
647 break;
648 default:
649 LOGP(DMM, LOGL_INFO,
650 "Authorized, ignored, IMSI=%s\n",
651 ctx->imsi);
652 }
653}
654
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100655void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200656{
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100657 if (gmm_cause == SGSN_ERROR_CAUSE_NONE)
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100658 gmm_cause = GMM_CAUSE_GPRS_NOTALLOWED;
659
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200660 switch (ctx->mm_state) {
661 case GMM_COMMON_PROC_INIT:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100662 LOGMMCTXP(LOGL_NOTICE, ctx,
663 "Not authorized, rejecting ATTACH REQUEST "
664 "with cause '%s' (%d)\n",
665 get_value_string(gsm48_gmm_cause_names, gmm_cause),
666 gmm_cause);
667 gsm48_tx_gmm_att_rej(ctx, gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200668 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");
669 break;
670 case GMM_REGISTERED_NORMAL:
671 case GMM_REGISTERED_SUSPENDED:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100672 LOGMMCTXP(LOGL_NOTICE, ctx,
673 "Authorization lost, detaching "
674 "with cause '%s' (%d)\n",
675 get_value_string(gsm48_gmm_cause_names, gmm_cause),
676 gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200677 gsm48_tx_gmm_detach_req(
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100678 ctx, GPRS_DET_T_MT_REATT_NOTREQ, gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200679
680 mm_ctx_cleanup_free(ctx, "auth lost");
681 break;
682 default:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100683 LOGMMCTXP(LOGL_INFO, ctx,
684 "Authorization lost, cause is '%s' (%d)\n",
685 get_value_string(gsm48_gmm_cause_names, gmm_cause),
686 gmm_cause);
687 mm_ctx_cleanup_free(ctx, "auth lost");
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200688 }
689}
690
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100691void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100692{
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100693 if (gmm_cause != SGSN_ERROR_CAUSE_NONE) {
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100694 LOGMMCTXP(LOGL_INFO, ctx,
695 "Cancelled with cause '%s' (%d), deleting context\n",
696 get_value_string(gsm48_gmm_cause_names, gmm_cause),
697 gmm_cause);
698 gsm0408_gprs_access_denied(ctx, gmm_cause);
699 return;
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100700 }
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100701
702 LOGMMCTXP(LOGL_INFO, ctx, "Cancelled, deleting context silently\n");
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100703 mm_ctx_cleanup_free(ctx, "access cancelled");
704}
705
Harald Welte9b455bf2010-03-14 15:45:01 +0800706/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200707static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800708{
Harald Welte943c5bc2010-04-30 16:33:12 +0200709 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200710 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800711 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800712
713 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte9b455bf2010-03-14 15:45:01 +0800714 if (!ctx) {
Daniel Willmann46553142014-09-03 17:46:44 +0200715 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800716 return -EINVAL;
717 }
718
Daniel Willmannf8070f42014-09-23 18:48:44 +0200719 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
720 mi_type, mi_string);
721
Jacob Erlbeckfb26c602014-10-22 12:20:26 +0200722 if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) {
723 LOGMMCTXP(LOGL_NOTICE, ctx,
724 "Got unexpected IDENTITY RESPONSE: mi_type=0x%02x MI(%s), "
725 "ignoring message\n",
726 mi_type, mi_string);
727 return -EINVAL;
728 }
729
Harald Weltec2e8cc42010-05-31 20:23:38 +0200730 if (mi_type == ctx->t3370_id_type)
731 mmctx_timer_stop(ctx, 3370);
732
Harald Welte9b455bf2010-03-14 15:45:01 +0800733 switch (mi_type) {
734 case GSM_MI_TYPE_IMSI:
735 /* we already have a mm context with current TLLI, but no
736 * P-TMSI / IMSI yet. What we now need to do is to fill
737 * this initial context with data from the HLR */
Harald Weltec728eea2010-12-24 23:07:18 +0100738 if (strlen(ctx->imsi) == 0) {
739 /* Check if we already have a MM context for this IMSI */
740 struct sgsn_mm_ctx *ictx;
741 ictx = sgsn_mm_ctx_by_imsi(mi_string);
742 if (ictx) {
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200743 /* Handle it like in gsm48_rx_gmm_det_req,
744 * except that no messages are sent to the BSS */
745
Daniel Willmann46553142014-09-03 17:46:44 +0200746 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
747 "p_tmsi_old=0x%08x\n",
748 ictx->p_tmsi);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200749
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200750 mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
Harald Weltec728eea2010-12-24 23:07:18 +0100751 }
752 }
Jacob Erlbeckfd636ae2014-07-08 09:49:07 +0200753 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
Harald Welte9b455bf2010-03-14 15:45:01 +0800754 break;
755 case GSM_MI_TYPE_IMEI:
756 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
757 break;
758 case GSM_MI_TYPE_IMEISV:
759 break;
760 }
761
Harald Welte9b455bf2010-03-14 15:45:01 +0800762 /* Check if we can let the mobile station enter */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100763 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800764}
765
766/* Section 9.4.1 Attach request */
Harald Welte807a5d82010-06-01 11:53:01 +0200767static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
768 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800769{
Harald Welte943c5bc2010-04-30 16:33:12 +0200770 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200771 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Welte56a01452010-05-31 22:12:30 +0200772 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welteeaa614c2010-05-02 11:26:34 +0200773 uint16_t drx_par;
774 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800775 char mi_string[GSM48_MI_SIZE];
776 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200777 uint16_t cid;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100778 enum gsm48_gmm_cause reject_cause;
779 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800780
Daniel Willmann46553142014-09-03 17:46:44 +0200781 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800782
783 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
784 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
785 * or with random TLLI. */
786
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200787 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800788
789 /* MS network capability 10.5.5.12 */
790 msnc_len = *cur++;
791 msnc = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400792 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte9b455bf2010-03-14 15:45:01 +0800793 goto err_inval;
794 cur += msnc_len;
795
796 /* aTTACH Type 10.5.5.2 */
797 att_type = *cur++ & 0x0f;
798
799 /* DRX parameter 10.5.5.6 */
Harald Welte56a01452010-05-31 22:12:30 +0200800 drx_par = *cur++ << 8;
801 drx_par |= *cur++;
Harald Welte9b455bf2010-03-14 15:45:01 +0800802
803 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
804 mi_len = *cur++;
805 mi = cur;
806 if (mi_len > 8)
807 goto err_inval;
808 mi_type = *mi & GSM_MI_TYPE_MASK;
809 cur += mi_len;
810
811 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
812
Harald Welte2720e732010-05-17 00:44:57 +0200813 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
814 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800815
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200816 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte9b455bf2010-03-14 15:45:01 +0800817 cur += 6;
818
819 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte56a01452010-05-31 22:12:30 +0200820 ms_ra_acc_cap_len = *cur++;
821 ms_ra_acc_cap = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400822 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Weltec48ac472010-07-03 21:20:06 +0200823 goto err_inval;
Harald Welte4b2ed352011-07-27 23:35:38 +0200824 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800825
Daniel Willmann46553142014-09-03 17:46:44 +0200826 LOGPC(DMM, LOGL_INFO, "\n");
827
Harald Welte9b455bf2010-03-14 15:45:01 +0800828 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
829
830 switch (mi_type) {
831 case GSM_MI_TYPE_IMSI:
832 /* Try to find MM context based on IMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200833 if (!ctx)
834 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte9b455bf2010-03-14 15:45:01 +0800835 if (!ctx) {
836#if 0
837 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
838#else
Harald Welte9b455bf2010-03-14 15:45:01 +0800839 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100840 if (!ctx) {
841 reject_cause = GMM_CAUSE_NET_FAIL;
842 goto rejected;
843 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800844 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
845#endif
846 }
Harald Welte943c5bc2010-04-30 16:33:12 +0200847 ctx->tlli = msgb_tlli(msg);
Harald Welte807a5d82010-06-01 11:53:01 +0200848 ctx->llme = llme;
Harald Welte68b99a42010-05-18 12:05:42 +0200849 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800850 break;
851 case GSM_MI_TYPE_TMSI:
Harald Welteab1d5622010-05-18 19:58:38 +0200852 memcpy(&tmsi, mi+1, 4);
853 tmsi = ntohl(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800854 /* Try to find MM context based on P-TMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200855 if (!ctx)
856 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800857 if (!ctx) {
Harald Weltec728eea2010-12-24 23:07:18 +0100858 /* Allocate a context as most of our code expects one.
859 * Context will not have an IMSI ultil ID RESP is received */
Harald Welte943c5bc2010-04-30 16:33:12 +0200860 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte807a5d82010-06-01 11:53:01 +0200861 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800862 }
Harald Welte807a5d82010-06-01 11:53:01 +0200863 ctx->tlli = msgb_tlli(msg);
864 ctx->llme = llme;
865 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800866 break;
867 default:
Harald Weltec2e8cc42010-05-31 20:23:38 +0200868 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
869 "MI type %u\n", mi_type);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100870 reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED;
871 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +0800872 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200873 /* Update MM Context with currient RA and Cell ID */
874 ctx->ra = ra_id;
875 ctx->cell_id = cid;
Harald Welte56a01452010-05-31 22:12:30 +0200876 /* Update MM Context with other data */
877 ctx->drx_parms = drx_par;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400878 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte4b2ed352011-07-27 23:35:38 +0200879 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
880 ctx->ms_radio_access_capa.len);
Harald Welte56a01452010-05-31 22:12:30 +0200881 ctx->ms_network_capa.len = msnc_len;
882 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
883
Harald Weltec2e8cc42010-05-31 20:23:38 +0200884#ifdef PTMSI_ALLOC
Harald Welte807a5d82010-06-01 11:53:01 +0200885 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +0100886 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
887 if (ctx->mm_state != GMM_COMMON_PROC_INIT) {
888 ctx->p_tmsi_old = ctx->p_tmsi;
889 ctx->p_tmsi = sgsn_alloc_ptmsi();
890 }
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100891 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welteab1d5622010-05-18 19:58:38 +0200892#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200893 /* Even if there is no P-TMSI allocated, the MS will switch from
894 * foreign TLLI to local TLLI */
895 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
896
897 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200898 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
899 GPRS_ALGO_GEA0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800900
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100901 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
902 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800903
904err_inval:
Daniel Willmann46553142014-09-03 17:46:44 +0200905 LOGPC(DMM, LOGL_INFO, "\n");
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100906 reject_cause = GMM_CAUSE_SEM_INCORR_MSG;
907
908rejected:
909 /* Send ATTACH REJECT */
910 LOGMMCTXP(LOGL_NOTICE, ctx,
911 "Rejecting Attach Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100912 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100913 rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause);
914 if (ctx)
915 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ");
916 else
917 /* TLLI unassignment */
918 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
919
920 return rc;
921
Harald Welte9b455bf2010-03-14 15:45:01 +0800922}
923
Harald Welte2720e732010-05-17 00:44:57 +0200924/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200925static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200926{
927 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +0200928 uint8_t detach_type, power_off;
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200929 int rc = 0;
Harald Welte2720e732010-05-17 00:44:57 +0200930
931 detach_type = gh->data[0] & 0x7;
932 power_off = gh->data[0] & 0x8;
933
934 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
935
Daniel Willmann46553142014-09-03 17:46:44 +0200936 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte2720e732010-05-17 00:44:57 +0200937 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
938 power_off ? "Power-off" : "");
939
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200940 /* Only send the Detach Accept (MO) if power off isn't indicated,
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +0200941 * see 04.08, 4.7.4.1.2/3 for details */
942 if (!power_off) {
943 /* force_stby = 0 */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200944 if (ctx)
945 rc = gsm48_tx_gmm_det_ack(ctx, 0);
946 else
947 rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0);
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +0200948 }
Harald Weltebd5c9122010-06-28 22:18:53 +0200949
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200950 if (ctx)
951 mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST");
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +0200952
Harald Weltebd5c9122010-06-28 22:18:53 +0200953 return rc;
Harald Welte2720e732010-05-17 00:44:57 +0200954}
955
Harald Welte9b455bf2010-03-14 15:45:01 +0800956/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +0200957static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800958{
959 struct msgb *msg = gsm48_msgb_alloc();
960 struct gsm48_hdr *gh;
961 struct gsm48_ra_upd_ack *rua;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200962 uint8_t *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800963
Daniel Willmann46553142014-09-03 17:46:44 +0200964 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800965
Harald Welte6463c072010-05-18 17:04:55 +0200966 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800967
968 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
969 gh->proto_discr = GSM48_PDISC_MM_GPRS;
970 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
971
972 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
973 rua->force_stby = 0; /* not indicated */
974 rua->upd_result = 0; /* RA updated */
975 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200976
Harald Welte6463c072010-05-18 17:04:55 +0200977 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800978
Harald Weltec2e8cc42010-05-31 20:23:38 +0200979#if 0
980 /* Optional: P-TMSI signature */
981 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
982 ptsig = msgb_put(msg, 3);
983 ptsig[0] = mm->p_tmsi_sig >> 16;
984 ptsig[1] = mm->p_tmsi_sig >> 8;
985 ptsig[2] = mm->p_tmsi_sig & 0xff;
986#endif
987
988#ifdef PTMSI_ALLOC
989 /* Optional: Allocated P-TMSI */
990 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
991 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
992 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
993#endif
994
995 /* Option: MS ID, ... */
Harald Welte56a01452010-05-31 22:12:30 +0200996 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800997}
998
999/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +02001000static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +08001001{
1002 struct msgb *msg = gsm48_msgb_alloc();
1003 struct gsm48_hdr *gh;
1004
Daniel Willmann46553142014-09-03 17:46:44 +02001005 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001006
Harald Welte11d7c102010-05-02 11:54:55 +02001007 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001008
1009 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
1010 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1011 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
1012 gh->data[0] = cause;
1013 gh->data[1] = 0; /* ? */
1014
1015 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +02001016 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001017}
1018
Harald Welte588d44e2010-06-09 10:28:29 +02001019static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001020 const uint8_t *pdp_status)
Harald Welte588d44e2010-06-09 10:28:29 +02001021{
1022 struct sgsn_pdp_ctx *pdp, *pdp2;
1023 /* 24.008 4.7.5.1.3: If the PDP context status information element is
1024 * included in ROUTING AREA UPDATE REQUEST message, then the network
1025 * shall deactivate all those PDP contexts locally (without peer to
1026 * peer signalling between the MS and the network), which are not in SM
1027 * state PDP-INACTIVE on network side but are indicated by the MS as
1028 * being in state PDP-INACTIVE. */
1029
1030 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Weltecdf76cf2011-08-05 21:23:46 +02001031 if (pdp->nsapi < 8) {
1032 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001033 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001034 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1035 pdp->nsapi, pdp_status[1], pdp_status[0]);
1036 sgsn_delete_pdp_ctx(pdp);
1037 }
1038 } else {
1039 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001040 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001041 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1042 pdp->nsapi, pdp_status[1], pdp_status[0]);
1043 sgsn_delete_pdp_ctx(pdp);
1044 }
Harald Welte588d44e2010-06-09 10:28:29 +02001045 }
1046 }
1047}
1048
Harald Welte9b455bf2010-03-14 15:45:01 +08001049/* Chapter 9.4.14: Routing area update request */
Harald Welte807a5d82010-06-01 11:53:01 +02001050static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1051 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001052{
Harald Welte943c5bc2010-04-30 16:33:12 +02001053 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001054 uint8_t *cur = gh->data;
Harald Welte588d44e2010-06-09 10:28:29 +02001055 uint8_t ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001056 struct gprs_ra_id old_ra_id;
Harald Welte588d44e2010-06-09 10:28:29 +02001057 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +02001058 uint8_t upd_type;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001059 enum gsm48_gmm_cause reject_cause;
1060 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001061
1062 /* Update Type 10.5.5.18 */
1063 upd_type = *cur++ & 0x0f;
1064
Daniel Willmann46553142014-09-03 17:46:44 +02001065 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte2720e732010-05-17 00:44:57 +02001066 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +08001067
1068 /* Old routing area identification 10.5.5.15 */
1069 gsm48_parse_ra(&old_ra_id, cur);
1070 cur += 6;
1071
1072 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte588d44e2010-06-09 10:28:29 +02001073 ms_ra_acc_cap_len = *cur++;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001074 if (ms_ra_acc_cap_len > 52) {
1075 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1076 goto rejected;
1077 }
Harald Welte4b2ed352011-07-27 23:35:38 +02001078 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001079
1080 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
1081 * DRX parameter, MS network capability */
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001082 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welte588d44e2010-06-09 10:28:29 +02001083 (msg->data + msg->len) - cur, 0, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +08001084
1085 switch (upd_type) {
1086 case GPRS_UPD_T_RA_LA:
1087 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmann46553142014-09-03 17:46:44 +02001088 LOGP(DMM, LOGL_NOTICE, "Update type %i unsupported in Mode III, is your SI13 corrupt?\n", upd_type);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001089 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1090 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001091 case GPRS_UPD_T_RA:
1092 case GPRS_UPD_T_PERIODIC:
1093 break;
1094 }
1095
1096 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Weltef6bd3402010-12-23 23:34:43 +01001097 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +08001098 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte2b697a62011-10-16 18:50:56 +02001099 /* send a XID reset to re-set all LLC sequence numbers
1100 * in the MS */
Daniel Willmann46553142014-09-03 17:46:44 +02001101 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte2b697a62011-10-16 18:50:56 +02001102 gprs_llgmm_reset(llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001103 /* The MS has to perform GPRS attach */
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001104 /* Device is still IMSI attached for CS but initiate GPRS ATTACH,
1105 * see GSM 04.08, 4.7.5.1.4 and G.6 */
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001106 reject_cause = GMM_CAUSE_IMPL_DETACHED;
1107 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001108 }
1109
Harald Weltef0901f02010-12-26 10:39:26 +01001110 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
1111 msgid2mmctx(mmctx, msg);
1112 /* Bump the statistics of received signalling msgs for this MM context */
1113 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
1114
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001115 /* Update the MM context with the new RA-ID */
1116 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Weltef0901f02010-12-26 10:39:26 +01001117 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001118 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001119 /* FIXME: Update the MM context with the MS radio acc capabilities */
1120 /* FIXME: Update the MM context with the MS network capabilities */
1121
Harald Welte77289c22010-05-18 14:32:29 +02001122 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
1123
Harald Weltec2e8cc42010-05-31 20:23:38 +02001124#ifdef PTMSI_ALLOC
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +01001125 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
1126 if (mmctx->mm_state != GMM_COMMON_PROC_INIT) {
1127 mmctx->p_tmsi_old = mmctx->p_tmsi;
1128 mmctx->p_tmsi = sgsn_alloc_ptmsi();
1129 }
Harald Weltec2e8cc42010-05-31 20:23:38 +02001130 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
1131 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
1132 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001133
1134 mmctx->mm_state = GMM_COMMON_PROC_INIT;
1135#else
1136 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1137 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001138#endif
Harald Welte807a5d82010-06-01 11:53:01 +02001139 /* Even if there is no P-TMSI allocated, the MS will switch from
1140 * foreign TLLI to local TLLI */
1141 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001142
Harald Welte807a5d82010-06-01 11:53:01 +02001143 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +02001144 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
1145 GPRS_ALGO_GEA0, NULL);
Harald Welte807a5d82010-06-01 11:53:01 +02001146
Harald Welte588d44e2010-06-09 10:28:29 +02001147 /* Look at PDP Context Status IE and see if MS's view of
1148 * activated/deactivated NSAPIs agrees with our view */
1149 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001150 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welte588d44e2010-06-09 10:28:29 +02001151 process_ms_ctx_status(mmctx, pdp_status);
1152 }
1153
Harald Welte807a5d82010-06-01 11:53:01 +02001154 /* Send RA UPDATE ACCEPT */
Harald Welte6463c072010-05-18 17:04:55 +02001155 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001156
1157rejected:
1158 /* Send RA UPDATE REJECT */
1159 LOGMMCTXP(LOGL_NOTICE, mmctx,
1160 "Rejecting RA Update Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001161 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001162 rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause);
1163 if (mmctx)
1164 mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ");
1165 else
1166 /* TLLI unassignment */
1167 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0,
1168 NULL);
1169
1170 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001171}
1172
Harald Welted193cb32010-05-17 22:58:03 +02001173static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001174{
1175 struct gsm48_hdr *gh = msgb_l3(msg);
1176
Daniel Willmann46553142014-09-03 17:46:44 +02001177 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001178 get_value_string(gsm48_gmm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001179
1180 return 0;
1181}
1182
1183/* GPRS Mobility Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001184static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1185 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001186{
Harald Welte943c5bc2010-04-30 16:33:12 +02001187 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001188 int rc;
1189
Harald Welte807a5d82010-06-01 11:53:01 +02001190 /* MMCTX can be NULL when called */
1191
Harald Welted193cb32010-05-17 22:58:03 +02001192 if (!mmctx &&
1193 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Weltec2e8cc42010-05-31 20:23:38 +02001194 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Welted193cb32010-05-17 22:58:03 +02001195 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001196 /* 4.7.10 */
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001197 if (gh->msg_type == GSM48_MT_GMM_STATUS) {
1198 /* TLLI unassignment */
1199 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1200 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001201 return 0;
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001202 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001203
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001204 /* Don't reply or establish a LLME on DETACH_ACK */
1205 if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) {
1206 /* TLLI unassignment */
1207 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1208 GPRS_ALGO_GEA0, NULL);
1209 }
1210
Jacob Erlbeckc4f9bf32014-11-11 08:55:13 +01001211 gprs_llgmm_reset(llme);
1212
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001213 /* Don't force it into re-attachment */
1214 if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) {
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001215 /* Handle Detach Request */
1216 rc = gsm48_rx_gmm_det_req(NULL, msg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001217
1218 /* TLLI unassignment */
1219 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1220 GPRS_ALGO_GEA0, NULL);
1221 return rc;
1222 }
1223
1224 /* Force the MS to re-attach */
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001225 rc = sgsn_force_reattach_oldmsg(msg);
1226
1227 /* TLLI unassignment */
1228 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1229 GPRS_ALGO_GEA0, NULL);
1230 return rc;
Harald Welted193cb32010-05-17 22:58:03 +02001231 }
1232
Harald Welte9b455bf2010-03-14 15:45:01 +08001233 switch (gh->msg_type) {
1234 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001235 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001236 break;
1237 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001238 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001239 break;
Daniel Willmann46553142014-09-03 17:46:44 +02001240 /* For all the following types mmctx can not be NULL */
Harald Welte9b455bf2010-03-14 15:45:01 +08001241 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +02001242 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001243 break;
1244 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001245 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001246 break;
Harald Welte2720e732010-05-17 00:44:57 +02001247 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001248 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +02001249 break;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001250 case GSM48_MT_GMM_DETACH_ACK:
1251 LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n");
1252 mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK");
1253 rc = 0;
1254 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001255 case GSM48_MT_GMM_ATTACH_COMPL:
1256 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001257 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001258 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001259 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001260 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001261 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001262 /* Unassign the old TLLI */
1263 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001264 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1265 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001266 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001267 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001268 break;
Harald Welteab1d5622010-05-18 19:58:38 +02001269 case GSM48_MT_GMM_RA_UPD_COMPL:
1270 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001271 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001272 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001273 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001274 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001275 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001276 /* Unassign the old TLLI */
1277 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001278 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1279 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001280 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001281 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001282 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001283 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmann46553142014-09-03 17:46:44 +02001284 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001285 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001286 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001287 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001288 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001289 /* Unassign the old TLLI */
1290 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001291 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001292 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001293 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001294 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001295 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001296 break;
1297 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001298 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001299 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001300 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001301 break;
1302 }
1303
1304 return rc;
1305}
1306
Harald Weltec2e8cc42010-05-31 20:23:38 +02001307static void mmctx_timer_cb(void *_mm)
1308{
1309 struct sgsn_mm_ctx *mm = _mm;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001310 struct gsm_auth_tuple *at;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001311
1312 mm->num_T_exp++;
1313
1314 switch (mm->T) {
1315 case 3350: /* waiting for ATTACH COMPLETE */
1316 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001317 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001318 mm_ctx_cleanup_free(mm, "T3350");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001319 /* FIXME: should we return some error? */
1320 break;
1321 }
1322 /* re-transmit the respective msg and re-start timer */
1323 switch (mm->t3350_mode) {
1324 case GMM_T3350_MODE_ATT:
1325 gsm48_tx_gmm_att_ack(mm);
1326 break;
1327 case GMM_T3350_MODE_RAU:
1328 gsm48_tx_gmm_ra_upd_ack(mm);
1329 break;
1330 case GMM_T3350_MODE_PTMSI_REALL:
1331 /* FIXME */
1332 break;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001333 case GMM_T3350_MODE_NONE:
1334 LOGMMCTXP(LOGL_NOTICE, mm,
1335 "T3350 mode wasn't set, ignoring timeout\n");
1336 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001337 }
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001338 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001339 break;
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001340 case 3360: /* waiting for AUTH AND CIPH RESP */
1341 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001342 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001343 mm_ctx_cleanup_free(mm, "T3360");
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001344 break;
1345 }
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001346 /* Re-transmit the respective msg and re-start timer */
1347 if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
1348 LOGMMCTXP(LOGL_ERROR, mm,
1349 "timeout: invalid auth triplet reference\n");
1350 mm_ctx_cleanup_free(mm, "T3360");
1351 break;
1352 }
1353 at = &mm->auth_triplet;
1354
1355 gsm48_tx_gmm_auth_ciph_req(mm, at->rand, at->key_seq, GPRS_ALGO_GEA0);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001356 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001357 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001358 case 3370: /* waiting for IDENTITY RESPONSE */
1359 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001360 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001361 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
Jacob Erlbecka7904562014-11-03 10:12:52 +01001362 mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001363 break;
1364 }
1365 /* re-tranmit IDENTITY REQUEST and re-start timer */
1366 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001367 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001368 break;
1369 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001370 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Weltec2e8cc42010-05-31 20:23:38 +02001371 mm->T);
1372 }
1373}
1374
1375/* GPRS SESSION MANAGEMENT */
1376
Harald Weltea9b473a2010-12-24 21:13:26 +01001377static void pdpctx_timer_cb(void *_mm);
1378
1379static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1380 unsigned int seconds)
1381{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001382 if (osmo_timer_pending(&pdp->timer))
Harald Weltea9b473a2010-12-24 21:13:26 +01001383 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1384 "timer %u pending\n", T, pdp->T);
1385 pdp->T = T;
1386 pdp->num_T_exp = 0;
1387
1388 /* FIXME: we should do this only once ? */
1389 pdp->timer.data = pdp;
1390 pdp->timer.cb = &pdpctx_timer_cb;
1391
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001392 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001393}
1394
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001395#if 0
Harald Welte421cba42010-05-02 23:11:50 +02001396static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1397{
1398 uint8_t v[6];
1399
1400 v[0] = PDP_TYPE_ORG_IETF;
1401 v[1] = PDP_TYPE_N_IETF_IPv4;
1402 *(uint32_t *)(v+2) = htonl(ipaddr);
1403
1404 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1405}
1406
1407static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1408{
1409 uint8_t v[2];
1410
1411 v[0] = PDP_TYPE_ORG_ETSI;
1412 v[1] = PDP_TYPE_N_ETSI_PPP;
1413
1414 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1415}
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001416#endif
Harald Welte421cba42010-05-02 23:11:50 +02001417
Harald Welte9b455bf2010-03-14 15:45:01 +08001418/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +02001419int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +08001420{
Harald Welte9b455bf2010-03-14 15:45:01 +08001421 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +08001422 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +02001423 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001424
Daniel Willmann46553142014-09-03 17:46:44 +02001425 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001426
Harald Welte6abf94e2010-05-18 10:35:06 +02001427 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001428
1429 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1430 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1431 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +02001432
1433 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +02001434 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +02001435
Harald Welte55e0df72010-05-18 13:20:08 +02001436 /* FIXME: copy QoS parameters from original request */
1437 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1438 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +02001439
Harald Welte421cba42010-05-02 23:11:50 +02001440 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +02001441 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +02001442
Harald Welte421cba42010-05-02 23:11:50 +02001443 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +02001444 /* Highest 4 bits of first byte need to be set to 1, otherwise
1445 * the IE is identical with the 04.08 PDP Address IE */
1446 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +02001447 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1448 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001449 pdp->lib->eua.v[0] |= 0xf0;
1450
Harald Welte77289c22010-05-18 14:32:29 +02001451 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freytherd2195072014-04-04 11:55:21 +02001452 if (pdp->lib->pco_req.l)
Harald Welte6abf94e2010-05-18 10:35:06 +02001453 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +02001454 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001455
Harald Welte421cba42010-05-02 23:11:50 +02001456 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +08001457
Harald Welte8acd88f2010-05-18 10:57:45 +02001458 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001459}
1460
Harald Welte6abf94e2010-05-18 10:35:06 +02001461/* Section 9.5.3: Activate PDP Context reject */
1462int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1463 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1464{
1465 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +02001466 struct gsm48_hdr *gh;
1467 uint8_t transaction_id = tid ^ 0x8; /* flip */
1468
Daniel Willmann46553142014-09-03 17:46:44 +02001469 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +02001470
1471 mmctx2msgid(msg, mm);
1472
1473 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1474 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1475 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1476
1477 msgb_v_put(msg, cause);
1478 if (pco_len && pco_v)
1479 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1480
Harald Welte8acd88f2010-05-18 10:57:45 +02001481 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +02001482}
1483
Harald Weltea9b473a2010-12-24 21:13:26 +01001484/* Section 9.5.8: Deactivate PDP Context Request */
1485static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1486 uint8_t sm_cause)
1487{
1488 struct msgb *msg = gsm48_msgb_alloc();
1489 struct gsm48_hdr *gh;
1490 uint8_t transaction_id = tid ^ 0x8; /* flip */
1491
Daniel Willmann46553142014-09-03 17:46:44 +02001492 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001493
1494 mmctx2msgid(msg, mm);
1495
1496 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1497 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1498 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1499
1500 msgb_v_put(msg, sm_cause);
1501
1502 return gsm48_gmm_sendmsg(msg, 0, mm);
1503}
1504int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1505{
1506 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1507
1508 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1509}
1510
Harald Welte9b455bf2010-03-14 15:45:01 +08001511/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte60ebc022010-06-08 18:08:59 +02001512static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte9b455bf2010-03-14 15:45:01 +08001513{
Harald Welte9b455bf2010-03-14 15:45:01 +08001514 struct msgb *msg = gsm48_msgb_alloc();
1515 struct gsm48_hdr *gh;
Harald Welte60ebc022010-06-08 18:08:59 +02001516 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001517
Daniel Willmann46553142014-09-03 17:46:44 +02001518 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001519
Harald Welte60ebc022010-06-08 18:08:59 +02001520 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001521
1522 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1523 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1524 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1525
Harald Welte60ebc022010-06-08 18:08:59 +02001526 return gsm48_gmm_sendmsg(msg, 0, mm);
1527}
1528int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1529{
1530 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte9b455bf2010-03-14 15:45:01 +08001531}
1532
1533/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +02001534static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1535 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001536{
Harald Welte943c5bc2010-04-30 16:33:12 +02001537 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001538 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +02001539 uint8_t req_qos_len, req_pdpa_len;
1540 uint8_t *req_qos, *req_pdpa;
1541 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +02001542 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte11ff3e82010-07-02 10:17:24 +02001543 struct sgsn_ggsn_ctx *ggsn;
Harald Welte6abf94e2010-05-18 10:35:06 +02001544 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001545
Daniel Willmann46553142014-09-03 17:46:44 +02001546 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte6abf94e2010-05-18 10:35:06 +02001547 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +02001548
Harald Welte23c13762010-07-02 22:52:04 +02001549 /* FIXME: length checks! */
Harald Welte0c3eae02010-05-02 21:07:14 +02001550 req_qos_len = act_req->data[0];
1551 req_qos = act_req->data + 1; /* 10.5.6.5 */
1552 req_pdpa_len = act_req->data[1 + req_qos_len];
1553 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +08001554
Harald Welte23c13762010-07-02 22:52:04 +02001555 /* Optional: Access Point Name, Protocol Config Options */
1556 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1557 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1558 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1559 else
1560 memset(&tp, 0, sizeof(tp));
1561
Harald Welte0c3eae02010-05-02 21:07:14 +02001562 switch (req_pdpa[0] & 0xf) {
1563 case 0x0:
1564 DEBUGPC(DMM, "ETSI ");
1565 break;
1566 case 0x1:
1567 DEBUGPC(DMM, "IETF ");
1568 break;
1569 case 0xf:
1570 DEBUGPC(DMM, "Empty ");
1571 break;
1572 }
1573
1574 switch (req_pdpa[1]) {
1575 case 0x21:
1576 DEBUGPC(DMM, "IPv4 ");
1577 if (req_pdpa_len >= 6) {
1578 struct in_addr ia;
1579 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1580 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1581 }
1582 break;
1583 case 0x57:
1584 DEBUGPC(DMM, "IPv6 ");
1585 if (req_pdpa_len >= 18) {
1586 /* FIXME: print IPv6 address */
1587 }
1588 break;
1589 default:
1590 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1591 break;
1592 }
1593
Daniel Willmann46553142014-09-03 17:46:44 +02001594 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welte77289c22010-05-18 14:32:29 +02001595
Harald Welte2720e732010-05-17 00:44:57 +02001596 /* put the non-TLV elements in the TLV parser structure to
1597 * pass them on to the SGSN / GTP code */
1598 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1599 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1600 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1601 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1602
Harald Welte6abf94e2010-05-18 10:35:06 +02001603 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +02001604 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +02001605
1606 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1607 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1608 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1609 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1610 GSM_CAUSE_INV_MAND_INFO,
1611 0, NULL);
1612 }
1613
1614 /* Check if NSAPI is already in use */
Harald Welteb37515e2010-07-02 10:12:58 +02001615 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1616 if (pdp) {
1617 /* We already have a PDP context for this TLLI + NSAPI tuple */
1618 if (pdp->sapi == act_req->req_llc_sapi &&
1619 pdp->ti == transaction_id) {
1620 /* This apparently is a re-transmission of a PDP CTX
1621 * ACT REQ (our ACT ACK must have got dropped) */
1622 return gsm48_tx_gsm_act_pdp_acc(pdp);
1623 }
1624
1625 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte6abf94e2010-05-18 10:35:06 +02001626 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1627 GSM_CAUSE_NSAPI_IN_USE,
1628 0, NULL);
1629 }
Harald Welte9b455bf2010-03-14 15:45:01 +08001630
Harald Welte19f9e302010-07-02 10:15:49 +02001631 /* Only increment counter for a real activation, after we checked
1632 * for re-transmissions */
1633 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1634
Harald Welte11ff3e82010-07-02 10:17:24 +02001635 ggsn = sgsn_ggsn_ctx_by_id(0);
1636 if (!ggsn) {
1637 LOGP(DGPRS, LOGL_ERROR, "No GGSN context 0 found!\n");
1638 return -EIO;
Harald Welted193cb32010-05-17 22:58:03 +02001639 }
Harald Welte11ff3e82010-07-02 10:17:24 +02001640 ggsn->gsn = sgsn->gsn;
1641 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1642 if (!pdp)
1643 return -1;
1644
1645 /* Store SAPI and Transaction Identifier */
1646 pdp->sapi = act_req->req_llc_sapi;
1647 pdp->ti = transaction_id;
1648
Harald Welte6463c072010-05-18 17:04:55 +02001649 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +08001650}
1651
1652/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +02001653static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001654{
Harald Welte943c5bc2010-04-30 16:33:12 +02001655 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +02001656 uint8_t transaction_id = (gh->proto_discr >> 4);
1657 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001658
Daniel Willmann46553142014-09-03 17:46:44 +02001659 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001660 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001661
Harald Welte77289c22010-05-18 14:32:29 +02001662 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1663 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001664 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welte77289c22010-05-18 14:32:29 +02001665 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1666 mm->imsi, transaction_id);
Harald Welte60ebc022010-06-08 18:08:59 +02001667 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welte77289c22010-05-18 14:32:29 +02001668 }
1669
1670 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +08001671}
1672
Harald Weltea9b473a2010-12-24 21:13:26 +01001673/* Section 9.5.9: Deactivate PDP Context Accept */
1674static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1675{
1676 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1677 uint8_t transaction_id = (gh->proto_discr >> 4);
1678 struct sgsn_pdp_ctx *pdp;
1679
Daniel Willmann46553142014-09-03 17:46:44 +02001680 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001681
1682 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1683 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001684 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Weltea9b473a2010-12-24 21:13:26 +01001685 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1686 mm->imsi, transaction_id);
1687 return 0;
1688 }
1689
1690 return sgsn_delete_pdp_ctx(pdp);
1691}
1692
Harald Welted193cb32010-05-17 22:58:03 +02001693static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001694{
1695 struct gsm48_hdr *gh = msgb_l3(msg);
1696
Daniel Willmann46553142014-09-03 17:46:44 +02001697 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001698 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001699
1700 return 0;
1701}
1702
Harald Weltea9b473a2010-12-24 21:13:26 +01001703static void pdpctx_timer_cb(void *_pdp)
1704{
1705 struct sgsn_pdp_ctx *pdp = _pdp;
1706
1707 pdp->num_T_exp++;
1708
1709 switch (pdp->T) {
1710 case 3395: /* waiting for PDP CTX DEACT ACK */
1711 if (pdp->num_T_exp >= 4) {
Daniel Willmann46553142014-09-03 17:46:44 +02001712 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001713 pdp->state = PDP_STATE_INACTIVE;
1714 sgsn_delete_pdp_ctx(pdp);
1715 break;
1716 }
1717 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001718 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001719 break;
1720 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001721 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Weltea9b473a2010-12-24 21:13:26 +01001722 pdp->T);
1723 }
1724}
1725
1726
Harald Welte9b455bf2010-03-14 15:45:01 +08001727/* GPRS Session Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001728static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1729 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001730{
Harald Welte943c5bc2010-04-30 16:33:12 +02001731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001732 int rc;
1733
Harald Welte807a5d82010-06-01 11:53:01 +02001734 /* MMCTX can be NULL when called */
1735
Harald Welted193cb32010-05-17 22:58:03 +02001736 if (!mmctx) {
1737 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001738 /* 6.1.3.6 */
1739 if (gh->msg_type == GSM48_MT_GSM_STATUS)
1740 return 0;
1741
1742 return sgsn_force_reattach_oldmsg(msg);
Harald Welted193cb32010-05-17 22:58:03 +02001743 }
1744
Harald Welte9b455bf2010-03-14 15:45:01 +08001745 switch (gh->msg_type) {
1746 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001747 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001748 break;
1749 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001750 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +02001751 break;
Harald Weltea9b473a2010-12-24 21:13:26 +01001752 case GSM48_MT_GSM_DEACT_PDP_ACK:
1753 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1754 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001755 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001756 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001757 break;
1758 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1759 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1760 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmann46553142014-09-03 17:46:44 +02001761 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001762 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001763 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001764 break;
1765 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001766 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001767 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001768 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001769 break;
1770
1771 }
1772
1773 return rc;
1774}
1775
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001776int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001777{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001778 int rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001779 gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM);
1780
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001781 rc = gsm48_tx_gmm_detach_req_oldmsg(
1782 msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001783
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001784 return rc;
1785}
1786
1787int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx)
1788{
1789 int rc;
1790 gprs_llgmm_reset(mmctx->llme);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001791
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001792 rc = gsm48_tx_gmm_detach_req(
1793 mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001794
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001795 mm_ctx_cleanup_free(mmctx, "forced reattach");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001796
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001797 return rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001798}
1799
Harald Welte9b455bf2010-03-14 15:45:01 +08001800/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte807a5d82010-06-01 11:53:01 +02001801int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001802{
Harald Welte943c5bc2010-04-30 16:33:12 +02001803 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001804 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +02001805 struct sgsn_mm_ctx *mmctx;
1806 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +08001807 int rc = -EINVAL;
1808
Harald Welted193cb32010-05-17 22:58:03 +02001809 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1810 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +02001811 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +02001812 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +02001813 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte807a5d82010-06-01 11:53:01 +02001814 mmctx->llme = llme;
Harald Welte8acd88f2010-05-18 10:57:45 +02001815 }
Harald Welted193cb32010-05-17 22:58:03 +02001816
1817 /* MMCTX can be NULL */
1818
Harald Welte9b455bf2010-03-14 15:45:01 +08001819 switch (pdisc) {
1820 case GSM48_PDISC_MM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001821 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001822 break;
1823 case GSM48_PDISC_SM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001824 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001825 break;
1826 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001827 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001828 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +02001829 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +08001830 break;
1831 }
1832
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +02001833 /* MMCTX can be invalid */
1834
Harald Welte9b455bf2010-03-14 15:45:01 +08001835 return rc;
1836}
Harald Welte5bfe4992010-06-09 11:22:47 +02001837
1838int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1839{
1840 struct sgsn_mm_ctx *mmctx;
1841
1842 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1843 if (!mmctx) {
1844 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1845 "TLLI=%08x\n", tlli);
1846 return -EINVAL;
1847 }
1848
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001849 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1850 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001851 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001852 "!= REGISTERED (TLLI=%08x)\n", tlli);
1853 return -EINVAL;
1854 }
1855
1856 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1857 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1858 return 0;
1859}
1860
1861int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1862 uint8_t suspend_ref)
1863{
1864 struct sgsn_mm_ctx *mmctx;
1865
1866 /* FIXME: make use of suspend reference? */
1867
1868 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1869 if (!mmctx) {
1870 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1871 "TLLI=%08x\n", tlli);
1872 return -EINVAL;
1873 }
1874
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001875 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1876 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001877 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001878 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1879 /* FIXME: should we not simply ignore it? */
1880 return -EINVAL;
1881 }
1882
1883 /* Transition from SUSPENDED to NORMAL */
1884 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1885 return 0;
1886}