blob: 027b6665a74954b21d1dcee6ceda49e6af103cf5 [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"
25
26#ifdef BUILD_IU
27
28#include <gtp.h>
29
30#include <osmocom/core/rate_ctr.h>
31
32#include <osmocom/ranap/ranap_common.h>
33
34#include <osmocom/sgsn/gprs_gmm.h>
35#include <osmocom/sgsn/debug.h>
36#include <osmocom/sgsn/sgsn.h>
37#include <osmocom/sgsn/gprs_ranap.h>
38#include <osmocom/sgsn/gprs_gmm_attach.h>
Pau Espin Pedrolccd12522019-08-30 17:06:36 +020039#include <osmocom/sgsn/gprs_mm_state_iu_fsm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020040
41/* Send RAB activation requests for all PDP contexts */
42void activate_pdp_rabs(struct sgsn_mm_ctx *ctx)
43{
44 struct sgsn_pdp_ctx *pdp;
45 if (ctx->ran_type != MM_CTX_T_UTRAN_Iu)
46 return;
47 llist_for_each_entry(pdp, &ctx->pdp_list, list) {
48 iu_rab_act_ps(pdp->nsapi, pdp);
49 }
50}
51
52/* Callback for RAB assignment response */
53static int sgsn_ranap_rab_ass_resp(struct sgsn_mm_ctx *ctx, RANAP_RAB_SetupOrModifiedItemIEs_t *setup_ies)
54{
55 uint8_t rab_id;
56 bool require_pdp_update = false;
57 struct sgsn_pdp_ctx *pdp = NULL;
58 RANAP_RAB_SetupOrModifiedItem_t *item = &setup_ies->raB_SetupOrModifiedItem;
59
60 rab_id = item->rAB_ID.buf[0];
61
62 pdp = sgsn_pdp_ctx_by_nsapi(ctx, rab_id);
63 if (!pdp) {
64 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Response for unknown RAB/NSAPI=%u\n", rab_id);
65 return -1;
66 }
67
68 if (item->transportLayerAddress) {
69 LOGPC(DRANAP, LOGL_INFO, " Setup: (%u/%s)", rab_id, osmo_hexdump(item->transportLayerAddress->buf,
70 item->transportLayerAddress->size));
71 switch (item->transportLayerAddress->size) {
72 case 7:
73 /* It must be IPv4 inside a X213 NSAP */
74 memcpy(pdp->lib->gsnlu.v, &item->transportLayerAddress->buf[3], 4);
75 break;
76 case 4:
77 /* It must be a raw IPv4 address */
78 memcpy(pdp->lib->gsnlu.v, item->transportLayerAddress->buf, 4);
79 break;
80 case 16:
81 /* TODO: It must be a raw IPv6 address */
82 case 19:
83 /* TODO: It must be IPv6 inside a X213 NSAP */
84 default:
85 LOGP(DRANAP, LOGL_ERROR, "RAB Assignment Resp: Unknown "
86 "transport layer address size %u\n",
87 item->transportLayerAddress->size);
88 return -1;
89 }
90 require_pdp_update = true;
91 }
92
93 /* The TEI on the RNC side might have changed, too */
94 if (item->iuTransportAssociation &&
95 item->iuTransportAssociation->present == RANAP_IuTransportAssociation_PR_gTP_TEI &&
96 item->iuTransportAssociation->choice.gTP_TEI.buf &&
97 item->iuTransportAssociation->choice.gTP_TEI.size >= 4) {
98 uint32_t tei = osmo_load32be(item->iuTransportAssociation->choice.gTP_TEI.buf);
99 LOGP(DRANAP, LOGL_DEBUG, "Updating TEID on RNC side from 0x%08x to 0x%08x\n",
100 pdp->lib->teid_own, tei);
101 pdp->lib->teid_own = tei;
102 require_pdp_update = true;
103 }
104
105 if (require_pdp_update)
106 gtp_update_context(pdp->ggsn->gsn, pdp->lib, pdp, &pdp->lib->hisaddr0);
107
108 if (pdp->state != PDP_STATE_CR_CONF) {
109 send_act_pdp_cont_acc(pdp);
110 pdp->state = PDP_STATE_CR_CONF;
111 }
112 return 0;
113
114}
115
116int sgsn_ranap_iu_event(struct ranap_ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data)
117{
118 struct sgsn_mm_ctx *mm;
119 int rc = -1;
120
121 mm = sgsn_mm_ctx_by_ue_ctx(ctx);
122
123#define REQUIRE_MM \
124 if (!mm) { \
125 LOGIUP(ctx, LOGL_NOTICE, "Cannot find mm ctx for IU event %d\n", type); \
126 return rc; \
127 }
128
129 switch (type) {
130 case RANAP_IU_EVENT_RAB_ASSIGN:
131 REQUIRE_MM
132 rc = sgsn_ranap_rab_ass_resp(mm, (RANAP_RAB_SetupOrModifiedItemIEs_t *)data);
133 break;
134 case RANAP_IU_EVENT_IU_RELEASE:
135 /* fall thru */
136 case RANAP_IU_EVENT_LINK_INVALIDATED:
137 /* Clean up ranap_ue_conn_ctx here */
Pau Espin Pedrolccd12522019-08-30 17:06:36 +0200138 if (mm) {
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200139 LOGMMCTXP(LOGL_INFO, mm, "IU release for imsi %s\n", mm->imsi);
Pau Espin Pedrolccd12522019-08-30 17:06:36 +0200140 osmo_fsm_inst_dispatch(mm->iu.mm_state_fsm, E_PMM_PS_CONN_RELEASE, NULL);
141 } else
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200142 LOGIUP(ctx, LOGL_INFO, "IU release\n");
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +0200143 rc = 0;
144 break;
145 case RANAP_IU_EVENT_SECURITY_MODE_COMPLETE:
146 REQUIRE_MM
147 /* Continue authentication here */
148 mm->iu.ue_ctx->integrity_active = 1;
149
150 /* FIXME: remove gmm_authorize */
151 if (mm->pending_req != GSM48_MT_GMM_ATTACH_REQ)
152 gsm48_gmm_authorize(mm);
153 else
154 osmo_fsm_inst_dispatch(mm->gmm_att_req.fsm, E_IU_SECURITY_CMD_COMPLETE, NULL);
155 break;
156 default:
157 if (mm)
158 LOGMMCTXP(LOGL_NOTICE, mm, "Unknown event received: %i\n", type);
159 else
160 LOGIUP(ctx, LOGL_NOTICE, "Unknown event received: %i\n", type);
161 rc = -1;
162 break;
163 }
164 return rc;
165}
166
167int iu_rab_act_ps(uint8_t rab_id, struct sgsn_pdp_ctx *pdp)
168{
169 struct msgb *msg;
170 struct sgsn_mm_ctx *mm = pdp->mm;
171 struct ranap_ue_conn_ctx *uectx;
172 uint32_t ggsn_ip;
173 bool use_x213_nsap;
174
175 uectx = mm->iu.ue_ctx;
176 use_x213_nsap = (uectx->rab_assign_addr_enc == RANAP_NSAP_ADDR_ENC_X213);
177
178 /* Get the IP address for ggsn user plane */
179 memcpy(&ggsn_ip, pdp->lib->gsnru.v, pdp->lib->gsnru.l);
180 ggsn_ip = htonl(ggsn_ip);
181
182 LOGP(DRANAP, LOGL_DEBUG, "Assigning RAB: rab_id=%d, ggsn_ip=%x,"
183 " teid_gn=%x, use_x213_nsap=%d\n",
184 rab_id, ggsn_ip, pdp->lib->teid_gn, use_x213_nsap);
185
186 msg = ranap_new_msg_rab_assign_data(rab_id, ggsn_ip,
187 pdp->lib->teid_gn, use_x213_nsap);
188 msg->l2h = msg->data;
189 return ranap_iu_rab_act(uectx, msg);
190}
191
192
193/* Main entry point for incoming 04.08 GPRS messages from Iu */
194int gsm0408_gprs_rcvmsg_iu(struct msgb *msg, struct gprs_ra_id *ra_id,
195 uint16_t *sai)
196{
197 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
198 uint8_t pdisc = gsm48_hdr_pdisc(gh);
199 struct sgsn_mm_ctx *mmctx;
200 int rc = -EINVAL;
201
202 mmctx = sgsn_mm_ctx_by_ue_ctx(MSG_IU_UE_CTX(msg));
203 if (mmctx) {
204 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
205 if (ra_id)
206 memcpy(&mmctx->ra, ra_id, sizeof(mmctx->ra));
207 }
208
209 /* MMCTX can be NULL */
210
211 switch (pdisc) {
212 case GSM48_PDISC_MM_GPRS:
213 rc = gsm0408_rcv_gmm(mmctx, msg, NULL, false);
214#pragma message "set drop_cipherable arg for gsm0408_rcv_gmm() from IuPS?"
215 break;
216 case GSM48_PDISC_SM_GPRS:
217 rc = gsm0408_rcv_gsm(mmctx, msg, NULL);
218 break;
219 default:
220 LOGMMCTXP(LOGL_NOTICE, mmctx,
221 "Unknown GSM 04.08 discriminator 0x%02x: %s\n",
222 pdisc, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
223 /* FIXME: return status message */
224 break;
225 }
226
227 /* MMCTX can be invalid */
228
229 return rc;
230}
231#endif