blob: ef61adecce4a0e82c7df59c11e47867301162b86 [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
4/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 *
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>
31
32#include <openbsc/db.h>
33#include <osmocore/msgb.h>
34#include <osmocore/tlv.h>
35#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <osmocore/gsm_utils.h>
38#include <openbsc/gsm_subscriber.h>
39#include <openbsc/gsm_04_08.h>
40#include <openbsc/gsm_04_08_gprs.h>
41#include <openbsc/paging.h>
42#include <osmocore/signal.h>
43#include <osmocore/talloc.h>
44#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020045#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080046#include <openbsc/gprs_llc.h>
47#include <openbsc/gprs_sgsn.h>
48
49/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
50struct value_string gmm_cause_names[] = {
51 /* FIXME */
52 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
53 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
54 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
55 "Message type non-existant or not implemented" },
56 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
57 "Message type not compatible with protocol state" },
58 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
59 "Information element non-existent or not implemented" },
60 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
61 { GMM_CAUSE_MSG_INCOMP_P_STATE,
62 "Message not compatible with protocol state " },
63 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
64 { 0, NULL }
65};
66
67/* 10.5.6.6 SM Cause / Table 10.5.157 */
68struct value_string gsm_cause_names[] = {
69 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
70 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
71 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
72 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
73 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
74 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
75 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
76 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
77 "Requested service option not subscribed" },
78 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
79 "Service option temporarily out of order" },
80 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
81 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
82 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
83 { GSM_CAUSE_NET_FAIL, "Network Failure" },
84 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
85 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
86 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
87 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
88 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
89 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
90 "Message type non-existant or not implemented" },
91 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
92 "Message type not compatible with protocol state" },
93 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
94 "Information element non-existent or not implemented" },
95 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
96 { GSM_CAUSE_MSG_INCOMP_P_STATE,
97 "Message not compatible with protocol state " },
98 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
99 { 0, NULL }
100};
101
Harald Welteeaa614c2010-05-02 11:26:34 +0200102static const char *att_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800103{
104 switch (type) {
105 case GPRS_ATT_T_ATTACH:
106 return "GPRS attach";
107 case GPRS_ATT_T_ATT_WHILE_IMSI:
108 return "GPRS attach while IMSI attached";
109 case GPRS_ATT_T_COMBINED:
110 return "Combined GPRS/IMSI attach";
111 default:
112 return "unknown";
113 }
114}
115
Harald Welteeaa614c2010-05-02 11:26:34 +0200116static const char *upd_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800117{
118 switch (type) {
119 case GPRS_UPD_T_RA:
120 return "RA updating";
121 case GPRS_UPD_T_RA_LA:
122 return "combined RA/LA updating";
123 case GPRS_UPD_T_RA_LA_IMSI_ATT:
124 return "combined RA/LA updating + IMSI attach";
125 case GPRS_UPD_T_PERIODIC:
126 return "periodic updating";
127 }
128 return "unknown";
129}
130
Harald Welte9b455bf2010-03-14 15:45:01 +0800131/* Send a message through the underlying layer */
132static int gsm48_gmm_sendmsg(struct msgb *msg, int command)
133{
Harald Welte11d7c102010-05-02 11:54:55 +0200134 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800135 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
136}
137
Harald Welte11d7c102010-05-02 11:54:55 +0200138/* copy identifiers from old message to new message, this
139 * is required so lower layers can route it correctly */
140static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
141{
142 msgb_tlli(msg) = msgb_tlli(old);
143 msgb_bvci(msg) = msgb_bvci(old);
144 msgb_nsei(msg) = msgb_nsei(old);
145}
146
Harald Welte9b455bf2010-03-14 15:45:01 +0800147/* Chapter 9.4.2: Attach accept */
148static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
149{
150 struct msgb *msg = gsm48_msgb_alloc();
151 struct gsm48_hdr *gh;
152 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200153 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800154
155 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
156
Harald Welte11d7c102010-05-02 11:54:55 +0200157 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800158
159 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
160 gh->proto_discr = GSM48_PDISC_MM_GPRS;
161 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
162
163 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
164 aa->force_stby = 0; /* not indicated */
165 aa->att_result = 1; /* GPRS only */
166 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
167 aa->radio_prio = 4; /* lowest */
Harald Welte11d7c102010-05-02 11:54:55 +0200168 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
169 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800170
171 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
172 return gsm48_gmm_sendmsg(msg, 0);
173}
174
175/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200176static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800177{
178 struct msgb *msg = gsm48_msgb_alloc();
179 struct gsm48_hdr *gh;
180
181 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
182
Harald Welte11d7c102010-05-02 11:54:55 +0200183 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800184
185 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
186 gh->proto_discr = GSM48_PDISC_MM_GPRS;
187 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
188 gh->data[0] = gmm_cause;
189
190 return gsm48_gmm_sendmsg(msg, 0);
191}
192
193/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200194static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800195{
196 struct msgb *msg = gsm48_msgb_alloc();
197 struct gsm48_hdr *gh;
198
199 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
200
Harald Welte11d7c102010-05-02 11:54:55 +0200201 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800202
203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
204 gh->proto_discr = GSM48_PDISC_MM_GPRS;
205 gh->msg_type = GSM48_MT_GMM_ID_REQ;
206 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
207 gh->data[0] = id_type & 0xf;
208
209 return gsm48_gmm_sendmsg(msg, 0);
210}
211
212/* Check if we can already authorize a subscriber */
213static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
214{
215 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
216 ctx->mm_state = GMM_REGISTERED_NORMAL;
217 return gsm48_tx_gmm_att_ack(msg);
218 }
219 if (!strlen(ctx->imei))
220 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
221
222 if (!strlen(ctx->imsi))
223 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
224
225 return 0;
226}
227
228/* Parse Chapter 9.4.13 Identity Response */
229static int gsm48_rx_gmm_id_resp(struct msgb *msg)
230{
Harald Welte943c5bc2010-04-30 16:33:12 +0200231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200232 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800233 char mi_string[GSM48_MI_SIZE];
234 struct gprs_ra_id ra_id;
235 struct sgsn_mm_ctx *ctx;
236
237 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
238 DEBUGP(DMM, "GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
239 mi_type, mi_string);
240
Harald Welte11d7c102010-05-02 11:54:55 +0200241 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte943c5bc2010-04-30 16:33:12 +0200242 ctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800243 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200244 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800245 return -EINVAL;
246 }
247
248 switch (mi_type) {
249 case GSM_MI_TYPE_IMSI:
250 /* we already have a mm context with current TLLI, but no
251 * P-TMSI / IMSI yet. What we now need to do is to fill
252 * this initial context with data from the HLR */
253 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
254 break;
255 case GSM_MI_TYPE_IMEI:
256 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
257 break;
258 case GSM_MI_TYPE_IMEISV:
259 break;
260 }
261
262 DEBUGPC(DMM, "\n");
263 /* Check if we can let the mobile station enter */
264 return gsm48_gmm_authorize(ctx, msg);
265}
266
267static void attach_rej_cb(void *data)
268{
269 struct sgsn_mm_ctx *ctx = data;
270
271 /* FIXME: determine through which BTS/TRX to send this */
272 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
273 ctx->mm_state = GMM_DEREGISTERED;
274 /* FIXME: release the context */
275}
276
277static void schedule_reject(struct sgsn_mm_ctx *ctx)
278{
279 ctx->T = 3370;
280 ctx->timer.cb = attach_rej_cb;
281 ctx->timer.data = ctx;
282 bsc_schedule_timer(&ctx->timer, 6, 0);
283}
284
285/* Section 9.4.1 Attach request */
286static int gsm48_rx_gmm_att_req(struct msgb *msg)
287{
Harald Welte943c5bc2010-04-30 16:33:12 +0200288 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200289 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
290 uint8_t msnc_len, att_type, mi_len, mi_type;
291 uint16_t drx_par;
292 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800293 char mi_string[GSM48_MI_SIZE];
294 struct gprs_ra_id ra_id;
295 struct sgsn_mm_ctx *ctx;
296
297 DEBUGP(DMM, "GMM ATTACH REQUEST ");
298
299 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
300 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
301 * or with random TLLI. */
302
Harald Welte11d7c102010-05-02 11:54:55 +0200303 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800304
305 /* MS network capability 10.5.5.12 */
306 msnc_len = *cur++;
307 msnc = cur;
308 if (msnc_len > 2)
309 goto err_inval;
310 cur += msnc_len;
311
312 /* aTTACH Type 10.5.5.2 */
313 att_type = *cur++ & 0x0f;
314
315 /* DRX parameter 10.5.5.6 */
316 drx_par = *cur++;
317 drx_par |= *cur++ << 8;
318
319 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
320 mi_len = *cur++;
321 mi = cur;
322 if (mi_len > 8)
323 goto err_inval;
324 mi_type = *mi & GSM_MI_TYPE_MASK;
325 cur += mi_len;
326
327 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
328
329 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, att_name(att_type));
330
331 /* Old routing area identification 10.5.5.15 */
332 old_ra_info = cur;
333 cur += 6;
334
335 /* MS Radio Access Capability 10.5.5.12a */
336
337 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
338
339 switch (mi_type) {
340 case GSM_MI_TYPE_IMSI:
341 /* Try to find MM context based on IMSI */
342 ctx = sgsn_mm_ctx_by_imsi(mi_string);
343 if (!ctx) {
344#if 0
345 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
346#else
347 /* As a temorary hack, we simply assume that the IMSI exists */
348 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
349 if (!ctx)
350 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
351 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
352#endif
353 }
354 /* FIXME: Start some timer */
355 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200356 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800357 break;
358 case GSM_MI_TYPE_TMSI:
359 tmsi = strtoul(mi_string, NULL, 10);
360 /* Try to find MM context based on P-TMSI */
361 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
362 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200363 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800364 /* FIXME: Start some timer */
365 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200366 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800367 }
368 break;
369 default:
370 break;
371 }
372
373 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
374 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
375
376 DEBUGPC(DMM, "\n");
377
378 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
379
380err_inval:
381 DEBUGPC(DMM, "\n");
382 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
383}
384
385/* Chapter 9.4.15: Routing area update accept */
386static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
387{
388 struct msgb *msg = gsm48_msgb_alloc();
389 struct gsm48_hdr *gh;
390 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200391 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800392
393 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
394
Harald Welte11d7c102010-05-02 11:54:55 +0200395 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800396
397 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
398 gh->proto_discr = GSM48_PDISC_MM_GPRS;
399 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
400
401 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
402 rua->force_stby = 0; /* not indicated */
403 rua->upd_result = 0; /* RA updated */
404 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200405
406 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
407 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800408
409 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
410 return gsm48_gmm_sendmsg(msg, 0);
411}
412
413/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200414static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800415{
416 struct msgb *msg = gsm48_msgb_alloc();
417 struct gsm48_hdr *gh;
418
419 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
420
Harald Welte11d7c102010-05-02 11:54:55 +0200421 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800422
423 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
424 gh->proto_discr = GSM48_PDISC_MM_GPRS;
425 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
426 gh->data[0] = cause;
427 gh->data[1] = 0; /* ? */
428
429 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
430 return gsm48_gmm_sendmsg(msg, 0);
431}
432
433/* Chapter 9.4.14: Routing area update request */
434static int gsm48_rx_gmm_ra_upd_req(struct msgb *msg)
435{
Harald Welte943c5bc2010-04-30 16:33:12 +0200436 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800437 struct sgsn_mm_ctx *mmctx;
Harald Welteeaa614c2010-05-02 11:26:34 +0200438 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800439 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200440 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800441
442 /* Update Type 10.5.5.18 */
443 upd_type = *cur++ & 0x0f;
444
445 DEBUGP(DMM, "GMM RA UPDATE REQUEST type=\"%s\" ", upd_name(upd_type));
446
447 /* Old routing area identification 10.5.5.15 */
448 gsm48_parse_ra(&old_ra_id, cur);
449 cur += 6;
450
451 /* MS Radio Access Capability 10.5.5.12a */
452
453 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
454 * DRX parameter, MS network capability */
455
456 switch (upd_type) {
457 case GPRS_UPD_T_RA_LA:
458 case GPRS_UPD_T_RA_LA_IMSI_ATT:
459 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
460 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
461 break;
462 case GPRS_UPD_T_RA:
463 case GPRS_UPD_T_PERIODIC:
464 break;
465 }
466
467 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte943c5bc2010-04-30 16:33:12 +0200468 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800469 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
470 /* The MS has to perform GPRS attach */
471 DEBUGPC(DMM, " REJECT\n");
472 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
473 }
474
475 /* FIXME: Update the MM context with the new RA-ID */
476 /* FIXME: Update the MM context with the new TLLI */
477 /* FIXME: Update the MM context with the MS radio acc capabilities */
478 /* FIXME: Update the MM context with the MS network capabilities */
479
480 DEBUGPC(DMM, " ACCEPT\n");
481 return gsm48_tx_gmm_ra_upd_ack(msg);
482}
483
484static int gsm48_rx_gmm_status(struct msgb *msg)
485{
486 struct gsm48_hdr *gh = msgb_l3(msg);
487
488 DEBUGP(DMM, "GPRS MM STATUS (cause: %s)\n",
489 get_value_string(gmm_cause_names, gh->data[0]));
490
491 return 0;
492}
493
494/* GPRS Mobility Management */
495static int gsm0408_rcv_gmm(struct msgb *msg)
496{
Harald Welte943c5bc2010-04-30 16:33:12 +0200497 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800498 int rc;
499
500 switch (gh->msg_type) {
501 case GSM48_MT_GMM_RA_UPD_REQ:
502 rc = gsm48_rx_gmm_ra_upd_req(msg);
503 break;
504 case GSM48_MT_GMM_ATTACH_REQ:
505 rc = gsm48_rx_gmm_att_req(msg);
506 break;
507 case GSM48_MT_GMM_ID_RESP:
508 rc = gsm48_rx_gmm_id_resp(msg);
509 break;
510 case GSM48_MT_GMM_STATUS:
511 rc = gsm48_rx_gmm_status(msg);
512 break;
513 case GSM48_MT_GMM_RA_UPD_COMPL:
514 /* only in case SGSN offered new P-TMSI */
515 case GSM48_MT_GMM_ATTACH_COMPL:
516 /* only in case SGSN offered new P-TMSI */
517 case GSM48_MT_GMM_DETACH_REQ:
518 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
519 case GSM48_MT_GMM_AUTH_CIPH_RESP:
520 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
521 gh->msg_type);
522 break;
523 default:
524 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
525 gh->msg_type);
526 break;
527 }
528
529 return rc;
530}
531
532/* Section 9.5.2: Ativate PDP Context Accept */
533static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req)
534{
Harald Welte943c5bc2010-04-30 16:33:12 +0200535 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800536 struct msgb *msg = gsm48_msgb_alloc();
537 struct gsm48_act_pdp_ctx_ack *act_ack;
538 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200539 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800540
541 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
542
Harald Welte11d7c102010-05-02 11:54:55 +0200543 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800544
545 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
546 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
547 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
548 act_ack = (struct gsm48_act_pdp_ctx_ack *)
549 msgb_put(msg, sizeof(*act_ack));
550 act_ack->llc_sapi = req->req_llc_sapi;
551 memcpy(act_ack->qos_lv, req->req_qos_lv, sizeof(act_ack->qos_lv));
552 //act_ack->radio_prio = 4;
553
554 return gsm48_gmm_sendmsg(msg, 0);
555}
556
557/* Section 9.5.9: Deactivate PDP Context Accept */
558static int gsm48_tx_gsm_deact_pdp_acc(struct msgb *old_msg)
559{
Harald Welte943c5bc2010-04-30 16:33:12 +0200560 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800561 struct msgb *msg = gsm48_msgb_alloc();
562 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200563 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800564
565 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
566
Harald Welte11d7c102010-05-02 11:54:55 +0200567 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800568
569 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
570 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
571 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
572
573 return gsm48_gmm_sendmsg(msg, 0);
574}
575
576/* Section 9.5.1: Activate PDP Context Request */
577static int gsm48_rx_gsm_act_pdp_req(struct msgb *msg)
578{
Harald Welte943c5bc2010-04-30 16:33:12 +0200579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800580 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200581 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800582
583 DEBUGP(DMM, "ACTIVATE PDP CONTEXT REQ\n");
584
585 /* FIXME: parse access point name + IPCP config options */
586
587 return gsm48_tx_gsm_act_pdp_acc(msg, act_req);
588}
589
590/* Section 9.5.8: Deactivate PDP Context Request */
591static int gsm48_rx_gsm_deact_pdp_req(struct msgb *msg)
592{
Harald Welte943c5bc2010-04-30 16:33:12 +0200593 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800594
595 DEBUGP(DMM, "DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
596 get_value_string(gsm_cause_names, gh->data[0]));
597
598 return gsm48_tx_gsm_deact_pdp_acc(msg);
599}
600
601static int gsm48_rx_gsm_status(struct msgb *msg)
602{
603 struct gsm48_hdr *gh = msgb_l3(msg);
604
605 DEBUGP(DMM, "GPRS SM STATUS (cause: %s)\n",
606 get_value_string(gsm_cause_names, gh->data[0]));
607
608 return 0;
609}
610
611/* GPRS Session Management */
612static int gsm0408_rcv_gsm(struct msgb *msg)
613{
Harald Welte943c5bc2010-04-30 16:33:12 +0200614 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800615 int rc;
616
617 switch (gh->msg_type) {
618 case GSM48_MT_GSM_ACT_PDP_REQ:
619 rc = gsm48_rx_gsm_act_pdp_req(msg);
620 break;
621 case GSM48_MT_GSM_DEACT_PDP_REQ:
622 rc = gsm48_rx_gsm_deact_pdp_req(msg);
623 case GSM48_MT_GSM_STATUS:
624 rc = gsm48_rx_gsm_status(msg);
625 break;
626 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
627 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
628 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
629 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
630 gh->msg_type);
631 break;
632 default:
633 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
634 gh->msg_type);
635 break;
636
637 }
638
639 return rc;
640}
641
642/* Main entry point for incoming 04.08 GPRS messages */
643int gsm0408_gprs_rcvmsg(struct msgb *msg)
644{
Harald Welte943c5bc2010-04-30 16:33:12 +0200645 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200646 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte9b455bf2010-03-14 15:45:01 +0800647 int rc = -EINVAL;
648
649 switch (pdisc) {
650 case GSM48_PDISC_MM_GPRS:
651 rc = gsm0408_rcv_gmm(msg);
652 break;
653 case GSM48_PDISC_SM_GPRS:
654 rc = gsm0408_rcv_gsm(msg);
655 break;
656 default:
657 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
658 pdisc);
659 break;
660 }
661
662 return rc;
663}