blob: 96157a01e2b03008081abc31366d3bc268c39485 [file] [log] [blame]
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +02001/* Messages on the Gb interface (A/Gb mode) */
2
3/* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On-Waves
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 <osmocom/core/rate_ctr.h>
25
26#include <osmocom/gprs/gprs_msgb.h>
27#include <osmocom/gprs/gprs_bssgp.h>
Alexander Couzensf23e2db2020-07-27 22:39:58 +020028#include <osmocom/gprs/gprs_ns2.h>
29#include <osmocom/gprs/gprs_bssgp_bss.h>
30#include <osmocom/sgsn/gprs_llc.h>
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020031
32#include "bscconfig.h"
33
Pau Espin Pedrol02514bc2019-08-30 16:14:22 +020034#include <osmocom/sgsn/gprs_mm_state_gb_fsm.h>
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020035#include <osmocom/sgsn/gprs_sgsn.h>
36#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +020037#include <osmocom/sgsn/gprs_sm.h>
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020038#include <osmocom/sgsn/debug.h>
39
Alexander Couzensa8f78252019-09-16 02:44:58 +020040/* Has to be called whenever any PDU (signaling, data, ...) has been received */
Pau Espin Pedrol11ccc432021-02-16 13:59:05 +010041void gprs_gb_recv_pdu(struct sgsn_mm_ctx *mmctx, const struct msgb *msg) {
42 msgid2mmctx(mmctx, msg);
Alexander Couzensa8f78252019-09-16 02:44:58 +020043 if (mmctx->gb.llme)
44 osmo_fsm_inst_dispatch(mmctx->gb.mm_state_fsm, E_MM_PDU_RECEPTION, NULL);
45}
46
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020047/* Main entry point for incoming 04.08 GPRS messages from Gb */
48int gsm0408_gprs_rcvmsg_gb(struct msgb *msg, struct gprs_llc_llme *llme,
49 bool drop_cipherable)
50{
51 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
52 uint8_t pdisc = gsm48_hdr_pdisc(gh);
53 struct sgsn_mm_ctx *mmctx;
54 struct gprs_ra_id ra_id;
55 int rc = -EINVAL;
56
57 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
58 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
59 if (mmctx) {
Pau Espin Pedrola33f0062021-06-04 17:27:03 +020060 rate_ctr_inc(rate_ctr_group_get_ctr(mmctx->ctrg, GMM_CTR_PKTS_SIG_IN));
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020061 mmctx->gb.llme = llme;
Pau Espin Pedrol11ccc432021-02-16 13:59:05 +010062 gprs_gb_recv_pdu(mmctx, msg);
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020063 }
64
65 /* MMCTX can be NULL */
66
Pau Espin Pedrol8333ef12019-08-29 19:26:12 +020067 switch (pdisc) {
68 case GSM48_PDISC_MM_GPRS:
69 rc = gsm0408_rcv_gmm(mmctx, msg, llme, drop_cipherable);
70 break;
71 case GSM48_PDISC_SM_GPRS:
72 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
73 break;
74 default:
75 LOGMMCTXP(LOGL_NOTICE, mmctx,
76 "Unknown GSM 04.08 discriminator 0x%02x: %s\n",
77 pdisc, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
78 /* FIXME: return status message */
79 break;
80 }
81
82 /* MMCTX can be invalid */
83
84 return rc;
85}
Alexander Couzens030824e2019-09-17 01:43:50 +020086
87
88int gprs_gb_page_ps_ra(struct sgsn_mm_ctx *mmctx)
89{
90 struct bssgp_paging_info pinfo;
91 int rc;
92
93 /* FIXME: page whole routing area, not only the last known cell */
94
95 /* initiate PS PAGING procedure */
96 memset(&pinfo, 0, sizeof(pinfo));
97 pinfo.mode = BSSGP_PAGING_PS;
98 pinfo.scope = BSSGP_PAGING_BVCI;
99 pinfo.bvci = mmctx->gb.bvci;
100 pinfo.imsi = mmctx->imsi;
101 pinfo.ptmsi = &mmctx->p_tmsi;
102 pinfo.drx_params = mmctx->drx_parms;
103 pinfo.qos[0] = 0; // FIXME
104 rc = bssgp_tx_paging(mmctx->gb.nsei, 0, &pinfo);
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200105 rate_ctr_inc(rate_ctr_group_get_ctr(mmctx->ctrg, GMM_CTR_PAGING_PS));
Alexander Couzens030824e2019-09-17 01:43:50 +0200106
107 return rc;
108}
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200109
110/* called by the bssgp layer to send NS PDUs */
111int gprs_gb_send_cb(void *ctx, struct msgb *msg)
112{
113 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
114 struct osmo_gprs_ns2_prim nsp = {};
115 nsp.nsei = msgb_nsei(msg);
116 nsp.bvci = msgb_bvci(msg);
Alexander Couzens21afdf92021-01-27 20:56:55 +0100117 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg);
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200118 return gprs_ns2_recv_prim(nsi, &nsp.oph);
119}
120
121void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
122{
123 switch (nsp->u.status.cause) {
Alexander Couzens21afdf92021-01-27 20:56:55 +0100124 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200125 LOGP(DGPRS, LOGL_NOTICE, "NS-E %d SNS configured.\n", nsp->nsei);
126 break;
Alexander Couzens21afdf92021-01-27 20:56:55 +0100127 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200128 LOGP(DGPRS, LOGL_NOTICE, "NS-E %d became available\n", nsp->nsei);
129 /* workaround for broken BSS which doesn't respond correct to BSSGP status message.
130 * Sent a BSSGP Reset when a persistent NSVC comes up for the first time. */
131 if (nsp->u.status.first && nsp->u.status.persistent) {
132 struct bssgp_bvc_ctx bctx = {
133 .nsei = nsp->nsei,
134 };
135 bssgp_tx_bvc_reset2(&bctx, BVCI_SIGNALLING, BSSGP_CAUSE_EQUIP_FAIL, false);
136 }
137 break;
Alexander Couzens21afdf92021-01-27 20:56:55 +0100138 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200139 LOGP(DGPRS, LOGL_NOTICE, "NS-E %d became unavailable\n", nsp->nsei);
140 break;
141 default:
142 LOGP(DGPRS, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
143 get_value_string(osmo_prim_op_names, nsp->oph.operation), nsp->oph.primitive);
144 break;
145 }
146}
147
148/* call-back function for the NS protocol */
149int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
150{
151 struct osmo_gprs_ns2_prim *nsp;
152 int rc = 0;
153
154 if (oph->sap != SAP_NS)
155 return 0;
156
157 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
158
159 if (oph->operation != PRIM_OP_INDICATION) {
160 LOGP(DGPRS, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
161 get_value_string(osmo_prim_op_names, oph->operation),
162 oph->operation);
163 return 0;
164 }
165
166 switch (oph->primitive) {
Alexander Couzens21afdf92021-01-27 20:56:55 +0100167 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200168 /* hand the message into the BSSGP implementation */
169 /* add required msg fields for Gb layer */
170 msgb_bssgph(oph->msg) = oph->msg->l3h;
171 msgb_bvci(oph->msg) = nsp->bvci;
172 msgb_nsei(oph->msg) = nsp->nsei;
173 rc = bssgp_rcvmsg(oph->msg);
174 break;
Alexander Couzens21afdf92021-01-27 20:56:55 +0100175 case GPRS_NS2_PRIM_STATUS:
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200176 gprs_ns_prim_status_cb(nsp);
177 break;
178 default:
179 LOGP(DGPRS, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
180 get_value_string(osmo_prim_op_names, oph->operation), oph->primitive);
181 break;
182 }
183
184 if (oph->msg)
185 msgb_free(oph->msg);
186
187 return rc;
188}