blob: 7a33491926b727860e92d9ee6da8f67342057774 [file] [log] [blame]
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +02001/* Messages on the RANAP interface (Iu mode) */
2
3/* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2015 by Holger Hans Peter Freyther
5 * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "bscconfig.h"
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020025#include <gtp.h>
26
27#include <osmocom/core/rate_ctr.h>
Alexander Couzensafadd102019-10-08 14:30:59 +020028#include <osmocom/core/tdef.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020029
30#include <osmocom/ranap/ranap_common.h>
31
32#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +020033#include <osmocom/sgsn/gprs_sm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020034#include <osmocom/sgsn/debug.h>
35#include <osmocom/sgsn/sgsn.h>
36#include <osmocom/sgsn/gprs_ranap.h>
37#include <osmocom/sgsn/gprs_gmm_attach.h>
Pau Espin Pedrolccd12522019-08-30 17:06:36 +020038#include <osmocom/sgsn/gprs_mm_state_iu_fsm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020039
40/* Send RAB activation requests for all PDP contexts */
41void activate_pdp_rabs(struct sgsn_mm_ctx *ctx)
42{
43 struct sgsn_pdp_ctx *pdp;
44 if (ctx->ran_type != MM_CTX_T_UTRAN_Iu)
45 return;
46 llist_for_each_entry(pdp, &ctx->pdp_list, list) {
47 iu_rab_act_ps(pdp->nsapi, pdp);
48 }
49}
50
51/* Callback for RAB assignment response */
52static int sgsn_ranap_rab_ass_resp(struct sgsn_mm_ctx *ctx, RANAP_RAB_SetupOrModifiedItemIEs_t *setup_ies)
53{
54 uint8_t rab_id;
55 bool require_pdp_update = false;
56 struct sgsn_pdp_ctx *pdp = NULL;
57 RANAP_RAB_SetupOrModifiedItem_t *item = &setup_ies->raB_SetupOrModifiedItem;
58
59 rab_id = item->rAB_ID.buf[0];
60
61 pdp = sgsn_pdp_ctx_by_nsapi(ctx, rab_id);
62 if (!pdp) {
63 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Response for unknown RAB/NSAPI=%u\n", rab_id);
64 return -1;
65 }
66
67 if (item->transportLayerAddress) {
68 LOGPC(DRANAP, LOGL_INFO, " Setup: (%u/%s)", rab_id, osmo_hexdump(item->transportLayerAddress->buf,
69 item->transportLayerAddress->size));
70 switch (item->transportLayerAddress->size) {
71 case 7:
72 /* It must be IPv4 inside a X213 NSAP */
73 memcpy(pdp->lib->gsnlu.v, &item->transportLayerAddress->buf[3], 4);
74 break;
75 case 4:
76 /* It must be a raw IPv4 address */
77 memcpy(pdp->lib->gsnlu.v, item->transportLayerAddress->buf, 4);
78 break;
79 case 16:
80 /* TODO: It must be a raw IPv6 address */
81 case 19:
82 /* TODO: It must be IPv6 inside a X213 NSAP */
83 default:
84 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Resp: Unknown "
85 "transport layer address size %u\n",
86 item->transportLayerAddress->size);
87 return -1;
88 }
89 require_pdp_update = true;
90 }
91
92 /* The TEI on the RNC side might have changed, too */
93 if (item->iuTransportAssociation &&
94 item->iuTransportAssociation->present == RANAP_IuTransportAssociation_PR_gTP_TEI &&
95 item->iuTransportAssociation->choice.gTP_TEI.buf &&
96 item->iuTransportAssociation->choice.gTP_TEI.size >= 4) {
97 uint32_t tei = osmo_load32be(item->iuTransportAssociation->choice.gTP_TEI.buf);
98 LOGP(DRANAP, LOGL_DEBUG, "Updating TEID on RNC side from 0x%08x to 0x%08x\n",
99 pdp->lib->teid_own, tei);
100 pdp->lib->teid_own = tei;
101 require_pdp_update = true;
102 }
103
104 if (require_pdp_update)
105 gtp_update_context(pdp->ggsn->gsn, pdp->lib, pdp, &pdp->lib->hisaddr0);
106
107 if (pdp->state != PDP_STATE_CR_CONF) {
108 send_act_pdp_cont_acc(pdp);
109 pdp->state = PDP_STATE_CR_CONF;
110 }
111 return 0;
112
113}
114
115int sgsn_ranap_iu_event(struct ranap_ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data)
116{
117 struct sgsn_mm_ctx *mm;
118 int rc = -1;
119
120 mm = sgsn_mm_ctx_by_ue_ctx(ctx);
Alexander Couzens1cb4be92019-09-10 19:21:31 +0200121 if (!mm) {
122 LOGIUP(ctx, LOGL_NOTICE, "Cannot find mm ctx for IU event %d\n", type);
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200123 ranap_iu_free_ue(ctx);
Alexander Couzens1cb4be92019-09-10 19:21:31 +0200124 return rc;
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200125 }
126
127 switch (type) {
128 case RANAP_IU_EVENT_RAB_ASSIGN:
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200129 rc = sgsn_ranap_rab_ass_resp(mm, (RANAP_RAB_SetupOrModifiedItemIEs_t *)data);
130 break;
131 case RANAP_IU_EVENT_IU_RELEASE:
132 /* fall thru */
133 case RANAP_IU_EVENT_LINK_INVALIDATED:
134 /* Clean up ranap_ue_conn_ctx here */
Alexander Couzens1cb4be92019-09-10 19:21:31 +0200135 LOGMMCTXP(LOGL_INFO, mm, "IU release for imsi %s\n", mm->imsi);
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200136 if (mm->iu.mm_state_fsm->state == ST_PMM_CONNECTED)
137 osmo_fsm_inst_dispatch(mm->iu.mm_state_fsm, E_PMM_PS_CONN_RELEASE, NULL);
138 else
139 sgsn_ranap_iu_free(mm);
140
Alexander Couzens62f6f9a2019-09-11 02:44:06 +0200141 /* TODO: move this into FSM */
142 if (mm->ran_type == MM_CTX_T_UTRAN_Iu && mm->gmm_att_req.fsm->state != ST_INIT)
143 osmo_fsm_inst_dispatch(mm->gmm_att_req.fsm, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200144 rc = 0;
145 break;
146 case RANAP_IU_EVENT_SECURITY_MODE_COMPLETE:
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200147 /* Continue authentication here */
148 mm->iu.ue_ctx->integrity_active = 1;
Alexander Couzens10b3d702019-09-11 02:31:12 +0200149 ranap_iu_tx_common_id(mm->iu.ue_ctx, mm->imsi);
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200150
151 /* FIXME: remove gmm_authorize */
152 if (mm->pending_req != GSM48_MT_GMM_ATTACH_REQ)
153 gsm48_gmm_authorize(mm);
154 else
155 osmo_fsm_inst_dispatch(mm->gmm_att_req.fsm, E_IU_SECURITY_CMD_COMPLETE, NULL);
Alexander Couzens743e6872019-10-08 12:40:50 +0200156 rc = 0;
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200157 break;
158 default:
Alexander Couzens1cb4be92019-09-10 19:21:31 +0200159 LOGMMCTXP(LOGL_NOTICE, mm, "Unknown event received: %i\n", type);
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200160 rc = -1;
161 break;
162 }
163 return rc;
164}
165
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200166void sgsn_ranap_iu_free(struct sgsn_mm_ctx *ctx)
167{
168 if (!ctx)
169 return;
170
171 if (!ctx->iu.ue_ctx)
172 return;
173
174 ranap_iu_free_ue(ctx->iu.ue_ctx);
175 ctx->iu.ue_ctx = NULL;
176}
177
178void sgsn_ranap_iu_release_free(struct sgsn_mm_ctx *ctx,
179 const struct RANAP_Cause *cause)
180{
Alexander Couzensafadd102019-10-08 14:30:59 +0200181 unsigned long X1001;
182
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200183 if (!ctx)
184 return;
185
186 if (!ctx->iu.ue_ctx)
187 return;
188
Alexander Couzensafadd102019-10-08 14:30:59 +0200189 X1001 = osmo_tdef_get(sgsn->cfg.T_defs, -1001, OSMO_TDEF_S, -1);
190
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200191 ranap_iu_tx_release_free(ctx->iu.ue_ctx,
192 cause,
Alexander Couzensafadd102019-10-08 14:30:59 +0200193 (int) X1001);
Alexander Couzenseb5aee52019-09-10 21:00:18 +0200194 ctx->iu.ue_ctx = NULL;
195}
196
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200197int iu_rab_act_ps(uint8_t rab_id, struct sgsn_pdp_ctx *pdp)
198{
199 struct msgb *msg;
200 struct sgsn_mm_ctx *mm = pdp->mm;
201 struct ranap_ue_conn_ctx *uectx;
202 uint32_t ggsn_ip;
203 bool use_x213_nsap;
204
205 uectx = mm->iu.ue_ctx;
206 use_x213_nsap = (uectx->rab_assign_addr_enc == RANAP_NSAP_ADDR_ENC_X213);
207
208 /* Get the IP address for ggsn user plane */
209 memcpy(&ggsn_ip, pdp->lib->gsnru.v, pdp->lib->gsnru.l);
210 ggsn_ip = htonl(ggsn_ip);
211
212 LOGP(DRANAP, LOGL_DEBUG, "Assigning RAB: rab_id=%d, ggsn_ip=%x,"
213 " teid_gn=%x, use_x213_nsap=%d\n",
214 rab_id, ggsn_ip, pdp->lib->teid_gn, use_x213_nsap);
215
216 msg = ranap_new_msg_rab_assign_data(rab_id, ggsn_ip,
217 pdp->lib->teid_gn, use_x213_nsap);
218 msg->l2h = msg->data;
219 return ranap_iu_rab_act(uectx, msg);
220}
221
222
223/* Main entry point for incoming 04.08 GPRS messages from Iu */
224int gsm0408_gprs_rcvmsg_iu(struct msgb *msg, struct gprs_ra_id *ra_id,
225 uint16_t *sai)
226{
227 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
228 uint8_t pdisc = gsm48_hdr_pdisc(gh);
229 struct sgsn_mm_ctx *mmctx;
230 int rc = -EINVAL;
231
232 mmctx = sgsn_mm_ctx_by_ue_ctx(MSG_IU_UE_CTX(msg));
233 if (mmctx) {
234 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
235 if (ra_id)
236 memcpy(&mmctx->ra, ra_id, sizeof(mmctx->ra));
237 }
238
239 /* MMCTX can be NULL */
240
241 switch (pdisc) {
242 case GSM48_PDISC_MM_GPRS:
243 rc = gsm0408_rcv_gmm(mmctx, msg, NULL, false);
244#pragma message "set drop_cipherable arg for gsm0408_rcv_gmm() from IuPS?"
245 break;
246 case GSM48_PDISC_SM_GPRS:
247 rc = gsm0408_rcv_gsm(mmctx, msg, NULL);
248 break;
249 default:
250 LOGMMCTXP(LOGL_NOTICE, mmctx,
251 "Unknown GSM 04.08 discriminator 0x%02x: %s\n",
252 pdisc, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
253 /* FIXME: return status message */
254 break;
255 }
256
257 /* MMCTX can be invalid */
258
259 return rc;
260}