Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 1 | /* 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 Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 28 | #include <osmocom/gprs/gprs_ns2.h> |
| 29 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
| 30 | #include <osmocom/sgsn/gprs_llc.h> |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 31 | |
| 32 | #include "bscconfig.h" |
| 33 | |
Pau Espin Pedrol | 02514bc | 2019-08-30 16:14:22 +0200 | [diff] [blame] | 34 | #include <osmocom/sgsn/gprs_mm_state_gb_fsm.h> |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 35 | #include <osmocom/sgsn/gprs_sgsn.h> |
| 36 | #include <osmocom/sgsn/gprs_gmm.h> |
Pau Espin Pedrol | 35f0e66 | 2019-09-02 18:27:27 +0200 | [diff] [blame] | 37 | #include <osmocom/sgsn/gprs_sm.h> |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 38 | #include <osmocom/sgsn/debug.h> |
| 39 | |
Alexander Couzens | a8f7825 | 2019-09-16 02:44:58 +0200 | [diff] [blame] | 40 | /* Has to be called whenever any PDU (signaling, data, ...) has been received */ |
| 41 | void gprs_gb_recv_pdu(struct sgsn_mm_ctx *mmctx) { |
| 42 | if (mmctx->gb.llme) |
| 43 | osmo_fsm_inst_dispatch(mmctx->gb.mm_state_fsm, E_MM_PDU_RECEPTION, NULL); |
| 44 | } |
| 45 | |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 46 | /* Main entry point for incoming 04.08 GPRS messages from Gb */ |
| 47 | int gsm0408_gprs_rcvmsg_gb(struct msgb *msg, struct gprs_llc_llme *llme, |
| 48 | bool drop_cipherable) |
| 49 | { |
| 50 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
| 51 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
| 52 | struct sgsn_mm_ctx *mmctx; |
| 53 | struct gprs_ra_id ra_id; |
| 54 | int rc = -EINVAL; |
| 55 | |
| 56 | bssgp_parse_cell_id(&ra_id, msgb_bcid(msg)); |
| 57 | mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id); |
| 58 | if (mmctx) { |
| 59 | msgid2mmctx(mmctx, msg); |
| 60 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]); |
| 61 | mmctx->gb.llme = llme; |
Alexander Couzens | a8f7825 | 2019-09-16 02:44:58 +0200 | [diff] [blame] | 62 | gprs_gb_recv_pdu(mmctx); |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* MMCTX can be NULL */ |
| 66 | |
Pau Espin Pedrol | 8333ef1 | 2019-08-29 19:26:12 +0200 | [diff] [blame] | 67 | 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 Couzens | 030824e | 2019-09-17 01:43:50 +0200 | [diff] [blame] | 86 | |
| 87 | |
| 88 | int 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); |
| 105 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PAGING_PS]); |
| 106 | |
| 107 | return rc; |
| 108 | } |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 109 | |
| 110 | /* called by the bssgp layer to send NS PDUs */ |
| 111 | int 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 Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 117 | osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg); |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 118 | return gprs_ns2_recv_prim(nsi, &nsp.oph); |
| 119 | } |
| 120 | |
| 121 | void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp) |
| 122 | { |
| 123 | switch (nsp->u.status.cause) { |
Alexander Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 124 | case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED: |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 125 | LOGP(DGPRS, LOGL_NOTICE, "NS-E %d SNS configured.\n", nsp->nsei); |
| 126 | break; |
Alexander Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 127 | case GPRS_NS2_AFF_CAUSE_RECOVERY: |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 128 | 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 Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 138 | case GPRS_NS2_AFF_CAUSE_FAILURE: |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 139 | 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 */ |
| 149 | int 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 Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 167 | case GPRS_NS2_PRIM_UNIT_DATA: |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 168 | /* 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 Couzens | 21afdf9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 175 | case GPRS_NS2_PRIM_STATUS: |
Alexander Couzens | f23e2db | 2020-07-27 22:39:58 +0200 | [diff] [blame] | 176 | 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 | } |