blob: 8ada3d41b3ebb4223f3b41fbc3bea19f39d9837c [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>
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +020055#include <openbsc/signal.h>
Harald Weltec1f6bfe2010-05-17 22:58:03 +020056
Harald Welte29d838e2010-05-18 10:35:06 +020057#include <pdp.h>
58
Harald Welte7e01d8e2010-05-31 20:23:38 +020059#define PTMSI_ALLOC
60
Jacob Erlbeck3dafa862015-01-23 12:32:38 +010061/* Section 11.2.2 / Table 11.3a GPRS Mobility management timers – MS side */
62#define GSM0408_T3312_SECS (10*60) /* periodic RAU interval, default 54min */
63
Harald Welted4b9b022013-07-21 17:41:46 +080064/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Welte7e01d8e2010-05-31 20:23:38 +020065#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
66#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
67#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
68#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
69
Harald Welted4b9b022013-07-21 17:41:46 +080070/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Welte7e01d8e2010-05-31 20:23:38 +020071#define GSM0408_T3313_SECS 30 /* waiting for paging response */
Jacob Erlbeck1063ae02015-01-23 11:44:39 +010072#define GSM0408_T3314_SECS 44 /* force to STBY on expiry, Ready timer */
Harald Welte7e01d8e2010-05-31 20:23:38 +020073#define GSM0408_T3316_SECS 44
Jacob Erlbecke6140d02015-01-23 11:33:51 +010074#define GSM0408_MOBILE_REACHABLE_SECS (GSM0408_T3312_SECS + 4 * 60)
Harald Welte7e01d8e2010-05-31 20:23:38 +020075
Harald Welte94ecef32010-12-24 21:13:26 +010076/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
77#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
78#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
79#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
80#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
81
Harald Weltec1f6bfe2010-05-17 22:58:03 +020082extern struct sgsn_instance *sgsn;
Harald Welte75bb8202010-03-14 15:45:01 +080083
Harald Welted9610722010-06-09 10:28:29 +020084static const struct tlv_definition gsm48_gmm_att_tlvdef = {
85 .def = {
Harald Welte45fcf682010-07-03 11:09:06 +020086 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welted9610722010-06-09 10:28:29 +020087 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
88 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +020089 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
90 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
91 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welted9610722010-06-09 10:28:29 +020092 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +020093 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welted9610722010-06-09 10:28:29 +020094 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
95 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
96 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
97 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
98 },
99};
100
101static const struct tlv_definition gsm48_sm_att_tlvdef = {
102 .def = {
103 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
104 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
105 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
106 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
107 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
108 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte45fcf682010-07-03 11:09:06 +0200109 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
110 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welted9610722010-06-09 10:28:29 +0200111 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
112 },
113};
114
Jacob Erlbeckac336982014-12-02 09:47:26 +0100115static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx);
116
Harald Welte8f77f192010-05-17 00:44:57 +0200117/* Our implementation, should be kept in SGSN */
Harald Welte75bb8202010-03-14 15:45:01 +0800118
Harald Welte7e01d8e2010-05-31 20:23:38 +0200119static void mmctx_timer_cb(void *_mm);
120
121static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
122 unsigned int seconds)
123{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200124 if (osmo_timer_pending(&mm->timer))
Harald Welte7e01d8e2010-05-31 20:23:38 +0200125 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
126 "timer %u pending\n", T, mm->T);
127 mm->T = T;
128 mm->num_T_exp = 0;
129
130 /* FIXME: we should do this only once ? */
131 mm->timer.data = mm;
132 mm->timer.cb = &mmctx_timer_cb;
133
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200134 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200135}
136
137static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
138{
139 if (mm->T != T)
140 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
141 "%u is running\n", T, mm->T);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200142 osmo_timer_del(&mm->timer);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200143}
144
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100145time_t gprs_max_time_to_idle(void)
146{
147 return GSM0408_T3314_SECS + GSM0408_MOBILE_REACHABLE_SECS;
148}
149
Harald Welte75bb8202010-03-14 15:45:01 +0800150/* Send a message through the underlying layer */
Harald Welte8a035af2010-05-18 10:57:45 +0200151static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +0200152 struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +0800153{
Harald Welte8a035af2010-05-18 10:57:45 +0200154 if (mm)
155 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
156
Harald Welte2351f1f2010-05-02 11:54:55 +0200157 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Weltecca80c92010-05-31 22:12:30 +0200158 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800159}
160
Harald Welte2351f1f2010-05-02 11:54:55 +0200161/* copy identifiers from old message to new message, this
162 * is required so lower layers can route it correctly */
163static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
164{
165 msgb_tlli(msg) = msgb_tlli(old);
166 msgb_bvci(msg) = msgb_bvci(old);
167 msgb_nsei(msg) = msgb_nsei(old);
168}
169
Harald Welte29d838e2010-05-18 10:35:06 +0200170/* Store BVCI/NSEI in MM context */
171static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
172{
173 mm->bvci = msgb_bvci(msg);
174 mm->nsei = msgb_nsei(msg);
175}
176
177/* Store BVCI/NSEI in MM context */
178static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
179{
180 msgb_tlli(msg) = mm->tlli;
181 msgb_bvci(msg) = mm->bvci;
182 msgb_nsei(msg) = mm->nsei;
183}
184
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200185static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text)
186{
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100187 LOGMMCTXP(LOGL_INFO, ctx, "Cleaning MM context due to %s\n", log_text);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200188
189 /* Mark MM state as deregistered */
190 ctx->mm_state = GMM_DEREGISTERED;
191
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100192 sgsn_mm_ctx_cleanup_free(ctx);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200193}
194
Harald Welte263edd12010-06-09 10:51:23 +0200195/* Chapter 9.4.18 */
196static int _tx_status(struct msgb *msg, uint8_t cause,
197 struct sgsn_mm_ctx *mmctx, int sm)
198{
199 struct gsm48_hdr *gh;
200
201 /* MMCTX might be NULL! */
202
203 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100204 get_value_string(gsm48_gmm_cause_names, cause));
Harald Welte263edd12010-06-09 10:51:23 +0200205
206 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
207 if (sm) {
208 gh->proto_discr = GSM48_PDISC_SM_GPRS;
209 gh->msg_type = GSM48_MT_GSM_STATUS;
210 } else {
211 gh->proto_discr = GSM48_PDISC_MM_GPRS;
212 gh->msg_type = GSM48_MT_GMM_STATUS;
213 }
214 gh->data[0] = cause;
215
216 return gsm48_gmm_sendmsg(msg, 0, mmctx);
217}
Jacob Erlbeckce61bb72014-10-29 22:57:30 +0100218
Harald Welte263edd12010-06-09 10:51:23 +0200219static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
220{
221 struct msgb *msg = gsm48_msgb_alloc();
222
223 mmctx2msgid(msg, mmctx);
224 return _tx_status(msg, cause, mmctx, 0);
Harald Welte263edd12010-06-09 10:51:23 +0200225}
Jacob Erlbeckce61bb72014-10-29 22:57:30 +0100226
Harald Welte263edd12010-06-09 10:51:23 +0200227static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
228{
229 struct msgb *msg = gsm48_msgb_alloc();
230
231 mmctx2msgid(msg, mmctx);
232 return _tx_status(msg, cause, mmctx, 1);
Harald Welte263edd12010-06-09 10:51:23 +0200233}
234
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200235static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause,
236 struct sgsn_mm_ctx *mmctx)
237{
238 struct gsm48_hdr *gh;
239
240 /* MMCTX might be NULL! */
241
242 DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n",
243 get_value_string(gprs_det_t_mt_strs, detach_type),
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100244 get_value_string(gsm48_gmm_cause_names, cause));
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200245
246 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
247
248 gh->proto_discr = GSM48_PDISC_MM_GPRS;
249 gh->msg_type = GSM48_MT_GMM_DETACH_REQ;
250 gh->data[0] = detach_type & 0x07;
251
252 msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause);
253
254 return gsm48_gmm_sendmsg(msg, 0, mmctx);
255}
256
257static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx,
258 uint8_t detach_type, uint8_t cause)
259{
260 struct msgb *msg = gsm48_msgb_alloc();
261
262 mmctx2msgid(msg, mmctx);
263 return _tx_detach_req(msg, detach_type, cause, mmctx);
264}
265
266static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg,
267 uint8_t detach_type, uint8_t cause)
268{
269 struct msgb *msg = gsm48_msgb_alloc();
270
271 gmm_copy_id(msg, oldmsg);
272 return _tx_detach_req(msg, detach_type, cause, NULL);
273}
Harald Welte263edd12010-06-09 10:51:23 +0200274
Harald Welte242d1722010-05-03 10:54:35 +0200275static struct gsm48_qos default_qos = {
276 .delay_class = 4, /* best effort */
277 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
278 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
279 .preced_class = GSM48_QOS_PC_NORMAL,
280 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
281 .traf_class = GSM48_QOS_TC_INTERACTIVE,
282 .deliv_order = GSM48_QOS_DO_UNORDERED,
283 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
284 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
285 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
286 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
287 .resid_ber = GSM48_QOS_RBER_5e_2,
288 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
289 .handling_prio = 3,
290 .xfer_delay = 0x10, /* 200ms */
291 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
292 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
293 .sig_ind = 0, /* not optimised for signalling */
294 .max_bitrate_down_ext = 0, /* use octet 9 */
295 .guar_bitrate_down_ext = 0, /* use octet 13 */
296};
297
Harald Welte75bb8202010-03-14 15:45:01 +0800298/* Chapter 9.4.2: Attach accept */
Harald Welte64df8ed2010-05-18 17:04:55 +0200299static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +0800300{
301 struct msgb *msg = gsm48_msgb_alloc();
302 struct gsm48_hdr *gh;
303 struct gsm48_attach_ack *aa;
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200304 uint8_t *mid;
305#if 0
306 uint8_t *ptsig;
307#endif
Harald Welte75bb8202010-03-14 15:45:01 +0800308
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200309 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800310
Harald Welte64df8ed2010-05-18 17:04:55 +0200311 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800312
313 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
314 gh->proto_discr = GSM48_PDISC_MM_GPRS;
315 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
316
317 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
318 aa->force_stby = 0; /* not indicated */
319 aa->att_result = 1; /* GPRS only */
Jacob Erlbeck3dafa862015-01-23 12:32:38 +0100320 aa->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte75bb8202010-03-14 15:45:01 +0800321 aa->radio_prio = 4; /* lowest */
Harald Welte64df8ed2010-05-18 17:04:55 +0200322 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte75bb8202010-03-14 15:45:01 +0800323
Harald Welteebe8a6d2010-05-18 19:58:38 +0200324#if 0
Harald Welte64df8ed2010-05-18 17:04:55 +0200325 /* Optional: P-TMSI signature */
326 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
327 ptsig = msgb_put(msg, 3);
328 ptsig[0] = mm->p_tmsi_sig >> 16;
329 ptsig[1] = mm->p_tmsi_sig >> 8;
330 ptsig[2] = mm->p_tmsi_sig & 0xff;
331
Harald Welte7e01d8e2010-05-31 20:23:38 +0200332#endif
Jacob Erlbeck1063ae02015-01-23 11:44:39 +0100333 /* Optional: Negotiated Ready timer value
334 * (fixed 44s, default value, GSM 04.08, table 11.4a) to safely limit
335 * the inactivity time READY->STANDBY.
336 */
337 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
338 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
Harald Welte64df8ed2010-05-18 17:04:55 +0200339
Harald Welte7e01d8e2010-05-31 20:23:38 +0200340#ifdef PTMSI_ALLOC
Harald Welte64df8ed2010-05-18 17:04:55 +0200341 /* Optional: Allocated P-TMSI */
Harald Welte64df8ed2010-05-18 17:04:55 +0200342 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
343 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteebe8a6d2010-05-18 19:58:38 +0200344 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
345#endif
Harald Welte7e01d8e2010-05-31 20:23:38 +0200346
Harald Welte64df8ed2010-05-18 17:04:55 +0200347 /* Optional: MS-identity (combined attach) */
348 /* Optional: GMM cause (partial attach result for combined attach) */
349
Harald Weltecca80c92010-05-31 22:12:30 +0200350 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800351}
352
353/* Chapter 9.4.5: Attach reject */
Harald Welte64df8ed2010-05-18 17:04:55 +0200354static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte75bb8202010-03-14 15:45:01 +0800355{
Harald Welte75bb8202010-03-14 15:45:01 +0800356 struct gsm48_hdr *gh;
357
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100358 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 +0800359
Harald Welte75bb8202010-03-14 15:45:01 +0800360 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
361 gh->proto_discr = GSM48_PDISC_MM_GPRS;
362 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
363 gh->data[0] = gmm_cause;
364
Harald Welte8a035af2010-05-18 10:57:45 +0200365 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +0800366}
Harald Welte64df8ed2010-05-18 17:04:55 +0200367static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
368 uint8_t gmm_cause)
369{
370 struct msgb *msg = gsm48_msgb_alloc();
371 gmm_copy_id(msg, old_msg);
372 return _tx_gmm_att_rej(msg, gmm_cause);
373}
374static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
375 uint8_t gmm_cause)
376{
377 struct msgb *msg = gsm48_msgb_alloc();
378 mmctx2msgid(msg, mm);
379 return _tx_gmm_att_rej(msg, gmm_cause);
380}
Harald Welte75bb8202010-03-14 15:45:01 +0800381
Harald Welte8f77f192010-05-17 00:44:57 +0200382/* Chapter 9.4.6.2 Detach accept */
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200383static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby,
384 struct sgsn_mm_ctx *mm)
Harald Welte8f77f192010-05-17 00:44:57 +0200385{
Harald Welte8f77f192010-05-17 00:44:57 +0200386 struct gsm48_hdr *gh;
387
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200388 /* MMCTX might be NULL! */
Harald Welte8f77f192010-05-17 00:44:57 +0200389
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200390 DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby);
Harald Welte64df8ed2010-05-18 17:04:55 +0200391
Harald Welte8f77f192010-05-17 00:44:57 +0200392 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
393 gh->proto_discr = GSM48_PDISC_MM_GPRS;
394 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
395 gh->data[0] = force_stby;
396
Harald Weltecca80c92010-05-31 22:12:30 +0200397 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte8f77f192010-05-17 00:44:57 +0200398}
399
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200400static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
401{
402 struct msgb *msg = gsm48_msgb_alloc();
403
404 mmctx2msgid(msg, mm);
405 return _tx_detach_ack(msg, force_stby, mm);
406}
407
408static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby)
409{
410 struct msgb *msg = gsm48_msgb_alloc();
411
412 gmm_copy_id(msg, oldmsg);
413 return _tx_detach_ack(msg, force_stby, NULL);
414}
415
Harald Welte75bb8202010-03-14 15:45:01 +0800416/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte64df8ed2010-05-18 17:04:55 +0200417static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte75bb8202010-03-14 15:45:01 +0800418{
419 struct msgb *msg = gsm48_msgb_alloc();
420 struct gsm48_hdr *gh;
421
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200422 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte75bb8202010-03-14 15:45:01 +0800423
Harald Welte64df8ed2010-05-18 17:04:55 +0200424 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800425
426 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
427 gh->proto_discr = GSM48_PDISC_MM_GPRS;
428 gh->msg_type = GSM48_MT_GMM_ID_REQ;
429 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
430 gh->data[0] = id_type & 0xf;
431
Harald Welte96cf2482010-05-31 22:31:04 +0200432 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte75bb8202010-03-14 15:45:01 +0800433}
434
Harald Welte45fcf682010-07-03 11:09:06 +0200435/* Section 9.4.9: Authentication and Ciphering Request */
436static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
437 uint8_t key_seq, uint8_t algo)
438{
439 struct msgb *msg = gsm48_msgb_alloc();
440 struct gsm48_hdr *gh;
441 struct gsm48_auth_ciph_req *acreq;
442 uint8_t *m_rand, *m_cksn;
443
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200444 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200445 osmo_hexdump(rand, 16));
Harald Welte45fcf682010-07-03 11:09:06 +0200446
447 mmctx2msgid(msg, mm);
448
449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
450 gh->proto_discr = GSM48_PDISC_MM_GPRS;
451 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
452
453 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
454 acreq->ciph_alg = algo & 0xf;
455 acreq->imeisv_req = 0x1;
456 acreq->force_stby = 0x0;
457 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
458
459 /* Only if authentication is requested we need to set RAND + CKSN */
460 if (rand) {
461 m_rand = msgb_put(msg, 16+1);
462 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
463 memcpy(m_rand+1, rand, 16);
464
Jacob Erlbeck79dcaa42014-12-01 10:36:17 +0100465 m_cksn = msgb_put(msg, 1);
466 m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07);
Harald Welte45fcf682010-07-03 11:09:06 +0200467 }
468
Harald Welte45fcf682010-07-03 11:09:06 +0200469 /* FIXME: make sure we don't send any other messages to the MS */
470
471 return gsm48_gmm_sendmsg(msg, 1, mm);
472}
473
474/* Section 9.4.11: Authentication and Ciphering Reject */
475static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
476{
477 struct msgb *msg = gsm48_msgb_alloc();
478 struct gsm48_hdr *gh;
479
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200480 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte45fcf682010-07-03 11:09:06 +0200481
482 mmctx2msgid(msg, mm);
483
484 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
485 gh->proto_discr = GSM48_PDISC_MM_GPRS;
486 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
487
488 return gsm48_gmm_sendmsg(msg, 0, mm);
489}
490
491/* Section 9.4.10: Authentication and Ciphering Response */
492static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
493 struct msgb *msg)
494{
495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
496 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
497 struct tlv_parsed tp;
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100498 struct gsm_auth_tuple *at;
499 int rc;
500
501 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n");
502
503 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
504 LOGMMCTXP(LOGL_NOTICE, ctx,
505 "Unexpected Auth & Ciph Response (ignored)\n");
506 return 0;
507 }
Harald Welte45fcf682010-07-03 11:09:06 +0200508
Jacob Erlbeck4d127672014-12-01 12:33:33 +0100509 /* Stop T3360 */
510 mmctx_timer_stop(ctx, 3360);
Harald Welte45fcf682010-07-03 11:09:06 +0200511
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200512 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte45fcf682010-07-03 11:09:06 +0200513 (msg->data + msg->len) - acr->data, 0, 0);
514
515 /* FIXME: compare ac_ref? */
516
517 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
518 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100519 /* TODO: missing mandatory IE, return STATUS or REJ? */
520 LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n");
521 return -EINVAL;
Harald Welte45fcf682010-07-03 11:09:06 +0200522 }
523
Jacob Erlbeckc9cb9b02014-12-01 12:44:24 +0100524 /* Compare SRES with what we expected */
525 LOGMMCTXP(LOGL_DEBUG, ctx, "checking received auth info, SRES = %s\n",
526 osmo_hexdump(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES),
527 TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES)));
528
529 at = &ctx->auth_triplet;
530
531 if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES) != sizeof(at->sres) ||
532 memcmp(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), at->sres,
533 sizeof(at->sres)) != 0) {
534
535 LOGMMCTXP(LOGL_NOTICE, ctx, "Received SRES doesn't match\n");
536 rc = gsm48_tx_gmm_auth_ciph_rej(ctx);
537 mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT");
538 return rc;
539 }
540
541 ctx->is_authenticated = 1;
542
Harald Welte45fcf682010-07-03 11:09:06 +0200543 /* FIXME: enable LLC cipheirng */
Jacob Erlbeckac336982014-12-02 09:47:26 +0100544
545 /* Check if we can let the mobile station enter */
546 return gsm48_gmm_authorize(ctx);
Harald Welte45fcf682010-07-03 11:09:06 +0200547}
548
Holger Hans Peter Freythera9f671c2015-05-05 22:52:40 +0200549static void extract_subscr_msisdn(struct sgsn_mm_ctx *ctx)
550{
551 struct gsm_mncc_number called;
552 uint8_t msisdn[sizeof(ctx->subscr->sgsn_data->msisdn) + 1];
553
554 /* Convert MSISDN from encoded to string.. */
555 if (!ctx->subscr)
556 return;
557
558 if (ctx->subscr->sgsn_data->msisdn_len < 1)
559 return;
560
561 /* prepare the data for the decoder */
562 memset(&called, 0, sizeof(called));
563 msisdn[0] = ctx->subscr->sgsn_data->msisdn_len;
564 memcpy(&msisdn[1], ctx->subscr->sgsn_data->msisdn,
565 ctx->subscr->sgsn_data->msisdn_len);
566
567 /* decode the string now */
568 gsm48_decode_called(&called, msisdn);
569
570 /* Prepend a '+' for international numbers */
571 if (called.plan == 1 && called.type == 1) {
572 ctx->msisdn[0] = '+';
573 strncpy(&ctx->msisdn[1], called.number,
574 sizeof(ctx->msisdn) - 1);
575 } else {
576 strncpy(&ctx->msisdn[0], called.number,
577 sizeof(ctx->msisdn) - 1);
578 }
579}
580
Harald Welte75bb8202010-03-14 15:45:01 +0800581/* Check if we can already authorize a subscriber */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100582static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte75bb8202010-03-14 15:45:01 +0800583{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200584#ifndef PTMSI_ALLOC
585 struct sgsn_signal_data sig_data;
586#endif
587
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100588 /* Request IMSI and IMEI from the MS if they are unknown */
Harald Welte7e01d8e2010-05-31 20:23:38 +0200589 if (!strlen(ctx->imei)) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200590 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
591 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte64df8ed2010-05-18 17:04:55 +0200592 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200593 }
Harald Welte7e01d8e2010-05-31 20:23:38 +0200594 if (!strlen(ctx->imsi)) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200595 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
596 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte64df8ed2010-05-18 17:04:55 +0200597 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Welte7e01d8e2010-05-31 20:23:38 +0200598 }
Harald Welte75bb8202010-03-14 15:45:01 +0800599
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100600 /* All information required for authentication is available */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100601 ctx->t3370_id_type = GSM_MI_TYPE_NONE;
602
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200603 if (ctx->auth_state == SGSN_AUTH_UNKNOWN) {
604 /* Request authorization, this leads to a call to
Jacob Erlbeck802d78a2014-11-07 14:17:44 +0100605 * sgsn_auth_update which in turn calls
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200606 * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */
607
Jacob Erlbeckb2acd742014-11-13 10:48:39 +0100608 sgsn_auth_request(ctx);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200609 /* Note that gsm48_gmm_authorize can be called recursively via
610 * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED
611 */
612 return 0;
613 }
614
Jacob Erlbeckac336982014-12-02 09:47:26 +0100615 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) {
616 struct gsm_auth_tuple *at = &ctx->auth_triplet;
617
618 mmctx_timer_start(ctx, 3360, GSM0408_T3360_SECS);
619 return gsm48_tx_gmm_auth_ciph_req(ctx, at->rand, at->key_seq,
620 GPRS_ALGO_GEA0);
621 }
622
623 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated &&
624 ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
625 /* Check again for authorization */
626 sgsn_auth_request(ctx);
627 return 0;
628 }
629
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200630 if (ctx->auth_state != SGSN_AUTH_ACCEPTED) {
631 LOGMMCTXP(LOGL_NOTICE, ctx,
632 "authorization is denied, aborting procedure\n");
633 return -EACCES;
Jacob Erlbeck79d438a2014-10-29 22:12:20 +0100634 }
635
636 /* The MS is authorized */
637
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100638 switch (ctx->pending_req) {
639 case 0:
640 LOGMMCTXP(LOGL_INFO, ctx,
641 "no pending request, authorization completed\n");
642 break;
643 case GSM48_MT_GMM_ATTACH_REQ:
Holger Hans Peter Freythera9f671c2015-05-05 22:52:40 +0200644
645 extract_subscr_msisdn(ctx);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100646#ifdef PTMSI_ALLOC
647 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
648 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
649 ctx->t3350_mode = GMM_T3350_MODE_ATT;
Jacob Erlbeck67318ef2014-10-28 16:23:46 +0100650#else
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200651 memset(&sig_data, 0, sizeof(sig_data));
652 sig_data.mm = mmctx;
653 osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +0100654 ctx->mm_state = GMM_REGISTERED_NORMAL;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100655#endif
656
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100657 return gsm48_tx_gmm_att_ack(ctx);
658 default:
659 LOGMMCTXP(LOGL_ERROR, ctx,
660 "only Attach Request is supported yet, "
661 "got request type %u\n", ctx->pending_req);
662 break;
663 }
664
Harald Welte75bb8202010-03-14 15:45:01 +0800665 return 0;
666}
667
Jacob Erlbeckac336982014-12-02 09:47:26 +0100668void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx)
669{
670 ctx->is_authenticated = 0;
671
672 gsm48_gmm_authorize(ctx);
673}
674
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200675void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)
676{
677 switch (ctx->mm_state) {
678 case GMM_COMMON_PROC_INIT:
679 LOGP(DMM, LOGL_NOTICE,
680 "Authorized, continuing procedure, IMSI=%s\n",
681 ctx->imsi);
682 /* Continue with the authorization */
683 gsm48_gmm_authorize(ctx);
684 break;
685 default:
686 LOGP(DMM, LOGL_INFO,
687 "Authorized, ignored, IMSI=%s\n",
688 ctx->imsi);
689 }
690}
691
Jacob Erlbeck41010082015-01-05 17:51:17 +0100692void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200693{
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100694 if (gmm_cause == SGSN_ERROR_CAUSE_NONE)
Jacob Erlbeck41010082015-01-05 17:51:17 +0100695 gmm_cause = GMM_CAUSE_GPRS_NOTALLOWED;
696
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200697 switch (ctx->mm_state) {
698 case GMM_COMMON_PROC_INIT:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100699 LOGMMCTXP(LOGL_NOTICE, ctx,
700 "Not authorized, rejecting ATTACH REQUEST "
701 "with cause '%s' (%d)\n",
702 get_value_string(gsm48_gmm_cause_names, gmm_cause),
703 gmm_cause);
704 gsm48_tx_gmm_att_rej(ctx, gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200705 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");
706 break;
707 case GMM_REGISTERED_NORMAL:
708 case GMM_REGISTERED_SUSPENDED:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100709 LOGMMCTXP(LOGL_NOTICE, ctx,
710 "Authorization lost, detaching "
711 "with cause '%s' (%d)\n",
712 get_value_string(gsm48_gmm_cause_names, gmm_cause),
713 gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200714 gsm48_tx_gmm_detach_req(
Jacob Erlbeck41010082015-01-05 17:51:17 +0100715 ctx, GPRS_DET_T_MT_REATT_NOTREQ, gmm_cause);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200716
717 mm_ctx_cleanup_free(ctx, "auth lost");
718 break;
719 default:
Jacob Erlbeck41010082015-01-05 17:51:17 +0100720 LOGMMCTXP(LOGL_INFO, ctx,
721 "Authorization lost, cause is '%s' (%d)\n",
722 get_value_string(gsm48_gmm_cause_names, gmm_cause),
723 gmm_cause);
724 mm_ctx_cleanup_free(ctx, "auth lost");
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200725 }
726}
727
Jacob Erlbeck41010082015-01-05 17:51:17 +0100728void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100729{
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100730 if (gmm_cause != SGSN_ERROR_CAUSE_NONE) {
Jacob Erlbeck41010082015-01-05 17:51:17 +0100731 LOGMMCTXP(LOGL_INFO, ctx,
732 "Cancelled with cause '%s' (%d), deleting context\n",
733 get_value_string(gsm48_gmm_cause_names, gmm_cause),
734 gmm_cause);
735 gsm0408_gprs_access_denied(ctx, gmm_cause);
736 return;
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100737 }
Jacob Erlbeck41010082015-01-05 17:51:17 +0100738
739 LOGMMCTXP(LOGL_INFO, ctx, "Cancelled, deleting context silently\n");
Jacob Erlbeckbf0b8742014-11-11 14:47:38 +0100740 mm_ctx_cleanup_free(ctx, "access cancelled");
741}
742
Harald Welte75bb8202010-03-14 15:45:01 +0800743/* Parse Chapter 9.4.13 Identity Response */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200744static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +0800745{
Harald Weltecf3c4142010-04-30 16:33:12 +0200746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +0200747 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte75bb8202010-03-14 15:45:01 +0800748 char mi_string[GSM48_MI_SIZE];
Harald Welte75bb8202010-03-14 15:45:01 +0800749
750 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte75bb8202010-03-14 15:45:01 +0800751 if (!ctx) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200752 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte75bb8202010-03-14 15:45:01 +0800753 return -EINVAL;
754 }
755
Daniel Willmann09827d52014-09-23 18:48:44 +0200756 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
757 mi_type, mi_string);
758
Jacob Erlbeck5233c0c2014-10-22 12:20:26 +0200759 if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) {
760 LOGMMCTXP(LOGL_NOTICE, ctx,
761 "Got unexpected IDENTITY RESPONSE: mi_type=0x%02x MI(%s), "
762 "ignoring message\n",
763 mi_type, mi_string);
764 return -EINVAL;
765 }
766
Harald Welte7e01d8e2010-05-31 20:23:38 +0200767 if (mi_type == ctx->t3370_id_type)
768 mmctx_timer_stop(ctx, 3370);
769
Harald Welte75bb8202010-03-14 15:45:01 +0800770 switch (mi_type) {
771 case GSM_MI_TYPE_IMSI:
772 /* we already have a mm context with current TLLI, but no
773 * P-TMSI / IMSI yet. What we now need to do is to fill
774 * this initial context with data from the HLR */
Harald Weltec6e196f2010-12-24 23:07:18 +0100775 if (strlen(ctx->imsi) == 0) {
776 /* Check if we already have a MM context for this IMSI */
777 struct sgsn_mm_ctx *ictx;
778 ictx = sgsn_mm_ctx_by_imsi(mi_string);
779 if (ictx) {
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200780 /* Handle it like in gsm48_rx_gmm_det_req,
781 * except that no messages are sent to the BSS */
782
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200783 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
784 "p_tmsi_old=0x%08x\n",
785 ictx->p_tmsi);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200786
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200787 mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
Harald Weltec6e196f2010-12-24 23:07:18 +0100788 }
789 }
Jacob Erlbeckb485ee92015-01-26 10:38:12 +0100790 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
Harald Welte75bb8202010-03-14 15:45:01 +0800791 break;
792 case GSM_MI_TYPE_IMEI:
Jacob Erlbeckb485ee92015-01-26 10:38:12 +0100793 strncpy(ctx->imei, mi_string, sizeof(ctx->imei) - 1);
Harald Welte75bb8202010-03-14 15:45:01 +0800794 break;
795 case GSM_MI_TYPE_IMEISV:
796 break;
797 }
798
Harald Welte75bb8202010-03-14 15:45:01 +0800799 /* Check if we can let the mobile station enter */
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100800 return gsm48_gmm_authorize(ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800801}
802
803/* Section 9.4.1 Attach request */
Harald Welte75871c72010-06-01 11:53:01 +0200804static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
805 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +0800806{
Harald Weltecf3c4142010-04-30 16:33:12 +0200807 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200808 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Weltecca80c92010-05-31 22:12:30 +0200809 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welted85d9a92010-05-02 11:26:34 +0200810 uint16_t drx_par;
811 uint32_t tmsi;
Harald Welte75bb8202010-03-14 15:45:01 +0800812 char mi_string[GSM48_MI_SIZE];
813 struct gprs_ra_id ra_id;
Harald Weltede34afa2010-05-02 12:56:57 +0200814 uint16_t cid;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100815 enum gsm48_gmm_cause reject_cause;
816 int rc;
Harald Welte75bb8202010-03-14 15:45:01 +0800817
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200818 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte75bb8202010-03-14 15:45:01 +0800819
820 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
821 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
822 * or with random TLLI. */
823
Harald Weltede34afa2010-05-02 12:56:57 +0200824 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte75bb8202010-03-14 15:45:01 +0800825
826 /* MS network capability 10.5.5.12 */
827 msnc_len = *cur++;
828 msnc = cur;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400829 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte75bb8202010-03-14 15:45:01 +0800830 goto err_inval;
831 cur += msnc_len;
832
833 /* aTTACH Type 10.5.5.2 */
834 att_type = *cur++ & 0x0f;
835
836 /* DRX parameter 10.5.5.6 */
Harald Weltecca80c92010-05-31 22:12:30 +0200837 drx_par = *cur++ << 8;
838 drx_par |= *cur++;
Harald Welte75bb8202010-03-14 15:45:01 +0800839
840 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
841 mi_len = *cur++;
842 mi = cur;
843 if (mi_len > 8)
844 goto err_inval;
845 mi_type = *mi & GSM_MI_TYPE_MASK;
846 cur += mi_len;
847
848 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
849
Harald Welte8f77f192010-05-17 00:44:57 +0200850 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
851 get_value_string(gprs_att_t_strs, att_type));
Harald Welte75bb8202010-03-14 15:45:01 +0800852
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +0200853 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte75bb8202010-03-14 15:45:01 +0800854 cur += 6;
855
856 /* MS Radio Access Capability 10.5.5.12a */
Harald Weltecca80c92010-05-31 22:12:30 +0200857 ms_ra_acc_cap_len = *cur++;
858 ms_ra_acc_cap = cur;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400859 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Welteabe06132010-07-03 21:20:06 +0200860 goto err_inval;
Harald Welte45ca5522011-07-27 23:35:38 +0200861 cur += ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +0800862
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200863 LOGPC(DMM, LOGL_INFO, "\n");
864
Harald Welte75bb8202010-03-14 15:45:01 +0800865 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
866
867 switch (mi_type) {
868 case GSM_MI_TYPE_IMSI:
869 /* Try to find MM context based on IMSI */
Harald Welte75871c72010-06-01 11:53:01 +0200870 if (!ctx)
871 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte75bb8202010-03-14 15:45:01 +0800872 if (!ctx) {
873#if 0
874 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
875#else
Harald Welte75bb8202010-03-14 15:45:01 +0800876 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100877 if (!ctx) {
878 reject_cause = GMM_CAUSE_NET_FAIL;
879 goto rejected;
880 }
Jacob Erlbeckb485ee92015-01-26 10:38:12 +0100881 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
Harald Welte75bb8202010-03-14 15:45:01 +0800882#endif
883 }
Harald Weltecf3c4142010-04-30 16:33:12 +0200884 ctx->tlli = msgb_tlli(msg);
Harald Welte75871c72010-06-01 11:53:01 +0200885 ctx->llme = llme;
Harald Welte5c23bfc2010-05-18 12:05:42 +0200886 msgid2mmctx(ctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800887 break;
888 case GSM_MI_TYPE_TMSI:
Harald Welteebe8a6d2010-05-18 19:58:38 +0200889 memcpy(&tmsi, mi+1, 4);
890 tmsi = ntohl(tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800891 /* Try to find MM context based on P-TMSI */
Harald Welte75871c72010-06-01 11:53:01 +0200892 if (!ctx)
893 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte75bb8202010-03-14 15:45:01 +0800894 if (!ctx) {
Harald Weltec6e196f2010-12-24 23:07:18 +0100895 /* Allocate a context as most of our code expects one.
896 * Context will not have an IMSI ultil ID RESP is received */
Harald Weltecf3c4142010-04-30 16:33:12 +0200897 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte75871c72010-06-01 11:53:01 +0200898 ctx->p_tmsi = tmsi;
Harald Welte75bb8202010-03-14 15:45:01 +0800899 }
Harald Welte75871c72010-06-01 11:53:01 +0200900 ctx->tlli = msgb_tlli(msg);
901 ctx->llme = llme;
902 msgid2mmctx(ctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800903 break;
904 default:
Harald Welte7e01d8e2010-05-31 20:23:38 +0200905 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
906 "MI type %u\n", mi_type);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100907 reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED;
908 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +0800909 }
Harald Weltede34afa2010-05-02 12:56:57 +0200910 /* Update MM Context with currient RA and Cell ID */
911 ctx->ra = ra_id;
912 ctx->cell_id = cid;
Harald Weltecca80c92010-05-31 22:12:30 +0200913 /* Update MM Context with other data */
914 ctx->drx_parms = drx_par;
Alexander Chemeris6824c502013-07-03 10:12:23 +0400915 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte45ca5522011-07-27 23:35:38 +0200916 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
917 ctx->ms_radio_access_capa.len);
Harald Weltecca80c92010-05-31 22:12:30 +0200918 ctx->ms_network_capa.len = msnc_len;
919 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
920
Harald Welte7e01d8e2010-05-31 20:23:38 +0200921#ifdef PTMSI_ALLOC
Harald Welte75871c72010-06-01 11:53:01 +0200922 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Jacob Erlbecke06476a2014-11-06 15:43:10 +0100923 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
924 if (ctx->mm_state != GMM_COMMON_PROC_INIT) {
925 ctx->p_tmsi_old = ctx->p_tmsi;
926 ctx->p_tmsi = sgsn_alloc_ptmsi();
927 }
Jacob Erlbeck67318ef2014-10-28 16:23:46 +0100928 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200929#endif
Harald Welte75871c72010-06-01 11:53:01 +0200930 /* Even if there is no P-TMSI allocated, the MS will switch from
931 * foreign TLLI to local TLLI */
932 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
933
934 /* Inform LLC layer about new TLLI but keep old active */
Harald Welte0c4304f2010-06-30 23:18:57 +0200935 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
936 GPRS_ALGO_GEA0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +0800937
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +0100938 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
939 return gsm48_gmm_authorize(ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800940
941err_inval:
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200942 LOGPC(DMM, LOGL_INFO, "\n");
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100943 reject_cause = GMM_CAUSE_SEM_INCORR_MSG;
944
945rejected:
946 /* Send ATTACH REJECT */
947 LOGMMCTXP(LOGL_NOTICE, ctx,
948 "Rejecting Attach Request with cause '%s' (%d)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +0100949 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +0100950 rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause);
951 if (ctx)
952 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ");
953 else
954 /* TLLI unassignment */
955 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
956
957 return rc;
958
Harald Welte75bb8202010-03-14 15:45:01 +0800959}
960
Harald Welte8f77f192010-05-17 00:44:57 +0200961/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200962static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte8f77f192010-05-17 00:44:57 +0200963{
964 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte8f77f192010-05-17 00:44:57 +0200965 uint8_t detach_type, power_off;
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200966 int rc = 0;
Harald Welte8f77f192010-05-17 00:44:57 +0200967
968 detach_type = gh->data[0] & 0x7;
969 power_off = gh->data[0] & 0x8;
970
971 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
972
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200973 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte8f77f192010-05-17 00:44:57 +0200974 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
975 power_off ? "Power-off" : "");
976
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200977 /* Only send the Detach Accept (MO) if power off isn't indicated,
Jacob Erlbeck97ee3762014-10-21 09:52:05 +0200978 * see 04.08, 4.7.4.1.2/3 for details */
979 if (!power_off) {
980 /* force_stby = 0 */
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200981 if (ctx)
982 rc = gsm48_tx_gmm_det_ack(ctx, 0);
983 else
984 rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0);
Jacob Erlbeck97ee3762014-10-21 09:52:05 +0200985 }
Harald Welte309b5932010-06-28 22:18:53 +0200986
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200987 if (ctx) {
988 struct sgsn_signal_data sig_data;
989 memset(&sig_data, 0, sizeof(sig_data));
990 sig_data.mm = ctx;
991 osmo_signal_dispatch(SS_SGSN, S_SGSN_DETACH, &sig_data);
Jacob Erlbeck42d284f2014-10-21 13:09:55 +0200992 mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST");
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200993 }
Jacob Erlbeck12396bd2014-09-30 13:51:45 +0200994
Harald Welte309b5932010-06-28 22:18:53 +0200995 return rc;
Harald Welte8f77f192010-05-17 00:44:57 +0200996}
997
Harald Welte75bb8202010-03-14 15:45:01 +0800998/* Chapter 9.4.15: Routing area update accept */
Harald Welte64df8ed2010-05-18 17:04:55 +0200999static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte75bb8202010-03-14 15:45:01 +08001000{
1001 struct msgb *msg = gsm48_msgb_alloc();
1002 struct gsm48_hdr *gh;
1003 struct gsm48_ra_upd_ack *rua;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001004 uint8_t *mid;
Harald Welte75bb8202010-03-14 15:45:01 +08001005
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001006 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001007
Harald Welte64df8ed2010-05-18 17:04:55 +02001008 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001009
1010 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1011 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1012 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
1013
1014 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
1015 rua->force_stby = 0; /* not indicated */
1016 rua->upd_result = 0; /* RA updated */
Jacob Erlbeck3dafa862015-01-23 12:32:38 +01001017 rua->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte2351f1f2010-05-02 11:54:55 +02001018
Harald Welte64df8ed2010-05-18 17:04:55 +02001019 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte75bb8202010-03-14 15:45:01 +08001020
Harald Welte7e01d8e2010-05-31 20:23:38 +02001021#if 0
1022 /* Optional: P-TMSI signature */
1023 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
1024 ptsig = msgb_put(msg, 3);
1025 ptsig[0] = mm->p_tmsi_sig >> 16;
1026 ptsig[1] = mm->p_tmsi_sig >> 8;
1027 ptsig[2] = mm->p_tmsi_sig & 0xff;
1028#endif
1029
1030#ifdef PTMSI_ALLOC
1031 /* Optional: Allocated P-TMSI */
1032 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
1033 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
1034 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
1035#endif
1036
Jacob Erlbeck1063ae02015-01-23 11:44:39 +01001037 /* Optional: Negotiated READY timer value */
1038 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
1039 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
1040
Harald Welte7e01d8e2010-05-31 20:23:38 +02001041 /* Option: MS ID, ... */
Harald Weltecca80c92010-05-31 22:12:30 +02001042 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001043}
1044
1045/* Chapter 9.4.17: Routing area update reject */
Harald Welted85d9a92010-05-02 11:26:34 +02001046static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte75bb8202010-03-14 15:45:01 +08001047{
1048 struct msgb *msg = gsm48_msgb_alloc();
1049 struct gsm48_hdr *gh;
1050
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001051 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001052
Harald Welte2351f1f2010-05-02 11:54:55 +02001053 gmm_copy_id(msg, old_msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001054
1055 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
1056 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1057 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
1058 gh->data[0] = cause;
1059 gh->data[1] = 0; /* ? */
1060
1061 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8a035af2010-05-18 10:57:45 +02001062 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte75bb8202010-03-14 15:45:01 +08001063}
1064
Harald Welted9610722010-06-09 10:28:29 +02001065static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001066 const uint8_t *pdp_status)
Harald Welted9610722010-06-09 10:28:29 +02001067{
1068 struct sgsn_pdp_ctx *pdp, *pdp2;
1069 /* 24.008 4.7.5.1.3: If the PDP context status information element is
1070 * included in ROUTING AREA UPDATE REQUEST message, then the network
1071 * shall deactivate all those PDP contexts locally (without peer to
1072 * peer signalling between the MS and the network), which are not in SM
1073 * state PDP-INACTIVE on network side but are indicated by the MS as
1074 * being in state PDP-INACTIVE. */
1075
1076 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Welte9e7093c2011-08-05 21:23:46 +02001077 if (pdp->nsapi < 8) {
1078 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001079 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Welte9e7093c2011-08-05 21:23:46 +02001080 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1081 pdp->nsapi, pdp_status[1], pdp_status[0]);
1082 sgsn_delete_pdp_ctx(pdp);
1083 }
1084 } else {
1085 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001086 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Welte9e7093c2011-08-05 21:23:46 +02001087 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1088 pdp->nsapi, pdp_status[1], pdp_status[0]);
1089 sgsn_delete_pdp_ctx(pdp);
1090 }
Harald Welted9610722010-06-09 10:28:29 +02001091 }
1092 }
1093}
1094
Harald Welte75bb8202010-03-14 15:45:01 +08001095/* Chapter 9.4.14: Routing area update request */
Harald Welte75871c72010-06-01 11:53:01 +02001096static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1097 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001098{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +02001099#ifndef PTMSI_ALLOC
1100 struct sgsn_signal_data sig_data;
1101#endif
Harald Weltecf3c4142010-04-30 16:33:12 +02001102 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +02001103 uint8_t *cur = gh->data;
Harald Welted9610722010-06-09 10:28:29 +02001104 uint8_t ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +08001105 struct gprs_ra_id old_ra_id;
Harald Welted9610722010-06-09 10:28:29 +02001106 struct tlv_parsed tp;
Harald Welted85d9a92010-05-02 11:26:34 +02001107 uint8_t upd_type;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001108 enum gsm48_gmm_cause reject_cause;
1109 int rc;
Harald Welte75bb8202010-03-14 15:45:01 +08001110
1111 /* Update Type 10.5.5.18 */
1112 upd_type = *cur++ & 0x0f;
1113
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001114 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte8f77f192010-05-17 00:44:57 +02001115 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte75bb8202010-03-14 15:45:01 +08001116
1117 /* Old routing area identification 10.5.5.15 */
1118 gsm48_parse_ra(&old_ra_id, cur);
1119 cur += 6;
1120
1121 /* MS Radio Access Capability 10.5.5.12a */
Harald Welted9610722010-06-09 10:28:29 +02001122 ms_ra_acc_cap_len = *cur++;
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001123 if (ms_ra_acc_cap_len > 52) {
1124 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1125 goto rejected;
1126 }
Harald Welte45ca5522011-07-27 23:35:38 +02001127 cur += ms_ra_acc_cap_len;
Harald Welte75bb8202010-03-14 15:45:01 +08001128
1129 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
1130 * DRX parameter, MS network capability */
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001131 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welted9610722010-06-09 10:28:29 +02001132 (msg->data + msg->len) - cur, 0, 0);
Harald Welte75bb8202010-03-14 15:45:01 +08001133
1134 switch (upd_type) {
1135 case GPRS_UPD_T_RA_LA:
1136 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001137 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 +01001138 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1139 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +08001140 case GPRS_UPD_T_RA:
1141 case GPRS_UPD_T_PERIODIC:
1142 break;
1143 }
1144
1145 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte51f78ee2010-12-23 23:34:43 +01001146 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte75bb8202010-03-14 15:45:01 +08001147 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte612c3d82011-10-16 18:50:56 +02001148 /* send a XID reset to re-set all LLC sequence numbers
1149 * in the MS */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001150 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte612c3d82011-10-16 18:50:56 +02001151 gprs_llgmm_reset(llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001152 /* The MS has to perform GPRS attach */
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001153 /* Device is still IMSI attached for CS but initiate GPRS ATTACH,
1154 * see GSM 04.08, 4.7.5.1.4 and G.6 */
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001155 reject_cause = GMM_CAUSE_IMPL_DETACHED;
1156 goto rejected;
Harald Welte75bb8202010-03-14 15:45:01 +08001157 }
1158
Harald Welte66d062a2010-12-26 10:39:26 +01001159 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
1160 msgid2mmctx(mmctx, msg);
1161 /* Bump the statistics of received signalling msgs for this MM context */
1162 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
1163
Harald Weltede34afa2010-05-02 12:56:57 +02001164 /* Update the MM context with the new RA-ID */
1165 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Welte66d062a2010-12-26 10:39:26 +01001166 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Weltede34afa2010-05-02 12:56:57 +02001167 mmctx->tlli = msgb_tlli(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001168 /* FIXME: Update the MM context with the MS radio acc capabilities */
1169 /* FIXME: Update the MM context with the MS network capabilities */
1170
Harald Welteeb471c92010-05-18 14:32:29 +02001171 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
1172
Harald Welte7e01d8e2010-05-31 20:23:38 +02001173#ifdef PTMSI_ALLOC
Jacob Erlbecke06476a2014-11-06 15:43:10 +01001174 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
1175 if (mmctx->mm_state != GMM_COMMON_PROC_INIT) {
1176 mmctx->p_tmsi_old = mmctx->p_tmsi;
1177 mmctx->p_tmsi = sgsn_alloc_ptmsi();
1178 }
Harald Welte7e01d8e2010-05-31 20:23:38 +02001179 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
1180 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
1181 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001182
1183 mmctx->mm_state = GMM_COMMON_PROC_INIT;
1184#else
1185 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1186 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +02001187
1188 memset(&sig_data, 0, sizeof(sig_data));
1189 sig_data.mm = mmctx;
1190 osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001191#endif
Harald Welte75871c72010-06-01 11:53:01 +02001192 /* Even if there is no P-TMSI allocated, the MS will switch from
1193 * foreign TLLI to local TLLI */
1194 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001195
Harald Welte75871c72010-06-01 11:53:01 +02001196 /* Inform LLC layer about new TLLI but keep old active */
Harald Welte0c4304f2010-06-30 23:18:57 +02001197 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
1198 GPRS_ALGO_GEA0, NULL);
Harald Welte75871c72010-06-01 11:53:01 +02001199
Harald Welted9610722010-06-09 10:28:29 +02001200 /* Look at PDP Context Status IE and see if MS's view of
1201 * activated/deactivated NSAPIs agrees with our view */
1202 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001203 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welted9610722010-06-09 10:28:29 +02001204 process_ms_ctx_status(mmctx, pdp_status);
1205 }
1206
Harald Welte75871c72010-06-01 11:53:01 +02001207 /* Send RA UPDATE ACCEPT */
Harald Welte64df8ed2010-05-18 17:04:55 +02001208 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001209
1210rejected:
1211 /* Send RA UPDATE REJECT */
1212 LOGMMCTXP(LOGL_NOTICE, mmctx,
1213 "Rejecting RA Update Request with cause '%s' (%d)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001214 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck6f4bbd42014-11-06 13:43:41 +01001215 rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause);
1216 if (mmctx)
1217 mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ");
1218 else
1219 /* TLLI unassignment */
1220 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0,
1221 NULL);
1222
1223 return rc;
Harald Welte75bb8202010-03-14 15:45:01 +08001224}
1225
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001226static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001227{
1228 struct gsm48_hdr *gh = msgb_l3(msg);
1229
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001230 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001231 get_value_string(gsm48_gmm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001232
1233 return 0;
1234}
1235
1236/* GPRS Mobility Management */
Harald Welte75871c72010-06-01 11:53:01 +02001237static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1238 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001239{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +02001240 struct sgsn_signal_data sig_data;
Harald Weltecf3c4142010-04-30 16:33:12 +02001241 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001242 int rc;
1243
Harald Welte75871c72010-06-01 11:53:01 +02001244 /* MMCTX can be NULL when called */
1245
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001246 if (!mmctx &&
1247 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Welte7e01d8e2010-05-31 20:23:38 +02001248 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001249 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001250 /* 4.7.10 */
Jacob Erlbeckb35ee6b2014-10-28 09:47:03 +01001251 if (gh->msg_type == GSM48_MT_GMM_STATUS) {
1252 /* TLLI unassignment */
1253 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1254 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001255 return 0;
Jacob Erlbeckb35ee6b2014-10-28 09:47:03 +01001256 }
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001257
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001258 /* Don't reply or establish a LLME on DETACH_ACK */
1259 if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) {
1260 /* TLLI unassignment */
1261 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1262 GPRS_ALGO_GEA0, NULL);
1263 }
1264
Jacob Erlbeck218ed402014-11-11 08:55:13 +01001265 gprs_llgmm_reset(llme);
1266
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001267 /* Don't force it into re-attachment */
1268 if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) {
Jacob Erlbeck42d284f2014-10-21 13:09:55 +02001269 /* Handle Detach Request */
1270 rc = gsm48_rx_gmm_det_req(NULL, msg);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001271
1272 /* TLLI unassignment */
1273 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1274 GPRS_ALGO_GEA0, NULL);
1275 return rc;
1276 }
1277
1278 /* Force the MS to re-attach */
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001279 rc = sgsn_force_reattach_oldmsg(msg);
1280
1281 /* TLLI unassignment */
1282 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1283 GPRS_ALGO_GEA0, NULL);
1284 return rc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001285 }
1286
Harald Welte75bb8202010-03-14 15:45:01 +08001287 switch (gh->msg_type) {
1288 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte75871c72010-06-01 11:53:01 +02001289 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001290 break;
1291 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte75871c72010-06-01 11:53:01 +02001292 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001293 break;
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001294 /* For all the following types mmctx can not be NULL */
Harald Welte75bb8202010-03-14 15:45:01 +08001295 case GSM48_MT_GMM_ID_RESP:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001296 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001297 break;
1298 case GSM48_MT_GMM_STATUS:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001299 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001300 break;
Harald Welte8f77f192010-05-17 00:44:57 +02001301 case GSM48_MT_GMM_DETACH_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001302 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte8f77f192010-05-17 00:44:57 +02001303 break;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001304 case GSM48_MT_GMM_DETACH_ACK:
1305 LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n");
1306 mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK");
1307 rc = 0;
1308 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001309 case GSM48_MT_GMM_ATTACH_COMPL:
1310 /* only in case SGSN offered new P-TMSI */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001311 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001312 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001313 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001314 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001315 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001316 /* Unassign the old TLLI */
1317 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001318 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1319 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001320 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001321 rc = 0;
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +02001322
1323 memset(&sig_data, 0, sizeof(sig_data));
1324 sig_data.mm = mmctx;
1325 osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001326 break;
Harald Welteebe8a6d2010-05-18 19:58:38 +02001327 case GSM48_MT_GMM_RA_UPD_COMPL:
1328 /* only in case SGSN offered new P-TMSI */
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001329 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001330 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001331 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001332 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001333 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001334 /* Unassign the old TLLI */
1335 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001336 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1337 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck67318ef2014-10-28 16:23:46 +01001338 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001339 rc = 0;
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +02001340
1341 memset(&sig_data, 0, sizeof(sig_data));
1342 sig_data.mm = mmctx;
1343 osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001344 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001345 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001346 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001347 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001348 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001349 mmctx->p_tmsi_old = 0;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001350 mmctx->pending_req = 0;
Harald Welte75871c72010-06-01 11:53:01 +02001351 /* Unassign the old TLLI */
1352 mmctx->tlli = mmctx->tlli_new;
Harald Welte0c4304f2010-06-30 23:18:57 +02001353 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freytherf01f18e2011-10-14 23:31:34 +02001354 rc = 0;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001355 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001356 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte45fcf682010-07-03 11:09:06 +02001357 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001358 break;
1359 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001360 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001361 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001362 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001363 break;
1364 }
1365
1366 return rc;
1367}
1368
Harald Welte7e01d8e2010-05-31 20:23:38 +02001369static void mmctx_timer_cb(void *_mm)
1370{
1371 struct sgsn_mm_ctx *mm = _mm;
Jacob Erlbeck4d127672014-12-01 12:33:33 +01001372 struct gsm_auth_tuple *at;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001373
1374 mm->num_T_exp++;
1375
1376 switch (mm->T) {
1377 case 3350: /* waiting for ATTACH COMPLETE */
1378 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001379 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Jacob Erlbeck25335882014-11-03 10:12:52 +01001380 mm_ctx_cleanup_free(mm, "T3350");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001381 /* FIXME: should we return some error? */
1382 break;
1383 }
1384 /* re-transmit the respective msg and re-start timer */
1385 switch (mm->t3350_mode) {
1386 case GMM_T3350_MODE_ATT:
1387 gsm48_tx_gmm_att_ack(mm);
1388 break;
1389 case GMM_T3350_MODE_RAU:
1390 gsm48_tx_gmm_ra_upd_ack(mm);
1391 break;
1392 case GMM_T3350_MODE_PTMSI_REALL:
1393 /* FIXME */
1394 break;
Jacob Erlbeck4f9b64a2014-10-28 12:23:29 +01001395 case GMM_T3350_MODE_NONE:
1396 LOGMMCTXP(LOGL_NOTICE, mm,
1397 "T3350 mode wasn't set, ignoring timeout\n");
1398 break;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001399 }
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001400 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001401 break;
Harald Welte45fcf682010-07-03 11:09:06 +02001402 case 3360: /* waiting for AUTH AND CIPH RESP */
1403 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001404 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Jacob Erlbeck25335882014-11-03 10:12:52 +01001405 mm_ctx_cleanup_free(mm, "T3360");
Harald Welte45fcf682010-07-03 11:09:06 +02001406 break;
1407 }
Jacob Erlbeck4d127672014-12-01 12:33:33 +01001408 /* Re-transmit the respective msg and re-start timer */
1409 if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
1410 LOGMMCTXP(LOGL_ERROR, mm,
1411 "timeout: invalid auth triplet reference\n");
1412 mm_ctx_cleanup_free(mm, "T3360");
1413 break;
1414 }
1415 at = &mm->auth_triplet;
1416
1417 gsm48_tx_gmm_auth_ciph_req(mm, at->rand, at->key_seq, GPRS_ALGO_GEA0);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001418 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte45fcf682010-07-03 11:09:06 +02001419 break;
Harald Welte7e01d8e2010-05-31 20:23:38 +02001420 case 3370: /* waiting for IDENTITY RESPONSE */
1421 if (mm->num_T_exp >= 5) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001422 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001423 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
Jacob Erlbeck25335882014-11-03 10:12:52 +01001424 mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)");
Harald Welte7e01d8e2010-05-31 20:23:38 +02001425 break;
1426 }
1427 /* re-tranmit IDENTITY REQUEST and re-start timer */
1428 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001429 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Welte7e01d8e2010-05-31 20:23:38 +02001430 break;
1431 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001432 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Welte7e01d8e2010-05-31 20:23:38 +02001433 mm->T);
1434 }
1435}
1436
1437/* GPRS SESSION MANAGEMENT */
1438
Harald Welte94ecef32010-12-24 21:13:26 +01001439static void pdpctx_timer_cb(void *_mm);
1440
1441static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1442 unsigned int seconds)
1443{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001444 if (osmo_timer_pending(&pdp->timer))
Harald Welte94ecef32010-12-24 21:13:26 +01001445 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1446 "timer %u pending\n", T, pdp->T);
1447 pdp->T = T;
1448 pdp->num_T_exp = 0;
1449
1450 /* FIXME: we should do this only once ? */
1451 pdp->timer.data = pdp;
1452 pdp->timer.cb = &pdpctx_timer_cb;
1453
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001454 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Welte94ecef32010-12-24 21:13:26 +01001455}
1456
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001457#if 0
Harald Welte2aea7492010-05-02 23:11:50 +02001458static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1459{
1460 uint8_t v[6];
1461
1462 v[0] = PDP_TYPE_ORG_IETF;
1463 v[1] = PDP_TYPE_N_IETF_IPv4;
1464 *(uint32_t *)(v+2) = htonl(ipaddr);
1465
1466 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1467}
1468
1469static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1470{
1471 uint8_t v[2];
1472
1473 v[0] = PDP_TYPE_ORG_ETSI;
1474 v[1] = PDP_TYPE_N_ETSI_PPP;
1475
1476 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1477}
Holger Hans Peter Freytherb32f34a2014-04-04 12:40:34 +02001478#endif
Harald Welte2aea7492010-05-02 23:11:50 +02001479
Harald Welte75bb8202010-03-14 15:45:01 +08001480/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte29d838e2010-05-18 10:35:06 +02001481int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte75bb8202010-03-14 15:45:01 +08001482{
Harald Welte75bb8202010-03-14 15:45:01 +08001483 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte75bb8202010-03-14 15:45:01 +08001484 struct gsm48_hdr *gh;
Harald Welte29d838e2010-05-18 10:35:06 +02001485 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte75bb8202010-03-14 15:45:01 +08001486
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001487 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001488
Harald Welte29d838e2010-05-18 10:35:06 +02001489 mmctx2msgid(msg, pdp->mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001490
1491 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1492 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1493 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte2aea7492010-05-02 23:11:50 +02001494
1495 /* Negotiated LLC SAPI */
Harald Welte29d838e2010-05-18 10:35:06 +02001496 msgb_v_put(msg, pdp->sapi);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001497
Harald Welte540dcbc2010-05-18 13:20:08 +02001498 /* FIXME: copy QoS parameters from original request */
1499 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1500 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001501
Harald Welte2aea7492010-05-02 23:11:50 +02001502 /* Radio priority 10.5.7.2 */
Harald Welte29d838e2010-05-18 10:35:06 +02001503 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001504
Harald Welte2aea7492010-05-02 23:11:50 +02001505 /* PDP address */
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001506 /* Highest 4 bits of first byte need to be set to 1, otherwise
1507 * the IE is identical with the 04.08 PDP Address IE */
1508 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte29d838e2010-05-18 10:35:06 +02001509 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1510 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001511 pdp->lib->eua.v[0] |= 0xf0;
1512
Harald Welteeb471c92010-05-18 14:32:29 +02001513 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freyther1b51fc22014-04-04 11:55:21 +02001514 if (pdp->lib->pco_req.l)
Harald Welte29d838e2010-05-18 10:35:06 +02001515 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Weltef2d66582010-05-18 14:36:11 +02001516 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte7d7fd2b2010-05-18 14:51:18 +02001517
Harald Welte2aea7492010-05-02 23:11:50 +02001518 /* Optional: Packet Flow Identifier */
Harald Welte75bb8202010-03-14 15:45:01 +08001519
Harald Welte8a035af2010-05-18 10:57:45 +02001520 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001521}
1522
Harald Welte29d838e2010-05-18 10:35:06 +02001523/* Section 9.5.3: Activate PDP Context reject */
1524int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1525 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1526{
1527 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte29d838e2010-05-18 10:35:06 +02001528 struct gsm48_hdr *gh;
1529 uint8_t transaction_id = tid ^ 0x8; /* flip */
1530
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001531 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte29d838e2010-05-18 10:35:06 +02001532
1533 mmctx2msgid(msg, mm);
1534
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_ACT_PDP_REJ;
1538
1539 msgb_v_put(msg, cause);
1540 if (pco_len && pco_v)
1541 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1542
Harald Welte8a035af2010-05-18 10:57:45 +02001543 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte29d838e2010-05-18 10:35:06 +02001544}
1545
Harald Welte94ecef32010-12-24 21:13:26 +01001546/* Section 9.5.8: Deactivate PDP Context Request */
1547static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1548 uint8_t sm_cause)
1549{
1550 struct msgb *msg = gsm48_msgb_alloc();
1551 struct gsm48_hdr *gh;
1552 uint8_t transaction_id = tid ^ 0x8; /* flip */
1553
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001554 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001555
1556 mmctx2msgid(msg, mm);
1557
1558 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1559 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1560 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1561
1562 msgb_v_put(msg, sm_cause);
1563
1564 return gsm48_gmm_sendmsg(msg, 0, mm);
1565}
1566int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1567{
1568 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1569
1570 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1571}
1572
Harald Welte75bb8202010-03-14 15:45:01 +08001573/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte3b807312010-06-08 18:08:59 +02001574static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte75bb8202010-03-14 15:45:01 +08001575{
Harald Welte75bb8202010-03-14 15:45:01 +08001576 struct msgb *msg = gsm48_msgb_alloc();
1577 struct gsm48_hdr *gh;
Harald Welte3b807312010-06-08 18:08:59 +02001578 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte75bb8202010-03-14 15:45:01 +08001579
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001580 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte75bb8202010-03-14 15:45:01 +08001581
Harald Welte3b807312010-06-08 18:08:59 +02001582 mmctx2msgid(msg, mm);
Harald Welte75bb8202010-03-14 15:45:01 +08001583
1584 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1585 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1586 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1587
Harald Welte3b807312010-06-08 18:08:59 +02001588 return gsm48_gmm_sendmsg(msg, 0, mm);
1589}
1590int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1591{
1592 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte75bb8202010-03-14 15:45:01 +08001593}
1594
1595/* Section 9.5.1: Activate PDP Context Request */
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001596static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1597 struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001598{
Harald Weltecf3c4142010-04-30 16:33:12 +02001599 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001600 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteb1fd23a2010-05-02 21:07:14 +02001601 uint8_t req_qos_len, req_pdpa_len;
1602 uint8_t *req_qos, *req_pdpa;
1603 struct tlv_parsed tp;
Harald Welte29d838e2010-05-18 10:35:06 +02001604 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte8bf65d02010-07-02 10:17:24 +02001605 struct sgsn_ggsn_ctx *ggsn;
Harald Welte29d838e2010-05-18 10:35:06 +02001606 struct sgsn_pdp_ctx *pdp;
Jacob Erlbeck3400f112015-02-02 18:03:05 +01001607 enum gsm48_gsm_cause gsm_cause;
Harald Welte75bb8202010-03-14 15:45:01 +08001608
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001609 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte29d838e2010-05-18 10:35:06 +02001610 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8a035af2010-05-18 10:57:45 +02001611
Harald Welteb7b0fd72010-07-02 22:52:04 +02001612 /* FIXME: length checks! */
Harald Welteb1fd23a2010-05-02 21:07:14 +02001613 req_qos_len = act_req->data[0];
1614 req_qos = act_req->data + 1; /* 10.5.6.5 */
1615 req_pdpa_len = act_req->data[1 + req_qos_len];
1616 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte75bb8202010-03-14 15:45:01 +08001617
Harald Welteb7b0fd72010-07-02 22:52:04 +02001618 /* Optional: Access Point Name, Protocol Config Options */
1619 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1620 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1621 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1622 else
1623 memset(&tp, 0, sizeof(tp));
1624
Harald Welteb1fd23a2010-05-02 21:07:14 +02001625 switch (req_pdpa[0] & 0xf) {
1626 case 0x0:
1627 DEBUGPC(DMM, "ETSI ");
1628 break;
1629 case 0x1:
1630 DEBUGPC(DMM, "IETF ");
1631 break;
1632 case 0xf:
1633 DEBUGPC(DMM, "Empty ");
1634 break;
1635 }
1636
1637 switch (req_pdpa[1]) {
1638 case 0x21:
1639 DEBUGPC(DMM, "IPv4 ");
1640 if (req_pdpa_len >= 6) {
1641 struct in_addr ia;
1642 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1643 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1644 }
1645 break;
1646 case 0x57:
1647 DEBUGPC(DMM, "IPv6 ");
1648 if (req_pdpa_len >= 18) {
1649 /* FIXME: print IPv6 address */
1650 }
1651 break;
1652 default:
1653 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1654 break;
1655 }
1656
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001657 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welteeb471c92010-05-18 14:32:29 +02001658
Harald Welte8f77f192010-05-17 00:44:57 +02001659 /* put the non-TLV elements in the TLV parser structure to
1660 * pass them on to the SGSN / GTP code */
1661 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1662 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1663 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1664 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1665
Harald Welte29d838e2010-05-18 10:35:06 +02001666 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1667 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1668 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1669 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1670 GSM_CAUSE_INV_MAND_INFO,
1671 0, NULL);
1672 }
1673
1674 /* Check if NSAPI is already in use */
Harald Welte5fbac982010-07-02 10:12:58 +02001675 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1676 if (pdp) {
1677 /* We already have a PDP context for this TLLI + NSAPI tuple */
1678 if (pdp->sapi == act_req->req_llc_sapi &&
1679 pdp->ti == transaction_id) {
1680 /* This apparently is a re-transmission of a PDP CTX
1681 * ACT REQ (our ACT ACK must have got dropped) */
1682 return gsm48_tx_gsm_act_pdp_acc(pdp);
1683 }
1684
1685 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte29d838e2010-05-18 10:35:06 +02001686 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1687 GSM_CAUSE_NSAPI_IN_USE,
1688 0, NULL);
1689 }
Harald Welte75bb8202010-03-14 15:45:01 +08001690
Harald Weltea456a232010-07-02 10:15:49 +02001691 /* Only increment counter for a real activation, after we checked
1692 * for re-transmissions */
1693 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1694
Jacob Erlbeck3400f112015-02-02 18:03:05 +01001695 /* Determine GGSN based on APN and subscription options */
1696 ggsn = sgsn_mm_ctx_find_ggsn_ctx(mmctx, &tp, &gsm_cause);
Harald Welte8bf65d02010-07-02 10:17:24 +02001697 if (!ggsn) {
Jacob Erlbeck3400f112015-02-02 18:03:05 +01001698 LOGP(DGPRS, LOGL_ERROR, "No GGSN context found!\n");
1699 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1700 gsm_cause, 0, NULL);
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001701 }
Jacob Erlbeck3400f112015-02-02 18:03:05 +01001702 LOGMMCTXP(LOGL_DEBUG, mmctx, "Using GGSN %d\n", ggsn->id);
Harald Welte8bf65d02010-07-02 10:17:24 +02001703 ggsn->gsn = sgsn->gsn;
1704 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1705 if (!pdp)
1706 return -1;
1707
1708 /* Store SAPI and Transaction Identifier */
1709 pdp->sapi = act_req->req_llc_sapi;
1710 pdp->ti = transaction_id;
1711
Harald Welte64df8ed2010-05-18 17:04:55 +02001712 return 0;
Harald Welte75bb8202010-03-14 15:45:01 +08001713}
1714
1715/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welteeb471c92010-05-18 14:32:29 +02001716static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001717{
Harald Weltecf3c4142010-04-30 16:33:12 +02001718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeb471c92010-05-18 14:32:29 +02001719 uint8_t transaction_id = (gh->proto_discr >> 4);
1720 struct sgsn_pdp_ctx *pdp;
Harald Welte75bb8202010-03-14 15:45:01 +08001721
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001722 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001723 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001724
Harald Welteeb471c92010-05-18 14:32:29 +02001725 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1726 if (!pdp) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001727 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welteeb471c92010-05-18 14:32:29 +02001728 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1729 mm->imsi, transaction_id);
Harald Welte3b807312010-06-08 18:08:59 +02001730 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welteeb471c92010-05-18 14:32:29 +02001731 }
1732
1733 return sgsn_delete_pdp_ctx(pdp);
Harald Welte75bb8202010-03-14 15:45:01 +08001734}
1735
Harald Welte94ecef32010-12-24 21:13:26 +01001736/* Section 9.5.9: Deactivate PDP Context Accept */
1737static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1738{
1739 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1740 uint8_t transaction_id = (gh->proto_discr >> 4);
1741 struct sgsn_pdp_ctx *pdp;
1742
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001743 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001744
1745 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1746 if (!pdp) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001747 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Welte94ecef32010-12-24 21:13:26 +01001748 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1749 mm->imsi, transaction_id);
1750 return 0;
1751 }
1752
1753 return sgsn_delete_pdp_ctx(pdp);
1754}
1755
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001756static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +08001757{
1758 struct gsm48_hdr *gh = msgb_l3(msg);
1759
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001760 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Jacob Erlbeck9346dad2015-01-05 17:30:02 +01001761 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte75bb8202010-03-14 15:45:01 +08001762
1763 return 0;
1764}
1765
Harald Welte94ecef32010-12-24 21:13:26 +01001766static void pdpctx_timer_cb(void *_pdp)
1767{
1768 struct sgsn_pdp_ctx *pdp = _pdp;
1769
1770 pdp->num_T_exp++;
1771
1772 switch (pdp->T) {
1773 case 3395: /* waiting for PDP CTX DEACT ACK */
1774 if (pdp->num_T_exp >= 4) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001775 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Welte94ecef32010-12-24 21:13:26 +01001776 pdp->state = PDP_STATE_INACTIVE;
1777 sgsn_delete_pdp_ctx(pdp);
1778 break;
1779 }
1780 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001781 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Welte94ecef32010-12-24 21:13:26 +01001782 break;
1783 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001784 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Welte94ecef32010-12-24 21:13:26 +01001785 pdp->T);
1786 }
1787}
1788
1789
Harald Welte75bb8202010-03-14 15:45:01 +08001790/* GPRS Session Management */
Harald Welte75871c72010-06-01 11:53:01 +02001791static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1792 struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001793{
Harald Weltecf3c4142010-04-30 16:33:12 +02001794 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001795 int rc;
1796
Harald Welte75871c72010-06-01 11:53:01 +02001797 /* MMCTX can be NULL when called */
1798
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001799 if (!mmctx) {
1800 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001801 /* 6.1.3.6 */
1802 if (gh->msg_type == GSM48_MT_GSM_STATUS)
1803 return 0;
1804
1805 return sgsn_force_reattach_oldmsg(msg);
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001806 }
1807
Harald Welte75bb8202010-03-14 15:45:01 +08001808 switch (gh->msg_type) {
1809 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001810 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001811 break;
1812 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001813 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welteeb471c92010-05-18 14:32:29 +02001814 break;
Harald Welte94ecef32010-12-24 21:13:26 +01001815 case GSM48_MT_GSM_DEACT_PDP_ACK:
1816 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1817 break;
Harald Welte75bb8202010-03-14 15:45:01 +08001818 case GSM48_MT_GSM_STATUS:
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001819 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte75bb8202010-03-14 15:45:01 +08001820 break;
1821 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1822 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1823 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001824 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001825 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001826 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001827 break;
1828 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001829 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001830 gh->msg_type);
Harald Welte263edd12010-06-09 10:51:23 +02001831 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte75bb8202010-03-14 15:45:01 +08001832 break;
1833
1834 }
1835
1836 return rc;
1837}
1838
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001839int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg)
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001840{
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001841 int rc;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001842 gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM);
1843
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001844 rc = gsm48_tx_gmm_detach_req_oldmsg(
1845 msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001846
Jacob Erlbeck6e15b732014-10-31 12:20:49 +01001847 return rc;
1848}
1849
1850int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx)
1851{
1852 int rc;
1853 gprs_llgmm_reset(mmctx->llme);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001854
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001855 rc = gsm48_tx_gmm_detach_req(
1856 mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001857
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001858 mm_ctx_cleanup_free(mmctx, "forced reattach");
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001859
Jacob Erlbeck0a6a02e2014-10-31 12:27:11 +01001860 return rc;
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +02001861}
1862
Harald Welte75bb8202010-03-14 15:45:01 +08001863/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte75871c72010-06-01 11:53:01 +02001864int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte75bb8202010-03-14 15:45:01 +08001865{
Harald Weltecf3c4142010-04-30 16:33:12 +02001866 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welted85d9a92010-05-02 11:26:34 +02001867 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001868 struct sgsn_mm_ctx *mmctx;
1869 struct gprs_ra_id ra_id;
Harald Welte75bb8202010-03-14 15:45:01 +08001870 int rc = -EINVAL;
1871
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001872 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1873 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8a035af2010-05-18 10:57:45 +02001874 if (mmctx) {
Harald Welte29d838e2010-05-18 10:35:06 +02001875 msgid2mmctx(mmctx, msg);
Harald Welte8a035af2010-05-18 10:57:45 +02001876 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte75871c72010-06-01 11:53:01 +02001877 mmctx->llme = llme;
Harald Welte8a035af2010-05-18 10:57:45 +02001878 }
Harald Weltec1f6bfe2010-05-17 22:58:03 +02001879
1880 /* MMCTX can be NULL */
1881
Harald Welte75bb8202010-03-14 15:45:01 +08001882 switch (pdisc) {
1883 case GSM48_PDISC_MM_GPRS:
Harald Welte75871c72010-06-01 11:53:01 +02001884 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001885 break;
1886 case GSM48_PDISC_SM_GPRS:
Harald Welte75871c72010-06-01 11:53:01 +02001887 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte75bb8202010-03-14 15:45:01 +08001888 break;
1889 default:
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001890 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte75bb8202010-03-14 15:45:01 +08001891 pdisc);
Harald Welte29d838e2010-05-18 10:35:06 +02001892 /* FIXME: return status message */
Harald Welte75bb8202010-03-14 15:45:01 +08001893 break;
1894 }
1895
Jacob Erlbeck12396bd2014-09-30 13:51:45 +02001896 /* MMCTX can be invalid */
1897
Harald Welte75bb8202010-03-14 15:45:01 +08001898 return rc;
1899}
Harald Welte50264822010-06-09 11:22:47 +02001900
1901int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1902{
1903 struct sgsn_mm_ctx *mmctx;
1904
1905 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1906 if (!mmctx) {
1907 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1908 "TLLI=%08x\n", tlli);
1909 return -EINVAL;
1910 }
1911
Jacob Erlbeck764b8d52014-12-22 17:58:18 +01001912 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1913 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001914 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte50264822010-06-09 11:22:47 +02001915 "!= REGISTERED (TLLI=%08x)\n", tlli);
1916 return -EINVAL;
1917 }
1918
1919 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1920 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1921 return 0;
1922}
1923
1924int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1925 uint8_t suspend_ref)
1926{
1927 struct sgsn_mm_ctx *mmctx;
1928
1929 /* FIXME: make use of suspend reference? */
1930
1931 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1932 if (!mmctx) {
1933 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1934 "TLLI=%08x\n", tlli);
1935 return -EINVAL;
1936 }
1937
Jacob Erlbeck764b8d52014-12-22 17:58:18 +01001938 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1939 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmannc9ac9732014-09-03 17:46:44 +02001940 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte50264822010-06-09 11:22:47 +02001941 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1942 /* FIXME: should we not simply ignore it? */
1943 return -EINVAL;
1944 }
1945
1946 /* Transition from SUSPENDED to NORMAL */
1947 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1948 return 0;
1949}