blob: d05f4cdd6a4f7904e1c4c58b3a0c288f80dae8b2 [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>
Harald Welte8acd88f2010-05-18 10:57:45 +020039#include <osmocore/rate_ctr.h>
Harald Welted6057092010-05-02 12:57:45 +020040
Harald Welte9b455bf2010-03-14 15:45:01 +080041#include <openbsc/debug.h>
42#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080043#include <openbsc/gsm_subscriber.h>
44#include <openbsc/gsm_04_08.h>
45#include <openbsc/gsm_04_08_gprs.h>
46#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080047#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020048#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080049#include <openbsc/gprs_llc.h>
50#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020051#include <openbsc/gprs_gmm.h>
Harald Welted193cb32010-05-17 22:58:03 +020052#include <openbsc/sgsn.h>
53
Harald Welte6abf94e2010-05-18 10:35:06 +020054#include <pdp.h>
55
Harald Welted193cb32010-05-17 22:58:03 +020056extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080057
Harald Welte2720e732010-05-17 00:44:57 +020058/* Protocol related stuff, should go into libosmocore */
59
Harald Welte9b455bf2010-03-14 15:45:01 +080060/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
Harald Welte2720e732010-05-17 00:44:57 +020061const struct value_string gmm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080062 /* FIXME */
63 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
64 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
65 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
66 "Message type non-existant or not implemented" },
67 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
68 "Message type not compatible with protocol state" },
69 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
70 "Information element non-existent or not implemented" },
71 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
72 { GMM_CAUSE_MSG_INCOMP_P_STATE,
73 "Message not compatible with protocol state " },
74 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
75 { 0, NULL }
76};
77
78/* 10.5.6.6 SM Cause / Table 10.5.157 */
Harald Welte2720e732010-05-17 00:44:57 +020079const struct value_string gsm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080080 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
81 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
82 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
83 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
84 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
85 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
86 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
87 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
88 "Requested service option not subscribed" },
89 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
90 "Service option temporarily out of order" },
91 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
92 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
93 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
94 { GSM_CAUSE_NET_FAIL, "Network Failure" },
95 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
96 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
97 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
98 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
99 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
100 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
101 "Message type non-existant or not implemented" },
102 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
103 "Message type not compatible with protocol state" },
104 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
105 "Information element non-existent or not implemented" },
106 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
107 { GSM_CAUSE_MSG_INCOMP_P_STATE,
108 "Message not compatible with protocol state " },
109 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
110 { 0, NULL }
111};
112
Harald Welte2720e732010-05-17 00:44:57 +0200113/* 10.5.5.2 */
114const struct value_string gprs_att_t_strs[] = {
115 { GPRS_ATT_T_ATTACH, "GPRS attach" },
116 { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" },
117 { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" },
118 { 0, NULL }
119};
Harald Welte9b455bf2010-03-14 15:45:01 +0800120
Harald Welte2720e732010-05-17 00:44:57 +0200121const struct value_string gprs_upd_t_strs[] = {
122 { GPRS_UPD_T_RA, "RA updating" },
123 { GPRS_UPD_T_RA_LA, "combined RA/LA updating" },
124 { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" },
125 { GPRS_UPD_T_PERIODIC, "periodic updating" },
126 { 0, NULL }
127};
128
129/* 10.5.5.5 */
130const struct value_string gprs_det_t_mo_strs[] = {
131 { GPRS_DET_T_MO_GPRS, "GPRS detach" },
132 { GPRS_DET_T_MO_IMSI, "IMSI detach" },
133 { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" },
134 { 0, NULL }
135};
136
137/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800138
Harald Welte9b455bf2010-03-14 15:45:01 +0800139/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200140static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
141 const struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800142{
Harald Welte8acd88f2010-05-18 10:57:45 +0200143 if (mm)
144 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
145
Harald Welte11d7c102010-05-02 11:54:55 +0200146 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800147 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
148}
149
Harald Welte11d7c102010-05-02 11:54:55 +0200150/* copy identifiers from old message to new message, this
151 * is required so lower layers can route it correctly */
152static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
153{
154 msgb_tlli(msg) = msgb_tlli(old);
155 msgb_bvci(msg) = msgb_bvci(old);
156 msgb_nsei(msg) = msgb_nsei(old);
157}
158
Harald Welte6abf94e2010-05-18 10:35:06 +0200159/* Store BVCI/NSEI in MM context */
160static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
161{
162 mm->bvci = msgb_bvci(msg);
163 mm->nsei = msgb_nsei(msg);
164}
165
166/* Store BVCI/NSEI in MM context */
167static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
168{
169 msgb_tlli(msg) = mm->tlli;
170 msgb_bvci(msg) = mm->bvci;
171 msgb_nsei(msg) = mm->nsei;
172}
173
Harald Welte86fda902010-05-03 10:54:35 +0200174static struct gsm48_qos default_qos = {
175 .delay_class = 4, /* best effort */
176 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
177 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
178 .preced_class = GSM48_QOS_PC_NORMAL,
179 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
180 .traf_class = GSM48_QOS_TC_INTERACTIVE,
181 .deliv_order = GSM48_QOS_DO_UNORDERED,
182 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
183 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
184 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
185 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
186 .resid_ber = GSM48_QOS_RBER_5e_2,
187 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
188 .handling_prio = 3,
189 .xfer_delay = 0x10, /* 200ms */
190 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
191 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
192 .sig_ind = 0, /* not optimised for signalling */
193 .max_bitrate_down_ext = 0, /* use octet 9 */
194 .guar_bitrate_down_ext = 0, /* use octet 13 */
195};
196
Harald Welte9b455bf2010-03-14 15:45:01 +0800197/* Chapter 9.4.2: Attach accept */
198static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
199{
200 struct msgb *msg = gsm48_msgb_alloc();
201 struct gsm48_hdr *gh;
202 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200203 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800204
205 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
206
Harald Welte11d7c102010-05-02 11:54:55 +0200207 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800208
209 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
210 gh->proto_discr = GSM48_PDISC_MM_GPRS;
211 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
212
213 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
214 aa->force_stby = 0; /* not indicated */
215 aa->att_result = 1; /* GPRS only */
216 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
217 aa->radio_prio = 4; /* lowest */
Harald Welteba021102010-05-02 21:36:36 +0200218 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200219 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800220
221 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200222 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800223}
224
225/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200226static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800227{
228 struct msgb *msg = gsm48_msgb_alloc();
229 struct gsm48_hdr *gh;
230
231 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
232
Harald Welte11d7c102010-05-02 11:54:55 +0200233 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800234
235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
236 gh->proto_discr = GSM48_PDISC_MM_GPRS;
237 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
238 gh->data[0] = gmm_cause;
239
Harald Welte8acd88f2010-05-18 10:57:45 +0200240 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800241}
242
Harald Welte2720e732010-05-17 00:44:57 +0200243/* Chapter 9.4.6.2 Detach accept */
244static gsm48_tx_gmm_det_ack(struct msgb *old_msg, uint8_t force_stby)
245{
246 struct msgb *msg = gsm48_msgb_alloc();
247 struct gsm48_hdr *gh;
248
249 DEBUGP(DMM, "<- GPRS DETACH ACCEPT\n");
250
251 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
252 gh->proto_discr = GSM48_PDISC_MM_GPRS;
253 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
254 gh->data[0] = force_stby;
255
Harald Welte8acd88f2010-05-18 10:57:45 +0200256 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte2720e732010-05-17 00:44:57 +0200257}
258
Harald Welte9b455bf2010-03-14 15:45:01 +0800259/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200260static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800261{
262 struct msgb *msg = gsm48_msgb_alloc();
263 struct gsm48_hdr *gh;
264
265 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
266
Harald Welte11d7c102010-05-02 11:54:55 +0200267 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800268
269 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
270 gh->proto_discr = GSM48_PDISC_MM_GPRS;
271 gh->msg_type = GSM48_MT_GMM_ID_REQ;
272 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
273 gh->data[0] = id_type & 0xf;
274
Harald Welte8acd88f2010-05-18 10:57:45 +0200275 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800276}
277
278/* Check if we can already authorize a subscriber */
279static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
280{
281 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
282 ctx->mm_state = GMM_REGISTERED_NORMAL;
283 return gsm48_tx_gmm_att_ack(msg);
284 }
285 if (!strlen(ctx->imei))
286 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
287
288 if (!strlen(ctx->imsi))
289 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
290
291 return 0;
292}
293
294/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200295static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800296{
Harald Welte943c5bc2010-04-30 16:33:12 +0200297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200298 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800299 char mi_string[GSM48_MI_SIZE];
300 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800301
302 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte2720e732010-05-17 00:44:57 +0200303 DEBUGP(DMM, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
Harald Welte9b455bf2010-03-14 15:45:01 +0800304 mi_type, mi_string);
305
Harald Welte9b455bf2010-03-14 15:45:01 +0800306 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200307 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800308 return -EINVAL;
309 }
310
311 switch (mi_type) {
312 case GSM_MI_TYPE_IMSI:
313 /* we already have a mm context with current TLLI, but no
314 * P-TMSI / IMSI yet. What we now need to do is to fill
315 * this initial context with data from the HLR */
316 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
317 break;
318 case GSM_MI_TYPE_IMEI:
319 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
320 break;
321 case GSM_MI_TYPE_IMEISV:
322 break;
323 }
324
325 DEBUGPC(DMM, "\n");
326 /* Check if we can let the mobile station enter */
327 return gsm48_gmm_authorize(ctx, msg);
328}
329
330static void attach_rej_cb(void *data)
331{
332 struct sgsn_mm_ctx *ctx = data;
333
334 /* FIXME: determine through which BTS/TRX to send this */
335 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
336 ctx->mm_state = GMM_DEREGISTERED;
337 /* FIXME: release the context */
338}
339
340static void schedule_reject(struct sgsn_mm_ctx *ctx)
341{
342 ctx->T = 3370;
343 ctx->timer.cb = attach_rej_cb;
344 ctx->timer.data = ctx;
345 bsc_schedule_timer(&ctx->timer, 6, 0);
346}
347
348/* Section 9.4.1 Attach request */
Harald Welted193cb32010-05-17 22:58:03 +0200349static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800350{
Harald Welte943c5bc2010-04-30 16:33:12 +0200351 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200352 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
353 uint8_t msnc_len, att_type, mi_len, mi_type;
354 uint16_t drx_par;
355 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800356 char mi_string[GSM48_MI_SIZE];
357 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200358 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800359 struct sgsn_mm_ctx *ctx;
360
Harald Welte2720e732010-05-17 00:44:57 +0200361 DEBUGP(DMM, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800362
363 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
364 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
365 * or with random TLLI. */
366
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200367 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800368
369 /* MS network capability 10.5.5.12 */
370 msnc_len = *cur++;
371 msnc = cur;
372 if (msnc_len > 2)
373 goto err_inval;
374 cur += msnc_len;
375
376 /* aTTACH Type 10.5.5.2 */
377 att_type = *cur++ & 0x0f;
378
379 /* DRX parameter 10.5.5.6 */
380 drx_par = *cur++;
381 drx_par |= *cur++ << 8;
382
383 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
384 mi_len = *cur++;
385 mi = cur;
386 if (mi_len > 8)
387 goto err_inval;
388 mi_type = *mi & GSM_MI_TYPE_MASK;
389 cur += mi_len;
390
391 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
392
Harald Welte2720e732010-05-17 00:44:57 +0200393 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
394 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800395
396 /* Old routing area identification 10.5.5.15 */
397 old_ra_info = cur;
398 cur += 6;
399
400 /* MS Radio Access Capability 10.5.5.12a */
401
402 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
403
404 switch (mi_type) {
405 case GSM_MI_TYPE_IMSI:
406 /* Try to find MM context based on IMSI */
407 ctx = sgsn_mm_ctx_by_imsi(mi_string);
408 if (!ctx) {
409#if 0
410 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
411#else
412 /* As a temorary hack, we simply assume that the IMSI exists */
413 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
414 if (!ctx)
415 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
416 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
417#endif
418 }
419 /* FIXME: Start some timer */
420 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200421 ctx->tlli = msgb_tlli(msg);
Harald Welte68b99a42010-05-18 12:05:42 +0200422 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800423 break;
424 case GSM_MI_TYPE_TMSI:
425 tmsi = strtoul(mi_string, NULL, 10);
426 /* Try to find MM context based on P-TMSI */
427 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
428 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200429 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800430 /* FIXME: Start some timer */
431 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200432 ctx->tlli = msgb_tlli(msg);
Harald Welte68b99a42010-05-18 12:05:42 +0200433 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800434 }
435 break;
436 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200437 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800438 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200439 /* Update MM Context with currient RA and Cell ID */
440 ctx->ra = ra_id;
441 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800442
443 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
444 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
445
446 DEBUGPC(DMM, "\n");
447
448 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
449
450err_inval:
451 DEBUGPC(DMM, "\n");
452 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
453}
454
Harald Welte2720e732010-05-17 00:44:57 +0200455/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200456static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200457{
458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +0200459 uint8_t detach_type, power_off;
Harald Welte2720e732010-05-17 00:44:57 +0200460
461 detach_type = gh->data[0] & 0x7;
462 power_off = gh->data[0] & 0x8;
463
464 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
465
466 DEBUGP(DMM, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
467 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
468 power_off ? "Power-off" : "");
469
470 /* Mark MM state as deregistered */
471 ctx->mm_state = GMM_DEREGISTERED;
472
473 /* force_stby = 0 */
474 return gsm48_tx_gmm_det_ack(msg, 0);
475}
476
Harald Welte9b455bf2010-03-14 15:45:01 +0800477/* Chapter 9.4.15: Routing area update accept */
478static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
479{
480 struct msgb *msg = gsm48_msgb_alloc();
481 struct gsm48_hdr *gh;
482 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200483 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800484
485 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
486
Harald Welte11d7c102010-05-02 11:54:55 +0200487 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800488
489 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
490 gh->proto_discr = GSM48_PDISC_MM_GPRS;
491 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
492
493 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
494 rua->force_stby = 0; /* not indicated */
495 rua->upd_result = 0; /* RA updated */
496 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200497
Harald Welteba021102010-05-02 21:36:36 +0200498 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200499 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800500
501 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200502 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800503}
504
505/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200506static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800507{
508 struct msgb *msg = gsm48_msgb_alloc();
509 struct gsm48_hdr *gh;
510
511 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
512
Harald Welte11d7c102010-05-02 11:54:55 +0200513 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800514
515 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
516 gh->proto_discr = GSM48_PDISC_MM_GPRS;
517 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
518 gh->data[0] = cause;
519 gh->data[1] = 0; /* ? */
520
521 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200522 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800523}
524
525/* Chapter 9.4.14: Routing area update request */
Harald Welted193cb32010-05-17 22:58:03 +0200526static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800527{
Harald Welte943c5bc2010-04-30 16:33:12 +0200528 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200529 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800530 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200531 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800532
Harald Welte8acd88f2010-05-18 10:57:45 +0200533 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
534
Harald Welte9b455bf2010-03-14 15:45:01 +0800535 /* Update Type 10.5.5.18 */
536 upd_type = *cur++ & 0x0f;
537
Harald Welte2720e732010-05-17 00:44:57 +0200538 DEBUGP(DMM, "-> GMM RA UPDATE REQUEST type=\"%s\" ",
539 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800540
541 /* Old routing area identification 10.5.5.15 */
542 gsm48_parse_ra(&old_ra_id, cur);
543 cur += 6;
544
545 /* MS Radio Access Capability 10.5.5.12a */
546
547 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
548 * DRX parameter, MS network capability */
549
550 switch (upd_type) {
551 case GPRS_UPD_T_RA_LA:
552 case GPRS_UPD_T_RA_LA_IMSI_ATT:
553 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
554 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
555 break;
556 case GPRS_UPD_T_RA:
557 case GPRS_UPD_T_PERIODIC:
558 break;
559 }
560
561 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800562 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
563 /* The MS has to perform GPRS attach */
564 DEBUGPC(DMM, " REJECT\n");
565 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
566 }
567
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200568 /* Update the MM context with the new RA-ID */
569 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
570 /* Update the MM context with the new TLLI */
571 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800572 /* FIXME: Update the MM context with the MS radio acc capabilities */
573 /* FIXME: Update the MM context with the MS network capabilities */
574
575 DEBUGPC(DMM, " ACCEPT\n");
576 return gsm48_tx_gmm_ra_upd_ack(msg);
577}
578
Harald Welted193cb32010-05-17 22:58:03 +0200579static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800580{
581 struct gsm48_hdr *gh = msgb_l3(msg);
582
Harald Welte2720e732010-05-17 00:44:57 +0200583 DEBUGP(DMM, "-> GPRS MM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800584 get_value_string(gmm_cause_names, gh->data[0]));
585
586 return 0;
587}
588
589/* GPRS Mobility Management */
Harald Welted193cb32010-05-17 22:58:03 +0200590static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800591{
Harald Welte943c5bc2010-04-30 16:33:12 +0200592 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800593 int rc;
594
Harald Welted193cb32010-05-17 22:58:03 +0200595 if (!mmctx &&
596 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
597 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
598 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
599 return -EINVAL;
600 }
601
602
Harald Welte9b455bf2010-03-14 15:45:01 +0800603 switch (gh->msg_type) {
604 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200605 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800606 break;
607 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200608 rc = gsm48_rx_gmm_att_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800609 break;
610 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +0200611 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800612 break;
613 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200614 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800615 break;
Harald Welte2720e732010-05-17 00:44:57 +0200616 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200617 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +0200618 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800619 case GSM48_MT_GMM_RA_UPD_COMPL:
620 /* only in case SGSN offered new P-TMSI */
621 case GSM48_MT_GMM_ATTACH_COMPL:
622 /* only in case SGSN offered new P-TMSI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800623 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
624 case GSM48_MT_GMM_AUTH_CIPH_RESP:
625 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
626 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200627 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800628 break;
629 default:
630 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
631 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200632 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800633 break;
634 }
635
636 return rc;
637}
638
Harald Welte421cba42010-05-02 23:11:50 +0200639static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
640{
641 uint8_t v[6];
642
643 v[0] = PDP_TYPE_ORG_IETF;
644 v[1] = PDP_TYPE_N_IETF_IPv4;
645 *(uint32_t *)(v+2) = htonl(ipaddr);
646
647 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
648}
649
650static void msgb_put_pdp_addr_ppp(struct msgb *msg)
651{
652 uint8_t v[2];
653
654 v[0] = PDP_TYPE_ORG_ETSI;
655 v[1] = PDP_TYPE_N_ETSI_PPP;
656
657 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
658}
659
Harald Welte9b455bf2010-03-14 15:45:01 +0800660/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +0200661int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +0800662{
Harald Welte9b455bf2010-03-14 15:45:01 +0800663 struct msgb *msg = gsm48_msgb_alloc();
664 struct gsm48_act_pdp_ctx_ack *act_ack;
665 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +0200666 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800667
668 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
669
Harald Welte6abf94e2010-05-18 10:35:06 +0200670 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800671
672 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
673 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
674 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +0200675
676 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +0200677 msgb_v_put(msg, pdp->sapi);
Harald Welte421cba42010-05-02 23:11:50 +0200678 /* copy QoS parameters from original request */
Harald Welte6abf94e2010-05-18 10:35:06 +0200679 msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
680 //msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte421cba42010-05-02 23:11:50 +0200681 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +0200682 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte421cba42010-05-02 23:11:50 +0200683 /* PDP address */
Harald Welte6abf94e2010-05-18 10:35:06 +0200684 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
685 pdp->lib->eua.l, pdp->lib->eua.v);
686 //msgb_put_pdp_addr_ipv4(msg, 0x01020304);
Harald Welte421cba42010-05-02 23:11:50 +0200687 /* Optional: Protocol configuration options */
Harald Welte6abf94e2010-05-18 10:35:06 +0200688 if (pdp->lib->pco_neg.l && pdp->lib->pco_neg.v)
689 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
690 pdp->lib->pco_neg.l, pdp->lib->pco_neg.v);
Harald Welte421cba42010-05-02 23:11:50 +0200691 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +0800692
Harald Welte8acd88f2010-05-18 10:57:45 +0200693 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800694}
695
Harald Welte6abf94e2010-05-18 10:35:06 +0200696/* Section 9.5.3: Activate PDP Context reject */
697int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
698 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
699{
700 struct msgb *msg = gsm48_msgb_alloc();
701 struct gsm48_act_pdp_ctx_ack *act_ack;
702 struct gsm48_hdr *gh;
703 uint8_t transaction_id = tid ^ 0x8; /* flip */
704
Harald Welte8acd88f2010-05-18 10:57:45 +0200705 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT REJ\n");
Harald Welte6abf94e2010-05-18 10:35:06 +0200706
707 mmctx2msgid(msg, mm);
708
709 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
710 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
711 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
712
713 msgb_v_put(msg, cause);
714 if (pco_len && pco_v)
715 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
716
Harald Welte8acd88f2010-05-18 10:57:45 +0200717 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +0200718}
719
Harald Welte9b455bf2010-03-14 15:45:01 +0800720/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welted193cb32010-05-17 22:58:03 +0200721static int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mmctx,
722 struct msgb *old_msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800723{
Harald Welte943c5bc2010-04-30 16:33:12 +0200724 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800725 struct msgb *msg = gsm48_msgb_alloc();
726 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200727 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800728
729 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
730
Harald Welte11d7c102010-05-02 11:54:55 +0200731 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800732
733 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
734 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
735 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
736
Harald Welte8acd88f2010-05-18 10:57:45 +0200737 return gsm48_gmm_sendmsg(msg, 0, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800738}
739
740/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +0200741static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
742 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800743{
Harald Welte943c5bc2010-04-30 16:33:12 +0200744 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800745 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200746 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte0c3eae02010-05-02 21:07:14 +0200747 uint8_t req_qos_len, req_pdpa_len;
748 uint8_t *req_qos, *req_pdpa;
749 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +0200750 uint8_t transaction_id = (gh->proto_discr >> 4);
751 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800752
Harald Welte6abf94e2010-05-18 10:35:06 +0200753 DEBUGP(DMM, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
754 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +0200755
756 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
757
Harald Welte0c3eae02010-05-02 21:07:14 +0200758 req_qos_len = act_req->data[0];
759 req_qos = act_req->data + 1; /* 10.5.6.5 */
760 req_pdpa_len = act_req->data[1 + req_qos_len];
761 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800762
Harald Welte0c3eae02010-05-02 21:07:14 +0200763 switch (req_pdpa[0] & 0xf) {
764 case 0x0:
765 DEBUGPC(DMM, "ETSI ");
766 break;
767 case 0x1:
768 DEBUGPC(DMM, "IETF ");
769 break;
770 case 0xf:
771 DEBUGPC(DMM, "Empty ");
772 break;
773 }
774
775 switch (req_pdpa[1]) {
776 case 0x21:
777 DEBUGPC(DMM, "IPv4 ");
778 if (req_pdpa_len >= 6) {
779 struct in_addr ia;
780 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
781 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
782 }
783 break;
784 case 0x57:
785 DEBUGPC(DMM, "IPv6 ");
786 if (req_pdpa_len >= 18) {
787 /* FIXME: print IPv6 address */
788 }
789 break;
790 default:
791 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
792 break;
793 }
794
Harald Welte2720e732010-05-17 00:44:57 +0200795 /* put the non-TLV elements in the TLV parser structure to
796 * pass them on to the SGSN / GTP code */
797 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
798 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
799 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
800 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
801
Harald Welte6abf94e2010-05-18 10:35:06 +0200802 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +0200803 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +0200804
805 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
806 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
807 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
808 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
809 GSM_CAUSE_INV_MAND_INFO,
810 0, NULL);
811 }
812
813 /* Check if NSAPI is already in use */
814 if (sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi)) {
815 /* FIXME: send reject with GSM_CAUSE_NSAPI_IN_USE */
816 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
817 GSM_CAUSE_NSAPI_IN_USE,
818 0, NULL);
819 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800820
Harald Welted193cb32010-05-17 22:58:03 +0200821#if 1
822 {
823 struct ggsn_ctx ggsn;
824 ggsn.gtp_version = 1;
825 inet_aton("192.168.100.239", &ggsn.remote_addr);
826 ggsn.gsn = sgsn->gsn;
Harald Welte6abf94e2010-05-18 10:35:06 +0200827 pdp = sgsn_create_pdp_ctx(&ggsn, mmctx, act_req->req_nsapi, &tp);
828 if (!pdp)
829 return -1;
830 pdp->sapi = act_req->req_llc_sapi;
831 pdp->ti = transaction_id;
832
Harald Welted193cb32010-05-17 22:58:03 +0200833 }
Harald Welte2720e732010-05-17 00:44:57 +0200834#else
Harald Welte6abf94e2010-05-18 10:35:06 +0200835 return gsm48_tx_gsm_act_pdp_acc(mmctx, transaction_id, act_req);
Harald Welte2720e732010-05-17 00:44:57 +0200836#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800837}
838
839/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +0200840static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800841{
Harald Welte943c5bc2010-04-30 16:33:12 +0200842 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800843
Harald Welte2720e732010-05-17 00:44:57 +0200844 DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800845 get_value_string(gsm_cause_names, gh->data[0]));
846
Harald Welted193cb32010-05-17 22:58:03 +0200847 return gsm48_tx_gsm_deact_pdp_acc(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800848}
849
Harald Welted193cb32010-05-17 22:58:03 +0200850static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800851{
852 struct gsm48_hdr *gh = msgb_l3(msg);
853
Harald Welte2720e732010-05-17 00:44:57 +0200854 DEBUGP(DMM, "-> GPRS SM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800855 get_value_string(gsm_cause_names, gh->data[0]));
856
857 return 0;
858}
859
860/* GPRS Session Management */
Harald Welted193cb32010-05-17 22:58:03 +0200861static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800862{
Harald Welte943c5bc2010-04-30 16:33:12 +0200863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800864 int rc;
865
Harald Welted193cb32010-05-17 22:58:03 +0200866 if (!mmctx) {
867 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
868 return -EINVAL;
869 }
870
Harald Welte9b455bf2010-03-14 15:45:01 +0800871 switch (gh->msg_type) {
872 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200873 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800874 break;
875 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200876 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800877 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200878 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800879 break;
880 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
881 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
882 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
883 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
884 gh->msg_type);
885 break;
886 default:
887 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
888 gh->msg_type);
889 break;
890
891 }
892
893 return rc;
894}
895
896/* Main entry point for incoming 04.08 GPRS messages */
897int gsm0408_gprs_rcvmsg(struct msgb *msg)
898{
Harald Welte943c5bc2010-04-30 16:33:12 +0200899 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200900 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +0200901 struct sgsn_mm_ctx *mmctx;
902 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800903 int rc = -EINVAL;
904
Harald Welted193cb32010-05-17 22:58:03 +0200905 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
906 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +0200907 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +0200908 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +0200909 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
910 }
Harald Welted193cb32010-05-17 22:58:03 +0200911
912 /* MMCTX can be NULL */
913
Harald Welte9b455bf2010-03-14 15:45:01 +0800914 switch (pdisc) {
915 case GSM48_PDISC_MM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200916 rc = gsm0408_rcv_gmm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800917 break;
918 case GSM48_PDISC_SM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200919 rc = gsm0408_rcv_gsm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800920 break;
921 default:
922 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
923 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +0200924 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +0800925 break;
926 }
927
928 return rc;
929}