blob: a2d61cfe9c742bf7dc04375520db0f673e361da5 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte9f1f3ad2010-05-02 12:56:57 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltec2e8cc42010-05-31 20:23:38 +02005 * (C) 2010 by On-Waves
Harald Welte9b455bf2010-03-14 15:45:01 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080028#include <errno.h>
29
30#include <netinet/in.h>
Harald Welte421cba42010-05-02 23:11:50 +020031#include <arpa/inet.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080032
33#include <openbsc/db.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/msgb.h>
Harald Welted36ff762011-03-23 18:26:56 +010035#include <osmocom/gsm/tlv.h>
36#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/signal.h>
38#include <osmocom/core/talloc.h>
39#include <osmocom/core/rate_ctr.h>
Harald Welted6057092010-05-02 12:57:45 +020040
Harald Welteea34a4e2012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_bssgp.h>
42
Harald Welte9b455bf2010-03-14 15:45:01 +080043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080045#include <openbsc/gsm_subscriber.h>
46#include <openbsc/gsm_04_08.h>
47#include <openbsc/gsm_04_08_gprs.h>
48#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080049#include <openbsc/transaction.h>
50#include <openbsc/gprs_llc.h>
51#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020052#include <openbsc/gprs_gmm.h>
Harald Welted193cb32010-05-17 22:58:03 +020053#include <openbsc/sgsn.h>
54
Harald Welte6abf94e2010-05-18 10:35:06 +020055#include <pdp.h>
56
Harald Weltec2e8cc42010-05-31 20:23:38 +020057#define PTMSI_ALLOC
58
Harald Welte649e1ff2013-07-21 17:41:46 +080059/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020060#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
61#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
62#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
63#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
64
Harald Welte649e1ff2013-07-21 17:41:46 +080065/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020066#define GSM0408_T3313_SECS 30 /* waiting for paging response */
67#define GSM0408_T3314_SECS 44 /* force to STBY on expiry */
68#define GSM0408_T3316_SECS 44
69
Harald Weltea9b473a2010-12-24 21:13:26 +010070/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
71#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
72#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
73#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
74#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
75
Harald Welted193cb32010-05-17 22:58:03 +020076extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080077
Harald Welte2720e732010-05-17 00:44:57 +020078/* Protocol related stuff, should go into libosmocore */
79
Harald Welte9b455bf2010-03-14 15:45:01 +080080/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
Harald Welte2720e732010-05-17 00:44:57 +020081const struct value_string gmm_cause_names[] = {
Harald Weltefd29a1f2010-07-01 12:26:59 +020082 { GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown in HLR" },
83 { GMM_CAUSE_ILLEGAL_MS, "Illegal MS" },
84 { GMM_CAUSE_ILLEGAL_ME, "Illegal ME" },
85 { GMM_CAUSE_GPRS_NOTALLOWED, "GPRS services not allowed" },
86 { GMM_CAUSE_GPRS_OTHER_NOTALLOWED,
87 "GPRS services and non-GPRS services not allowed" },
88 { GMM_CAUSE_MS_ID_NOT_DERIVED,
89 "MS identity cannot be derived by the network" },
90 { GMM_CAUSE_IMPL_DETACHED, "Implicitly detached" },
91 { GMM_CAUSE_PLMN_NOTALLOWED, "PLMN not allowed" },
92 { GMM_CAUSE_LA_NOTALLOWED, "Location Area not allowed" },
93 { GMM_CAUSE_ROAMING_NOTALLOWED,
94 "Roaming not allowed in this location area" },
95 { GMM_CAUSE_NO_GPRS_PLMN,
96 "GPRS services not allowed in this PLMN" },
97 { GMM_CAUSE_MSC_TEMP_NOTREACH, "MSC temporarily not reachable" },
98 { GMM_CAUSE_NET_FAIL, "Network failure" },
99 { GMM_CAUSE_CONGESTION, "Congestion" },
100 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800101 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
102 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
103 "Message type non-existant or not implemented" },
104 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
105 "Message type not compatible with protocol state" },
106 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
107 "Information element non-existent or not implemented" },
Harald Weltefd29a1f2010-07-01 12:26:59 +0200108 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800109 { GMM_CAUSE_MSG_INCOMP_P_STATE,
110 "Message not compatible with protocol state " },
Harald Weltefd29a1f2010-07-01 12:26:59 +0200111 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800112 { 0, NULL }
113};
114
115/* 10.5.6.6 SM Cause / Table 10.5.157 */
Harald Welte2720e732010-05-17 00:44:57 +0200116const struct value_string gsm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +0800117 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
118 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
119 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
120 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
121 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
122 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
123 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
124 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
125 "Requested service option not subscribed" },
126 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
127 "Service option temporarily out of order" },
128 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
129 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
130 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
131 { GSM_CAUSE_NET_FAIL, "Network Failure" },
132 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
133 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
134 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
135 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
136 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
137 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
138 "Message type non-existant or not implemented" },
139 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
140 "Message type not compatible with protocol state" },
141 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
142 "Information element non-existent or not implemented" },
143 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
144 { GSM_CAUSE_MSG_INCOMP_P_STATE,
145 "Message not compatible with protocol state " },
146 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
147 { 0, NULL }
148};
149
Harald Welte2720e732010-05-17 00:44:57 +0200150/* 10.5.5.2 */
151const struct value_string gprs_att_t_strs[] = {
152 { GPRS_ATT_T_ATTACH, "GPRS attach" },
153 { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" },
154 { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" },
155 { 0, NULL }
156};
Harald Welte9b455bf2010-03-14 15:45:01 +0800157
Harald Welte2720e732010-05-17 00:44:57 +0200158const struct value_string gprs_upd_t_strs[] = {
159 { GPRS_UPD_T_RA, "RA updating" },
160 { GPRS_UPD_T_RA_LA, "combined RA/LA updating" },
161 { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" },
162 { GPRS_UPD_T_PERIODIC, "periodic updating" },
163 { 0, NULL }
164};
165
166/* 10.5.5.5 */
167const struct value_string gprs_det_t_mo_strs[] = {
168 { GPRS_DET_T_MO_GPRS, "GPRS detach" },
169 { GPRS_DET_T_MO_IMSI, "IMSI detach" },
170 { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" },
171 { 0, NULL }
172};
173
Harald Welte588d44e2010-06-09 10:28:29 +0200174static const struct tlv_definition gsm48_gmm_att_tlvdef = {
175 .def = {
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200176 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welte588d44e2010-06-09 10:28:29 +0200177 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
178 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200179 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
180 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
181 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welte588d44e2010-06-09 10:28:29 +0200182 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200183 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welte588d44e2010-06-09 10:28:29 +0200184 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
185 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
186 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
187 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
188 },
189};
190
191static const struct tlv_definition gsm48_sm_att_tlvdef = {
192 .def = {
193 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
194 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
195 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
196 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
197 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
198 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200199 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
200 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welte588d44e2010-06-09 10:28:29 +0200201 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
202 },
203};
204
Harald Welte2720e732010-05-17 00:44:57 +0200205/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800206
Harald Weltec2e8cc42010-05-31 20:23:38 +0200207static void mmctx_timer_cb(void *_mm);
208
209static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
210 unsigned int seconds)
211{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200212 if (osmo_timer_pending(&mm->timer))
Harald Weltec2e8cc42010-05-31 20:23:38 +0200213 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
214 "timer %u pending\n", T, mm->T);
215 mm->T = T;
216 mm->num_T_exp = 0;
217
218 /* FIXME: we should do this only once ? */
219 mm->timer.data = mm;
220 mm->timer.cb = &mmctx_timer_cb;
221
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200222 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200223}
224
225static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
226{
227 if (mm->T != T)
228 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
229 "%u is running\n", T, mm->T);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200230 osmo_timer_del(&mm->timer);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200231}
232
Harald Welte9b455bf2010-03-14 15:45:01 +0800233/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200234static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200235 struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800236{
Harald Welte8acd88f2010-05-18 10:57:45 +0200237 if (mm)
238 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
239
Harald Welte11d7c102010-05-02 11:54:55 +0200240 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte56a01452010-05-31 22:12:30 +0200241 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800242}
243
Harald Welte11d7c102010-05-02 11:54:55 +0200244/* copy identifiers from old message to new message, this
245 * is required so lower layers can route it correctly */
246static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
247{
248 msgb_tlli(msg) = msgb_tlli(old);
249 msgb_bvci(msg) = msgb_bvci(old);
250 msgb_nsei(msg) = msgb_nsei(old);
251}
252
Harald Welte6abf94e2010-05-18 10:35:06 +0200253/* Store BVCI/NSEI in MM context */
254static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
255{
256 mm->bvci = msgb_bvci(msg);
257 mm->nsei = msgb_nsei(msg);
258}
259
260/* Store BVCI/NSEI in MM context */
261static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
262{
263 msgb_tlli(msg) = mm->tlli;
264 msgb_bvci(msg) = mm->bvci;
265 msgb_nsei(msg) = mm->nsei;
266}
267
Harald Weltef54e7e22010-06-09 10:51:23 +0200268/* Chapter 9.4.18 */
269static int _tx_status(struct msgb *msg, uint8_t cause,
270 struct sgsn_mm_ctx *mmctx, int sm)
271{
272 struct gsm48_hdr *gh;
273
274 /* MMCTX might be NULL! */
275
276 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
277 get_value_string(gmm_cause_names, cause));
278
279 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
280 if (sm) {
281 gh->proto_discr = GSM48_PDISC_SM_GPRS;
282 gh->msg_type = GSM48_MT_GSM_STATUS;
283 } else {
284 gh->proto_discr = GSM48_PDISC_MM_GPRS;
285 gh->msg_type = GSM48_MT_GMM_STATUS;
286 }
287 gh->data[0] = cause;
288
289 return gsm48_gmm_sendmsg(msg, 0, mmctx);
290}
291static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
292{
293 struct msgb *msg = gsm48_msgb_alloc();
294
295 mmctx2msgid(msg, mmctx);
296 return _tx_status(msg, cause, mmctx, 0);
297};
298static int gsm48_tx_gmm_status_oldmsg(struct msgb *oldmsg, uint8_t cause)
299{
300 struct msgb *msg = gsm48_msgb_alloc();
301
302 gmm_copy_id(msg, oldmsg);
303 return _tx_status(msg, cause, NULL, 0);
304}
305static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
306{
307 struct msgb *msg = gsm48_msgb_alloc();
308
309 mmctx2msgid(msg, mmctx);
310 return _tx_status(msg, cause, mmctx, 1);
311};
312static int gsm48_tx_sm_status_oldmsg(struct msgb *oldmsg, uint8_t cause)
313{
314 struct msgb *msg = gsm48_msgb_alloc();
315
316 gmm_copy_id(msg, oldmsg);
317 return _tx_status(msg, cause, NULL, 1);
318}
319
320
Harald Welte86fda902010-05-03 10:54:35 +0200321static struct gsm48_qos default_qos = {
322 .delay_class = 4, /* best effort */
323 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
324 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
325 .preced_class = GSM48_QOS_PC_NORMAL,
326 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
327 .traf_class = GSM48_QOS_TC_INTERACTIVE,
328 .deliv_order = GSM48_QOS_DO_UNORDERED,
329 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
330 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
331 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
332 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
333 .resid_ber = GSM48_QOS_RBER_5e_2,
334 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
335 .handling_prio = 3,
336 .xfer_delay = 0x10, /* 200ms */
337 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
338 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
339 .sig_ind = 0, /* not optimised for signalling */
340 .max_bitrate_down_ext = 0, /* use octet 9 */
341 .guar_bitrate_down_ext = 0, /* use octet 13 */
342};
343
Harald Welte9b455bf2010-03-14 15:45:01 +0800344/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200345static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800346{
347 struct msgb *msg = gsm48_msgb_alloc();
348 struct gsm48_hdr *gh;
349 struct gsm48_attach_ack *aa;
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200350 uint8_t *mid;
351#if 0
352 uint8_t *ptsig;
353#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800354
Daniel Willmann46553142014-09-03 17:46:44 +0200355 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800356
Harald Welte6463c072010-05-18 17:04:55 +0200357 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800358
359 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
360 gh->proto_discr = GSM48_PDISC_MM_GPRS;
361 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
362
363 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
364 aa->force_stby = 0; /* not indicated */
365 aa->att_result = 1; /* GPRS only */
366 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
367 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200368 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800369
Harald Welteab1d5622010-05-18 19:58:38 +0200370#if 0
Harald Welte6463c072010-05-18 17:04:55 +0200371 /* Optional: P-TMSI signature */
372 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
373 ptsig = msgb_put(msg, 3);
374 ptsig[0] = mm->p_tmsi_sig >> 16;
375 ptsig[1] = mm->p_tmsi_sig >> 8;
376 ptsig[2] = mm->p_tmsi_sig & 0xff;
377
378 /* Optional: Negotiated Ready timer value */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200379#endif
Harald Welte6463c072010-05-18 17:04:55 +0200380
Harald Weltec2e8cc42010-05-31 20:23:38 +0200381#ifdef PTMSI_ALLOC
Harald Welte6463c072010-05-18 17:04:55 +0200382 /* Optional: Allocated P-TMSI */
Harald Welte6463c072010-05-18 17:04:55 +0200383 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
384 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteab1d5622010-05-18 19:58:38 +0200385 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
386#endif
Harald Weltec2e8cc42010-05-31 20:23:38 +0200387
Harald Welte6463c072010-05-18 17:04:55 +0200388 /* Optional: MS-identity (combined attach) */
389 /* Optional: GMM cause (partial attach result for combined attach) */
390
Harald Welte56a01452010-05-31 22:12:30 +0200391 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800392}
393
394/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200395static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800396{
Harald Welte9b455bf2010-03-14 15:45:01 +0800397 struct gsm48_hdr *gh;
398
Daniel Willmann46553142014-09-03 17:46:44 +0200399 LOGP(DMM, LOGL_NOTICE, "<- GPRS ATTACH REJECT: %s\n", get_value_string(gmm_cause_names, gmm_cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800400
Harald Welte9b455bf2010-03-14 15:45:01 +0800401 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
402 gh->proto_discr = GSM48_PDISC_MM_GPRS;
403 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
404 gh->data[0] = gmm_cause;
405
Harald Welte8acd88f2010-05-18 10:57:45 +0200406 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800407}
Harald Welte6463c072010-05-18 17:04:55 +0200408static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
409 uint8_t gmm_cause)
410{
411 struct msgb *msg = gsm48_msgb_alloc();
412 gmm_copy_id(msg, old_msg);
413 return _tx_gmm_att_rej(msg, gmm_cause);
414}
415static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
416 uint8_t gmm_cause)
417{
418 struct msgb *msg = gsm48_msgb_alloc();
419 mmctx2msgid(msg, mm);
420 return _tx_gmm_att_rej(msg, gmm_cause);
421}
Harald Welte9b455bf2010-03-14 15:45:01 +0800422
Harald Welte2720e732010-05-17 00:44:57 +0200423/* Chapter 9.4.6.2 Detach accept */
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200424static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
Harald Welte2720e732010-05-17 00:44:57 +0200425{
426 struct msgb *msg = gsm48_msgb_alloc();
427 struct gsm48_hdr *gh;
428
Daniel Willmann46553142014-09-03 17:46:44 +0200429 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS DETACH ACCEPT\n");
Harald Welte2720e732010-05-17 00:44:57 +0200430
Harald Welte6463c072010-05-18 17:04:55 +0200431 mmctx2msgid(msg, mm);
432
Harald Welte2720e732010-05-17 00:44:57 +0200433 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
434 gh->proto_discr = GSM48_PDISC_MM_GPRS;
435 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
436 gh->data[0] = force_stby;
437
Harald Welte56a01452010-05-31 22:12:30 +0200438 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200439}
440
Harald Welte9b455bf2010-03-14 15:45:01 +0800441/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200442static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800443{
444 struct msgb *msg = gsm48_msgb_alloc();
445 struct gsm48_hdr *gh;
446
Daniel Willmann46553142014-09-03 17:46:44 +0200447 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800448
Harald Welte6463c072010-05-18 17:04:55 +0200449 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800450
451 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
452 gh->proto_discr = GSM48_PDISC_MM_GPRS;
453 gh->msg_type = GSM48_MT_GMM_ID_REQ;
454 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
455 gh->data[0] = id_type & 0xf;
456
Harald Weltedce12552010-05-31 22:31:04 +0200457 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800458}
459
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200460#if 0
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200461/* Section 9.4.9: Authentication and Ciphering Request */
462static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
463 uint8_t key_seq, uint8_t algo)
464{
465 struct msgb *msg = gsm48_msgb_alloc();
466 struct gsm48_hdr *gh;
467 struct gsm48_auth_ciph_req *acreq;
468 uint8_t *m_rand, *m_cksn;
469
Daniel Willmann46553142014-09-03 17:46:44 +0200470 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200471 osmo_hexdump(rand, 16));
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200472
473 mmctx2msgid(msg, mm);
474
475 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
476 gh->proto_discr = GSM48_PDISC_MM_GPRS;
477 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
478
479 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
480 acreq->ciph_alg = algo & 0xf;
481 acreq->imeisv_req = 0x1;
482 acreq->force_stby = 0x0;
483 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
484
485 /* Only if authentication is requested we need to set RAND + CKSN */
486 if (rand) {
487 m_rand = msgb_put(msg, 16+1);
488 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
489 memcpy(m_rand+1, rand, 16);
490
491 m_cksn = msgb_put(msg, 1+1);
492 m_cksn[0] = GSM48_IE_GMM_CIPH_CKSN;
493 m_cksn[1] = key_seq;
494 }
495
496 /* Start T3360 */
497 mmctx_timer_start(mm, 3360, GSM0408_T3360_SECS);
498
499 /* FIXME: make sure we don't send any other messages to the MS */
500
501 return gsm48_gmm_sendmsg(msg, 1, mm);
502}
503
504/* Section 9.4.11: Authentication and Ciphering Reject */
505static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
506{
507 struct msgb *msg = gsm48_msgb_alloc();
508 struct gsm48_hdr *gh;
509
Daniel Willmann46553142014-09-03 17:46:44 +0200510 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200511
512 mmctx2msgid(msg, mm);
513
514 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
515 gh->proto_discr = GSM48_PDISC_MM_GPRS;
516 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
517
518 return gsm48_gmm_sendmsg(msg, 0, mm);
519}
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200520#endif
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200521
522/* Section 9.4.10: Authentication and Ciphering Response */
523static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
524 struct msgb *msg)
525{
526 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
527 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
528 struct tlv_parsed tp;
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200529
530 /* FIXME: Stop T3360 */
531
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200532 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200533 (msg->data + msg->len) - acr->data, 0, 0);
534
535 /* FIXME: compare ac_ref? */
536
537 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
538 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
539 /* FIXME: missing mandatory IE */
540 }
541
542 /* FIXME: compare SRES with what we expected */
543 /* FIXME: enable LLC cipheirng */
544 return 0;
545}
546
Harald Welte9b455bf2010-03-14 15:45:01 +0800547/* Check if we can already authorize a subscriber */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200548static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx,
549 enum gprs_t3350_mode t3350_mode)
Harald Welte9b455bf2010-03-14 15:45:01 +0800550{
551 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200552#ifdef PTMSI_ALLOC
553 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
554 ctx->t3350_mode = t3350_mode;
555 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
556#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800557 ctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Welte6463c072010-05-18 17:04:55 +0200558 return gsm48_tx_gmm_att_ack(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800559 }
Harald Weltec2e8cc42010-05-31 20:23:38 +0200560 if (!strlen(ctx->imei)) {
561 ctx->mm_state = GMM_COMMON_PROC_INIT;
562 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
563 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200564 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200565 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800566
Harald Weltec2e8cc42010-05-31 20:23:38 +0200567 if (!strlen(ctx->imsi)) {
568 ctx->mm_state = GMM_COMMON_PROC_INIT;
569 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
570 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200571 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200572 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800573
574 return 0;
575}
576
577/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200578static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800579{
Harald Welte943c5bc2010-04-30 16:33:12 +0200580 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200581 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800582 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800583
584 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte9b455bf2010-03-14 15:45:01 +0800585 if (!ctx) {
Daniel Willmann46553142014-09-03 17:46:44 +0200586 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800587 return -EINVAL;
588 }
589
Daniel Willmannf8070f42014-09-23 18:48:44 +0200590 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
591 mi_type, mi_string);
592
Harald Weltec2e8cc42010-05-31 20:23:38 +0200593 if (mi_type == ctx->t3370_id_type)
594 mmctx_timer_stop(ctx, 3370);
595
Harald Welte9b455bf2010-03-14 15:45:01 +0800596 switch (mi_type) {
597 case GSM_MI_TYPE_IMSI:
598 /* we already have a mm context with current TLLI, but no
599 * P-TMSI / IMSI yet. What we now need to do is to fill
600 * this initial context with data from the HLR */
Harald Weltec728eea2010-12-24 23:07:18 +0100601 if (strlen(ctx->imsi) == 0) {
602 /* Check if we already have a MM context for this IMSI */
603 struct sgsn_mm_ctx *ictx;
604 ictx = sgsn_mm_ctx_by_imsi(mi_string);
605 if (ictx) {
Daniel Willmann46553142014-09-03 17:46:44 +0200606 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
607 "p_tmsi_old=0x%08x\n",
608 ictx->p_tmsi);
Harald Weltec728eea2010-12-24 23:07:18 +0100609 gprs_llgmm_assign(ictx->llme, ictx->tlli,
610 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Welte7b022ee2012-07-14 12:04:04 +0200611 /* FIXME: this is a hard free, we don't
612 * clean-up any PDP contexts on the
613 * libgtp side */
Harald Weltec728eea2010-12-24 23:07:18 +0100614 sgsn_mm_ctx_free(ictx);
615 }
616 }
Jacob Erlbeckfd636ae2014-07-08 09:49:07 +0200617 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
Harald Welte9b455bf2010-03-14 15:45:01 +0800618 break;
619 case GSM_MI_TYPE_IMEI:
620 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
621 break;
622 case GSM_MI_TYPE_IMEISV:
623 break;
624 }
625
Harald Welte9b455bf2010-03-14 15:45:01 +0800626 /* Check if we can let the mobile station enter */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200627 return gsm48_gmm_authorize(ctx, ctx->t3350_mode);
Harald Welte9b455bf2010-03-14 15:45:01 +0800628}
629
630/* Section 9.4.1 Attach request */
Harald Welte807a5d82010-06-01 11:53:01 +0200631static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
632 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800633{
Harald Welte943c5bc2010-04-30 16:33:12 +0200634 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200635 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Welte56a01452010-05-31 22:12:30 +0200636 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welteeaa614c2010-05-02 11:26:34 +0200637 uint16_t drx_par;
638 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800639 char mi_string[GSM48_MI_SIZE];
640 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200641 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800642
Daniel Willmann46553142014-09-03 17:46:44 +0200643 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800644
645 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
646 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
647 * or with random TLLI. */
648
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200649 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800650
651 /* MS network capability 10.5.5.12 */
652 msnc_len = *cur++;
653 msnc = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400654 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte9b455bf2010-03-14 15:45:01 +0800655 goto err_inval;
656 cur += msnc_len;
657
658 /* aTTACH Type 10.5.5.2 */
659 att_type = *cur++ & 0x0f;
660
661 /* DRX parameter 10.5.5.6 */
Harald Welte56a01452010-05-31 22:12:30 +0200662 drx_par = *cur++ << 8;
663 drx_par |= *cur++;
Harald Welte9b455bf2010-03-14 15:45:01 +0800664
665 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
666 mi_len = *cur++;
667 mi = cur;
668 if (mi_len > 8)
669 goto err_inval;
670 mi_type = *mi & GSM_MI_TYPE_MASK;
671 cur += mi_len;
672
673 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
674
Harald Welte2720e732010-05-17 00:44:57 +0200675 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
676 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800677
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200678 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte9b455bf2010-03-14 15:45:01 +0800679 cur += 6;
680
681 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte56a01452010-05-31 22:12:30 +0200682 ms_ra_acc_cap_len = *cur++;
683 ms_ra_acc_cap = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400684 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Weltec48ac472010-07-03 21:20:06 +0200685 goto err_inval;
Harald Welte4b2ed352011-07-27 23:35:38 +0200686 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800687
Daniel Willmann46553142014-09-03 17:46:44 +0200688 LOGPC(DMM, LOGL_INFO, "\n");
689
Harald Welte9b455bf2010-03-14 15:45:01 +0800690 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
691
692 switch (mi_type) {
693 case GSM_MI_TYPE_IMSI:
694 /* Try to find MM context based on IMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200695 if (!ctx)
696 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte9b455bf2010-03-14 15:45:01 +0800697 if (!ctx) {
698#if 0
699 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
700#else
Harald Welteeafe22c2010-12-24 15:39:08 +0100701 /* As a temorary hack, we simply assume that the IMSI exists,
702 * as long as it is part of 'our' network */
703 char mccmnc[16];
704 snprintf(mccmnc, sizeof(mccmnc), "%03d%02d", ra_id.mcc, ra_id.mnc);
Harald Welte7f6da482013-03-19 11:00:13 +0100705 if (strncmp(mccmnc, mi_string, 5) &&
Harald Welte3dfb5492013-03-19 11:48:54 +0100706 (sgsn->cfg.acl_enabled &&
707 !sgsn_acl_lookup(mi_string))) {
Daniel Willmann46553142014-09-03 17:46:44 +0200708 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST IMSI=%s\n",
Harald Welteeafe22c2010-12-24 15:39:08 +0100709 mi_string);
710 return gsm48_tx_gmm_att_rej_oldmsg(msg,
711 GMM_CAUSE_GPRS_NOTALLOWED);
712 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800713 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
714 if (!ctx)
Harald Welte6463c072010-05-18 17:04:55 +0200715 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_NET_FAIL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800716 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
717#endif
718 }
Harald Welte943c5bc2010-04-30 16:33:12 +0200719 ctx->tlli = msgb_tlli(msg);
Harald Welte807a5d82010-06-01 11:53:01 +0200720 ctx->llme = llme;
Harald Welte68b99a42010-05-18 12:05:42 +0200721 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800722 break;
723 case GSM_MI_TYPE_TMSI:
Harald Welteab1d5622010-05-18 19:58:38 +0200724 memcpy(&tmsi, mi+1, 4);
725 tmsi = ntohl(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800726 /* Try to find MM context based on P-TMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200727 if (!ctx)
728 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800729 if (!ctx) {
Harald Weltec728eea2010-12-24 23:07:18 +0100730 /* Allocate a context as most of our code expects one.
731 * Context will not have an IMSI ultil ID RESP is received */
Harald Welte943c5bc2010-04-30 16:33:12 +0200732 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte807a5d82010-06-01 11:53:01 +0200733 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800734 }
Harald Welte807a5d82010-06-01 11:53:01 +0200735 ctx->tlli = msgb_tlli(msg);
736 ctx->llme = llme;
737 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800738 break;
739 default:
Harald Weltec2e8cc42010-05-31 20:23:38 +0200740 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
741 "MI type %u\n", mi_type);
742 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800743 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200744 /* Update MM Context with currient RA and Cell ID */
745 ctx->ra = ra_id;
746 ctx->cell_id = cid;
Harald Welte56a01452010-05-31 22:12:30 +0200747 /* Update MM Context with other data */
748 ctx->drx_parms = drx_par;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400749 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte4b2ed352011-07-27 23:35:38 +0200750 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
751 ctx->ms_radio_access_capa.len);
Harald Welte56a01452010-05-31 22:12:30 +0200752 ctx->ms_network_capa.len = msnc_len;
753 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
754
Harald Weltec2e8cc42010-05-31 20:23:38 +0200755#ifdef PTMSI_ALLOC
Harald Welte807a5d82010-06-01 11:53:01 +0200756 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200757 ctx->p_tmsi_old = ctx->p_tmsi;
Harald Welte6463c072010-05-18 17:04:55 +0200758 ctx->p_tmsi = sgsn_alloc_ptmsi();
Harald Welteab1d5622010-05-18 19:58:38 +0200759#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200760 /* Even if there is no P-TMSI allocated, the MS will switch from
761 * foreign TLLI to local TLLI */
762 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
763
764 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200765 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
766 GPRS_ALGO_GEA0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800767
Harald Weltef6b606f2011-07-16 13:45:57 +0200768 return gsm48_gmm_authorize(ctx, GMM_T3350_MODE_ATT);
Harald Welte9b455bf2010-03-14 15:45:01 +0800769
770err_inval:
Daniel Willmann46553142014-09-03 17:46:44 +0200771 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welte6463c072010-05-18 17:04:55 +0200772 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_SEM_INCORR_MSG);
Harald Welte9b455bf2010-03-14 15:45:01 +0800773}
774
Harald Welte2720e732010-05-17 00:44:57 +0200775/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200776static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200777{
778 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Weltec0d59502010-06-28 19:11:41 +0200779 struct sgsn_pdp_ctx *pdp, *pdp2;
Harald Welte2720e732010-05-17 00:44:57 +0200780 uint8_t detach_type, power_off;
Harald Weltebd5c9122010-06-28 22:18:53 +0200781 int rc;
Harald Welte2720e732010-05-17 00:44:57 +0200782
783 detach_type = gh->data[0] & 0x7;
784 power_off = gh->data[0] & 0x8;
785
786 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
787
Daniel Willmann46553142014-09-03 17:46:44 +0200788 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte2720e732010-05-17 00:44:57 +0200789 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
790 power_off ? "Power-off" : "");
791
792 /* Mark MM state as deregistered */
793 ctx->mm_state = GMM_DEREGISTERED;
794
Harald Weltec0d59502010-06-28 19:11:41 +0200795 /* delete all existing PDP contexts for this MS */
796 llist_for_each_entry_safe(pdp, pdp2, &ctx->pdp_list, list) {
Daniel Willmann46553142014-09-03 17:46:44 +0200797 LOGMMCTXP(LOGL_NOTICE, ctx, "Dropping PDP context for NSAPI=%u "
Harald Weltec0d59502010-06-28 19:11:41 +0200798 "due to GPRS DETACH REQUEST\n", pdp->nsapi);
799 sgsn_delete_pdp_ctx(pdp);
800 /* FIXME: the callback wants to transmit a DEACT PDP CTX ACK,
801 * which is quite stupid for a MS that has just detached.. */
802 }
803
Harald Welte2720e732010-05-17 00:44:57 +0200804 /* force_stby = 0 */
Harald Weltebd5c9122010-06-28 22:18:53 +0200805 rc = gsm48_tx_gmm_det_ack(ctx, 0);
806
807 /* TLLI unassignment */
Harald Welted6f582b2010-06-30 23:18:57 +0200808 gprs_llgmm_assign(ctx->llme, ctx->tlli, 0xffffffff,
809 GPRS_ALGO_GEA0, NULL);
Harald Weltebd5c9122010-06-28 22:18:53 +0200810
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +0200811 sgsn_mm_ctx_free(ctx);
812
Harald Weltebd5c9122010-06-28 22:18:53 +0200813 return rc;
Harald Welte2720e732010-05-17 00:44:57 +0200814}
815
Harald Welte9b455bf2010-03-14 15:45:01 +0800816/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +0200817static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800818{
819 struct msgb *msg = gsm48_msgb_alloc();
820 struct gsm48_hdr *gh;
821 struct gsm48_ra_upd_ack *rua;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200822 uint8_t *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800823
Daniel Willmann46553142014-09-03 17:46:44 +0200824 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800825
Harald Welte6463c072010-05-18 17:04:55 +0200826 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800827
828 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
829 gh->proto_discr = GSM48_PDISC_MM_GPRS;
830 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
831
832 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
833 rua->force_stby = 0; /* not indicated */
834 rua->upd_result = 0; /* RA updated */
835 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200836
Harald Welte6463c072010-05-18 17:04:55 +0200837 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800838
Harald Weltec2e8cc42010-05-31 20:23:38 +0200839#if 0
840 /* Optional: P-TMSI signature */
841 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
842 ptsig = msgb_put(msg, 3);
843 ptsig[0] = mm->p_tmsi_sig >> 16;
844 ptsig[1] = mm->p_tmsi_sig >> 8;
845 ptsig[2] = mm->p_tmsi_sig & 0xff;
846#endif
847
848#ifdef PTMSI_ALLOC
849 /* Optional: Allocated P-TMSI */
850 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
851 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
852 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
853#endif
854
855 /* Option: MS ID, ... */
Harald Welte56a01452010-05-31 22:12:30 +0200856 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800857}
858
859/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200860static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800861{
862 struct msgb *msg = gsm48_msgb_alloc();
863 struct gsm48_hdr *gh;
864
Daniel Willmann46553142014-09-03 17:46:44 +0200865 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800866
Harald Welte11d7c102010-05-02 11:54:55 +0200867 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800868
869 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
870 gh->proto_discr = GSM48_PDISC_MM_GPRS;
871 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
872 gh->data[0] = cause;
873 gh->data[1] = 0; /* ? */
874
875 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200876 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800877}
878
Harald Welte588d44e2010-06-09 10:28:29 +0200879static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200880 const uint8_t *pdp_status)
Harald Welte588d44e2010-06-09 10:28:29 +0200881{
882 struct sgsn_pdp_ctx *pdp, *pdp2;
883 /* 24.008 4.7.5.1.3: If the PDP context status information element is
884 * included in ROUTING AREA UPDATE REQUEST message, then the network
885 * shall deactivate all those PDP contexts locally (without peer to
886 * peer signalling between the MS and the network), which are not in SM
887 * state PDP-INACTIVE on network side but are indicated by the MS as
888 * being in state PDP-INACTIVE. */
889
890 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Weltecdf76cf2011-08-05 21:23:46 +0200891 if (pdp->nsapi < 8) {
892 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmann46553142014-09-03 17:46:44 +0200893 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +0200894 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
895 pdp->nsapi, pdp_status[1], pdp_status[0]);
896 sgsn_delete_pdp_ctx(pdp);
897 }
898 } else {
899 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmann46553142014-09-03 17:46:44 +0200900 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +0200901 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
902 pdp->nsapi, pdp_status[1], pdp_status[0]);
903 sgsn_delete_pdp_ctx(pdp);
904 }
Harald Welte588d44e2010-06-09 10:28:29 +0200905 }
906 }
907}
908
Harald Welte9b455bf2010-03-14 15:45:01 +0800909/* Chapter 9.4.14: Routing area update request */
Harald Welte807a5d82010-06-01 11:53:01 +0200910static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
911 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800912{
Harald Welte943c5bc2010-04-30 16:33:12 +0200913 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200914 uint8_t *cur = gh->data;
Harald Welte588d44e2010-06-09 10:28:29 +0200915 uint8_t ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800916 struct gprs_ra_id old_ra_id;
Harald Welte588d44e2010-06-09 10:28:29 +0200917 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200918 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800919
920 /* Update Type 10.5.5.18 */
921 upd_type = *cur++ & 0x0f;
922
Daniel Willmann46553142014-09-03 17:46:44 +0200923 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte2720e732010-05-17 00:44:57 +0200924 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800925
926 /* Old routing area identification 10.5.5.15 */
927 gsm48_parse_ra(&old_ra_id, cur);
928 cur += 6;
929
930 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte588d44e2010-06-09 10:28:29 +0200931 ms_ra_acc_cap_len = *cur++;
Harald Welte4b2ed352011-07-27 23:35:38 +0200932 if (ms_ra_acc_cap_len > 52)
933 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
934 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800935
936 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
937 * DRX parameter, MS network capability */
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200938 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welte588d44e2010-06-09 10:28:29 +0200939 (msg->data + msg->len) - cur, 0, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800940
941 switch (upd_type) {
942 case GPRS_UPD_T_RA_LA:
943 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmann46553142014-09-03 17:46:44 +0200944 LOGP(DMM, LOGL_NOTICE, "Update type %i unsupported in Mode III, is your SI13 corrupt?\n", upd_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800945 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
946 break;
947 case GPRS_UPD_T_RA:
948 case GPRS_UPD_T_PERIODIC:
949 break;
950 }
951
952 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Weltef6bd3402010-12-23 23:34:43 +0100953 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800954 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte2b697a62011-10-16 18:50:56 +0200955 /* send a XID reset to re-set all LLC sequence numbers
956 * in the MS */
Daniel Willmann46553142014-09-03 17:46:44 +0200957 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte2b697a62011-10-16 18:50:56 +0200958 gprs_llgmm_reset(llme);
Harald Welte9b455bf2010-03-14 15:45:01 +0800959 /* The MS has to perform GPRS attach */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200960 /* Device is still IMSI atached for CS but initiate GPRS ATTACH */
961 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800962 }
963
Harald Weltef0901f02010-12-26 10:39:26 +0100964 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
965 msgid2mmctx(mmctx, msg);
966 /* Bump the statistics of received signalling msgs for this MM context */
967 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
968
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200969 /* Update the MM context with the new RA-ID */
970 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Weltef0901f02010-12-26 10:39:26 +0100971 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200972 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800973 /* FIXME: Update the MM context with the MS radio acc capabilities */
974 /* FIXME: Update the MM context with the MS network capabilities */
975
Harald Welte77289c22010-05-18 14:32:29 +0200976 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
977
Harald Weltec2e8cc42010-05-31 20:23:38 +0200978#ifdef PTMSI_ALLOC
979 mmctx->p_tmsi_old = mmctx->p_tmsi;
980 mmctx->p_tmsi = sgsn_alloc_ptmsi();
981 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
982 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
983 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
984#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200985 /* Even if there is no P-TMSI allocated, the MS will switch from
986 * foreign TLLI to local TLLI */
987 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200988
Harald Welte807a5d82010-06-01 11:53:01 +0200989 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200990 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
991 GPRS_ALGO_GEA0, NULL);
Harald Welte807a5d82010-06-01 11:53:01 +0200992
Harald Welte588d44e2010-06-09 10:28:29 +0200993 /* Look at PDP Context Status IE and see if MS's view of
994 * activated/deactivated NSAPIs agrees with our view */
995 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200996 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welte588d44e2010-06-09 10:28:29 +0200997 process_ms_ctx_status(mmctx, pdp_status);
998 }
999
Harald Welted4c6f8b2011-10-16 21:35:21 +02001000 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1001 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1002
Harald Welte807a5d82010-06-01 11:53:01 +02001003 /* Send RA UPDATE ACCEPT */
Harald Welte6463c072010-05-18 17:04:55 +02001004 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +08001005}
1006
Harald Welted193cb32010-05-17 22:58:03 +02001007static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001008{
1009 struct gsm48_hdr *gh = msgb_l3(msg);
1010
Daniel Willmann46553142014-09-03 17:46:44 +02001011 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001012 get_value_string(gmm_cause_names, gh->data[0]));
1013
1014 return 0;
1015}
1016
1017/* GPRS Mobility Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001018static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1019 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001020{
Harald Welte943c5bc2010-04-30 16:33:12 +02001021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001022 int rc;
1023
Harald Welte807a5d82010-06-01 11:53:01 +02001024 /* MMCTX can be NULL when called */
1025
Harald Welted193cb32010-05-17 22:58:03 +02001026 if (!mmctx &&
1027 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Weltec2e8cc42010-05-31 20:23:38 +02001028 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Welted193cb32010-05-17 22:58:03 +02001029 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Harald Welte2b697a62011-10-16 18:50:56 +02001030 gprs_llgmm_reset(llme);
Harald Weltef54e7e22010-06-09 10:51:23 +02001031 return gsm48_tx_gmm_status_oldmsg(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welted193cb32010-05-17 22:58:03 +02001032 }
1033
Harald Welte9b455bf2010-03-14 15:45:01 +08001034 switch (gh->msg_type) {
1035 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001036 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001037 break;
1038 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001039 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001040 break;
Daniel Willmann46553142014-09-03 17:46:44 +02001041 /* For all the following types mmctx can not be NULL */
Harald Welte9b455bf2010-03-14 15:45:01 +08001042 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +02001043 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001044 break;
1045 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001046 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001047 break;
Harald Welte2720e732010-05-17 00:44:57 +02001048 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001049 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +02001050 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001051 case GSM48_MT_GMM_ATTACH_COMPL:
1052 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001053 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001054 mmctx_timer_stop(mmctx, 3350);
1055 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001056 /* Unassign the old TLLI */
1057 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001058 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1059 GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001060 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001061 break;
Harald Welteab1d5622010-05-18 19:58:38 +02001062 case GSM48_MT_GMM_RA_UPD_COMPL:
1063 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001064 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001065 mmctx_timer_stop(mmctx, 3350);
1066 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001067 /* Unassign the old TLLI */
1068 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001069 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1070 GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001071 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001072 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001073 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmann46553142014-09-03 17:46:44 +02001074 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001075 mmctx_timer_stop(mmctx, 3350);
1076 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001077 /* Unassign the old TLLI */
1078 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001079 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001080 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001081 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001082 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001083 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001084 break;
1085 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001086 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001087 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001088 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001089 break;
1090 }
1091
1092 return rc;
1093}
1094
Harald Weltec2e8cc42010-05-31 20:23:38 +02001095static void mmctx_timer_cb(void *_mm)
1096{
1097 struct sgsn_mm_ctx *mm = _mm;
1098
1099 mm->num_T_exp++;
1100
1101 switch (mm->T) {
1102 case 3350: /* waiting for ATTACH COMPLETE */
1103 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001104 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001105 mm->mm_state = GMM_DEREGISTERED;
1106 /* FIXME: should we return some error? */
1107 break;
1108 }
1109 /* re-transmit the respective msg and re-start timer */
1110 switch (mm->t3350_mode) {
1111 case GMM_T3350_MODE_ATT:
1112 gsm48_tx_gmm_att_ack(mm);
1113 break;
1114 case GMM_T3350_MODE_RAU:
1115 gsm48_tx_gmm_ra_upd_ack(mm);
1116 break;
1117 case GMM_T3350_MODE_PTMSI_REALL:
1118 /* FIXME */
1119 break;
1120 }
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001121 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001122 break;
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001123 case 3360: /* waiting for AUTH AND CIPH RESP */
1124 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001125 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001126 mm->mm_state = GMM_DEREGISTERED;
1127 break;
1128 }
1129 /* FIXME: re-transmit the respective msg and re-start timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001130 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001131 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001132 case 3370: /* waiting for IDENTITY RESPONSE */
1133 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001134 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001135 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
1136 mm->mm_state = GMM_DEREGISTERED;
1137 break;
1138 }
1139 /* re-tranmit IDENTITY REQUEST and re-start timer */
1140 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001141 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001142 break;
1143 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001144 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Weltec2e8cc42010-05-31 20:23:38 +02001145 mm->T);
1146 }
1147}
1148
1149/* GPRS SESSION MANAGEMENT */
1150
Harald Weltea9b473a2010-12-24 21:13:26 +01001151static void pdpctx_timer_cb(void *_mm);
1152
1153static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1154 unsigned int seconds)
1155{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001156 if (osmo_timer_pending(&pdp->timer))
Harald Weltea9b473a2010-12-24 21:13:26 +01001157 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1158 "timer %u pending\n", T, pdp->T);
1159 pdp->T = T;
1160 pdp->num_T_exp = 0;
1161
1162 /* FIXME: we should do this only once ? */
1163 pdp->timer.data = pdp;
1164 pdp->timer.cb = &pdpctx_timer_cb;
1165
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001166 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001167}
1168
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001169#if 0
Harald Welte421cba42010-05-02 23:11:50 +02001170static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1171{
1172 uint8_t v[6];
1173
1174 v[0] = PDP_TYPE_ORG_IETF;
1175 v[1] = PDP_TYPE_N_IETF_IPv4;
1176 *(uint32_t *)(v+2) = htonl(ipaddr);
1177
1178 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1179}
1180
1181static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1182{
1183 uint8_t v[2];
1184
1185 v[0] = PDP_TYPE_ORG_ETSI;
1186 v[1] = PDP_TYPE_N_ETSI_PPP;
1187
1188 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1189}
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001190#endif
Harald Welte421cba42010-05-02 23:11:50 +02001191
Harald Welte9b455bf2010-03-14 15:45:01 +08001192/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +02001193int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +08001194{
Harald Welte9b455bf2010-03-14 15:45:01 +08001195 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +08001196 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +02001197 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001198
Daniel Willmann46553142014-09-03 17:46:44 +02001199 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001200
Harald Welte6abf94e2010-05-18 10:35:06 +02001201 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001202
1203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1204 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1205 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +02001206
1207 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +02001208 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +02001209
Harald Welte55e0df72010-05-18 13:20:08 +02001210 /* FIXME: copy QoS parameters from original request */
1211 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1212 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +02001213
Harald Welte421cba42010-05-02 23:11:50 +02001214 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +02001215 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +02001216
Harald Welte421cba42010-05-02 23:11:50 +02001217 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +02001218 /* Highest 4 bits of first byte need to be set to 1, otherwise
1219 * the IE is identical with the 04.08 PDP Address IE */
1220 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +02001221 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1222 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001223 pdp->lib->eua.v[0] |= 0xf0;
1224
Harald Welte77289c22010-05-18 14:32:29 +02001225 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freytherd2195072014-04-04 11:55:21 +02001226 if (pdp->lib->pco_req.l)
Harald Welte6abf94e2010-05-18 10:35:06 +02001227 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +02001228 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001229
Harald Welte421cba42010-05-02 23:11:50 +02001230 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +08001231
Harald Welte8acd88f2010-05-18 10:57:45 +02001232 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001233}
1234
Harald Welte6abf94e2010-05-18 10:35:06 +02001235/* Section 9.5.3: Activate PDP Context reject */
1236int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1237 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1238{
1239 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +02001240 struct gsm48_hdr *gh;
1241 uint8_t transaction_id = tid ^ 0x8; /* flip */
1242
Daniel Willmann46553142014-09-03 17:46:44 +02001243 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +02001244
1245 mmctx2msgid(msg, mm);
1246
1247 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1248 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1249 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1250
1251 msgb_v_put(msg, cause);
1252 if (pco_len && pco_v)
1253 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1254
Harald Welte8acd88f2010-05-18 10:57:45 +02001255 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +02001256}
1257
Harald Weltea9b473a2010-12-24 21:13:26 +01001258/* Section 9.5.8: Deactivate PDP Context Request */
1259static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1260 uint8_t sm_cause)
1261{
1262 struct msgb *msg = gsm48_msgb_alloc();
1263 struct gsm48_hdr *gh;
1264 uint8_t transaction_id = tid ^ 0x8; /* flip */
1265
Daniel Willmann46553142014-09-03 17:46:44 +02001266 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001267
1268 mmctx2msgid(msg, mm);
1269
1270 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1271 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1272 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1273
1274 msgb_v_put(msg, sm_cause);
1275
1276 return gsm48_gmm_sendmsg(msg, 0, mm);
1277}
1278int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1279{
1280 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1281
1282 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1283}
1284
Harald Welte9b455bf2010-03-14 15:45:01 +08001285/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte60ebc022010-06-08 18:08:59 +02001286static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte9b455bf2010-03-14 15:45:01 +08001287{
Harald Welte9b455bf2010-03-14 15:45:01 +08001288 struct msgb *msg = gsm48_msgb_alloc();
1289 struct gsm48_hdr *gh;
Harald Welte60ebc022010-06-08 18:08:59 +02001290 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001291
Daniel Willmann46553142014-09-03 17:46:44 +02001292 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001293
Harald Welte60ebc022010-06-08 18:08:59 +02001294 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001295
1296 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1297 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1298 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1299
Harald Welte60ebc022010-06-08 18:08:59 +02001300 return gsm48_gmm_sendmsg(msg, 0, mm);
1301}
1302int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1303{
1304 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte9b455bf2010-03-14 15:45:01 +08001305}
1306
1307/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +02001308static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1309 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001310{
Harald Welte943c5bc2010-04-30 16:33:12 +02001311 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001312 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +02001313 uint8_t req_qos_len, req_pdpa_len;
1314 uint8_t *req_qos, *req_pdpa;
1315 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +02001316 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte11ff3e82010-07-02 10:17:24 +02001317 struct sgsn_ggsn_ctx *ggsn;
Harald Welte6abf94e2010-05-18 10:35:06 +02001318 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001319
Daniel Willmann46553142014-09-03 17:46:44 +02001320 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte6abf94e2010-05-18 10:35:06 +02001321 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +02001322
Harald Welte23c13762010-07-02 22:52:04 +02001323 /* FIXME: length checks! */
Harald Welte0c3eae02010-05-02 21:07:14 +02001324 req_qos_len = act_req->data[0];
1325 req_qos = act_req->data + 1; /* 10.5.6.5 */
1326 req_pdpa_len = act_req->data[1 + req_qos_len];
1327 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +08001328
Harald Welte23c13762010-07-02 22:52:04 +02001329 /* Optional: Access Point Name, Protocol Config Options */
1330 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1331 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1332 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1333 else
1334 memset(&tp, 0, sizeof(tp));
1335
Harald Welte0c3eae02010-05-02 21:07:14 +02001336 switch (req_pdpa[0] & 0xf) {
1337 case 0x0:
1338 DEBUGPC(DMM, "ETSI ");
1339 break;
1340 case 0x1:
1341 DEBUGPC(DMM, "IETF ");
1342 break;
1343 case 0xf:
1344 DEBUGPC(DMM, "Empty ");
1345 break;
1346 }
1347
1348 switch (req_pdpa[1]) {
1349 case 0x21:
1350 DEBUGPC(DMM, "IPv4 ");
1351 if (req_pdpa_len >= 6) {
1352 struct in_addr ia;
1353 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1354 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1355 }
1356 break;
1357 case 0x57:
1358 DEBUGPC(DMM, "IPv6 ");
1359 if (req_pdpa_len >= 18) {
1360 /* FIXME: print IPv6 address */
1361 }
1362 break;
1363 default:
1364 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1365 break;
1366 }
1367
Daniel Willmann46553142014-09-03 17:46:44 +02001368 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welte77289c22010-05-18 14:32:29 +02001369
Harald Welte2720e732010-05-17 00:44:57 +02001370 /* put the non-TLV elements in the TLV parser structure to
1371 * pass them on to the SGSN / GTP code */
1372 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1373 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1374 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1375 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1376
Harald Welte6abf94e2010-05-18 10:35:06 +02001377 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +02001378 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +02001379
1380 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1381 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1382 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1383 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1384 GSM_CAUSE_INV_MAND_INFO,
1385 0, NULL);
1386 }
1387
1388 /* Check if NSAPI is already in use */
Harald Welteb37515e2010-07-02 10:12:58 +02001389 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1390 if (pdp) {
1391 /* We already have a PDP context for this TLLI + NSAPI tuple */
1392 if (pdp->sapi == act_req->req_llc_sapi &&
1393 pdp->ti == transaction_id) {
1394 /* This apparently is a re-transmission of a PDP CTX
1395 * ACT REQ (our ACT ACK must have got dropped) */
1396 return gsm48_tx_gsm_act_pdp_acc(pdp);
1397 }
1398
1399 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte6abf94e2010-05-18 10:35:06 +02001400 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1401 GSM_CAUSE_NSAPI_IN_USE,
1402 0, NULL);
1403 }
Harald Welte9b455bf2010-03-14 15:45:01 +08001404
Harald Welte19f9e302010-07-02 10:15:49 +02001405 /* Only increment counter for a real activation, after we checked
1406 * for re-transmissions */
1407 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1408
Harald Welte11ff3e82010-07-02 10:17:24 +02001409 ggsn = sgsn_ggsn_ctx_by_id(0);
1410 if (!ggsn) {
1411 LOGP(DGPRS, LOGL_ERROR, "No GGSN context 0 found!\n");
1412 return -EIO;
Harald Welted193cb32010-05-17 22:58:03 +02001413 }
Harald Welte11ff3e82010-07-02 10:17:24 +02001414 ggsn->gsn = sgsn->gsn;
1415 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1416 if (!pdp)
1417 return -1;
1418
1419 /* Store SAPI and Transaction Identifier */
1420 pdp->sapi = act_req->req_llc_sapi;
1421 pdp->ti = transaction_id;
1422
Harald Welte6463c072010-05-18 17:04:55 +02001423 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +08001424}
1425
1426/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +02001427static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001428{
Harald Welte943c5bc2010-04-30 16:33:12 +02001429 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +02001430 uint8_t transaction_id = (gh->proto_discr >> 4);
1431 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001432
Daniel Willmann46553142014-09-03 17:46:44 +02001433 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001434 get_value_string(gsm_cause_names, gh->data[0]));
1435
Harald Welte77289c22010-05-18 14:32:29 +02001436 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1437 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001438 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welte77289c22010-05-18 14:32:29 +02001439 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1440 mm->imsi, transaction_id);
Harald Welte60ebc022010-06-08 18:08:59 +02001441 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welte77289c22010-05-18 14:32:29 +02001442 }
1443
1444 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +08001445}
1446
Harald Weltea9b473a2010-12-24 21:13:26 +01001447/* Section 9.5.9: Deactivate PDP Context Accept */
1448static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1449{
1450 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1451 uint8_t transaction_id = (gh->proto_discr >> 4);
1452 struct sgsn_pdp_ctx *pdp;
1453
Daniel Willmann46553142014-09-03 17:46:44 +02001454 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001455
1456 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1457 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001458 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Weltea9b473a2010-12-24 21:13:26 +01001459 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1460 mm->imsi, transaction_id);
1461 return 0;
1462 }
1463
1464 return sgsn_delete_pdp_ctx(pdp);
1465}
1466
Harald Welted193cb32010-05-17 22:58:03 +02001467static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001468{
1469 struct gsm48_hdr *gh = msgb_l3(msg);
1470
Daniel Willmann46553142014-09-03 17:46:44 +02001471 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001472 get_value_string(gsm_cause_names, gh->data[0]));
1473
1474 return 0;
1475}
1476
Harald Weltea9b473a2010-12-24 21:13:26 +01001477static void pdpctx_timer_cb(void *_pdp)
1478{
1479 struct sgsn_pdp_ctx *pdp = _pdp;
1480
1481 pdp->num_T_exp++;
1482
1483 switch (pdp->T) {
1484 case 3395: /* waiting for PDP CTX DEACT ACK */
1485 if (pdp->num_T_exp >= 4) {
Daniel Willmann46553142014-09-03 17:46:44 +02001486 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001487 pdp->state = PDP_STATE_INACTIVE;
1488 sgsn_delete_pdp_ctx(pdp);
1489 break;
1490 }
1491 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001492 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001493 break;
1494 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001495 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Weltea9b473a2010-12-24 21:13:26 +01001496 pdp->T);
1497 }
1498}
1499
1500
Harald Welte9b455bf2010-03-14 15:45:01 +08001501/* GPRS Session Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001502static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1503 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001504{
Harald Welte943c5bc2010-04-30 16:33:12 +02001505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001506 int rc;
1507
Harald Welte807a5d82010-06-01 11:53:01 +02001508 /* MMCTX can be NULL when called */
1509
Harald Welted193cb32010-05-17 22:58:03 +02001510 if (!mmctx) {
1511 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Harald Welte18d14522010-07-02 16:18:18 +02001512 gsm48_tx_gmm_status_oldmsg(msg, GMM_CAUSE_IMPL_DETACHED);
1513 return gsm48_tx_sm_status_oldmsg(msg, GSM_CAUSE_PROTO_ERR_UNSPEC);
Harald Welted193cb32010-05-17 22:58:03 +02001514 }
1515
Harald Welte9b455bf2010-03-14 15:45:01 +08001516 switch (gh->msg_type) {
1517 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001518 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001519 break;
1520 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001521 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +02001522 break;
Harald Weltea9b473a2010-12-24 21:13:26 +01001523 case GSM48_MT_GSM_DEACT_PDP_ACK:
1524 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1525 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001526 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001527 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001528 break;
1529 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1530 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1531 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmann46553142014-09-03 17:46:44 +02001532 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001533 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001534 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001535 break;
1536 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001537 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001538 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001539 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001540 break;
1541
1542 }
1543
1544 return rc;
1545}
1546
1547/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte807a5d82010-06-01 11:53:01 +02001548int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001549{
Harald Welte943c5bc2010-04-30 16:33:12 +02001550 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001551 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +02001552 struct sgsn_mm_ctx *mmctx;
1553 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +08001554 int rc = -EINVAL;
1555
Harald Welted193cb32010-05-17 22:58:03 +02001556 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1557 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +02001558 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +02001559 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +02001560 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte807a5d82010-06-01 11:53:01 +02001561 mmctx->llme = llme;
Harald Welte8acd88f2010-05-18 10:57:45 +02001562 }
Harald Welted193cb32010-05-17 22:58:03 +02001563
1564 /* MMCTX can be NULL */
1565
Harald Welte9b455bf2010-03-14 15:45:01 +08001566 switch (pdisc) {
1567 case GSM48_PDISC_MM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001568 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001569 break;
1570 case GSM48_PDISC_SM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001571 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001572 break;
1573 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001574 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001575 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +02001576 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +08001577 break;
1578 }
1579
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +02001580 /* MMCTX can be invalid */
1581
Harald Welte9b455bf2010-03-14 15:45:01 +08001582 return rc;
1583}
Harald Welte5bfe4992010-06-09 11:22:47 +02001584
1585int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1586{
1587 struct sgsn_mm_ctx *mmctx;
1588
1589 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1590 if (!mmctx) {
1591 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1592 "TLLI=%08x\n", tlli);
1593 return -EINVAL;
1594 }
1595
1596 if (mmctx->mm_state != GMM_REGISTERED_NORMAL) {
Daniel Willmann46553142014-09-03 17:46:44 +02001597 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001598 "!= REGISTERED (TLLI=%08x)\n", tlli);
1599 return -EINVAL;
1600 }
1601
1602 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1603 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1604 return 0;
1605}
1606
1607int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1608 uint8_t suspend_ref)
1609{
1610 struct sgsn_mm_ctx *mmctx;
1611
1612 /* FIXME: make use of suspend reference? */
1613
1614 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1615 if (!mmctx) {
1616 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1617 "TLLI=%08x\n", tlli);
1618 return -EINVAL;
1619 }
1620
1621 if (mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001622 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001623 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1624 /* FIXME: should we not simply ignore it? */
1625 return -EINVAL;
1626 }
1627
1628 /* Transition from SUSPENDED to NORMAL */
1629 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1630 return 0;
1631}