blob: 391a0b11a8ee24b8645e9c40f5e70cd1fecaef1d [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 Welte9b455bf2010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
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>
34#include <osmocore/msgb.h>
35#include <osmocore/tlv.h>
Harald Welted6057092010-05-02 12:57:45 +020036#include <osmocore/gsm_utils.h>
37#include <osmocore/signal.h>
38#include <osmocore/talloc.h>
39
Harald Welte9b455bf2010-03-14 15:45:01 +080040#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080042#include <openbsc/gsm_subscriber.h>
43#include <openbsc/gsm_04_08.h>
44#include <openbsc/gsm_04_08_gprs.h>
45#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080046#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020047#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080048#include <openbsc/gprs_llc.h>
49#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020050#include <openbsc/gprs_gmm.h>
Harald Welted193cb32010-05-17 22:58:03 +020051#include <openbsc/sgsn.h>
52
Harald Welte6abf94e2010-05-18 10:35:06 +020053#include <pdp.h>
54
Harald Welted193cb32010-05-17 22:58:03 +020055extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080056
Harald Welte2720e732010-05-17 00:44:57 +020057/* Protocol related stuff, should go into libosmocore */
58
Harald Welte9b455bf2010-03-14 15:45:01 +080059/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
Harald Welte2720e732010-05-17 00:44:57 +020060const struct value_string gmm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080061 /* FIXME */
62 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
63 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
64 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
65 "Message type non-existant or not implemented" },
66 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
67 "Message type not compatible with protocol state" },
68 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
69 "Information element non-existent or not implemented" },
70 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
71 { GMM_CAUSE_MSG_INCOMP_P_STATE,
72 "Message not compatible with protocol state " },
73 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
74 { 0, NULL }
75};
76
77/* 10.5.6.6 SM Cause / Table 10.5.157 */
Harald Welte2720e732010-05-17 00:44:57 +020078const struct value_string gsm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080079 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
80 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
81 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
82 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
83 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
84 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
85 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
86 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
87 "Requested service option not subscribed" },
88 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
89 "Service option temporarily out of order" },
90 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
91 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
92 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
93 { GSM_CAUSE_NET_FAIL, "Network Failure" },
94 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
95 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
96 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
97 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
98 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
99 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
100 "Message type non-existant or not implemented" },
101 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
102 "Message type not compatible with protocol state" },
103 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
104 "Information element non-existent or not implemented" },
105 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
106 { GSM_CAUSE_MSG_INCOMP_P_STATE,
107 "Message not compatible with protocol state " },
108 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
109 { 0, NULL }
110};
111
Harald Welte2720e732010-05-17 00:44:57 +0200112/* 10.5.5.2 */
113const struct value_string gprs_att_t_strs[] = {
114 { GPRS_ATT_T_ATTACH, "GPRS attach" },
115 { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" },
116 { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" },
117 { 0, NULL }
118};
Harald Welte9b455bf2010-03-14 15:45:01 +0800119
Harald Welte2720e732010-05-17 00:44:57 +0200120const struct value_string gprs_upd_t_strs[] = {
121 { GPRS_UPD_T_RA, "RA updating" },
122 { GPRS_UPD_T_RA_LA, "combined RA/LA updating" },
123 { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" },
124 { GPRS_UPD_T_PERIODIC, "periodic updating" },
125 { 0, NULL }
126};
127
128/* 10.5.5.5 */
129const struct value_string gprs_det_t_mo_strs[] = {
130 { GPRS_DET_T_MO_GPRS, "GPRS detach" },
131 { GPRS_DET_T_MO_IMSI, "IMSI detach" },
132 { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" },
133 { 0, NULL }
134};
135
136/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800137
Harald Welte9b455bf2010-03-14 15:45:01 +0800138/* Send a message through the underlying layer */
139static int gsm48_gmm_sendmsg(struct msgb *msg, int command)
140{
Harald Welte11d7c102010-05-02 11:54:55 +0200141 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800142 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
143}
144
Harald Welte11d7c102010-05-02 11:54:55 +0200145/* copy identifiers from old message to new message, this
146 * is required so lower layers can route it correctly */
147static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
148{
149 msgb_tlli(msg) = msgb_tlli(old);
150 msgb_bvci(msg) = msgb_bvci(old);
151 msgb_nsei(msg) = msgb_nsei(old);
152}
153
Harald Welte6abf94e2010-05-18 10:35:06 +0200154/* Store BVCI/NSEI in MM context */
155static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
156{
157 mm->bvci = msgb_bvci(msg);
158 mm->nsei = msgb_nsei(msg);
159}
160
161/* Store BVCI/NSEI in MM context */
162static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
163{
164 msgb_tlli(msg) = mm->tlli;
165 msgb_bvci(msg) = mm->bvci;
166 msgb_nsei(msg) = mm->nsei;
167}
168
Harald Welte86fda902010-05-03 10:54:35 +0200169static struct gsm48_qos default_qos = {
170 .delay_class = 4, /* best effort */
171 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
172 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
173 .preced_class = GSM48_QOS_PC_NORMAL,
174 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
175 .traf_class = GSM48_QOS_TC_INTERACTIVE,
176 .deliv_order = GSM48_QOS_DO_UNORDERED,
177 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
178 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
179 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
180 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
181 .resid_ber = GSM48_QOS_RBER_5e_2,
182 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
183 .handling_prio = 3,
184 .xfer_delay = 0x10, /* 200ms */
185 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
186 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
187 .sig_ind = 0, /* not optimised for signalling */
188 .max_bitrate_down_ext = 0, /* use octet 9 */
189 .guar_bitrate_down_ext = 0, /* use octet 13 */
190};
191
Harald Welte9b455bf2010-03-14 15:45:01 +0800192/* Chapter 9.4.2: Attach accept */
193static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
194{
195 struct msgb *msg = gsm48_msgb_alloc();
196 struct gsm48_hdr *gh;
197 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200198 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800199
200 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
201
Harald Welte11d7c102010-05-02 11:54:55 +0200202 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800203
204 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
205 gh->proto_discr = GSM48_PDISC_MM_GPRS;
206 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
207
208 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
209 aa->force_stby = 0; /* not indicated */
210 aa->att_result = 1; /* GPRS only */
211 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
212 aa->radio_prio = 4; /* lowest */
Harald Welteba021102010-05-02 21:36:36 +0200213 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200214 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800215
216 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
217 return gsm48_gmm_sendmsg(msg, 0);
218}
219
220/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200221static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800222{
223 struct msgb *msg = gsm48_msgb_alloc();
224 struct gsm48_hdr *gh;
225
226 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
227
Harald Welte11d7c102010-05-02 11:54:55 +0200228 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800229
230 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
231 gh->proto_discr = GSM48_PDISC_MM_GPRS;
232 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
233 gh->data[0] = gmm_cause;
234
235 return gsm48_gmm_sendmsg(msg, 0);
236}
237
Harald Welte2720e732010-05-17 00:44:57 +0200238/* Chapter 9.4.6.2 Detach accept */
239static gsm48_tx_gmm_det_ack(struct msgb *old_msg, uint8_t force_stby)
240{
241 struct msgb *msg = gsm48_msgb_alloc();
242 struct gsm48_hdr *gh;
243
244 DEBUGP(DMM, "<- GPRS DETACH ACCEPT\n");
245
246 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
247 gh->proto_discr = GSM48_PDISC_MM_GPRS;
248 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
249 gh->data[0] = force_stby;
250
251 return gsm48_gmm_sendmsg(msg, 0);
252}
253
Harald Welte9b455bf2010-03-14 15:45:01 +0800254/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200255static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800256{
257 struct msgb *msg = gsm48_msgb_alloc();
258 struct gsm48_hdr *gh;
259
260 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
261
Harald Welte11d7c102010-05-02 11:54:55 +0200262 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800263
264 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
265 gh->proto_discr = GSM48_PDISC_MM_GPRS;
266 gh->msg_type = GSM48_MT_GMM_ID_REQ;
267 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
268 gh->data[0] = id_type & 0xf;
269
270 return gsm48_gmm_sendmsg(msg, 0);
271}
272
273/* Check if we can already authorize a subscriber */
274static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
275{
276 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
277 ctx->mm_state = GMM_REGISTERED_NORMAL;
278 return gsm48_tx_gmm_att_ack(msg);
279 }
280 if (!strlen(ctx->imei))
281 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
282
283 if (!strlen(ctx->imsi))
284 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
285
286 return 0;
287}
288
289/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200290static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800291{
Harald Welte943c5bc2010-04-30 16:33:12 +0200292 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200293 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800294 char mi_string[GSM48_MI_SIZE];
295 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800296
297 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte2720e732010-05-17 00:44:57 +0200298 DEBUGP(DMM, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
Harald Welte9b455bf2010-03-14 15:45:01 +0800299 mi_type, mi_string);
300
Harald Welte9b455bf2010-03-14 15:45:01 +0800301 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200302 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800303 return -EINVAL;
304 }
305
306 switch (mi_type) {
307 case GSM_MI_TYPE_IMSI:
308 /* we already have a mm context with current TLLI, but no
309 * P-TMSI / IMSI yet. What we now need to do is to fill
310 * this initial context with data from the HLR */
311 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
312 break;
313 case GSM_MI_TYPE_IMEI:
314 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
315 break;
316 case GSM_MI_TYPE_IMEISV:
317 break;
318 }
319
320 DEBUGPC(DMM, "\n");
321 /* Check if we can let the mobile station enter */
322 return gsm48_gmm_authorize(ctx, msg);
323}
324
325static void attach_rej_cb(void *data)
326{
327 struct sgsn_mm_ctx *ctx = data;
328
329 /* FIXME: determine through which BTS/TRX to send this */
330 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
331 ctx->mm_state = GMM_DEREGISTERED;
332 /* FIXME: release the context */
333}
334
335static void schedule_reject(struct sgsn_mm_ctx *ctx)
336{
337 ctx->T = 3370;
338 ctx->timer.cb = attach_rej_cb;
339 ctx->timer.data = ctx;
340 bsc_schedule_timer(&ctx->timer, 6, 0);
341}
342
343/* Section 9.4.1 Attach request */
Harald Welted193cb32010-05-17 22:58:03 +0200344static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800345{
Harald Welte943c5bc2010-04-30 16:33:12 +0200346 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200347 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
348 uint8_t msnc_len, att_type, mi_len, mi_type;
349 uint16_t drx_par;
350 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800351 char mi_string[GSM48_MI_SIZE];
352 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200353 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800354 struct sgsn_mm_ctx *ctx;
355
Harald Welte2720e732010-05-17 00:44:57 +0200356 DEBUGP(DMM, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800357
358 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
359 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
360 * or with random TLLI. */
361
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200362 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800363
364 /* MS network capability 10.5.5.12 */
365 msnc_len = *cur++;
366 msnc = cur;
367 if (msnc_len > 2)
368 goto err_inval;
369 cur += msnc_len;
370
371 /* aTTACH Type 10.5.5.2 */
372 att_type = *cur++ & 0x0f;
373
374 /* DRX parameter 10.5.5.6 */
375 drx_par = *cur++;
376 drx_par |= *cur++ << 8;
377
378 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
379 mi_len = *cur++;
380 mi = cur;
381 if (mi_len > 8)
382 goto err_inval;
383 mi_type = *mi & GSM_MI_TYPE_MASK;
384 cur += mi_len;
385
386 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
387
Harald Welte2720e732010-05-17 00:44:57 +0200388 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
389 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800390
391 /* Old routing area identification 10.5.5.15 */
392 old_ra_info = cur;
393 cur += 6;
394
395 /* MS Radio Access Capability 10.5.5.12a */
396
397 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
398
399 switch (mi_type) {
400 case GSM_MI_TYPE_IMSI:
401 /* Try to find MM context based on IMSI */
402 ctx = sgsn_mm_ctx_by_imsi(mi_string);
403 if (!ctx) {
404#if 0
405 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
406#else
407 /* As a temorary hack, we simply assume that the IMSI exists */
408 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
409 if (!ctx)
410 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
411 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
412#endif
413 }
414 /* FIXME: Start some timer */
415 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200416 ctx->tlli = msgb_tlli(msg);
Harald Welte6abf94e2010-05-18 10:35:06 +0200417 msgid2mmctx(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800418 break;
419 case GSM_MI_TYPE_TMSI:
420 tmsi = strtoul(mi_string, NULL, 10);
421 /* Try to find MM context based on P-TMSI */
422 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
423 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200424 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800425 /* FIXME: Start some timer */
426 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200427 ctx->tlli = msgb_tlli(msg);
Harald Welte6abf94e2010-05-18 10:35:06 +0200428 msgid2mmctx(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800429 }
430 break;
431 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200432 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800433 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200434 /* Update MM Context with currient RA and Cell ID */
435 ctx->ra = ra_id;
436 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800437
438 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
439 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
440
441 DEBUGPC(DMM, "\n");
442
443 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
444
445err_inval:
446 DEBUGPC(DMM, "\n");
447 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
448}
449
Harald Welte2720e732010-05-17 00:44:57 +0200450/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200451static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200452{
453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +0200454 uint8_t detach_type, power_off;
Harald Welte2720e732010-05-17 00:44:57 +0200455
456 detach_type = gh->data[0] & 0x7;
457 power_off = gh->data[0] & 0x8;
458
459 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
460
461 DEBUGP(DMM, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
462 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
463 power_off ? "Power-off" : "");
464
465 /* Mark MM state as deregistered */
466 ctx->mm_state = GMM_DEREGISTERED;
467
468 /* force_stby = 0 */
469 return gsm48_tx_gmm_det_ack(msg, 0);
470}
471
Harald Welte9b455bf2010-03-14 15:45:01 +0800472/* Chapter 9.4.15: Routing area update accept */
473static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
474{
475 struct msgb *msg = gsm48_msgb_alloc();
476 struct gsm48_hdr *gh;
477 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200478 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800479
480 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
481
Harald Welte11d7c102010-05-02 11:54:55 +0200482 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800483
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_RA_UPD_ACK;
487
488 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
489 rua->force_stby = 0; /* not indicated */
490 rua->upd_result = 0; /* RA updated */
491 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200492
Harald Welteba021102010-05-02 21:36:36 +0200493 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200494 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800495
496 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
497 return gsm48_gmm_sendmsg(msg, 0);
498}
499
500/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200501static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800502{
503 struct msgb *msg = gsm48_msgb_alloc();
504 struct gsm48_hdr *gh;
505
506 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
507
Harald Welte11d7c102010-05-02 11:54:55 +0200508 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800509
510 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
511 gh->proto_discr = GSM48_PDISC_MM_GPRS;
512 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
513 gh->data[0] = cause;
514 gh->data[1] = 0; /* ? */
515
516 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
517 return gsm48_gmm_sendmsg(msg, 0);
518}
519
520/* Chapter 9.4.14: Routing area update request */
Harald Welted193cb32010-05-17 22:58:03 +0200521static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800522{
Harald Welte943c5bc2010-04-30 16:33:12 +0200523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200524 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800525 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200526 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800527
528 /* Update Type 10.5.5.18 */
529 upd_type = *cur++ & 0x0f;
530
Harald Welte2720e732010-05-17 00:44:57 +0200531 DEBUGP(DMM, "-> GMM RA UPDATE REQUEST type=\"%s\" ",
532 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800533
534 /* Old routing area identification 10.5.5.15 */
535 gsm48_parse_ra(&old_ra_id, cur);
536 cur += 6;
537
538 /* MS Radio Access Capability 10.5.5.12a */
539
540 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
541 * DRX parameter, MS network capability */
542
543 switch (upd_type) {
544 case GPRS_UPD_T_RA_LA:
545 case GPRS_UPD_T_RA_LA_IMSI_ATT:
546 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
547 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
548 break;
549 case GPRS_UPD_T_RA:
550 case GPRS_UPD_T_PERIODIC:
551 break;
552 }
553
554 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800555 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
556 /* The MS has to perform GPRS attach */
557 DEBUGPC(DMM, " REJECT\n");
558 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
559 }
560
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200561 /* Update the MM context with the new RA-ID */
562 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
563 /* Update the MM context with the new TLLI */
564 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800565 /* FIXME: Update the MM context with the MS radio acc capabilities */
566 /* FIXME: Update the MM context with the MS network capabilities */
567
568 DEBUGPC(DMM, " ACCEPT\n");
569 return gsm48_tx_gmm_ra_upd_ack(msg);
570}
571
Harald Welted193cb32010-05-17 22:58:03 +0200572static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800573{
574 struct gsm48_hdr *gh = msgb_l3(msg);
575
Harald Welte2720e732010-05-17 00:44:57 +0200576 DEBUGP(DMM, "-> GPRS MM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800577 get_value_string(gmm_cause_names, gh->data[0]));
578
579 return 0;
580}
581
582/* GPRS Mobility Management */
Harald Welted193cb32010-05-17 22:58:03 +0200583static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800584{
Harald Welte943c5bc2010-04-30 16:33:12 +0200585 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800586 int rc;
587
Harald Welted193cb32010-05-17 22:58:03 +0200588 if (!mmctx &&
589 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
590 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
591 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
592 return -EINVAL;
593 }
594
595
Harald Welte9b455bf2010-03-14 15:45:01 +0800596 switch (gh->msg_type) {
597 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200598 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800599 break;
600 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200601 rc = gsm48_rx_gmm_att_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800602 break;
603 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +0200604 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800605 break;
606 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200607 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800608 break;
Harald Welte2720e732010-05-17 00:44:57 +0200609 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200610 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +0200611 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800612 case GSM48_MT_GMM_RA_UPD_COMPL:
613 /* only in case SGSN offered new P-TMSI */
614 case GSM48_MT_GMM_ATTACH_COMPL:
615 /* only in case SGSN offered new P-TMSI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800616 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
617 case GSM48_MT_GMM_AUTH_CIPH_RESP:
618 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
619 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200620 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800621 break;
622 default:
623 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
624 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200625 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800626 break;
627 }
628
629 return rc;
630}
631
Harald Welte421cba42010-05-02 23:11:50 +0200632static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
633{
634 uint8_t v[6];
635
636 v[0] = PDP_TYPE_ORG_IETF;
637 v[1] = PDP_TYPE_N_IETF_IPv4;
638 *(uint32_t *)(v+2) = htonl(ipaddr);
639
640 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
641}
642
643static void msgb_put_pdp_addr_ppp(struct msgb *msg)
644{
645 uint8_t v[2];
646
647 v[0] = PDP_TYPE_ORG_ETSI;
648 v[1] = PDP_TYPE_N_ETSI_PPP;
649
650 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
651}
652
Harald Welte9b455bf2010-03-14 15:45:01 +0800653/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +0200654int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +0800655{
Harald Welte9b455bf2010-03-14 15:45:01 +0800656 struct msgb *msg = gsm48_msgb_alloc();
657 struct gsm48_act_pdp_ctx_ack *act_ack;
658 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +0200659 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800660
661 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
662
Harald Welte6abf94e2010-05-18 10:35:06 +0200663 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800664
665 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
666 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
667 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +0200668
669 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +0200670 msgb_v_put(msg, pdp->sapi);
Harald Welte421cba42010-05-02 23:11:50 +0200671 /* copy QoS parameters from original request */
Harald Welte6abf94e2010-05-18 10:35:06 +0200672 msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
673 //msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte421cba42010-05-02 23:11:50 +0200674 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +0200675 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte421cba42010-05-02 23:11:50 +0200676 /* PDP address */
Harald Welte6abf94e2010-05-18 10:35:06 +0200677 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
678 pdp->lib->eua.l, pdp->lib->eua.v);
679 //msgb_put_pdp_addr_ipv4(msg, 0x01020304);
Harald Welte421cba42010-05-02 23:11:50 +0200680 /* Optional: Protocol configuration options */
Harald Welte6abf94e2010-05-18 10:35:06 +0200681 if (pdp->lib->pco_neg.l && pdp->lib->pco_neg.v)
682 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
683 pdp->lib->pco_neg.l, pdp->lib->pco_neg.v);
Harald Welte421cba42010-05-02 23:11:50 +0200684 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +0800685
686 return gsm48_gmm_sendmsg(msg, 0);
687}
688
Harald Welte6abf94e2010-05-18 10:35:06 +0200689/* Section 9.5.3: Activate PDP Context reject */
690int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
691 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
692{
693 struct msgb *msg = gsm48_msgb_alloc();
694 struct gsm48_act_pdp_ctx_ack *act_ack;
695 struct gsm48_hdr *gh;
696 uint8_t transaction_id = tid ^ 0x8; /* flip */
697
698 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
699
700 mmctx2msgid(msg, mm);
701
702 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
703 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
704 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
705
706 msgb_v_put(msg, cause);
707 if (pco_len && pco_v)
708 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
709
710 return gsm48_gmm_sendmsg(msg, 0);
711}
712
Harald Welte9b455bf2010-03-14 15:45:01 +0800713/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welted193cb32010-05-17 22:58:03 +0200714static int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mmctx,
715 struct msgb *old_msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800716{
Harald Welte943c5bc2010-04-30 16:33:12 +0200717 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800718 struct msgb *msg = gsm48_msgb_alloc();
719 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200720 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800721
722 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
723
Harald Welte11d7c102010-05-02 11:54:55 +0200724 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800725
726 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
727 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
728 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
729
730 return gsm48_gmm_sendmsg(msg, 0);
731}
732
733/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +0200734static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
735 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800736{
Harald Welte943c5bc2010-04-30 16:33:12 +0200737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800738 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200739 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte0c3eae02010-05-02 21:07:14 +0200740 uint8_t req_qos_len, req_pdpa_len;
741 uint8_t *req_qos, *req_pdpa;
742 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +0200743 uint8_t transaction_id = (gh->proto_discr >> 4);
744 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800745
Harald Welte6abf94e2010-05-18 10:35:06 +0200746 DEBUGP(DMM, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
747 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte0c3eae02010-05-02 21:07:14 +0200748 req_qos_len = act_req->data[0];
749 req_qos = act_req->data + 1; /* 10.5.6.5 */
750 req_pdpa_len = act_req->data[1 + req_qos_len];
751 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800752
Harald Welte0c3eae02010-05-02 21:07:14 +0200753 switch (req_pdpa[0] & 0xf) {
754 case 0x0:
755 DEBUGPC(DMM, "ETSI ");
756 break;
757 case 0x1:
758 DEBUGPC(DMM, "IETF ");
759 break;
760 case 0xf:
761 DEBUGPC(DMM, "Empty ");
762 break;
763 }
764
765 switch (req_pdpa[1]) {
766 case 0x21:
767 DEBUGPC(DMM, "IPv4 ");
768 if (req_pdpa_len >= 6) {
769 struct in_addr ia;
770 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
771 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
772 }
773 break;
774 case 0x57:
775 DEBUGPC(DMM, "IPv6 ");
776 if (req_pdpa_len >= 18) {
777 /* FIXME: print IPv6 address */
778 }
779 break;
780 default:
781 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
782 break;
783 }
784
Harald Welte2720e732010-05-17 00:44:57 +0200785 /* put the non-TLV elements in the TLV parser structure to
786 * pass them on to the SGSN / GTP code */
787 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
788 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
789 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
790 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
791
Harald Welte6abf94e2010-05-18 10:35:06 +0200792 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +0200793 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +0200794
795 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
796 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
797 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
798 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
799 GSM_CAUSE_INV_MAND_INFO,
800 0, NULL);
801 }
802
803 /* Check if NSAPI is already in use */
804 if (sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi)) {
805 /* FIXME: send reject with GSM_CAUSE_NSAPI_IN_USE */
806 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
807 GSM_CAUSE_NSAPI_IN_USE,
808 0, NULL);
809 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800810
Harald Welted193cb32010-05-17 22:58:03 +0200811#if 1
812 {
813 struct ggsn_ctx ggsn;
814 ggsn.gtp_version = 1;
815 inet_aton("192.168.100.239", &ggsn.remote_addr);
816 ggsn.gsn = sgsn->gsn;
Harald Welte6abf94e2010-05-18 10:35:06 +0200817 pdp = sgsn_create_pdp_ctx(&ggsn, mmctx, act_req->req_nsapi, &tp);
818 if (!pdp)
819 return -1;
820 pdp->sapi = act_req->req_llc_sapi;
821 pdp->ti = transaction_id;
822
Harald Welted193cb32010-05-17 22:58:03 +0200823 }
Harald Welte2720e732010-05-17 00:44:57 +0200824#else
Harald Welte6abf94e2010-05-18 10:35:06 +0200825 return gsm48_tx_gsm_act_pdp_acc(mmctx, transaction_id, act_req);
Harald Welte2720e732010-05-17 00:44:57 +0200826#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800827}
828
829/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +0200830static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800831{
Harald Welte943c5bc2010-04-30 16:33:12 +0200832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800833
Harald Welte2720e732010-05-17 00:44:57 +0200834 DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800835 get_value_string(gsm_cause_names, gh->data[0]));
836
Harald Welted193cb32010-05-17 22:58:03 +0200837 return gsm48_tx_gsm_deact_pdp_acc(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800838}
839
Harald Welted193cb32010-05-17 22:58:03 +0200840static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800841{
842 struct gsm48_hdr *gh = msgb_l3(msg);
843
Harald Welte2720e732010-05-17 00:44:57 +0200844 DEBUGP(DMM, "-> GPRS SM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800845 get_value_string(gsm_cause_names, gh->data[0]));
846
847 return 0;
848}
849
850/* GPRS Session Management */
Harald Welted193cb32010-05-17 22:58:03 +0200851static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800852{
Harald Welte943c5bc2010-04-30 16:33:12 +0200853 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800854 int rc;
855
Harald Welted193cb32010-05-17 22:58:03 +0200856 if (!mmctx) {
857 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
858 return -EINVAL;
859 }
860
Harald Welte9b455bf2010-03-14 15:45:01 +0800861 switch (gh->msg_type) {
862 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200863 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800864 break;
865 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200866 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800867 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200868 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800869 break;
870 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
871 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
872 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
873 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
874 gh->msg_type);
875 break;
876 default:
877 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
878 gh->msg_type);
879 break;
880
881 }
882
883 return rc;
884}
885
886/* Main entry point for incoming 04.08 GPRS messages */
887int gsm0408_gprs_rcvmsg(struct msgb *msg)
888{
Harald Welte943c5bc2010-04-30 16:33:12 +0200889 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200890 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +0200891 struct sgsn_mm_ctx *mmctx;
892 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800893 int rc = -EINVAL;
894
Harald Welted193cb32010-05-17 22:58:03 +0200895 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
896 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte6abf94e2010-05-18 10:35:06 +0200897 if (mmctx)
898 msgid2mmctx(mmctx, msg);
Harald Welted193cb32010-05-17 22:58:03 +0200899
900 /* MMCTX can be NULL */
901
Harald Welte9b455bf2010-03-14 15:45:01 +0800902 switch (pdisc) {
903 case GSM48_PDISC_MM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200904 rc = gsm0408_rcv_gmm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800905 break;
906 case GSM48_PDISC_SM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200907 rc = gsm0408_rcv_gsm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800908 break;
909 default:
910 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
911 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +0200912 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +0800913 break;
914 }
915
916 return rc;
917}