blob: 3977c66578cadcbe0a4457b46ebcb6e5c583b0f1 [file] [log] [blame]
Harald Welte75bb8202010-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 Weltede34afa2010-05-02 12:56:57 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte7e01d8e2010-05-31 20:23:38 +02005 * (C) 2010 by On-Waves
Harald Welte75bb8202010-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 Welte0e3e88e2011-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 Welte75bb8202010-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 Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte75bb8202010-03-14 15:45:01 +080018 *
Harald Welte0e3e88e2011-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 Welte75bb8202010-03-14 15:45:01 +080021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Harald Welted85d9a92010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080028#include <errno.h>
29
30#include <netinet/in.h>
Harald Welte2aea7492010-05-02 23:11:50 +020031#include <arpa/inet.h>
Harald Welte75bb8202010-03-14 15:45:01 +080032
33#include <openbsc/db.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010034#include <osmocom/core/msgb.h>
Harald Welte8b1713a2011-03-23 18:26:56 +010035#include <osmocom/gsm/tlv.h>
36#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayusodd5fff42011-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 Weltee4cc19a2010-05-02 12:57:45 +020040
Harald Weltecfb6b282012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_bssgp.h>
42
Harald Welte75bb8202010-03-14 15:45:01 +080043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
Harald Welte75bb8202010-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 Welte75bb8202010-03-14 15:45:01 +080049#include <openbsc/transaction.h>
50#include <openbsc/gprs_llc.h>
51#include <openbsc/gprs_sgsn.h>
Harald Welte29d838e2010-05-18 10:35:06 +020052#include <openbsc/gprs_gmm.h>
Jacob Erlbeck1063ae02015-01-23 11:44:39 +010053#include <openbsc/gprs_utils.h>
Harald Weltec1f6bfe2010-05-17 22:58:03 +020054#include <openbsc/sgsn.h>
55
Harald Welte29d838e2010-05-18 10:35:06 +020056#include <pdp.h>
57
Harald Welte7e01d8e2010-05-31 20:23:38 +020058#define PTMSI_ALLOC
59
Jacob Erlbeck3dafa862015-01-23 12:32:38 +010060/* Section 11.2.2 / Table 11.3a GPRS Mobility management timers – MS side */
61#define GSM0408_T3312_SECS (10*60) /* periodic RAU interval, default 54min */
62
Harald Welted4b9b022013-07-21 17:41:46 +080063/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Welte7e01d8e2010-05-31 20:23:38 +020064#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
65#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
66#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
67#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
68
Harald Welted4b9b022013-07-21 17:41:46 +080069/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Welte7e01d8e2010-05-31 20:23:38 +020070#define GSM0408_T3313_SECS 30 /* waiting for paging response */
Jacob Erlbeck1063ae02015-01-23 11:44:39 +010071#define GSM0408_T3314_SECS 44 /* force to STBY on expiry, Ready timer */
Harald Welte7e01d8e2010-05-31 20:23:38 +020072#define GSM0408_T3316_SECS 44
73
Harald Welte94ecef32010-12-24 21:13:26 +010074/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
75#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
76#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
77#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
78#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
79
Harald Weltec1f6bfe2010-05-17 22:58:03 +020080extern struct sgsn_instance *sgsn;
Harald Welte75bb8202010-03-14 15:45:01 +080081
Harald Welted9610722010-06-09 10:28:29 +020082static const struct tlv_definition gsm48_gmm_att_tlvdef = {
83 .def = {
Harald Welte45fcf682010-07-03 11:09:06 +020084 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welted9610722010-06-09 10:28:29 +020085 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
86 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +020087 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
88 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
89 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welted9610722010-06-09 10:28:29 +020090 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +020091 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welted9610722010-06-09 10:28:29 +020092 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
93 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
94 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
95 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
96 },
97};
98
99static const struct tlv_definition gsm48_sm_att_tlvdef = {
100 .def = {
101 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
102 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
103 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
104 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
105 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
106 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +0200107 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
108 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welted9610722010-06-09 10:28:29 +0200109 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
110 },
111};
112
Jacob Erlbeckac336982014-12-02 09:47:26 +0100113static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx);
114
Harald Welte8f77f192010-05-17 00:44:57 +0200115/* Our implementation, should be kept in SGSN */
Harald Welte75bb8202010-03-14 15:45:01 +0800116
Harald Welte7e01d8e2010-05-31 20:23:38 +0200117static void mmctx_timer_cb(void *_mm);
118
119static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
120 unsigned int seconds)
121{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200122 if (osmo_timer_pending(&mm->timer))
Harald Welte7e01d8e2010-05-31 20:23:38 +0200123 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
124 "timer %u pending\n", T, mm->T);
125 mm->T = T;
126 mm->num_T_exp = 0;
127
128 /* FIXME: we should do this only once ? */
129 mm->timer.data = mm;
130 mm->timer.cb = &mmctx_timer_cb;
131
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200132 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200133}
134
135static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
136{
137 if (mm->T != T)
138 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
139 "%u is running\n", T, mm->T);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200140 osmo_timer_del(&mm->timer);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200141}
142
Harald Welte75bb8202010-03-14 15:45:01 +0800143/* Send a message through the underlying layer */
Harald Welte8a035af2010-05-18 10:57:45 +0200144static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +0200145 struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +0800146{
Harald Welte8a035af2010-05-18 10:57:45 +0200147 if (mm)
148 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
149
Harald Welte2351f1f2010-05-02 11:54:55 +0200150 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Weltecca80c92010-05-31 22:12:30 +0200151 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800152}
153
Harald Welte2351f1f2010-05-02 11:54:55 +0200154/* copy identifiers from old message to new message, this
155 * is required so lower layers can route it correctly */
156static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
157{
158 msgb_tlli(msg) = msgb_tlli(old);
159 msgb_bvci(msg) = msgb_bvci(old);
160 msgb_nsei(msg) = msgb_nsei(old);
161}
162
Harald Welte29d838e2010-05-18 10:35:06 +0200163/* Store BVCI/NSEI in MM context */
164static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
165{
166 mm->bvci = msgb_bvci(msg);
167 mm->nsei = msgb_nsei(msg);
168}
169
170/* Store BVCI/NSEI in MM context */
171static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
172{
173 msgb_tlli(msg) = mm->tlli;
174 msgb_bvci(msg) = mm->bvci;
175 msgb_nsei(msg) = mm->nsei;
176}
177
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200178static void delete_pdp_contexts(struct sgsn_mm_ctx *ctx, const char *log_text)
179{
180 struct sgsn_pdp_ctx *pdp, *pdp2;
181
182 /* delete all existing PDP contexts for this MS */
183 llist_for_each_entry_safe(pdp, pdp2, &ctx->pdp_list, list) {
184 LOGMMCTXP(LOGL_NOTICE, ctx,
185 "Dropping PDP context for NSAPI=%u due to %s\n",
186 pdp->nsapi, log_text);
187 sgsn_pdp_ctx_terminate(pdp);
188 }
189}
190
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200191static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text)
192{
193 struct gprs_llc_llme *llme = ctx->llme;
194 uint32_t tlli = ctx->tlli;
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200195
196 /* Mark MM state as deregistered */
197 ctx->mm_state = GMM_DEREGISTERED;
198
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200199 delete_pdp_contexts(ctx, log_text);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200200
201 sgsn_mm_ctx_free(ctx);
202 ctx = NULL;
203
204 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
205 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
206}
207
Harald Welte263edd12010-06-09 10:51:23 +0200208/* Chapter 9.4.18 */
209static int _tx_status(struct msgb *msg, uint8_t cause,
210 struct sgsn_mm_ctx *mmctx, int sm)
211{
212 struct gsm48_hdr *gh;
213
214 /* MMCTX might be NULL! */
215
216 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100217 get_value_string(gsm48_gmm_cause_names, cause));
Harald Welte263edd12010-06-09 10:51:23 +0200218
219 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
220 if (sm) {
221 gh->proto_discr = GSM48_PDISC_SM_GPRS;
222 gh->msg_type = GSM48_MT_GSM_STATUS;
223 } else {
224 gh->proto_discr = GSM48_PDISC_MM_GPRS;
225 gh->msg_type = GSM48_MT_GMM_STATUS;
226 }
227 gh->data[0] = cause;
228
229 return gsm48_gmm_sendmsg(msg, 0, mmctx);
230}
Jacob Erlbeckce61bb72014-10-29 22:57:30 +0100231
Harald Welte263edd12010-06-09 10:51:23 +0200232static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
233{
234 struct msgb *msg = gsm48_msgb_alloc();
235
236 mmctx2msgid(msg, mmctx);
237 return _tx_status(msg, cause, mmctx, 0);
Harald Welte263edd12010-06-09 10:51:23 +0200238}
Jacob Erlbeckce61bb72014-10-29 22:57:30 +0100239
Harald Welte263edd12010-06-09 10:51:23 +0200240static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
241{
242 struct msgb *msg = gsm48_msgb_alloc();
243
244 mmctx2msgid(msg, mmctx);
245 return _tx_status(msg, cause, mmctx, 1);
Harald Welte263edd12010-06-09 10:51:23 +0200246}
247
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200248static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause,
249 struct sgsn_mm_ctx *mmctx)
250{
251 struct gsm48_hdr *gh;
252
253 /* MMCTX might be NULL! */
254
255 DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n",
256 get_value_string(gprs_det_t_mt_strs, detach_type),
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100257 get_value_string(gsm48_gmm_cause_names, cause));
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200258
259 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
260
261 gh->proto_discr = GSM48_PDISC_MM_GPRS;
262 gh->msg_type = GSM48_MT_GMM_DETACH_REQ;
263 gh->data[0] = detach_type & 0x07;
264
265 msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause);
266
267 return gsm48_gmm_sendmsg(msg, 0, mmctx);
268}
269
270static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx,
271 uint8_t detach_type, uint8_t cause)
272{
273 struct msgb *msg = gsm48_msgb_alloc();
274
275 mmctx2msgid(msg, mmctx);
276 return _tx_detach_req(msg, detach_type, cause, mmctx);
277}
278
279static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg,
280 uint8_t detach_type, uint8_t cause)
281{
282 struct msgb *msg = gsm48_msgb_alloc();
283
284 gmm_copy_id(msg, oldmsg);
285 return _tx_detach_req(msg, detach_type, cause, NULL);
286}
Harald Welte263edd12010-06-09 10:51:23 +0200287
Harald Welte242d1722010-05-03 10:54:35 +0200288static struct gsm48_qos default_qos = {
289 .delay_class = 4, /* best effort */
290 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
291 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
292 .preced_class = GSM48_QOS_PC_NORMAL,
293 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
294 .traf_class = GSM48_QOS_TC_INTERACTIVE,
295 .deliv_order = GSM48_QOS_DO_UNORDERED,
296 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
297 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
298 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
299 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
300 .resid_ber = GSM48_QOS_RBER_5e_2,
301 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
302 .handling_prio = 3,
303 .xfer_delay = 0x10, /* 200ms */
304 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
305 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
306 .sig_ind = 0, /* not optimised for signalling */
307 .max_bitrate_down_ext = 0, /* use octet 9 */
308 .guar_bitrate_down_ext = 0, /* use octet 13 */
309};
310
Harald Welte75bb8202010-03-14 15:45:01 +0800311/* Chapter 9.4.2: Attach accept */
Harald Welte64df8ed2010-05-18 17:04:55 +0200312static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +0800313{
314 struct msgb *msg = gsm48_msgb_alloc();
315 struct gsm48_hdr *gh;
316 struct gsm48_attach_ack *aa;
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200317 uint8_t *mid;
318#if 0
319 uint8_t *ptsig;
320#endif
Harald Welte75bb8202010-03-14 15:45:01 +0800321
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200322 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800323
Harald Welte64df8ed2010-05-18 17:04:55 +0200324 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800325
326 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
327 gh->proto_discr = GSM48_PDISC_MM_GPRS;
328 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
329
330 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
331 aa->force_stby = 0; /* not indicated */
332 aa->att_result = 1; /* GPRS only */
Jacob Erlbeck3dafa862015-01-23 12:32:38 +0100333 aa->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte75bb8202010-03-14 15:45:01 +0800334 aa->radio_prio = 4; /* lowest */
Harald Welte64df8ed2010-05-18 17:04:55 +0200335 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte75bb8202010-03-14 15:45:01 +0800336
Harald Welteebe8a6d2010-05-18 19:58:38 +0200337#if 0
Harald Welte64df8ed2010-05-18 17:04:55 +0200338 /* Optional: P-TMSI signature */
339 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
340 ptsig = msgb_put(msg, 3);
341 ptsig[0] = mm->p_tmsi_sig >> 16;
342 ptsig[1] = mm->p_tmsi_sig >> 8;
343 ptsig[2] = mm->p_tmsi_sig & 0xff;
344
Harald Welte7e01d8e2010-05-31 20:23:38 +0200345#endif
Jacob Erlbeck1063ae02015-01-23 11:44:39 +0100346 /* Optional: Negotiated Ready timer value
347 * (fixed 44s, default value, GSM 04.08, table 11.4a) to safely limit
348 * the inactivity time READY->STANDBY.
349 */
350 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
351 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
Harald Welte64df8ed2010-05-18 17:04:55 +0200352
Harald Welte7e01d8e2010-05-31 20:23:38 +0200353#ifdef PTMSI_ALLOC
Harald Welte64df8ed2010-05-18 17:04:55 +0200354 /* Optional: Allocated P-TMSI */
Harald Welte64df8ed2010-05-18 17:04:55 +0200355 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
356 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteebe8a6d2010-05-18 19:58:38 +0200357 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
358#endif
Harald Welte7e01d8e2010-05-31 20:23:38 +0200359
Harald Welte64df8ed2010-05-18 17:04:55 +0200360 /* Optional: MS-identity (combined attach) */
361 /* Optional: GMM cause (partial attach result for combined attach) */
362
Harald Weltecca80c92010-05-31 22:12:30 +0200363 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800364}
365
366/* Chapter 9.4.5: Attach reject */
Harald Welte64df8ed2010-05-18 17:04:55 +0200367static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte75bb8202010-03-14 15:45:01 +0800368{
Harald Welte75bb8202010-03-14 15:45:01 +0800369 struct gsm48_hdr *gh;
370
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100371 LOGP(DMM, LOGL_NOTICE, "<- GPRS ATTACH REJECT: %s\n", get_value_string(gsm48_gmm_cause_names, gmm_cause));
Harald Welte75bb8202010-03-14 15:45:01 +0800372
Harald Welte75bb8202010-03-14 15:45:01 +0800373 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
374 gh->proto_discr = GSM48_PDISC_MM_GPRS;
375 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
376 gh->data[0] = gmm_cause;
377
Harald Welte8a035af2010-05-18 10:57:45 +0200378 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +0800379}
Harald Welte64df8ed2010-05-18 17:04:55 +0200380static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
381 uint8_t gmm_cause)
382{
383 struct msgb *msg = gsm48_msgb_alloc();
384 gmm_copy_id(msg, old_msg);
385 return _tx_gmm_att_rej(msg, gmm_cause);
386}
387static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
388 uint8_t gmm_cause)
389{
390 struct msgb *msg = gsm48_msgb_alloc();
391 mmctx2msgid(msg, mm);
392 return _tx_gmm_att_rej(msg, gmm_cause);
393}
Harald Welte75bb8202010-03-14 15:45:01 +0800394
Harald Welte8f77f192010-05-17 00:44:57 +0200395/* Chapter 9.4.6.2 Detach accept */
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200396static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby,
397 struct sgsn_mm_ctx *mm)
Harald Welte8f77f192010-05-17 00:44:57 +0200398{
Harald Welte8f77f192010-05-17 00:44:57 +0200399 struct gsm48_hdr *gh;
400
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200401 /* MMCTX might be NULL! */
Harald Welte8f77f192010-05-17 00:44:57 +0200402
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200403 DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby);
Harald Welte64df8ed2010-05-18 17:04:55 +0200404
Harald Welte8f77f192010-05-17 00:44:57 +0200405 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
406 gh->proto_discr = GSM48_PDISC_MM_GPRS;
407 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
408 gh->data[0] = force_stby;
409
Harald Weltecca80c92010-05-31 22:12:30 +0200410 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte8f77f192010-05-17 00:44:57 +0200411}
412
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200413static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
414{
415 struct msgb *msg = gsm48_msgb_alloc();
416
417 mmctx2msgid(msg, mm);
418 return _tx_detach_ack(msg, force_stby, mm);
419}
420
421static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby)
422{
423 struct msgb *msg = gsm48_msgb_alloc();
424
425 gmm_copy_id(msg, oldmsg);
426 return _tx_detach_ack(msg, force_stby, NULL);
427}
428
Harald Welte75bb8202010-03-14 15:45:01 +0800429/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte64df8ed2010-05-18 17:04:55 +0200430static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte75bb8202010-03-14 15:45:01 +0800431{
432 struct msgb *msg = gsm48_msgb_alloc();
433 struct gsm48_hdr *gh;
434
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200435 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte75bb8202010-03-14 15:45:01 +0800436
Harald Welte64df8ed2010-05-18 17:04:55 +0200437 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800438
439 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
440 gh->proto_discr = GSM48_PDISC_MM_GPRS;
441 gh->msg_type = GSM48_MT_GMM_ID_REQ;
442 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
443 gh->data[0] = id_type & 0xf;
444
Harald Welte96cf2482010-05-31 22:31:04 +0200445 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800446}
447
Harald Welte45fcf682010-07-03 11:09:06 +0200448/* Section 9.4.9: Authentication and Ciphering Request */
449static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
450 uint8_t key_seq, uint8_t algo)
451{
452 struct msgb *msg = gsm48_msgb_alloc();
453 struct gsm48_hdr *gh;
454 struct gsm48_auth_ciph_req *acreq;
455 uint8_t *m_rand, *m_cksn;
456
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200457 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200458 osmo_hexdump(rand, 16));
Harald Welte45fcf682010-07-03 11:09:06 +0200459
460 mmctx2msgid(msg, mm);
461
462 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
463 gh->proto_discr = GSM48_PDISC_MM_GPRS;
464 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
465
466 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
467 acreq->ciph_alg = algo & 0xf;
468 acreq->imeisv_req = 0x1;
469 acreq->force_stby = 0x0;
470 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
471
472 /* Only if authentication is requested we need to set RAND + CKSN */
473 if (rand) {
474 m_rand = msgb_put(msg, 16+1);
475 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
476 memcpy(m_rand+1, rand, 16);
477
Jacob Erlbeck79dcaa42014-12-01 10:36:17 +0100478 m_cksn = msgb_put(msg, 1);
479 m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07);
Harald Welte45fcf682010-07-03 11:09:06 +0200480 }
481
Harald Welte45fcf682010-07-03 11:09:06 +0200482 /* FIXME: make sure we don't send any other messages to the MS */
483
484 return gsm48_gmm_sendmsg(msg, 1, mm);
485}
486
487/* Section 9.4.11: Authentication and Ciphering Reject */
488static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
489{
490 struct msgb *msg = gsm48_msgb_alloc();
491 struct gsm48_hdr *gh;
492
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200493 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte45fcf682010-07-03 11:09:06 +0200494
495 mmctx2msgid(msg, mm);
496
497 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
498 gh->proto_discr = GSM48_PDISC_MM_GPRS;
499 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
500
501 return gsm48_gmm_sendmsg(msg, 0, mm);
502}
503
504/* Section 9.4.10: Authentication and Ciphering Response */
505static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
506 struct msgb *msg)
507{
508 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
509 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
510 struct tlv_parsed tp;
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100511 struct gsm_auth_tuple *at;
512 int rc;
513
514 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n");
515
516 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
517 LOGMMCTXP(LOGL_NOTICE, ctx,
518 "Unexpected Auth & Ciph Response (ignored)\n");
519 return 0;
520 }
Harald Welte45fcf682010-07-03 11:09:06 +0200521
Jacob Erlbeck4d127672014-12-01 12:33:33 +0100522 /* Stop T3360 */
523 mmctx_timer_stop(ctx, 3360);
Harald Welte45fcf682010-07-03 11:09:06 +0200524
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200525 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte45fcf682010-07-03 11:09:06 +0200526 (msg->data + msg->len) - acr->data, 0, 0);
527
528 /* FIXME: compare ac_ref? */
529
530 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
531 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100532 /* TODO: missing mandatory IE, return STATUS or REJ? */
533 LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n");
534 return -EINVAL;
Harald Welte45fcf682010-07-03 11:09:06 +0200535 }
536
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100537 /* Compare SRES with what we expected */
538 LOGMMCTXP(LOGL_DEBUG, ctx, "checking received auth info, SRES = %s\n",
539 osmo_hexdump(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES),
540 TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES)));
541
542 at = &ctx->auth_triplet;
543
544 if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES) != sizeof(at->sres) ||
545 memcmp(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), at->sres,
546 sizeof(at->sres)) != 0) {
547
548 LOGMMCTXP(LOGL_NOTICE, ctx, "Received SRES doesn't match\n");
549 rc = gsm48_tx_gmm_auth_ciph_rej(ctx);
550 mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT");
551 return rc;
552 }
553
554 ctx->is_authenticated = 1;
555
Harald Welte45fcf682010-07-03 11:09:06 +0200556 /* FIXME: enable LLC cipheirng */
Jacob Erlbeckac336982014-12-02 09:47:26 +0100557
558 /* Check if we can let the mobile station enter */
559 return gsm48_gmm_authorize(ctx);
Harald Welte45fcf682010-07-03 11:09:06 +0200560}
561
Harald Welte75bb8202010-03-14 15:45:01 +0800562/* Check if we can already authorize a subscriber */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100563static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte75bb8202010-03-14 15:45:01 +0800564{
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100565 /* Request IMSI and IMEI from the MS if they are unknown */
Harald Welte7e01d8e2010-05-31 20:23:38 +0200566 if (!strlen(ctx->imei)) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200567 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
568 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte64df8ed2010-05-18 17:04:55 +0200569 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200570 }
Harald Welte7e01d8e2010-05-31 20:23:38 +0200571 if (!strlen(ctx->imsi)) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200572 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
573 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte64df8ed2010-05-18 17:04:55 +0200574 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200575 }
Harald Welte75bb8202010-03-14 15:45:01 +0800576
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100577 /* All information required for authentication is available */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100578 ctx->t3370_id_type = GSM_MI_TYPE_NONE;
579
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200580 if (ctx->auth_state == SGSN_AUTH_UNKNOWN) {
581 /* Request authorization, this leads to a call to
Jacob Erlbeck802d78a2014-11-07 14:17:44 +0100582 * sgsn_auth_update which in turn calls
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200583 * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */
584
Jacob Erlbeckb2acd742014-11-13 10:48:39 +0100585 sgsn_auth_request(ctx);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200586 /* Note that gsm48_gmm_authorize can be called recursively via
587 * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED
588 */
589 return 0;
590 }
591
Jacob Erlbeckac336982014-12-02 09:47:26 +0100592 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) {
593 struct gsm_auth_tuple *at = &ctx->auth_triplet;
594
595 mmctx_timer_start(ctx, 3360, GSM0408_T3360_SECS);
596 return gsm48_tx_gmm_auth_ciph_req(ctx, at->rand, at->key_seq,
597 GPRS_ALGO_GEA0);
598 }
599
600 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated &&
601 ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
602 /* Check again for authorization */
603 sgsn_auth_request(ctx);
604 return 0;
605 }
606
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200607 if (ctx->auth_state != SGSN_AUTH_ACCEPTED) {
608 LOGMMCTXP(LOGL_NOTICE, ctx,
609 "authorization is denied, aborting procedure\n");
610 return -EACCES;
Jacob Erlbeck79d438a2014-10-29 22:12:20 +0100611 }
612
613 /* The MS is authorized */
614
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100615 switch (ctx->pending_req) {
616 case 0:
617 LOGMMCTXP(LOGL_INFO, ctx,
618 "no pending request, authorization completed\n");
619 break;
620 case GSM48_MT_GMM_ATTACH_REQ:
621#ifdef PTMSI_ALLOC
622 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
623 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
624 ctx->t3350_mode = GMM_T3350_MODE_ATT;
Jacob Erlbeck67318ef2014-10-28 16:23:46 +0100625#else
626 ctx->mm_state = GMM_REGISTERED_NORMAL;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100627#endif
628
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100629 return gsm48_tx_gmm_att_ack(ctx);
630 default:
631 LOGMMCTXP(LOGL_ERROR, ctx,
632 "only Attach Request is supported yet, "
633 "got request type %u\n", ctx->pending_req);
634 break;
635 }
636
Harald Welte75bb8202010-03-14 15:45:01 +0800637 return 0;
638}
639
Jacob Erlbeckac336982014-12-02 09:47:26 +0100640void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx)
641{
642 ctx->is_authenticated = 0;
643
644 gsm48_gmm_authorize(ctx);
645}
646
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200647void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)
648{
649 switch (ctx->mm_state) {
650 case GMM_COMMON_PROC_INIT:
651 LOGP(DMM, LOGL_NOTICE,
652 "Authorized, continuing procedure, IMSI=%s\n",
653 ctx->imsi);
654 /* Continue with the authorization */
655 gsm48_gmm_authorize(ctx);
656 break;
657 default:
658 LOGP(DMM, LOGL_INFO,
659 "Authorized, ignored, IMSI=%s\n",
660 ctx->imsi);
661 }
662}
663
Jacob Erlbeck41010082015-01-05 17:51:17 +0100664void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200665{
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100666 if (gmm_cause == SGSN_ERROR_CAUSE_NONE)
Jacob Erlbeck41010082015-01-05 17:51:17 +0100667 gmm_cause = GMM_CAUSE_GPRS_NOTALLOWED;
668
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200669 switch (ctx->mm_state) {
670 case GMM_COMMON_PROC_INIT:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100671 LOGMMCTXP(LOGL_NOTICE, ctx,
672 "Not authorized, rejecting ATTACH REQUEST "
673 "with cause '%s' (%d)\n",
674 get_value_string(gsm48_gmm_cause_names, gmm_cause),
675 gmm_cause);
676 gsm48_tx_gmm_att_rej(ctx, gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200677 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");
678 break;
679 case GMM_REGISTERED_NORMAL:
680 case GMM_REGISTERED_SUSPENDED:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100681 LOGMMCTXP(LOGL_NOTICE, ctx,
682 "Authorization lost, detaching "
683 "with cause '%s' (%d)\n",
684 get_value_string(gsm48_gmm_cause_names, gmm_cause),
685 gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200686 gsm48_tx_gmm_detach_req(
Jacob Erlbeck41010082015-01-05 17:51:17 +0100687 ctx, GPRS_DET_T_MT_REATT_NOTREQ, gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200688
689 mm_ctx_cleanup_free(ctx, "auth lost");
690 break;
691 default:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100692 LOGMMCTXP(LOGL_INFO, ctx,
693 "Authorization lost, cause is '%s' (%d)\n",
694 get_value_string(gsm48_gmm_cause_names, gmm_cause),
695 gmm_cause);
696 mm_ctx_cleanup_free(ctx, "auth lost");
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200697 }
698}
699
Jacob Erlbeck41010082015-01-05 17:51:17 +0100700void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100701{
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100702 if (gmm_cause != SGSN_ERROR_CAUSE_NONE) {
Jacob Erlbeck41010082015-01-05 17:51:17 +0100703 LOGMMCTXP(LOGL_INFO, ctx,
704 "Cancelled with cause '%s' (%d), deleting context\n",
705 get_value_string(gsm48_gmm_cause_names, gmm_cause),
706 gmm_cause);
707 gsm0408_gprs_access_denied(ctx, gmm_cause);
708 return;
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100709 }
Jacob Erlbeck41010082015-01-05 17:51:17 +0100710
711 LOGMMCTXP(LOGL_INFO, ctx, "Cancelled, deleting context silently\n");
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100712 mm_ctx_cleanup_free(ctx, "access cancelled");
713}
714
Harald Welte75bb8202010-03-14 15:45:01 +0800715/* Parse Chapter 9.4.13 Identity Response */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200716static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +0800717{
Harald Weltecf3c4142010-04-30 16:33:12 +0200718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +0200719 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte75bb8202010-03-14 15:45:01 +0800720 char mi_string[GSM48_MI_SIZE];
Harald Welte75bb8202010-03-14 15:45:01 +0800721
722 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte75bb8202010-03-14 15:45:01 +0800723 if (!ctx) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200724 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte75bb8202010-03-14 15:45:01 +0800725 return -EINVAL;
726 }
727
Daniel Willmann09827d52014-09-23 18:48:44 +0200728 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
729 mi_type, mi_string);
730
Jacob Erlbeck5233c0c2014-10-22 12:20:26 +0200731 if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) {
732 LOGMMCTXP(LOGL_NOTICE, ctx,
733 "Got unexpected IDENTITY RESPONSE: mi_type=0x%02x MI(%s), "
734 "ignoring message\n",
735 mi_type, mi_string);
736 return -EINVAL;
737 }
738
Harald Welte7e01d8e2010-05-31 20:23:38 +0200739 if (mi_type == ctx->t3370_id_type)
740 mmctx_timer_stop(ctx, 3370);
741
Harald Welte75bb8202010-03-14 15:45:01 +0800742 switch (mi_type) {
743 case GSM_MI_TYPE_IMSI:
744 /* we already have a mm context with current TLLI, but no
745 * P-TMSI / IMSI yet. What we now need to do is to fill
746 * this initial context with data from the HLR */
Harald Weltec6e196f2010-12-24 23:07:18 +0100747 if (strlen(ctx->imsi) == 0) {
748 /* Check if we already have a MM context for this IMSI */
749 struct sgsn_mm_ctx *ictx;
750 ictx = sgsn_mm_ctx_by_imsi(mi_string);
751 if (ictx) {
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200752 /* Handle it like in gsm48_rx_gmm_det_req,
753 * except that no messages are sent to the BSS */
754
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200755 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
756 "p_tmsi_old=0x%08x\n",
757 ictx->p_tmsi);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200758
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200759 mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
Harald Weltec6e196f2010-12-24 23:07:18 +0100760 }
761 }
Jacob Erlbeckaa45c762014-07-08 09:49:07 +0200762 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
Harald Welte75bb8202010-03-14 15:45:01 +0800763 break;
764 case GSM_MI_TYPE_IMEI:
765 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
766 break;
767 case GSM_MI_TYPE_IMEISV:
768 break;
769 }
770
Harald Welte75bb8202010-03-14 15:45:01 +0800771 /* Check if we can let the mobile station enter */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100772 return gsm48_gmm_authorize(ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800773}
774
775/* Section 9.4.1 Attach request */
Harald Welte75871c72010-06-01 11:53:01 +0200776static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
777 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +0800778{
Harald Weltecf3c4142010-04-30 16:33:12 +0200779 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200780 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Weltecca80c92010-05-31 22:12:30 +0200781 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welted85d9a92010-05-02 11:26:34 +0200782 uint16_t drx_par;
783 uint32_t tmsi;
Harald Welte75bb8202010-03-14 15:45:01 +0800784 char mi_string[GSM48_MI_SIZE];
785 struct gprs_ra_id ra_id;
Harald Weltede34afa2010-05-02 12:56:57 +0200786 uint16_t cid;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100787 enum gsm48_gmm_cause reject_cause;
788 int rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800789
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200790 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte75bb8202010-03-14 15:45:01 +0800791
792 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
793 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
794 * or with random TLLI. */
795
Harald Weltede34afa2010-05-02 12:56:57 +0200796 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte75bb8202010-03-14 15:45:01 +0800797
798 /* MS network capability 10.5.5.12 */
799 msnc_len = *cur++;
800 msnc = cur;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400801 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte75bb8202010-03-14 15:45:01 +0800802 goto err_inval;
803 cur += msnc_len;
804
805 /* aTTACH Type 10.5.5.2 */
806 att_type = *cur++ & 0x0f;
807
808 /* DRX parameter 10.5.5.6 */
Harald Weltecca80c92010-05-31 22:12:30 +0200809 drx_par = *cur++ << 8;
810 drx_par |= *cur++;
Harald Welte75bb8202010-03-14 15:45:01 +0800811
812 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
813 mi_len = *cur++;
814 mi = cur;
815 if (mi_len > 8)
816 goto err_inval;
817 mi_type = *mi & GSM_MI_TYPE_MASK;
818 cur += mi_len;
819
820 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
821
Harald Welte8f77f192010-05-17 00:44:57 +0200822 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
823 get_value_string(gprs_att_t_strs, att_type));
Harald Welte75bb8202010-03-14 15:45:01 +0800824
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200825 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte75bb8202010-03-14 15:45:01 +0800826 cur += 6;
827
828 /* MS Radio Access Capability 10.5.5.12a */
Harald Weltecca80c92010-05-31 22:12:30 +0200829 ms_ra_acc_cap_len = *cur++;
830 ms_ra_acc_cap = cur;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400831 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Welteabe06132010-07-03 21:20:06 +0200832 goto err_inval;
Harald Welte45ca5522011-07-27 23:35:38 +0200833 cur += ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +0800834
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200835 LOGPC(DMM, LOGL_INFO, "\n");
836
Harald Welte75bb8202010-03-14 15:45:01 +0800837 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
838
839 switch (mi_type) {
840 case GSM_MI_TYPE_IMSI:
841 /* Try to find MM context based on IMSI */
Harald Welte75871c72010-06-01 11:53:01 +0200842 if (!ctx)
843 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte75bb8202010-03-14 15:45:01 +0800844 if (!ctx) {
845#if 0
846 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
847#else
Harald Welte75bb8202010-03-14 15:45:01 +0800848 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100849 if (!ctx) {
850 reject_cause = GMM_CAUSE_NET_FAIL;
851 goto rejected;
852 }
Harald Welte75bb8202010-03-14 15:45:01 +0800853 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
854#endif
855 }
Harald Weltecf3c4142010-04-30 16:33:12 +0200856 ctx->tlli = msgb_tlli(msg);
Harald Welte75871c72010-06-01 11:53:01 +0200857 ctx->llme = llme;
Harald Welte5c23bfc2010-05-18 12:05:42 +0200858 msgid2mmctx(ctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800859 break;
860 case GSM_MI_TYPE_TMSI:
Harald Welteebe8a6d2010-05-18 19:58:38 +0200861 memcpy(&tmsi, mi+1, 4);
862 tmsi = ntohl(tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800863 /* Try to find MM context based on P-TMSI */
Harald Welte75871c72010-06-01 11:53:01 +0200864 if (!ctx)
865 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800866 if (!ctx) {
Harald Weltec6e196f2010-12-24 23:07:18 +0100867 /* Allocate a context as most of our code expects one.
868 * Context will not have an IMSI ultil ID RESP is received */
Harald Weltecf3c4142010-04-30 16:33:12 +0200869 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte75871c72010-06-01 11:53:01 +0200870 ctx->p_tmsi = tmsi;
Harald Welte75bb8202010-03-14 15:45:01 +0800871 }
Harald Welte75871c72010-06-01 11:53:01 +0200872 ctx->tlli = msgb_tlli(msg);
873 ctx->llme = llme;
874 msgid2mmctx(ctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800875 break;
876 default:
Harald Welte7e01d8e2010-05-31 20:23:38 +0200877 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
878 "MI type %u\n", mi_type);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100879 reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED;
880 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +0800881 }
Harald Weltede34afa2010-05-02 12:56:57 +0200882 /* Update MM Context with currient RA and Cell ID */
883 ctx->ra = ra_id;
884 ctx->cell_id = cid;
Harald Weltecca80c92010-05-31 22:12:30 +0200885 /* Update MM Context with other data */
886 ctx->drx_parms = drx_par;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400887 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte45ca5522011-07-27 23:35:38 +0200888 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
889 ctx->ms_radio_access_capa.len);
Harald Weltecca80c92010-05-31 22:12:30 +0200890 ctx->ms_network_capa.len = msnc_len;
891 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
892
Harald Welte7e01d8e2010-05-31 20:23:38 +0200893#ifdef PTMSI_ALLOC
Harald Welte75871c72010-06-01 11:53:01 +0200894 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Jacob Erlbecke06476a2014-11-06 15:43:10 +0100895 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
896 if (ctx->mm_state != GMM_COMMON_PROC_INIT) {
897 ctx->p_tmsi_old = ctx->p_tmsi;
898 ctx->p_tmsi = sgsn_alloc_ptmsi();
899 }
Jacob Erlbeck67318ef2014-10-28 16:23:46 +0100900 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200901#endif
Harald Welte75871c72010-06-01 11:53:01 +0200902 /* Even if there is no P-TMSI allocated, the MS will switch from
903 * foreign TLLI to local TLLI */
904 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
905
906 /* Inform LLC layer about new TLLI but keep old active */
Harald Welte0c4304f2010-06-30 23:18:57 +0200907 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
908 GPRS_ALGO_GEA0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +0800909
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100910 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
911 return gsm48_gmm_authorize(ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800912
913err_inval:
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200914 LOGPC(DMM, LOGL_INFO, "\n");
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100915 reject_cause = GMM_CAUSE_SEM_INCORR_MSG;
916
917rejected:
918 /* Send ATTACH REJECT */
919 LOGMMCTXP(LOGL_NOTICE, ctx,
920 "Rejecting Attach Request with cause '%s' (%d)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100921 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100922 rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause);
923 if (ctx)
924 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ");
925 else
926 /* TLLI unassignment */
927 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
928
929 return rc;
930
Harald Welte75bb8202010-03-14 15:45:01 +0800931}
932
Harald Welte8f77f192010-05-17 00:44:57 +0200933/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200934static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte8f77f192010-05-17 00:44:57 +0200935{
936 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte8f77f192010-05-17 00:44:57 +0200937 uint8_t detach_type, power_off;
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200938 int rc = 0;
Harald Welte8f77f192010-05-17 00:44:57 +0200939
940 detach_type = gh->data[0] & 0x7;
941 power_off = gh->data[0] & 0x8;
942
943 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
944
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200945 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte8f77f192010-05-17 00:44:57 +0200946 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
947 power_off ? "Power-off" : "");
948
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200949 /* Only send the Detach Accept (MO) if power off isn't indicated,
Jacob Erlbeck97ee3762014-10-21 09:52:05 +0200950 * see 04.08, 4.7.4.1.2/3 for details */
951 if (!power_off) {
952 /* force_stby = 0 */
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200953 if (ctx)
954 rc = gsm48_tx_gmm_det_ack(ctx, 0);
955 else
956 rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0);
Jacob Erlbeck97ee3762014-10-21 09:52:05 +0200957 }
Harald Welte309b5932010-06-28 22:18:53 +0200958
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200959 if (ctx)
960 mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST");
Jacob Erlbeck12396bd2014-09-30 13:51:45 +0200961
Harald Welte309b5932010-06-28 22:18:53 +0200962 return rc;
Harald Welte8f77f192010-05-17 00:44:57 +0200963}
964
Harald Welte75bb8202010-03-14 15:45:01 +0800965/* Chapter 9.4.15: Routing area update accept */
Harald Welte64df8ed2010-05-18 17:04:55 +0200966static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +0800967{
968 struct msgb *msg = gsm48_msgb_alloc();
969 struct gsm48_hdr *gh;
970 struct gsm48_ra_upd_ack *rua;
Harald Welte7e01d8e2010-05-31 20:23:38 +0200971 uint8_t *mid;
Harald Welte75bb8202010-03-14 15:45:01 +0800972
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200973 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte75bb8202010-03-14 15:45:01 +0800974
Harald Welte64df8ed2010-05-18 17:04:55 +0200975 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800976
977 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
978 gh->proto_discr = GSM48_PDISC_MM_GPRS;
979 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
980
981 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
982 rua->force_stby = 0; /* not indicated */
983 rua->upd_result = 0; /* RA updated */
Jacob Erlbeck3dafa862015-01-23 12:32:38 +0100984 rua->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte2351f1f2010-05-02 11:54:55 +0200985
Harald Welte64df8ed2010-05-18 17:04:55 +0200986 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte75bb8202010-03-14 15:45:01 +0800987
Harald Welte7e01d8e2010-05-31 20:23:38 +0200988#if 0
989 /* Optional: P-TMSI signature */
990 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
991 ptsig = msgb_put(msg, 3);
992 ptsig[0] = mm->p_tmsi_sig >> 16;
993 ptsig[1] = mm->p_tmsi_sig >> 8;
994 ptsig[2] = mm->p_tmsi_sig & 0xff;
995#endif
996
997#ifdef PTMSI_ALLOC
998 /* Optional: Allocated P-TMSI */
999 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
1000 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
1001 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
1002#endif
1003
Jacob Erlbeck1063ae02015-01-23 11:44:39 +01001004 /* Optional: Negotiated READY timer value */
1005 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
1006 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
1007
Harald Welte7e01d8e2010-05-31 20:23:38 +02001008 /* Option: MS ID, ... */
Harald Weltecca80c92010-05-31 22:12:30 +02001009 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001010}
1011
1012/* Chapter 9.4.17: Routing area update reject */
Harald Welted85d9a92010-05-02 11:26:34 +02001013static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte75bb8202010-03-14 15:45:01 +08001014{
1015 struct msgb *msg = gsm48_msgb_alloc();
1016 struct gsm48_hdr *gh;
1017
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001018 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001019
Harald Welte2351f1f2010-05-02 11:54:55 +02001020 gmm_copy_id(msg, old_msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001021
1022 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
1023 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1024 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
1025 gh->data[0] = cause;
1026 gh->data[1] = 0; /* ? */
1027
1028 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8a035af2010-05-18 10:57:45 +02001029 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +08001030}
1031
Harald Welted9610722010-06-09 10:28:29 +02001032static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001033 const uint8_t *pdp_status)
Harald Welted9610722010-06-09 10:28:29 +02001034{
1035 struct sgsn_pdp_ctx *pdp, *pdp2;
1036 /* 24.008 4.7.5.1.3: If the PDP context status information element is
1037 * included in ROUTING AREA UPDATE REQUEST message, then the network
1038 * shall deactivate all those PDP contexts locally (without peer to
1039 * peer signalling between the MS and the network), which are not in SM
1040 * state PDP-INACTIVE on network side but are indicated by the MS as
1041 * being in state PDP-INACTIVE. */
1042
1043 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Welte9e7093c2011-08-05 21:23:46 +02001044 if (pdp->nsapi < 8) {
1045 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001046 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Welte9e7093c2011-08-05 21:23:46 +02001047 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1048 pdp->nsapi, pdp_status[1], pdp_status[0]);
1049 sgsn_delete_pdp_ctx(pdp);
1050 }
1051 } else {
1052 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001053 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Welte9e7093c2011-08-05 21:23:46 +02001054 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1055 pdp->nsapi, pdp_status[1], pdp_status[0]);
1056 sgsn_delete_pdp_ctx(pdp);
1057 }
Harald Welted9610722010-06-09 10:28:29 +02001058 }
1059 }
1060}
1061
Harald Welte75bb8202010-03-14 15:45:01 +08001062/* Chapter 9.4.14: Routing area update request */
Harald Welte75871c72010-06-01 11:53:01 +02001063static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1064 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001065{
Harald Weltecf3c4142010-04-30 16:33:12 +02001066 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +02001067 uint8_t *cur = gh->data;
Harald Welted9610722010-06-09 10:28:29 +02001068 uint8_t ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +08001069 struct gprs_ra_id old_ra_id;
Harald Welted9610722010-06-09 10:28:29 +02001070 struct tlv_parsed tp;
Harald Welted85d9a92010-05-02 11:26:34 +02001071 uint8_t upd_type;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001072 enum gsm48_gmm_cause reject_cause;
1073 int rc;
Harald Welte75bb8202010-03-14 15:45:01 +08001074
1075 /* Update Type 10.5.5.18 */
1076 upd_type = *cur++ & 0x0f;
1077
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001078 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte8f77f192010-05-17 00:44:57 +02001079 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte75bb8202010-03-14 15:45:01 +08001080
1081 /* Old routing area identification 10.5.5.15 */
1082 gsm48_parse_ra(&old_ra_id, cur);
1083 cur += 6;
1084
1085 /* MS Radio Access Capability 10.5.5.12a */
Harald Welted9610722010-06-09 10:28:29 +02001086 ms_ra_acc_cap_len = *cur++;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001087 if (ms_ra_acc_cap_len > 52) {
1088 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1089 goto rejected;
1090 }
Harald Welte45ca5522011-07-27 23:35:38 +02001091 cur += ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +08001092
1093 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
1094 * DRX parameter, MS network capability */
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001095 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welted9610722010-06-09 10:28:29 +02001096 (msg->data + msg->len) - cur, 0, 0);
Harald Welte75bb8202010-03-14 15:45:01 +08001097
1098 switch (upd_type) {
1099 case GPRS_UPD_T_RA_LA:
1100 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001101 LOGP(DMM, LOGL_NOTICE, "Update type %i unsupported in Mode III, is your SI13 corrupt?\n", upd_type);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001102 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1103 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +08001104 case GPRS_UPD_T_RA:
1105 case GPRS_UPD_T_PERIODIC:
1106 break;
1107 }
1108
1109 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte51f78ee2010-12-23 23:34:43 +01001110 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte75bb8202010-03-14 15:45:01 +08001111 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte612c3d82011-10-16 18:50:56 +02001112 /* send a XID reset to re-set all LLC sequence numbers
1113 * in the MS */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001114 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte612c3d82011-10-16 18:50:56 +02001115 gprs_llgmm_reset(llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001116 /* The MS has to perform GPRS attach */
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001117 /* Device is still IMSI attached for CS but initiate GPRS ATTACH,
1118 * see GSM 04.08, 4.7.5.1.4 and G.6 */
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001119 reject_cause = GMM_CAUSE_IMPL_DETACHED;
1120 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +08001121 }
1122
Harald Welte66d062a2010-12-26 10:39:26 +01001123 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
1124 msgid2mmctx(mmctx, msg);
1125 /* Bump the statistics of received signalling msgs for this MM context */
1126 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
1127
Harald Weltede34afa2010-05-02 12:56:57 +02001128 /* Update the MM context with the new RA-ID */
1129 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Welte66d062a2010-12-26 10:39:26 +01001130 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Weltede34afa2010-05-02 12:56:57 +02001131 mmctx->tlli = msgb_tlli(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001132 /* FIXME: Update the MM context with the MS radio acc capabilities */
1133 /* FIXME: Update the MM context with the MS network capabilities */
1134
Harald Welteeb471c92010-05-18 14:32:29 +02001135 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
1136
Harald Welte7e01d8e2010-05-31 20:23:38 +02001137#ifdef PTMSI_ALLOC
Jacob Erlbecke06476a2014-11-06 15:43:10 +01001138 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
1139 if (mmctx->mm_state != GMM_COMMON_PROC_INIT) {
1140 mmctx->p_tmsi_old = mmctx->p_tmsi;
1141 mmctx->p_tmsi = sgsn_alloc_ptmsi();
1142 }
Harald Welte7e01d8e2010-05-31 20:23:38 +02001143 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
1144 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
1145 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001146
1147 mmctx->mm_state = GMM_COMMON_PROC_INIT;
1148#else
1149 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1150 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001151#endif
Harald Welte75871c72010-06-01 11:53:01 +02001152 /* Even if there is no P-TMSI allocated, the MS will switch from
1153 * foreign TLLI to local TLLI */
1154 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001155
Harald Welte75871c72010-06-01 11:53:01 +02001156 /* Inform LLC layer about new TLLI but keep old active */
Harald Welte0c4304f2010-06-30 23:18:57 +02001157 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
1158 GPRS_ALGO_GEA0, NULL);
Harald Welte75871c72010-06-01 11:53:01 +02001159
Harald Welted9610722010-06-09 10:28:29 +02001160 /* Look at PDP Context Status IE and see if MS's view of
1161 * activated/deactivated NSAPIs agrees with our view */
1162 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001163 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welted9610722010-06-09 10:28:29 +02001164 process_ms_ctx_status(mmctx, pdp_status);
1165 }
1166
Harald Welte75871c72010-06-01 11:53:01 +02001167 /* Send RA UPDATE ACCEPT */
Harald Welte64df8ed2010-05-18 17:04:55 +02001168 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001169
1170rejected:
1171 /* Send RA UPDATE REJECT */
1172 LOGMMCTXP(LOGL_NOTICE, mmctx,
1173 "Rejecting RA Update Request with cause '%s' (%d)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001174 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001175 rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause);
1176 if (mmctx)
1177 mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ");
1178 else
1179 /* TLLI unassignment */
1180 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0,
1181 NULL);
1182
1183 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +08001184}
1185
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001186static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001187{
1188 struct gsm48_hdr *gh = msgb_l3(msg);
1189
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001190 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001191 get_value_string(gsm48_gmm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001192
1193 return 0;
1194}
1195
1196/* GPRS Mobility Management */
Harald Welte75871c72010-06-01 11:53:01 +02001197static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1198 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001199{
Harald Weltecf3c4142010-04-30 16:33:12 +02001200 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001201 int rc;
1202
Harald Welte75871c72010-06-01 11:53:01 +02001203 /* MMCTX can be NULL when called */
1204
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001205 if (!mmctx &&
1206 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Welte7e01d8e2010-05-31 20:23:38 +02001207 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001208 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001209 /* 4.7.10 */
Jacob Erlbeckb35ee6b2014-10-28 09:47:03 +01001210 if (gh->msg_type == GSM48_MT_GMM_STATUS) {
1211 /* TLLI unassignment */
1212 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1213 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001214 return 0;
Jacob Erlbeckb35ee6b2014-10-28 09:47:03 +01001215 }
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001216
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001217 /* Don't reply or establish a LLME on DETACH_ACK */
1218 if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) {
1219 /* TLLI unassignment */
1220 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1221 GPRS_ALGO_GEA0, NULL);
1222 }
1223
Jacob Erlbeck218ed402014-11-11 08:55:13 +01001224 gprs_llgmm_reset(llme);
1225
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001226 /* Don't force it into re-attachment */
1227 if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) {
Jacob Erlbeck42d284f2014-10-21 13:09:55 +02001228 /* Handle Detach Request */
1229 rc = gsm48_rx_gmm_det_req(NULL, msg);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001230
1231 /* TLLI unassignment */
1232 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1233 GPRS_ALGO_GEA0, NULL);
1234 return rc;
1235 }
1236
1237 /* Force the MS to re-attach */
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001238 rc = sgsn_force_reattach_oldmsg(msg);
1239
1240 /* TLLI unassignment */
1241 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1242 GPRS_ALGO_GEA0, NULL);
1243 return rc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001244 }
1245
Harald Welte75bb8202010-03-14 15:45:01 +08001246 switch (gh->msg_type) {
1247 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte75871c72010-06-01 11:53:01 +02001248 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001249 break;
1250 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte75871c72010-06-01 11:53:01 +02001251 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001252 break;
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001253 /* For all the following types mmctx can not be NULL */
Harald Welte75bb8202010-03-14 15:45:01 +08001254 case GSM48_MT_GMM_ID_RESP:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001255 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001256 break;
1257 case GSM48_MT_GMM_STATUS:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001258 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001259 break;
Harald Welte8f77f192010-05-17 00:44:57 +02001260 case GSM48_MT_GMM_DETACH_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001261 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte8f77f192010-05-17 00:44:57 +02001262 break;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001263 case GSM48_MT_GMM_DETACH_ACK:
1264 LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n");
1265 mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK");
1266 rc = 0;
1267 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001268 case GSM48_MT_GMM_ATTACH_COMPL:
1269 /* only in case SGSN offered new P-TMSI */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001270 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001271 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001272 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001273 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001274 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001275 /* Unassign the old TLLI */
1276 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001277 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1278 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001279 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001280 rc = 0;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001281 break;
Harald Welteebe8a6d2010-05-18 19:58:38 +02001282 case GSM48_MT_GMM_RA_UPD_COMPL:
1283 /* only in case SGSN offered new P-TMSI */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001284 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001285 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001286 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001287 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001288 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001289 /* Unassign the old TLLI */
1290 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001291 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1292 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001293 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001294 rc = 0;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001295 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001296 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001297 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001298 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001299 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001300 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001301 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001302 /* Unassign the old TLLI */
1303 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001304 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001305 rc = 0;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001306 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001307 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte45fcf682010-07-03 11:09:06 +02001308 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001309 break;
1310 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001311 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001312 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001313 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001314 break;
1315 }
1316
1317 return rc;
1318}
1319
Harald Welte7e01d8e2010-05-31 20:23:38 +02001320static void mmctx_timer_cb(void *_mm)
1321{
1322 struct sgsn_mm_ctx *mm = _mm;
Jacob Erlbeck4d127672014-12-01 12:33:33 +01001323 struct gsm_auth_tuple *at;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001324
1325 mm->num_T_exp++;
1326
1327 switch (mm->T) {
1328 case 3350: /* waiting for ATTACH COMPLETE */
1329 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001330 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Jacob Erlbeck25335882014-11-03 10:12:52 +01001331 mm_ctx_cleanup_free(mm, "T3350");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001332 /* FIXME: should we return some error? */
1333 break;
1334 }
1335 /* re-transmit the respective msg and re-start timer */
1336 switch (mm->t3350_mode) {
1337 case GMM_T3350_MODE_ATT:
1338 gsm48_tx_gmm_att_ack(mm);
1339 break;
1340 case GMM_T3350_MODE_RAU:
1341 gsm48_tx_gmm_ra_upd_ack(mm);
1342 break;
1343 case GMM_T3350_MODE_PTMSI_REALL:
1344 /* FIXME */
1345 break;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001346 case GMM_T3350_MODE_NONE:
1347 LOGMMCTXP(LOGL_NOTICE, mm,
1348 "T3350 mode wasn't set, ignoring timeout\n");
1349 break;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001350 }
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001351 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001352 break;
Harald Welte45fcf682010-07-03 11:09:06 +02001353 case 3360: /* waiting for AUTH AND CIPH RESP */
1354 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001355 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Jacob Erlbeck25335882014-11-03 10:12:52 +01001356 mm_ctx_cleanup_free(mm, "T3360");
Harald Welte45fcf682010-07-03 11:09:06 +02001357 break;
1358 }
Jacob Erlbeck4d127672014-12-01 12:33:33 +01001359 /* Re-transmit the respective msg and re-start timer */
1360 if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
1361 LOGMMCTXP(LOGL_ERROR, mm,
1362 "timeout: invalid auth triplet reference\n");
1363 mm_ctx_cleanup_free(mm, "T3360");
1364 break;
1365 }
1366 at = &mm->auth_triplet;
1367
1368 gsm48_tx_gmm_auth_ciph_req(mm, at->rand, at->key_seq, GPRS_ALGO_GEA0);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001369 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte45fcf682010-07-03 11:09:06 +02001370 break;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001371 case 3370: /* waiting for IDENTITY RESPONSE */
1372 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001373 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001374 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
Jacob Erlbeck25335882014-11-03 10:12:52 +01001375 mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001376 break;
1377 }
1378 /* re-tranmit IDENTITY REQUEST and re-start timer */
1379 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001380 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001381 break;
1382 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001383 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Welte7e01d8e2010-05-31 20:23:38 +02001384 mm->T);
1385 }
1386}
1387
1388/* GPRS SESSION MANAGEMENT */
1389
Harald Welte94ecef32010-12-24 21:13:26 +01001390static void pdpctx_timer_cb(void *_mm);
1391
1392static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1393 unsigned int seconds)
1394{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001395 if (osmo_timer_pending(&pdp->timer))
Harald Welte94ecef32010-12-24 21:13:26 +01001396 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1397 "timer %u pending\n", T, pdp->T);
1398 pdp->T = T;
1399 pdp->num_T_exp = 0;
1400
1401 /* FIXME: we should do this only once ? */
1402 pdp->timer.data = pdp;
1403 pdp->timer.cb = &pdpctx_timer_cb;
1404
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001405 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Welte94ecef32010-12-24 21:13:26 +01001406}
1407
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001408#if 0
Harald Welte2aea7492010-05-02 23:11:50 +02001409static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1410{
1411 uint8_t v[6];
1412
1413 v[0] = PDP_TYPE_ORG_IETF;
1414 v[1] = PDP_TYPE_N_IETF_IPv4;
1415 *(uint32_t *)(v+2) = htonl(ipaddr);
1416
1417 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1418}
1419
1420static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1421{
1422 uint8_t v[2];
1423
1424 v[0] = PDP_TYPE_ORG_ETSI;
1425 v[1] = PDP_TYPE_N_ETSI_PPP;
1426
1427 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1428}
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001429#endif
Harald Welte2aea7492010-05-02 23:11:50 +02001430
Harald Welte75bb8202010-03-14 15:45:01 +08001431/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte29d838e2010-05-18 10:35:06 +02001432int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte75bb8202010-03-14 15:45:01 +08001433{
Harald Welte75bb8202010-03-14 15:45:01 +08001434 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte75bb8202010-03-14 15:45:01 +08001435 struct gsm48_hdr *gh;
Harald Welte29d838e2010-05-18 10:35:06 +02001436 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte75bb8202010-03-14 15:45:01 +08001437
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001438 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001439
Harald Welte29d838e2010-05-18 10:35:06 +02001440 mmctx2msgid(msg, pdp->mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001441
1442 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1443 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1444 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte2aea7492010-05-02 23:11:50 +02001445
1446 /* Negotiated LLC SAPI */
Harald Welte29d838e2010-05-18 10:35:06 +02001447 msgb_v_put(msg, pdp->sapi);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001448
Harald Welte540dcbc2010-05-18 13:20:08 +02001449 /* FIXME: copy QoS parameters from original request */
1450 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1451 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001452
Harald Welte2aea7492010-05-02 23:11:50 +02001453 /* Radio priority 10.5.7.2 */
Harald Welte29d838e2010-05-18 10:35:06 +02001454 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001455
Harald Welte2aea7492010-05-02 23:11:50 +02001456 /* PDP address */
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001457 /* Highest 4 bits of first byte need to be set to 1, otherwise
1458 * the IE is identical with the 04.08 PDP Address IE */
1459 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte29d838e2010-05-18 10:35:06 +02001460 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1461 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001462 pdp->lib->eua.v[0] |= 0xf0;
1463
Harald Welteeb471c92010-05-18 14:32:29 +02001464 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freyther1b51fc22014-04-04 11:55:21 +02001465 if (pdp->lib->pco_req.l)
Harald Welte29d838e2010-05-18 10:35:06 +02001466 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Weltef2d66582010-05-18 14:36:11 +02001467 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001468
Harald Welte2aea7492010-05-02 23:11:50 +02001469 /* Optional: Packet Flow Identifier */
Harald Welte75bb8202010-03-14 15:45:01 +08001470
Harald Welte8a035af2010-05-18 10:57:45 +02001471 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001472}
1473
Harald Welte29d838e2010-05-18 10:35:06 +02001474/* Section 9.5.3: Activate PDP Context reject */
1475int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1476 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1477{
1478 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte29d838e2010-05-18 10:35:06 +02001479 struct gsm48_hdr *gh;
1480 uint8_t transaction_id = tid ^ 0x8; /* flip */
1481
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001482 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte29d838e2010-05-18 10:35:06 +02001483
1484 mmctx2msgid(msg, mm);
1485
1486 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1487 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1488 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1489
1490 msgb_v_put(msg, cause);
1491 if (pco_len && pco_v)
1492 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1493
Harald Welte8a035af2010-05-18 10:57:45 +02001494 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte29d838e2010-05-18 10:35:06 +02001495}
1496
Harald Welte94ecef32010-12-24 21:13:26 +01001497/* Section 9.5.8: Deactivate PDP Context Request */
1498static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1499 uint8_t sm_cause)
1500{
1501 struct msgb *msg = gsm48_msgb_alloc();
1502 struct gsm48_hdr *gh;
1503 uint8_t transaction_id = tid ^ 0x8; /* flip */
1504
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001505 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001506
1507 mmctx2msgid(msg, mm);
1508
1509 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1510 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1511 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1512
1513 msgb_v_put(msg, sm_cause);
1514
1515 return gsm48_gmm_sendmsg(msg, 0, mm);
1516}
1517int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1518{
1519 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1520
1521 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1522}
1523
Harald Welte75bb8202010-03-14 15:45:01 +08001524/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte3b807312010-06-08 18:08:59 +02001525static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte75bb8202010-03-14 15:45:01 +08001526{
Harald Welte75bb8202010-03-14 15:45:01 +08001527 struct msgb *msg = gsm48_msgb_alloc();
1528 struct gsm48_hdr *gh;
Harald Welte3b807312010-06-08 18:08:59 +02001529 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte75bb8202010-03-14 15:45:01 +08001530
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001531 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001532
Harald Welte3b807312010-06-08 18:08:59 +02001533 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001534
1535 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1536 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1537 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1538
Harald Welte3b807312010-06-08 18:08:59 +02001539 return gsm48_gmm_sendmsg(msg, 0, mm);
1540}
1541int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1542{
1543 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte75bb8202010-03-14 15:45:01 +08001544}
1545
1546/* Section 9.5.1: Activate PDP Context Request */
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001547static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1548 struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001549{
Harald Weltecf3c4142010-04-30 16:33:12 +02001550 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001551 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteb1fd23a2010-05-02 21:07:14 +02001552 uint8_t req_qos_len, req_pdpa_len;
1553 uint8_t *req_qos, *req_pdpa;
1554 struct tlv_parsed tp;
Harald Welte29d838e2010-05-18 10:35:06 +02001555 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte8bf65d02010-07-02 10:17:24 +02001556 struct sgsn_ggsn_ctx *ggsn;
Harald Welte29d838e2010-05-18 10:35:06 +02001557 struct sgsn_pdp_ctx *pdp;
Harald Welte75bb8202010-03-14 15:45:01 +08001558
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001559 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte29d838e2010-05-18 10:35:06 +02001560 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8a035af2010-05-18 10:57:45 +02001561
Harald Welteb7b0fd72010-07-02 22:52:04 +02001562 /* FIXME: length checks! */
Harald Welteb1fd23a2010-05-02 21:07:14 +02001563 req_qos_len = act_req->data[0];
1564 req_qos = act_req->data + 1; /* 10.5.6.5 */
1565 req_pdpa_len = act_req->data[1 + req_qos_len];
1566 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte75bb8202010-03-14 15:45:01 +08001567
Harald Welteb7b0fd72010-07-02 22:52:04 +02001568 /* Optional: Access Point Name, Protocol Config Options */
1569 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1570 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1571 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1572 else
1573 memset(&tp, 0, sizeof(tp));
1574
Harald Welteb1fd23a2010-05-02 21:07:14 +02001575 switch (req_pdpa[0] & 0xf) {
1576 case 0x0:
1577 DEBUGPC(DMM, "ETSI ");
1578 break;
1579 case 0x1:
1580 DEBUGPC(DMM, "IETF ");
1581 break;
1582 case 0xf:
1583 DEBUGPC(DMM, "Empty ");
1584 break;
1585 }
1586
1587 switch (req_pdpa[1]) {
1588 case 0x21:
1589 DEBUGPC(DMM, "IPv4 ");
1590 if (req_pdpa_len >= 6) {
1591 struct in_addr ia;
1592 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1593 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1594 }
1595 break;
1596 case 0x57:
1597 DEBUGPC(DMM, "IPv6 ");
1598 if (req_pdpa_len >= 18) {
1599 /* FIXME: print IPv6 address */
1600 }
1601 break;
1602 default:
1603 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1604 break;
1605 }
1606
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001607 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welteeb471c92010-05-18 14:32:29 +02001608
Harald Welte8f77f192010-05-17 00:44:57 +02001609 /* put the non-TLV elements in the TLV parser structure to
1610 * pass them on to the SGSN / GTP code */
1611 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1612 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1613 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1614 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1615
Harald Welte29d838e2010-05-18 10:35:06 +02001616 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welteb1fd23a2010-05-02 21:07:14 +02001617 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte29d838e2010-05-18 10:35:06 +02001618
1619 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1620 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1621 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1622 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1623 GSM_CAUSE_INV_MAND_INFO,
1624 0, NULL);
1625 }
1626
1627 /* Check if NSAPI is already in use */
Harald Welte5fbac982010-07-02 10:12:58 +02001628 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1629 if (pdp) {
1630 /* We already have a PDP context for this TLLI + NSAPI tuple */
1631 if (pdp->sapi == act_req->req_llc_sapi &&
1632 pdp->ti == transaction_id) {
1633 /* This apparently is a re-transmission of a PDP CTX
1634 * ACT REQ (our ACT ACK must have got dropped) */
1635 return gsm48_tx_gsm_act_pdp_acc(pdp);
1636 }
1637
1638 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte29d838e2010-05-18 10:35:06 +02001639 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1640 GSM_CAUSE_NSAPI_IN_USE,
1641 0, NULL);
1642 }
Harald Welte75bb8202010-03-14 15:45:01 +08001643
Harald Weltea456a232010-07-02 10:15:49 +02001644 /* Only increment counter for a real activation, after we checked
1645 * for re-transmissions */
1646 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1647
Harald Welte8bf65d02010-07-02 10:17:24 +02001648 ggsn = sgsn_ggsn_ctx_by_id(0);
1649 if (!ggsn) {
1650 LOGP(DGPRS, LOGL_ERROR, "No GGSN context 0 found!\n");
1651 return -EIO;
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001652 }
Harald Welte8bf65d02010-07-02 10:17:24 +02001653 ggsn->gsn = sgsn->gsn;
1654 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1655 if (!pdp)
1656 return -1;
1657
1658 /* Store SAPI and Transaction Identifier */
1659 pdp->sapi = act_req->req_llc_sapi;
1660 pdp->ti = transaction_id;
1661
Harald Welte64df8ed2010-05-18 17:04:55 +02001662 return 0;
Harald Welte75bb8202010-03-14 15:45:01 +08001663}
1664
1665/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welteeb471c92010-05-18 14:32:29 +02001666static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001667{
Harald Weltecf3c4142010-04-30 16:33:12 +02001668 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeb471c92010-05-18 14:32:29 +02001669 uint8_t transaction_id = (gh->proto_discr >> 4);
1670 struct sgsn_pdp_ctx *pdp;
Harald Welte75bb8202010-03-14 15:45:01 +08001671
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001672 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001673 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001674
Harald Welteeb471c92010-05-18 14:32:29 +02001675 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1676 if (!pdp) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001677 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welteeb471c92010-05-18 14:32:29 +02001678 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1679 mm->imsi, transaction_id);
Harald Welte3b807312010-06-08 18:08:59 +02001680 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welteeb471c92010-05-18 14:32:29 +02001681 }
1682
1683 return sgsn_delete_pdp_ctx(pdp);
Harald Welte75bb8202010-03-14 15:45:01 +08001684}
1685
Harald Welte94ecef32010-12-24 21:13:26 +01001686/* Section 9.5.9: Deactivate PDP Context Accept */
1687static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1688{
1689 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1690 uint8_t transaction_id = (gh->proto_discr >> 4);
1691 struct sgsn_pdp_ctx *pdp;
1692
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001693 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001694
1695 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1696 if (!pdp) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001697 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Welte94ecef32010-12-24 21:13:26 +01001698 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1699 mm->imsi, transaction_id);
1700 return 0;
1701 }
1702
1703 return sgsn_delete_pdp_ctx(pdp);
1704}
1705
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001706static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001707{
1708 struct gsm48_hdr *gh = msgb_l3(msg);
1709
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001710 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001711 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001712
1713 return 0;
1714}
1715
Harald Welte94ecef32010-12-24 21:13:26 +01001716static void pdpctx_timer_cb(void *_pdp)
1717{
1718 struct sgsn_pdp_ctx *pdp = _pdp;
1719
1720 pdp->num_T_exp++;
1721
1722 switch (pdp->T) {
1723 case 3395: /* waiting for PDP CTX DEACT ACK */
1724 if (pdp->num_T_exp >= 4) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001725 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001726 pdp->state = PDP_STATE_INACTIVE;
1727 sgsn_delete_pdp_ctx(pdp);
1728 break;
1729 }
1730 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001731 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Welte94ecef32010-12-24 21:13:26 +01001732 break;
1733 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001734 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Welte94ecef32010-12-24 21:13:26 +01001735 pdp->T);
1736 }
1737}
1738
1739
Harald Welte75bb8202010-03-14 15:45:01 +08001740/* GPRS Session Management */
Harald Welte75871c72010-06-01 11:53:01 +02001741static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1742 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001743{
Harald Weltecf3c4142010-04-30 16:33:12 +02001744 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001745 int rc;
1746
Harald Welte75871c72010-06-01 11:53:01 +02001747 /* MMCTX can be NULL when called */
1748
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001749 if (!mmctx) {
1750 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001751 /* 6.1.3.6 */
1752 if (gh->msg_type == GSM48_MT_GSM_STATUS)
1753 return 0;
1754
1755 return sgsn_force_reattach_oldmsg(msg);
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001756 }
1757
Harald Welte75bb8202010-03-14 15:45:01 +08001758 switch (gh->msg_type) {
1759 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001760 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001761 break;
1762 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001763 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welteeb471c92010-05-18 14:32:29 +02001764 break;
Harald Welte94ecef32010-12-24 21:13:26 +01001765 case GSM48_MT_GSM_DEACT_PDP_ACK:
1766 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1767 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001768 case GSM48_MT_GSM_STATUS:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001769 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001770 break;
1771 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1772 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1773 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001774 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001775 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001776 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001777 break;
1778 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001779 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001780 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001781 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001782 break;
1783
1784 }
1785
1786 return rc;
1787}
1788
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001789int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg)
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001790{
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001791 int rc;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001792 gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM);
1793
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001794 rc = gsm48_tx_gmm_detach_req_oldmsg(
1795 msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001796
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001797 return rc;
1798}
1799
1800int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx)
1801{
1802 int rc;
1803 gprs_llgmm_reset(mmctx->llme);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001804
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001805 rc = gsm48_tx_gmm_detach_req(
1806 mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001807
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001808 mm_ctx_cleanup_free(mmctx, "forced reattach");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001809
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001810 return rc;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001811}
1812
Harald Welte75bb8202010-03-14 15:45:01 +08001813/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte75871c72010-06-01 11:53:01 +02001814int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001815{
Harald Weltecf3c4142010-04-30 16:33:12 +02001816 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +02001817 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001818 struct sgsn_mm_ctx *mmctx;
1819 struct gprs_ra_id ra_id;
Harald Welte75bb8202010-03-14 15:45:01 +08001820 int rc = -EINVAL;
1821
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001822 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1823 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8a035af2010-05-18 10:57:45 +02001824 if (mmctx) {
Harald Welte29d838e2010-05-18 10:35:06 +02001825 msgid2mmctx(mmctx, msg);
Harald Welte8a035af2010-05-18 10:57:45 +02001826 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte75871c72010-06-01 11:53:01 +02001827 mmctx->llme = llme;
Harald Welte8a035af2010-05-18 10:57:45 +02001828 }
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001829
1830 /* MMCTX can be NULL */
1831
Harald Welte75bb8202010-03-14 15:45:01 +08001832 switch (pdisc) {
1833 case GSM48_PDISC_MM_GPRS:
Harald Welte75871c72010-06-01 11:53:01 +02001834 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001835 break;
1836 case GSM48_PDISC_SM_GPRS:
Harald Welte75871c72010-06-01 11:53:01 +02001837 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001838 break;
1839 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001840 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001841 pdisc);
Harald Welte29d838e2010-05-18 10:35:06 +02001842 /* FIXME: return status message */
Harald Welte75bb8202010-03-14 15:45:01 +08001843 break;
1844 }
1845
Jacob Erlbeck12396bd2014-09-30 13:51:45 +02001846 /* MMCTX can be invalid */
1847
Harald Welte75bb8202010-03-14 15:45:01 +08001848 return rc;
1849}
Harald Welte50264822010-06-09 11:22:47 +02001850
1851int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1852{
1853 struct sgsn_mm_ctx *mmctx;
1854
1855 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1856 if (!mmctx) {
1857 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1858 "TLLI=%08x\n", tlli);
1859 return -EINVAL;
1860 }
1861
Jacob Erlbeck764b8d52014-12-22 17:58:18 +01001862 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1863 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001864 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte50264822010-06-09 11:22:47 +02001865 "!= REGISTERED (TLLI=%08x)\n", tlli);
1866 return -EINVAL;
1867 }
1868
1869 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1870 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1871 return 0;
1872}
1873
1874int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1875 uint8_t suspend_ref)
1876{
1877 struct sgsn_mm_ctx *mmctx;
1878
1879 /* FIXME: make use of suspend reference? */
1880
1881 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1882 if (!mmctx) {
1883 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1884 "TLLI=%08x\n", tlli);
1885 return -EINVAL;
1886 }
1887
Jacob Erlbeck764b8d52014-12-22 17:58:18 +01001888 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1889 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001890 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte50264822010-06-09 11:22:47 +02001891 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1892 /* FIXME: should we not simply ignore it? */
1893 return -EINVAL;
1894 }
1895
1896 /* Transition from SUSPENDED to NORMAL */
1897 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1898 return 0;
1899}