blob: 35c850c308feb9b41164958865746a872a3994db [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 Weltecd4dd4d2010-05-18 17:20:49 +020057extern struct sgsn_ggsn_ctx *dummy_ggsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080058
Harald Welte2720e732010-05-17 00:44:57 +020059/* Protocol related stuff, should go into libosmocore */
60
Harald Welte9b455bf2010-03-14 15:45:01 +080061/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
Harald Welte2720e732010-05-17 00:44:57 +020062const struct value_string gmm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080063 /* FIXME */
64 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
65 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
66 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
67 "Message type non-existant or not implemented" },
68 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
69 "Message type not compatible with protocol state" },
70 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
71 "Information element non-existent or not implemented" },
72 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
73 { GMM_CAUSE_MSG_INCOMP_P_STATE,
74 "Message not compatible with protocol state " },
75 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
76 { 0, NULL }
77};
78
79/* 10.5.6.6 SM Cause / Table 10.5.157 */
Harald Welte2720e732010-05-17 00:44:57 +020080const struct value_string gsm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +080081 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
82 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
83 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
84 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
85 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
86 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
87 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
88 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
89 "Requested service option not subscribed" },
90 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
91 "Service option temporarily out of order" },
92 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
93 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
94 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
95 { GSM_CAUSE_NET_FAIL, "Network Failure" },
96 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
97 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
98 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
99 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
100 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
101 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
102 "Message type non-existant or not implemented" },
103 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
104 "Message type not compatible with protocol state" },
105 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
106 "Information element non-existent or not implemented" },
107 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
108 { GSM_CAUSE_MSG_INCOMP_P_STATE,
109 "Message not compatible with protocol state " },
110 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
111 { 0, NULL }
112};
113
Harald Welte2720e732010-05-17 00:44:57 +0200114/* 10.5.5.2 */
115const struct value_string gprs_att_t_strs[] = {
116 { GPRS_ATT_T_ATTACH, "GPRS attach" },
117 { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" },
118 { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" },
119 { 0, NULL }
120};
Harald Welte9b455bf2010-03-14 15:45:01 +0800121
Harald Welte2720e732010-05-17 00:44:57 +0200122const struct value_string gprs_upd_t_strs[] = {
123 { GPRS_UPD_T_RA, "RA updating" },
124 { GPRS_UPD_T_RA_LA, "combined RA/LA updating" },
125 { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" },
126 { GPRS_UPD_T_PERIODIC, "periodic updating" },
127 { 0, NULL }
128};
129
130/* 10.5.5.5 */
131const struct value_string gprs_det_t_mo_strs[] = {
132 { GPRS_DET_T_MO_GPRS, "GPRS detach" },
133 { GPRS_DET_T_MO_IMSI, "IMSI detach" },
134 { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" },
135 { 0, NULL }
136};
137
138/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800139
Harald Welte9b455bf2010-03-14 15:45:01 +0800140/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200141static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
142 const struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800143{
Harald Welte8acd88f2010-05-18 10:57:45 +0200144 if (mm)
145 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
146
Harald Welte11d7c102010-05-02 11:54:55 +0200147 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800148 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
149}
150
Harald Welte11d7c102010-05-02 11:54:55 +0200151/* copy identifiers from old message to new message, this
152 * is required so lower layers can route it correctly */
153static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
154{
155 msgb_tlli(msg) = msgb_tlli(old);
156 msgb_bvci(msg) = msgb_bvci(old);
157 msgb_nsei(msg) = msgb_nsei(old);
158}
159
Harald Welte6abf94e2010-05-18 10:35:06 +0200160/* Store BVCI/NSEI in MM context */
161static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
162{
163 mm->bvci = msgb_bvci(msg);
164 mm->nsei = msgb_nsei(msg);
165}
166
167/* Store BVCI/NSEI in MM context */
168static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
169{
170 msgb_tlli(msg) = mm->tlli;
171 msgb_bvci(msg) = mm->bvci;
172 msgb_nsei(msg) = mm->nsei;
173}
174
Harald Welte86fda902010-05-03 10:54:35 +0200175static struct gsm48_qos default_qos = {
176 .delay_class = 4, /* best effort */
177 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
178 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
179 .preced_class = GSM48_QOS_PC_NORMAL,
180 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
181 .traf_class = GSM48_QOS_TC_INTERACTIVE,
182 .deliv_order = GSM48_QOS_DO_UNORDERED,
183 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
184 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
185 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
186 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
187 .resid_ber = GSM48_QOS_RBER_5e_2,
188 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
189 .handling_prio = 3,
190 .xfer_delay = 0x10, /* 200ms */
191 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
192 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
193 .sig_ind = 0, /* not optimised for signalling */
194 .max_bitrate_down_ext = 0, /* use octet 9 */
195 .guar_bitrate_down_ext = 0, /* use octet 13 */
196};
197
Harald Welte9b455bf2010-03-14 15:45:01 +0800198/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200199static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800200{
201 struct msgb *msg = gsm48_msgb_alloc();
202 struct gsm48_hdr *gh;
203 struct gsm48_attach_ack *aa;
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200204 uint8_t *ptsig, *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800205
206 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
207
Harald Welte6463c072010-05-18 17:04:55 +0200208 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800209
210 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
211 gh->proto_discr = GSM48_PDISC_MM_GPRS;
212 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
213
214 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
215 aa->force_stby = 0; /* not indicated */
216 aa->att_result = 1; /* GPRS only */
217 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
218 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200219 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800220
Harald Welte6463c072010-05-18 17:04:55 +0200221 /* Optional: P-TMSI signature */
222 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
223 ptsig = msgb_put(msg, 3);
224 ptsig[0] = mm->p_tmsi_sig >> 16;
225 ptsig[1] = mm->p_tmsi_sig >> 8;
226 ptsig[2] = mm->p_tmsi_sig & 0xff;
227
228 /* Optional: Negotiated Ready timer value */
229
230 /* Optional: Allocated P-TMSI */
231 msgb_v_put(msg, GSM48_IE_GMM_ALLOC_PTMSI);
232 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
233 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
234
235 /* Optional: MS-identity (combined attach) */
236 /* Optional: GMM cause (partial attach result for combined attach) */
237
Harald Welte8acd88f2010-05-18 10:57:45 +0200238 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800239}
240
241/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200242static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800243{
Harald Welte9b455bf2010-03-14 15:45:01 +0800244 struct gsm48_hdr *gh;
245
246 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
247
Harald Welte9b455bf2010-03-14 15:45:01 +0800248 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
249 gh->proto_discr = GSM48_PDISC_MM_GPRS;
250 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
251 gh->data[0] = gmm_cause;
252
Harald Welte8acd88f2010-05-18 10:57:45 +0200253 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800254}
Harald Welte6463c072010-05-18 17:04:55 +0200255static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
256 uint8_t gmm_cause)
257{
258 struct msgb *msg = gsm48_msgb_alloc();
259 gmm_copy_id(msg, old_msg);
260 return _tx_gmm_att_rej(msg, gmm_cause);
261}
262static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
263 uint8_t gmm_cause)
264{
265 struct msgb *msg = gsm48_msgb_alloc();
266 mmctx2msgid(msg, mm);
267 return _tx_gmm_att_rej(msg, gmm_cause);
268}
Harald Welte9b455bf2010-03-14 15:45:01 +0800269
Harald Welte2720e732010-05-17 00:44:57 +0200270/* Chapter 9.4.6.2 Detach accept */
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200271static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
Harald Welte2720e732010-05-17 00:44:57 +0200272{
273 struct msgb *msg = gsm48_msgb_alloc();
274 struct gsm48_hdr *gh;
275
276 DEBUGP(DMM, "<- GPRS DETACH ACCEPT\n");
277
Harald Welte6463c072010-05-18 17:04:55 +0200278 mmctx2msgid(msg, mm);
279
Harald Welte2720e732010-05-17 00:44:57 +0200280 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
281 gh->proto_discr = GSM48_PDISC_MM_GPRS;
282 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
283 gh->data[0] = force_stby;
284
Harald Welte8acd88f2010-05-18 10:57:45 +0200285 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte2720e732010-05-17 00:44:57 +0200286}
287
Harald Welte9b455bf2010-03-14 15:45:01 +0800288/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200289static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800290{
291 struct msgb *msg = gsm48_msgb_alloc();
292 struct gsm48_hdr *gh;
293
294 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
295
Harald Welte6463c072010-05-18 17:04:55 +0200296 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800297
298 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
299 gh->proto_discr = GSM48_PDISC_MM_GPRS;
300 gh->msg_type = GSM48_MT_GMM_ID_REQ;
301 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
302 gh->data[0] = id_type & 0xf;
303
Harald Welte8acd88f2010-05-18 10:57:45 +0200304 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800305}
306
307/* Check if we can already authorize a subscriber */
Harald Welte6463c072010-05-18 17:04:55 +0200308static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800309{
310 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
311 ctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Welte6463c072010-05-18 17:04:55 +0200312 return gsm48_tx_gmm_att_ack(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800313 }
314 if (!strlen(ctx->imei))
Harald Welte6463c072010-05-18 17:04:55 +0200315 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800316
317 if (!strlen(ctx->imsi))
Harald Welte6463c072010-05-18 17:04:55 +0200318 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800319
320 return 0;
321}
322
323/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200324static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800325{
Harald Welte943c5bc2010-04-30 16:33:12 +0200326 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200327 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800328 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800329
330 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte2720e732010-05-17 00:44:57 +0200331 DEBUGP(DMM, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
Harald Welte9b455bf2010-03-14 15:45:01 +0800332 mi_type, mi_string);
333
Harald Welte9b455bf2010-03-14 15:45:01 +0800334 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200335 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800336 return -EINVAL;
337 }
338
339 switch (mi_type) {
340 case GSM_MI_TYPE_IMSI:
341 /* we already have a mm context with current TLLI, but no
342 * P-TMSI / IMSI yet. What we now need to do is to fill
343 * this initial context with data from the HLR */
344 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
345 break;
346 case GSM_MI_TYPE_IMEI:
347 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
348 break;
349 case GSM_MI_TYPE_IMEISV:
350 break;
351 }
352
353 DEBUGPC(DMM, "\n");
354 /* Check if we can let the mobile station enter */
Harald Welte6463c072010-05-18 17:04:55 +0200355 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800356}
357
358static void attach_rej_cb(void *data)
359{
360 struct sgsn_mm_ctx *ctx = data;
361
Harald Welte6463c072010-05-18 17:04:55 +0200362 gsm48_tx_gmm_att_rej(ctx, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800363 ctx->mm_state = GMM_DEREGISTERED;
364 /* FIXME: release the context */
365}
366
367static void schedule_reject(struct sgsn_mm_ctx *ctx)
368{
369 ctx->T = 3370;
370 ctx->timer.cb = attach_rej_cb;
371 ctx->timer.data = ctx;
372 bsc_schedule_timer(&ctx->timer, 6, 0);
373}
374
375/* Section 9.4.1 Attach request */
Harald Welted193cb32010-05-17 22:58:03 +0200376static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800377{
Harald Welte943c5bc2010-04-30 16:33:12 +0200378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200379 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
380 uint8_t msnc_len, att_type, mi_len, mi_type;
381 uint16_t drx_par;
382 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800383 char mi_string[GSM48_MI_SIZE];
384 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200385 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800386 struct sgsn_mm_ctx *ctx;
387
Harald Welte2720e732010-05-17 00:44:57 +0200388 DEBUGP(DMM, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800389
390 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
391 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
392 * or with random TLLI. */
393
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200394 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800395
396 /* MS network capability 10.5.5.12 */
397 msnc_len = *cur++;
398 msnc = cur;
399 if (msnc_len > 2)
400 goto err_inval;
401 cur += msnc_len;
402
403 /* aTTACH Type 10.5.5.2 */
404 att_type = *cur++ & 0x0f;
405
406 /* DRX parameter 10.5.5.6 */
407 drx_par = *cur++;
408 drx_par |= *cur++ << 8;
409
410 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
411 mi_len = *cur++;
412 mi = cur;
413 if (mi_len > 8)
414 goto err_inval;
415 mi_type = *mi & GSM_MI_TYPE_MASK;
416 cur += mi_len;
417
418 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
419
Harald Welte2720e732010-05-17 00:44:57 +0200420 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
421 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800422
423 /* Old routing area identification 10.5.5.15 */
424 old_ra_info = cur;
425 cur += 6;
426
427 /* MS Radio Access Capability 10.5.5.12a */
428
429 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
430
431 switch (mi_type) {
432 case GSM_MI_TYPE_IMSI:
433 /* Try to find MM context based on IMSI */
434 ctx = sgsn_mm_ctx_by_imsi(mi_string);
435 if (!ctx) {
436#if 0
437 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
438#else
439 /* As a temorary hack, we simply assume that the IMSI exists */
440 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
441 if (!ctx)
Harald Welte6463c072010-05-18 17:04:55 +0200442 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_NET_FAIL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800443 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
444#endif
445 }
446 /* FIXME: Start some timer */
447 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200448 ctx->tlli = msgb_tlli(msg);
Harald Welte68b99a42010-05-18 12:05:42 +0200449 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800450 break;
451 case GSM_MI_TYPE_TMSI:
452 tmsi = strtoul(mi_string, NULL, 10);
453 /* Try to find MM context based on P-TMSI */
454 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
455 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200456 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800457 /* FIXME: Start some timer */
458 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200459 ctx->tlli = msgb_tlli(msg);
Harald Welte68b99a42010-05-18 12:05:42 +0200460 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800461 }
Harald Welte6463c072010-05-18 17:04:55 +0200462 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800463 break;
464 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200465 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800466 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200467 /* Update MM Context with currient RA and Cell ID */
468 ctx->ra = ra_id;
469 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800470
Harald Welte6463c072010-05-18 17:04:55 +0200471 /* Allocate a new P-TMSI (+ P-TMSI signature) */
472 ctx->p_tmsi = sgsn_alloc_ptmsi();
473
Harald Welte9b455bf2010-03-14 15:45:01 +0800474 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
475
476 DEBUGPC(DMM, "\n");
477
Harald Welte6463c072010-05-18 17:04:55 +0200478 return ctx ? gsm48_gmm_authorize(ctx) : 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800479
480err_inval:
481 DEBUGPC(DMM, "\n");
Harald Welte6463c072010-05-18 17:04:55 +0200482 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_SEM_INCORR_MSG);
Harald Welte9b455bf2010-03-14 15:45:01 +0800483}
484
Harald Welte2720e732010-05-17 00:44:57 +0200485/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200486static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200487{
488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +0200489 uint8_t detach_type, power_off;
Harald Welte2720e732010-05-17 00:44:57 +0200490
491 detach_type = gh->data[0] & 0x7;
492 power_off = gh->data[0] & 0x8;
493
494 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
495
496 DEBUGP(DMM, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
497 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
498 power_off ? "Power-off" : "");
499
500 /* Mark MM state as deregistered */
501 ctx->mm_state = GMM_DEREGISTERED;
502
503 /* force_stby = 0 */
Harald Welte6463c072010-05-18 17:04:55 +0200504 return gsm48_tx_gmm_det_ack(ctx, 0);
Harald Welte2720e732010-05-17 00:44:57 +0200505}
506
Harald Welte9b455bf2010-03-14 15:45:01 +0800507/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +0200508static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800509{
510 struct msgb *msg = gsm48_msgb_alloc();
511 struct gsm48_hdr *gh;
512 struct gsm48_ra_upd_ack *rua;
513
514 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
515
Harald Welte6463c072010-05-18 17:04:55 +0200516 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800517
518 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
519 gh->proto_discr = GSM48_PDISC_MM_GPRS;
520 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
521
522 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
523 rua->force_stby = 0; /* not indicated */
524 rua->upd_result = 0; /* RA updated */
525 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200526
Harald Welte6463c072010-05-18 17:04:55 +0200527 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800528
529 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200530 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800531}
532
533/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200534static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800535{
536 struct msgb *msg = gsm48_msgb_alloc();
537 struct gsm48_hdr *gh;
538
539 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
540
Harald Welte11d7c102010-05-02 11:54:55 +0200541 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800542
543 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
544 gh->proto_discr = GSM48_PDISC_MM_GPRS;
545 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
546 gh->data[0] = cause;
547 gh->data[1] = 0; /* ? */
548
549 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200550 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800551}
552
553/* Chapter 9.4.14: Routing area update request */
Harald Welted193cb32010-05-17 22:58:03 +0200554static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800555{
Harald Welte943c5bc2010-04-30 16:33:12 +0200556 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200557 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800558 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200559 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800560
561 /* Update Type 10.5.5.18 */
562 upd_type = *cur++ & 0x0f;
563
Harald Welte2720e732010-05-17 00:44:57 +0200564 DEBUGP(DMM, "-> GMM RA UPDATE REQUEST type=\"%s\" ",
565 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800566
567 /* Old routing area identification 10.5.5.15 */
568 gsm48_parse_ra(&old_ra_id, cur);
569 cur += 6;
570
571 /* MS Radio Access Capability 10.5.5.12a */
572
573 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
574 * DRX parameter, MS network capability */
575
576 switch (upd_type) {
577 case GPRS_UPD_T_RA_LA:
578 case GPRS_UPD_T_RA_LA_IMSI_ATT:
579 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
580 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
581 break;
582 case GPRS_UPD_T_RA:
583 case GPRS_UPD_T_PERIODIC:
584 break;
585 }
586
587 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800588 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
589 /* The MS has to perform GPRS attach */
590 DEBUGPC(DMM, " REJECT\n");
591 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
592 }
593
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200594 /* Update the MM context with the new RA-ID */
595 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
596 /* Update the MM context with the new TLLI */
597 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800598 /* FIXME: Update the MM context with the MS radio acc capabilities */
599 /* FIXME: Update the MM context with the MS network capabilities */
600
Harald Welte77289c22010-05-18 14:32:29 +0200601 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
602
Harald Welte9b455bf2010-03-14 15:45:01 +0800603 DEBUGPC(DMM, " ACCEPT\n");
Harald Welte6463c072010-05-18 17:04:55 +0200604 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800605}
606
Harald Welted193cb32010-05-17 22:58:03 +0200607static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800608{
609 struct gsm48_hdr *gh = msgb_l3(msg);
610
Harald Welte2720e732010-05-17 00:44:57 +0200611 DEBUGP(DMM, "-> GPRS MM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800612 get_value_string(gmm_cause_names, gh->data[0]));
613
614 return 0;
615}
616
617/* GPRS Mobility Management */
Harald Welted193cb32010-05-17 22:58:03 +0200618static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800619{
Harald Welte943c5bc2010-04-30 16:33:12 +0200620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800621 int rc;
622
Harald Welted193cb32010-05-17 22:58:03 +0200623 if (!mmctx &&
624 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
625 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
626 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Harald Welte77289c22010-05-18 14:32:29 +0200627 /* FIXME: Send GMM_CAUSE_IMPL_DETACHED */
Harald Welted193cb32010-05-17 22:58:03 +0200628 return -EINVAL;
629 }
630
631
Harald Welte9b455bf2010-03-14 15:45:01 +0800632 switch (gh->msg_type) {
633 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200634 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800635 break;
636 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200637 rc = gsm48_rx_gmm_att_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800638 break;
639 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +0200640 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800641 break;
642 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200643 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800644 break;
Harald Welte2720e732010-05-17 00:44:57 +0200645 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200646 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +0200647 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800648 case GSM48_MT_GMM_RA_UPD_COMPL:
649 /* only in case SGSN offered new P-TMSI */
650 case GSM48_MT_GMM_ATTACH_COMPL:
651 /* only in case SGSN offered new P-TMSI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800652 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
653 case GSM48_MT_GMM_AUTH_CIPH_RESP:
654 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
655 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200656 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800657 break;
658 default:
659 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
660 gh->msg_type);
Harald Welte2720e732010-05-17 00:44:57 +0200661 /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */
Harald Welte9b455bf2010-03-14 15:45:01 +0800662 break;
663 }
664
665 return rc;
666}
667
Harald Welte421cba42010-05-02 23:11:50 +0200668static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
669{
670 uint8_t v[6];
671
672 v[0] = PDP_TYPE_ORG_IETF;
673 v[1] = PDP_TYPE_N_IETF_IPv4;
674 *(uint32_t *)(v+2) = htonl(ipaddr);
675
676 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
677}
678
679static void msgb_put_pdp_addr_ppp(struct msgb *msg)
680{
681 uint8_t v[2];
682
683 v[0] = PDP_TYPE_ORG_ETSI;
684 v[1] = PDP_TYPE_N_ETSI_PPP;
685
686 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
687}
688
Harald Welte9b455bf2010-03-14 15:45:01 +0800689/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +0200690int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +0800691{
Harald Welte9b455bf2010-03-14 15:45:01 +0800692 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +0800693 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +0200694 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800695
696 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
697
Harald Welte6abf94e2010-05-18 10:35:06 +0200698 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800699
700 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
701 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
702 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +0200703
704 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +0200705 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +0200706
Harald Welte55e0df72010-05-18 13:20:08 +0200707 /* FIXME: copy QoS parameters from original request */
708 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
709 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +0200710
Harald Welte421cba42010-05-02 23:11:50 +0200711 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +0200712 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +0200713
Harald Welte421cba42010-05-02 23:11:50 +0200714 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +0200715 /* Highest 4 bits of first byte need to be set to 1, otherwise
716 * the IE is identical with the 04.08 PDP Address IE */
717 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +0200718 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
719 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +0200720 pdp->lib->eua.v[0] |= 0xf0;
721
Harald Welte77289c22010-05-18 14:32:29 +0200722 /* Optional: Protocol configuration options (FIXME: why 'req') */
723 if (pdp->lib->pco_req.l && pdp->lib->pco_req.v)
Harald Welte6abf94e2010-05-18 10:35:06 +0200724 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +0200725 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +0200726
Harald Welte421cba42010-05-02 23:11:50 +0200727 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +0800728
Harald Welte8acd88f2010-05-18 10:57:45 +0200729 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800730}
731
Harald Welte6abf94e2010-05-18 10:35:06 +0200732/* Section 9.5.3: Activate PDP Context reject */
733int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
734 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
735{
736 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +0200737 struct gsm48_hdr *gh;
738 uint8_t transaction_id = tid ^ 0x8; /* flip */
739
Harald Welte77289c22010-05-18 14:32:29 +0200740 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +0200741
742 mmctx2msgid(msg, mm);
743
744 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
745 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
746 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
747
748 msgb_v_put(msg, cause);
749 if (pco_len && pco_v)
750 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
751
Harald Welte8acd88f2010-05-18 10:57:45 +0200752 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +0200753}
754
Harald Welte9b455bf2010-03-14 15:45:01 +0800755/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte77289c22010-05-18 14:32:29 +0200756int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +0800757{
Harald Welte9b455bf2010-03-14 15:45:01 +0800758 struct msgb *msg = gsm48_msgb_alloc();
759 struct gsm48_hdr *gh;
Harald Welte77289c22010-05-18 14:32:29 +0200760 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800761
762 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
763
Harald Welte77289c22010-05-18 14:32:29 +0200764 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800765
766 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
767 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
768 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
769
Harald Welte77289c22010-05-18 14:32:29 +0200770 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800771}
772
773/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +0200774static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
775 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800776{
Harald Welte943c5bc2010-04-30 16:33:12 +0200777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800778 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +0200779 uint8_t req_qos_len, req_pdpa_len;
780 uint8_t *req_qos, *req_pdpa;
781 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +0200782 uint8_t transaction_id = (gh->proto_discr >> 4);
783 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800784
Harald Welte6abf94e2010-05-18 10:35:06 +0200785 DEBUGP(DMM, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
786 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +0200787
788 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
789
Harald Welte0c3eae02010-05-02 21:07:14 +0200790 req_qos_len = act_req->data[0];
791 req_qos = act_req->data + 1; /* 10.5.6.5 */
792 req_pdpa_len = act_req->data[1 + req_qos_len];
793 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800794
Harald Welte0c3eae02010-05-02 21:07:14 +0200795 switch (req_pdpa[0] & 0xf) {
796 case 0x0:
797 DEBUGPC(DMM, "ETSI ");
798 break;
799 case 0x1:
800 DEBUGPC(DMM, "IETF ");
801 break;
802 case 0xf:
803 DEBUGPC(DMM, "Empty ");
804 break;
805 }
806
807 switch (req_pdpa[1]) {
808 case 0x21:
809 DEBUGPC(DMM, "IPv4 ");
810 if (req_pdpa_len >= 6) {
811 struct in_addr ia;
812 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
813 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
814 }
815 break;
816 case 0x57:
817 DEBUGPC(DMM, "IPv6 ");
818 if (req_pdpa_len >= 18) {
819 /* FIXME: print IPv6 address */
820 }
821 break;
822 default:
823 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
824 break;
825 }
826
Harald Welte77289c22010-05-18 14:32:29 +0200827 DEBUGPC(DMM, "\n");
828
Harald Welte2720e732010-05-17 00:44:57 +0200829 /* put the non-TLV elements in the TLV parser structure to
830 * pass them on to the SGSN / GTP code */
831 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
832 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
833 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
834 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
835
Harald Welte6abf94e2010-05-18 10:35:06 +0200836 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +0200837 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +0200838
839 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
840 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
841 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
842 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
843 GSM_CAUSE_INV_MAND_INFO,
844 0, NULL);
845 }
846
847 /* Check if NSAPI is already in use */
848 if (sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi)) {
849 /* FIXME: send reject with GSM_CAUSE_NSAPI_IN_USE */
850 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
851 GSM_CAUSE_NSAPI_IN_USE,
852 0, NULL);
853 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800854
Harald Welted193cb32010-05-17 22:58:03 +0200855#if 1
856 {
Harald Welte77289c22010-05-18 14:32:29 +0200857 pdp = sgsn_create_pdp_ctx(dummy_ggsn, mmctx, act_req->req_nsapi, &tp);
Harald Welte6abf94e2010-05-18 10:35:06 +0200858 if (!pdp)
859 return -1;
860 pdp->sapi = act_req->req_llc_sapi;
861 pdp->ti = transaction_id;
862
Harald Welted193cb32010-05-17 22:58:03 +0200863 }
Harald Welte6463c072010-05-18 17:04:55 +0200864 return 0;
Harald Welte2720e732010-05-17 00:44:57 +0200865#else
Harald Welte6abf94e2010-05-18 10:35:06 +0200866 return gsm48_tx_gsm_act_pdp_acc(mmctx, transaction_id, act_req);
Harald Welte2720e732010-05-17 00:44:57 +0200867#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800868}
869
870/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +0200871static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800872{
Harald Welte943c5bc2010-04-30 16:33:12 +0200873 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +0200874 uint8_t transaction_id = (gh->proto_discr >> 4);
875 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800876
Harald Welte2720e732010-05-17 00:44:57 +0200877 DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800878 get_value_string(gsm_cause_names, gh->data[0]));
879
Harald Welte77289c22010-05-18 14:32:29 +0200880 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
881 if (!pdp) {
882 LOGP(DMM, LOGL_NOTICE, "Deactivate PDP Context Request for "
883 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
884 mm->imsi, transaction_id);
885 return -EINVAL;
886 }
887
888 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +0800889}
890
Harald Welted193cb32010-05-17 22:58:03 +0200891static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800892{
893 struct gsm48_hdr *gh = msgb_l3(msg);
894
Harald Welte2720e732010-05-17 00:44:57 +0200895 DEBUGP(DMM, "-> GPRS SM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800896 get_value_string(gsm_cause_names, gh->data[0]));
897
898 return 0;
899}
900
901/* GPRS Session Management */
Harald Welted193cb32010-05-17 22:58:03 +0200902static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800903{
Harald Welte943c5bc2010-04-30 16:33:12 +0200904 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800905 int rc;
906
Harald Welted193cb32010-05-17 22:58:03 +0200907 if (!mmctx) {
908 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
909 return -EINVAL;
910 }
911
Harald Welte9b455bf2010-03-14 15:45:01 +0800912 switch (gh->msg_type) {
913 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200914 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800915 break;
916 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +0200917 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +0200918 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800919 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +0200920 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800921 break;
922 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
923 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
924 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
925 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
926 gh->msg_type);
927 break;
928 default:
929 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
930 gh->msg_type);
931 break;
932
933 }
934
935 return rc;
936}
937
938/* Main entry point for incoming 04.08 GPRS messages */
939int gsm0408_gprs_rcvmsg(struct msgb *msg)
940{
Harald Welte943c5bc2010-04-30 16:33:12 +0200941 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200942 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +0200943 struct sgsn_mm_ctx *mmctx;
944 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800945 int rc = -EINVAL;
946
Harald Welted193cb32010-05-17 22:58:03 +0200947 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
948 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +0200949 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +0200950 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +0200951 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
952 }
Harald Welted193cb32010-05-17 22:58:03 +0200953
954 /* MMCTX can be NULL */
955
Harald Welte9b455bf2010-03-14 15:45:01 +0800956 switch (pdisc) {
957 case GSM48_PDISC_MM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200958 rc = gsm0408_rcv_gmm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800959 break;
960 case GSM48_PDISC_SM_GPRS:
Harald Welted193cb32010-05-17 22:58:03 +0200961 rc = gsm0408_rcv_gsm(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800962 break;
963 default:
964 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
965 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +0200966 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +0800967 break;
968 }
969
970 return rc;
971}