Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_bssgp.c |
| 2 | * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */ |
| 3 | /* |
| 4 | * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 18 | * GNU General Public License for more details. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 22 | * |
Harald Welte | 4e5721d | 2010-05-17 23:41:43 +0200 | [diff] [blame] | 23 | * TODO: |
| 24 | * o properly count incoming BVC-RESET packets in counter group |
| 25 | * o set log context as early as possible for outgoing packets |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <errno.h> |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 29 | #include <stdint.h> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/msgb.h> |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 32 | #include <osmocom/core/byteswap.h> |
| 33 | #include <osmocom/core/bit16gen.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/tlv.h> |
| 35 | #include <osmocom/core/talloc.h> |
| 36 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 37 | #include <osmocom/core/stats.h> |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 38 | |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 39 | #include <osmocom/gprs/gprs_bssgp.h> |
Max | 8b8938f | 2017-06-29 19:48:29 +0200 | [diff] [blame] | 40 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 41 | #include <osmocom/gprs/gprs_ns.h> |
| 42 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 43 | #include "common_vty.h" |
| 44 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 45 | void *bssgp_tall_ctx = NULL; |
| 46 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 47 | static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg); |
| 48 | |
| 49 | bssgp_bvc_send bssgp_ns_send = _gprs_ns_sendmsg; |
| 50 | void *bssgp_ns_send_data = NULL; |
| 51 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 52 | static const struct rate_ctr_desc bssgp_ctr_description[] = { |
Harald Welte | a7a5065 | 2017-10-03 17:49:21 +0800 | [diff] [blame] | 53 | { "packets:in", "Packets at BSSGP Level ( In)" }, |
| 54 | { "packets:out","Packets at BSSGP Level (Out)" }, |
| 55 | { "bytes:in", "Bytes at BSSGP Level ( In)" }, |
| 56 | { "bytes:out", "Bytes at BSSGP Level (Out)" }, |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 57 | { "blocked", "BVC Blocking count" }, |
| 58 | { "discarded", "BVC LLC Discarded count" }, |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 59 | { "status", "BVC Status count" }, |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static const struct rate_ctr_group_desc bssgp_ctrg_desc = { |
Harald Welte | a7a5065 | 2017-10-03 17:49:21 +0800 | [diff] [blame] | 63 | .group_name_prefix = "bssgp:bss_ctx", |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 64 | .group_description = "BSSGP Peer Statistics", |
| 65 | .num_ctr = ARRAY_SIZE(bssgp_ctr_description), |
| 66 | .ctr_desc = bssgp_ctr_description, |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 67 | .class_id = OSMO_STATS_CLASS_PEER, |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Harald Welte | a78b9c2 | 2010-05-17 23:02:42 +0200 | [diff] [blame] | 70 | LLIST_HEAD(bssgp_bvc_ctxts); |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 71 | |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 72 | static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg, |
| 73 | uint32_t llc_pdu_len, void *priv); |
| 74 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 75 | |
Alexander Couzens | 83fb686 | 2020-09-03 19:30:08 +0200 | [diff] [blame] | 76 | /* callback to be backward compatible with old users which do not set the bssgp_ns_send function */ |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 77 | static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg) |
| 78 | { |
| 79 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 80 | } |
| 81 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 82 | /* Find a BTS Context based on parsed RA ID and Cell ID */ |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 83 | struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid) |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 84 | { |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 85 | struct bssgp_bvc_ctx *bctx; |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 86 | |
Harald Welte | a78b9c2 | 2010-05-17 23:02:42 +0200 | [diff] [blame] | 87 | llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) { |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 88 | if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) && |
| 89 | bctx->cell_id == cid) |
| 90 | return bctx; |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Max | 8b8938f | 2017-06-29 19:48:29 +0200 | [diff] [blame] | 95 | /*! Initiate reset procedure for all PTP BVC on a given NSEI. |
| 96 | * |
| 97 | * This function initiates reset procedure for all PTP BVC with a given cause. |
| 98 | * \param[in] nsei NSEI to which PTP BVC should belong to |
| 99 | * \param[in] cause Cause of BVC RESET |
| 100 | * \returns 0 on success, negative error code otherwise |
| 101 | */ |
| 102 | int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause) |
| 103 | { |
| 104 | int rc; |
| 105 | struct bssgp_bvc_ctx *bctx; |
| 106 | |
| 107 | llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) { |
| 108 | if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) { |
| 109 | LOGP(DBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n", |
| 110 | nsei, bctx->bvci, bssgp_cause_str(cause)); |
| 111 | rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause); |
| 112 | if (rc < 0) |
| 113 | return rc; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 120 | /* Find a BTS context based on BVCI+NSEI tuple */ |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 121 | struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei) |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 122 | { |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 123 | struct bssgp_bvc_ctx *bctx; |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 124 | |
Harald Welte | a78b9c2 | 2010-05-17 23:02:42 +0200 | [diff] [blame] | 125 | llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) { |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 126 | if (bctx->nsei == nsei && bctx->bvci == bvci) |
| 127 | return bctx; |
| 128 | } |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 132 | void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data) |
| 133 | { |
| 134 | bssgp_ns_send = ns_send; |
| 135 | bssgp_ns_send_data = data; |
| 136 | } |
| 137 | |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 138 | struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei) |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 139 | { |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 140 | struct bssgp_bvc_ctx *ctx; |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 141 | |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 142 | ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx); |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 143 | if (!ctx) |
| 144 | return NULL; |
| 145 | ctx->bvci = bvci; |
| 146 | ctx->nsei = nsei; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 147 | /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */ |
| 148 | ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci); |
Alexander Couzens | 6a2c074 | 2020-09-16 23:09:24 +0200 | [diff] [blame] | 149 | if (!ctx->ctrg) |
| 150 | goto err_ctrg; |
| 151 | |
Harald Welte | d8b4769 | 2012-09-07 11:29:32 +0200 | [diff] [blame] | 152 | ctx->fc = talloc_zero(ctx, struct bssgp_flow_control); |
Alexander Couzens | 6a2c074 | 2020-09-16 23:09:24 +0200 | [diff] [blame] | 153 | if (!ctx->fc) |
| 154 | goto err_fc; |
| 155 | |
Harald Welte | d8b4769 | 2012-09-07 11:29:32 +0200 | [diff] [blame] | 156 | /* cofigure for 2Mbit, 30 packets in queue */ |
| 157 | bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 158 | |
Harald Welte | a78b9c2 | 2010-05-17 23:02:42 +0200 | [diff] [blame] | 159 | llist_add(&ctx->list, &bssgp_bvc_ctxts); |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 160 | |
| 161 | return ctx; |
Alexander Couzens | 6a2c074 | 2020-09-16 23:09:24 +0200 | [diff] [blame] | 162 | |
| 163 | err_fc: |
| 164 | rate_ctr_group_free(ctx->ctrg); |
| 165 | err_ctrg: |
| 166 | talloc_free(ctx); |
| 167 | return NULL; |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Vadim Yanitskiy | 8eae2fc | 2019-11-09 01:45:11 +0700 | [diff] [blame] | 170 | void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx) |
| 171 | { |
| 172 | if (!ctx) |
| 173 | return; |
Alexander Couzens | 495b4a7 | 2020-09-16 23:10:03 +0200 | [diff] [blame] | 174 | |
| 175 | osmo_timer_del(&ctx->fc->timer); |
Vadim Yanitskiy | 8eae2fc | 2019-11-09 01:45:11 +0700 | [diff] [blame] | 176 | rate_ctr_group_free(ctx->ctrg); |
| 177 | llist_del(&ctx->list); |
| 178 | talloc_free(ctx); |
| 179 | } |
| 180 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 181 | /* Chapter 10.4.5: Flow Control BVC ACK */ |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 182 | static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 183 | { |
| 184 | struct msgb *msg = bssgp_msgb_alloc(); |
| 185 | struct bssgp_normal_hdr *bgph = |
| 186 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 187 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 188 | msgb_nsei(msg) = nsei; |
| 189 | msgb_bvci(msg) = ns_bvci; |
| 190 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 191 | bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK; |
| 192 | msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag); |
| 193 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 194 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 195 | } |
| 196 | |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 197 | /* 10.3.7 SUSPEND-ACK PDU */ |
| 198 | int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli, |
| 199 | const struct gprs_ra_id *ra_id, uint8_t suspend_ref) |
| 200 | { |
| 201 | struct msgb *msg = bssgp_msgb_alloc(); |
| 202 | struct bssgp_normal_hdr *bgph = |
| 203 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 204 | |
| 205 | msgb_nsei(msg) = nsei; |
| 206 | msgb_bvci(msg) = 0; /* Signalling */ |
| 207 | bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK; |
| 208 | |
Max | e29ec85 | 2018-01-05 14:30:22 +0100 | [diff] [blame] | 209 | bssgp_msgb_tlli_put(msg, tlli); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 210 | bssgp_msgb_ra_put(msg, ra_id); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 211 | msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref); |
| 212 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 213 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* 10.3.8 SUSPEND-NACK PDU */ |
| 217 | int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli, |
Dieter Spaar | d2b13fc | 2010-12-12 12:45:08 +0100 | [diff] [blame] | 218 | const struct gprs_ra_id *ra_id, |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 219 | uint8_t *cause) |
| 220 | { |
| 221 | struct msgb *msg = bssgp_msgb_alloc(); |
| 222 | struct bssgp_normal_hdr *bgph = |
| 223 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 224 | |
| 225 | msgb_nsei(msg) = nsei; |
| 226 | msgb_bvci(msg) = 0; /* Signalling */ |
| 227 | bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK; |
| 228 | |
Max | e29ec85 | 2018-01-05 14:30:22 +0100 | [diff] [blame] | 229 | bssgp_msgb_tlli_put(msg, tlli); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 230 | bssgp_msgb_ra_put(msg, ra_id); |
| 231 | |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 232 | if (cause) |
| 233 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause); |
| 234 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 235 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* 10.3.10 RESUME-ACK PDU */ |
| 239 | int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli, |
| 240 | const struct gprs_ra_id *ra_id) |
| 241 | { |
| 242 | struct msgb *msg = bssgp_msgb_alloc(); |
| 243 | struct bssgp_normal_hdr *bgph = |
| 244 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 245 | |
| 246 | msgb_nsei(msg) = nsei; |
| 247 | msgb_bvci(msg) = 0; /* Signalling */ |
| 248 | bgph->pdu_type = BSSGP_PDUT_RESUME_ACK; |
| 249 | |
Max | e29ec85 | 2018-01-05 14:30:22 +0100 | [diff] [blame] | 250 | bssgp_msgb_tlli_put(msg, tlli); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 251 | bssgp_msgb_ra_put(msg, ra_id); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 252 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 253 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* 10.3.11 RESUME-NACK PDU */ |
| 257 | int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli, |
| 258 | const struct gprs_ra_id *ra_id, uint8_t *cause) |
| 259 | { |
| 260 | struct msgb *msg = bssgp_msgb_alloc(); |
| 261 | struct bssgp_normal_hdr *bgph = |
| 262 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 263 | |
| 264 | msgb_nsei(msg) = nsei; |
| 265 | msgb_bvci(msg) = 0; /* Signalling */ |
| 266 | bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK; |
| 267 | |
Max | e29ec85 | 2018-01-05 14:30:22 +0100 | [diff] [blame] | 268 | bssgp_msgb_tlli_put(msg, tlli); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 269 | bssgp_msgb_ra_put(msg, ra_id); |
| 270 | |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 271 | if (cause) |
| 272 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause); |
| 273 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 274 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Harald Welte | a2ca4ed | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 277 | uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf) |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 278 | { |
| 279 | /* 6 octets RAC */ |
| 280 | gsm48_parse_ra(raid, buf); |
| 281 | /* 2 octets CID */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 282 | return osmo_load16be(buf+6); |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Harald Welte | 2861007 | 2011-11-24 21:32:07 +0100 | [diff] [blame] | 285 | int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid, |
| 286 | uint16_t cid) |
| 287 | { |
Harald Welte | 2861007 | 2011-11-24 21:32:07 +0100 | [diff] [blame] | 288 | /* 6 octets RAC */ |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 289 | gsm48_encode_ra((struct gsm48_ra_id *)buf, raid); |
Harald Welte | 2861007 | 2011-11-24 21:32:07 +0100 | [diff] [blame] | 290 | /* 2 octets CID */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 291 | osmo_store16be(cid, buf+6); |
Harald Welte | 2861007 | 2011-11-24 21:32:07 +0100 | [diff] [blame] | 292 | |
| 293 | return 8; |
| 294 | } |
| 295 | |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 296 | /* Chapter 8.4 BVC-Reset Procedure */ |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 297 | static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp, |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 298 | uint16_t ns_bvci) |
| 299 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 300 | struct osmo_bssgp_prim nmp; |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 301 | struct bssgp_bvc_ctx *bctx; |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 302 | uint16_t nsei = msgb_nsei(msg); |
| 303 | uint16_t bvci; |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 304 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 305 | bvci = tlvp_val16be(tp, BSSGP_IE_BVCI); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 306 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci, |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 307 | bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE))); |
| 308 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 309 | /* look-up or create the BTS context for this BVC */ |
| 310 | bctx = btsctx_by_bvci_nsei(bvci, nsei); |
| 311 | if (!bctx) |
| 312 | bctx = btsctx_alloc(bvci, nsei); |
| 313 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 314 | /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */ |
| 315 | bctx->state &= ~BVC_S_BLOCKED; |
| 316 | |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 317 | /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS |
| 318 | * informs us about its RAC + Cell ID, so we can create a mapping */ |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 319 | if (bvci != 0 && bvci != 1) { |
| 320 | if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) { |
Harald Welte | 086fe32 | 2011-08-19 16:45:19 +0200 | [diff] [blame] | 321 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET " |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 322 | "missing mandatory IE\n", bvci); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | /* actually extract RAC / CID */ |
Harald Welte | a2ca4ed | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 326 | bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id, |
| 327 | TLVP_VAL(tp, BSSGP_IE_CELL_ID)); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 328 | LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n", |
| 329 | osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci); |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 330 | } |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 331 | |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 332 | /* Send NM_BVC_RESET.ind to NM */ |
| 333 | memset(&nmp, 0, sizeof(nmp)); |
| 334 | nmp.nsei = nsei; |
| 335 | nmp.bvci = bvci; |
| 336 | nmp.tp = tp; |
| 337 | nmp.ra_id = &bctx->ra_id; |
| 338 | osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET, |
| 339 | PRIM_OP_INDICATION, msg); |
| 340 | bssgp_prim_cb(&nmp.oph, NULL); |
| 341 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 342 | /* Acknowledge the RESET to the BTS */ |
Harald Welte | 5b3bffb | 2012-09-07 12:03:40 +0200 | [diff] [blame] | 343 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
| 344 | nsei, bvci, ns_bvci); |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 348 | static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp) |
| 349 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 350 | struct osmo_bssgp_prim nmp; |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 351 | uint16_t bvci, nsei = msgb_nsei(msg); |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 352 | struct bssgp_bvc_ctx *ptp_ctx; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 353 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 354 | bvci = tlvp_val16be(tp, BSSGP_IE_BVCI); |
Harald Welte | 61c0784 | 2010-05-18 11:57:08 +0200 | [diff] [blame] | 355 | if (bvci == BVCI_SIGNALLING) { |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 356 | /* 8.3.2: Signalling BVC shall never be blocked */ |
| 357 | LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u " |
| 358 | "received block for signalling BVC!?!\n", |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 359 | nsei, msgb_bvci(msg)); |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | } |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 362 | |
Harald Welte | 086fe32 | 2011-08-19 16:45:19 +0200 | [diff] [blame] | 363 | LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 364 | |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 365 | ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 366 | if (!ptp_ctx) |
| 367 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg); |
| 368 | |
| 369 | ptp_ctx->state |= BVC_S_BLOCKED; |
| 370 | rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]); |
| 371 | |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 372 | /* Send NM_BVC_BLOCK.ind to NM */ |
| 373 | memset(&nmp, 0, sizeof(nmp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 374 | nmp.nsei = nsei; |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 375 | nmp.bvci = bvci; |
| 376 | nmp.tp = tp; |
| 377 | osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK, |
| 378 | PRIM_OP_INDICATION, msg); |
| 379 | bssgp_prim_cb(&nmp.oph, NULL); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 380 | |
| 381 | /* We always acknowledge the BLOCKing */ |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 382 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei, |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 383 | bvci, msgb_bvci(msg)); |
| 384 | }; |
| 385 | |
| 386 | static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp) |
| 387 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 388 | struct osmo_bssgp_prim nmp; |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 389 | uint16_t bvci, nsei = msgb_nsei(msg); |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 390 | struct bssgp_bvc_ctx *ptp_ctx; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 391 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 392 | bvci = tlvp_val16be(tp, BSSGP_IE_BVCI); |
Harald Welte | 61c0784 | 2010-05-18 11:57:08 +0200 | [diff] [blame] | 393 | if (bvci == BVCI_SIGNALLING) { |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 394 | /* 8.3.2: Signalling BVC shall never be blocked */ |
| 395 | LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u " |
| 396 | "received unblock for signalling BVC!?!\n", |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 397 | nsei, msgb_bvci(msg)); |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 398 | return 0; |
| 399 | } |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 400 | |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 401 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 402 | |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 403 | ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 404 | if (!ptp_ctx) |
| 405 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg); |
| 406 | |
| 407 | ptp_ctx->state &= ~BVC_S_BLOCKED; |
| 408 | |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 409 | /* Send NM_BVC_UNBLOCK.ind to NM */ |
| 410 | memset(&nmp, 0, sizeof(nmp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 411 | nmp.nsei = nsei; |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 412 | nmp.bvci = bvci; |
| 413 | nmp.tp = tp; |
| 414 | osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK, |
| 415 | PRIM_OP_INDICATION, msg); |
| 416 | bssgp_prim_cb(&nmp.oph, NULL); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 417 | |
| 418 | /* We always acknowledge the unBLOCKing */ |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 419 | return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei, |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 420 | bvci, msgb_bvci(msg)); |
| 421 | }; |
| 422 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | /* Uplink unit-data */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 424 | static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp, |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 425 | struct bssgp_bvc_ctx *ctx) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 426 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 427 | struct osmo_bssgp_prim gbp; |
Harald Welte | ec19c10 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 428 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 429 | |
Harald Welte | 6752fa4 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 430 | /* extract TLLI and parse TLV IEs */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 431 | msgb_tlli(msg) = osmo_ntohl(budh->tlli); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 432 | |
Harald Welte | 086fe32 | 2011-08-19 16:45:19 +0200 | [diff] [blame] | 433 | DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg)); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 434 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 435 | /* Cell ID and LLC_PDU are the only mandatory IE */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 436 | if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 437 | !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) { |
| 438 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD " |
| 439 | "missing mandatory IE\n", msgb_tlli(msg)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 440 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 441 | } |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 442 | |
Harald Welte | a2ca4ed | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 443 | /* store pointer to LLC header and CELL ID in msgb->cb */ |
Holger Hans Peter Freyther | b6eded8 | 2010-05-23 21:11:19 +0800 | [diff] [blame] | 444 | msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 445 | msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 447 | /* Send BSSGP_UL_UD.ind to NM */ |
| 448 | memset(&gbp, 0, sizeof(gbp)); |
| 449 | gbp.nsei = ctx->nsei; |
| 450 | gbp.bvci = ctx->bvci; |
| 451 | gbp.tlli = msgb_tlli(msg); |
| 452 | gbp.tp = tp; |
| 453 | osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD, |
| 454 | PRIM_OP_INDICATION, msg); |
| 455 | return bssgp_prim_cb(&gbp.oph, NULL); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 456 | } |
| 457 | |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 458 | static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 459 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 460 | struct osmo_bssgp_prim gbp; |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 461 | struct gprs_ra_id raid; |
| 462 | uint32_t tlli; |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 463 | uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 464 | int rc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 465 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 466 | if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 467 | !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) { |
| 468 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND " |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 469 | "missing mandatory IE\n", ns_bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 470 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 471 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 472 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 473 | tlli = tlvp_val32be(tp, BSSGP_IE_TLLI); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 474 | |
Harald Welte | 1792532 | 2010-05-31 20:18:35 +0200 | [diff] [blame] | 475 | DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n", |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 476 | ns_bvci, tlli); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 477 | |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 478 | gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
| 479 | |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 480 | /* Inform GMM about the SUSPEND request */ |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 481 | memset(&gbp, 0, sizeof(gbp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 482 | gbp.nsei = nsei; |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 483 | gbp.bvci = ns_bvci; |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 484 | gbp.tlli = tlli; |
| 485 | gbp.ra_id = &raid; |
| 486 | osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND, |
| 487 | PRIM_OP_REQUEST, msg); |
| 488 | |
| 489 | rc = bssgp_prim_cb(&gbp.oph, NULL); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 490 | if (rc < 0) |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 491 | return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 492 | |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 493 | bssgp_tx_suspend_ack(nsei, tlli, &raid, 0); |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 494 | |
Holger Hans Peter Freyther | d30cefa | 2010-05-23 21:12:15 +0800 | [diff] [blame] | 495 | return 0; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 498 | static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 499 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 500 | struct osmo_bssgp_prim gbp; |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 501 | struct gprs_ra_id raid; |
| 502 | uint32_t tlli; |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 503 | uint8_t suspend_ref; |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 504 | uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 505 | int rc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 506 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 507 | if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) || |
| 508 | !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 509 | !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) { |
| 510 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME " |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 511 | "missing mandatory IE\n", ns_bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 512 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 513 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 514 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 515 | tlli = tlvp_val32be(tp, BSSGP_IE_TLLI); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 516 | suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 517 | |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 518 | DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 519 | |
Harald Welte | a8aa4df | 2010-05-30 22:00:53 +0200 | [diff] [blame] | 520 | gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
| 521 | |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 522 | /* Inform GMM about the RESUME request */ |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 523 | memset(&gbp, 0, sizeof(gbp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 524 | gbp.nsei = nsei; |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 525 | gbp.bvci = ns_bvci; |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 526 | gbp.tlli = tlli; |
| 527 | gbp.ra_id = &raid; |
| 528 | gbp.u.resume.suspend_ref = suspend_ref; |
| 529 | osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME, |
| 530 | PRIM_OP_REQUEST, msg); |
| 531 | |
| 532 | rc = bssgp_prim_cb(&gbp.oph, NULL); |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 533 | if (rc < 0) |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 534 | return bssgp_tx_resume_nack(nsei, tlli, &raid, |
Harald Welte | 313cccf | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 535 | NULL); |
| 536 | |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 537 | bssgp_tx_resume_ack(nsei, tlli, &raid); |
Holger Hans Peter Freyther | d30cefa | 2010-05-23 21:12:15 +0800 | [diff] [blame] | 538 | return 0; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 539 | } |
| 540 | |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 541 | |
| 542 | static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp, |
| 543 | struct bssgp_bvc_ctx *ctx) |
| 544 | { |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 545 | struct osmo_bssgp_prim nmp; |
Harald Welte | b736314 | 2010-07-23 21:59:29 +0200 | [diff] [blame] | 546 | uint32_t tlli = 0; |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 547 | uint16_t nsei = msgb_nsei(msg); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 548 | |
| 549 | if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) || |
| 550 | !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) || |
| 551 | !TLVP_PRESENT(tp, BSSGP_IE_BVCI) || |
| 552 | !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) { |
| 553 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED " |
| 554 | "missing mandatory IE\n", ctx->bvci); |
| 555 | } |
| 556 | |
Harald Welte | b736314 | 2010-07-23 21:59:29 +0200 | [diff] [blame] | 557 | if (TLVP_PRESENT(tp, BSSGP_IE_TLLI)) |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 558 | tlli = tlvp_val32be(tp, BSSGP_IE_TLLI); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 559 | |
Harald Welte | 086fe32 | 2011-08-19 16:45:19 +0200 | [diff] [blame] | 560 | DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n", |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 561 | ctx->bvci, tlli); |
| 562 | |
| 563 | rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]); |
| 564 | |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 565 | /* send NM_LLC_DISCARDED to NM */ |
| 566 | memset(&nmp, 0, sizeof(nmp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 567 | nmp.nsei = nsei; |
Harald Welte | 15a3643 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 568 | nmp.bvci = ctx->bvci; |
| 569 | nmp.tlli = tlli; |
| 570 | nmp.tp = tp; |
| 571 | osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED, |
| 572 | PRIM_OP_INDICATION, msg); |
| 573 | |
| 574 | return bssgp_prim_cb(&nmp.oph, NULL); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 577 | int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp, |
| 578 | uint16_t bvci, struct bssgp_bvc_ctx *bctx) |
| 579 | { |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 580 | uint16_t nsei = msgb_nsei(msg); |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 581 | struct osmo_bssgp_prim nmp; |
| 582 | enum gprs_bssgp_cause cause; |
| 583 | |
| 584 | if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) { |
| 585 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS " |
| 586 | "missing mandatory IE\n", bvci); |
| 587 | cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC; |
| 588 | } else { |
| 589 | cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE); |
| 590 | } |
| 591 | |
| 592 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n", |
| 593 | bvci, bssgp_cause_str(cause)); |
| 594 | |
| 595 | if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) { |
| 596 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) |
| 597 | LOGP(DBSSGP, LOGL_ERROR, |
| 598 | "BSSGP BVCI=%u Rx STATUS cause=%s " |
| 599 | "missing conditional BVCI IE\n", |
| 600 | bvci, bssgp_cause_str(cause)); |
| 601 | } |
| 602 | |
| 603 | if (bctx) |
| 604 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]); |
| 605 | |
| 606 | /* send NM_STATUS to NM */ |
| 607 | memset(&nmp, 0, sizeof(nmp)); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 608 | nmp.nsei = nsei; |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 609 | nmp.bvci = bvci; |
| 610 | nmp.tp = tp; |
| 611 | osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS, |
| 612 | PRIM_OP_INDICATION, msg); |
| 613 | |
| 614 | return bssgp_prim_cb(&nmp.oph, NULL); |
| 615 | } |
| 616 | |
| 617 | |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 618 | /* One element (msgb) in a BSSGP Flow Control queue */ |
| 619 | struct bssgp_fc_queue_element { |
| 620 | /* linked list of queue elements */ |
| 621 | struct llist_head list; |
| 622 | /* The message that we have enqueued */ |
| 623 | struct msgb *msg; |
| 624 | /* Length of the LLC PDU part of the contained message */ |
| 625 | uint32_t llc_pdu_len; |
| 626 | /* private pointer passed to the flow control out_cb function */ |
| 627 | void *priv; |
| 628 | }; |
| 629 | |
| 630 | static int fc_queue_timer_cfg(struct bssgp_flow_control *fc); |
| 631 | static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len); |
| 632 | |
| 633 | static void fc_timer_cb(void *data) |
| 634 | { |
| 635 | struct bssgp_flow_control *fc = data; |
| 636 | struct bssgp_fc_queue_element *fcqe; |
| 637 | struct timeval time_now; |
| 638 | |
| 639 | /* if the queue is empty, we return without sending something |
| 640 | * and without re-starting the timer */ |
| 641 | if (llist_empty(&fc->queue)) |
| 642 | return; |
| 643 | |
| 644 | /* get the first entry from the queue */ |
| 645 | fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element, |
| 646 | list); |
| 647 | |
| 648 | if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) { |
| 649 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still " |
| 650 | "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len); |
| 651 | /* make sure we re-start the timer */ |
| 652 | fc_queue_timer_cfg(fc); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | /* remove from the queue */ |
| 657 | llist_del(&fcqe->list); |
| 658 | |
| 659 | fc->queue_depth--; |
| 660 | |
| 661 | /* record the time we transmitted this PDU */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 662 | osmo_gettimeofday(&time_now, NULL); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 663 | fc->time_last_pdu = time_now; |
| 664 | |
| 665 | /* call the output callback for this FC instance */ |
| 666 | fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL); |
| 667 | |
| 668 | /* we expect that out_cb will in the end free the msgb once |
| 669 | * it is no longer needed */ |
| 670 | |
| 671 | /* but we have to free the queue element ourselves */ |
| 672 | talloc_free(fcqe); |
| 673 | |
| 674 | /* re-configure the timer for the next PDU */ |
| 675 | fc_queue_timer_cfg(fc); |
| 676 | } |
| 677 | |
| 678 | /* configure/schedule the flow control timer to expire once the bucket |
| 679 | * will have leaked a sufficient number of bytes to transmit the next |
| 680 | * PDU in the queue */ |
| 681 | static int fc_queue_timer_cfg(struct bssgp_flow_control *fc) |
| 682 | { |
| 683 | struct bssgp_fc_queue_element *fcqe; |
| 684 | uint32_t msecs; |
| 685 | |
| 686 | if (llist_empty(&fc->queue)) |
| 687 | return 0; |
| 688 | |
Jacob Erlbeck | 9731935 | 2015-04-30 19:28:03 +0200 | [diff] [blame] | 689 | fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element, |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 690 | list); |
| 691 | |
Harald Welte | 27b2bb7 | 2013-06-22 09:44:00 +0200 | [diff] [blame] | 692 | if (fc->bucket_leak_rate != 0) { |
| 693 | /* Calculate the point in time at which we will have leaked |
| 694 | * a sufficient number of bytes from the bucket to transmit |
| 695 | * the first PDU in the queue */ |
| 696 | msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate; |
| 697 | /* FIXME: add that time to fc->time_last_pdu and subtract it from |
| 698 | * current time */ |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 699 | osmo_timer_setup(&fc->timer, fc_timer_cb, fc); |
Harald Welte | 27b2bb7 | 2013-06-22 09:44:00 +0200 | [diff] [blame] | 700 | osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000); |
| 701 | } else { |
| 702 | /* If the PCU is telling us to not send any more data at all, |
| 703 | * there's no point starting a timer. */ |
| 704 | } |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /* Enqueue a PDU in the flow control queue for delayed transmission */ |
| 710 | static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg, |
| 711 | uint32_t llc_pdu_len, void *priv) |
| 712 | { |
| 713 | struct bssgp_fc_queue_element *fcqe; |
| 714 | |
| 715 | if (fc->queue_depth >= fc->max_queue_depth) |
| 716 | return -ENOSPC; |
| 717 | |
| 718 | fcqe = talloc_zero(fc, struct bssgp_fc_queue_element); |
| 719 | if (!fcqe) |
| 720 | return -ENOMEM; |
| 721 | fcqe->msg = msg; |
| 722 | fcqe->llc_pdu_len = llc_pdu_len; |
| 723 | fcqe->priv = priv; |
| 724 | |
| 725 | llist_add_tail(&fcqe->list, &fc->queue); |
| 726 | |
| 727 | fc->queue_depth++; |
| 728 | |
| 729 | /* re-configure the timer for dequeueing the pdu */ |
| 730 | fc_queue_timer_cfg(fc); |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /* According to Section 8.2 */ |
| 736 | static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len) |
| 737 | { |
| 738 | struct timeval time_now, time_diff; |
| 739 | int64_t bucket_predicted; |
| 740 | uint32_t csecs_elapsed, leaked; |
| 741 | |
| 742 | /* B' = B + L(p) - (Tc - Tp)*R */ |
| 743 | |
| 744 | /* compute number of centi-seconds that have elapsed since transmitting |
| 745 | * the last PDU (Tc - Tp) */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 746 | osmo_gettimeofday(&time_now, NULL); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 747 | timersub(&time_now, &fc->time_last_pdu, &time_diff); |
| 748 | csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000; |
| 749 | |
| 750 | /* compute number of bytes that have leaked in the elapsed number |
| 751 | * of centi-seconds */ |
| 752 | leaked = csecs_elapsed * (fc->bucket_leak_rate / 100); |
| 753 | /* add the current PDU length to the last bucket level */ |
| 754 | bucket_predicted = fc->bucket_counter + pdu_len; |
| 755 | /* ... and subtract the number of leaked bytes */ |
| 756 | bucket_predicted -= leaked; |
| 757 | |
Vadim Yanitskiy | f178695 | 2017-06-12 03:41:35 +0700 | [diff] [blame] | 758 | if (bucket_predicted < pdu_len) |
| 759 | return 0; |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 760 | |
| 761 | if (bucket_predicted <= fc->bucket_size_max) { |
| 762 | /* the bucket is not full yet, we can pass the packet */ |
| 763 | fc->bucket_counter = bucket_predicted; |
Vadim Yanitskiy | f178695 | 2017-06-12 03:41:35 +0700 | [diff] [blame] | 764 | return 0; |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* bucket is full, PDU needs to be delayed */ |
| 768 | return 1; |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* output callback for BVC flow control */ |
| 772 | static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg, |
| 773 | uint32_t llc_pdu_len, void *priv) |
| 774 | { |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 775 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* input function of the flow control implementation, called first |
| 779 | * for the MM flow control, and then as the MM flow control output |
| 780 | * callback in order to perform BVC flow control */ |
| 781 | int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg, |
| 782 | uint32_t llc_pdu_len, void *priv) |
| 783 | { |
| 784 | struct timeval time_now; |
| 785 | |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 786 | if (llc_pdu_len > fc->bucket_size_max) { |
| 787 | LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger " |
| 788 | "than maximum bucket size (%u)!\n", llc_pdu_len, |
| 789 | fc->bucket_size_max); |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 790 | msgb_free(msg); |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 791 | return -EIO; |
| 792 | } |
| 793 | |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 794 | if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) { |
Neels Hofmeyr | cd325ef | 2017-11-16 22:32:36 +0100 | [diff] [blame] | 795 | int rc; |
| 796 | rc = fc_enqueue(fc, msg, llc_pdu_len, priv); |
| 797 | if (rc) |
| 798 | msgb_free(msg); |
| 799 | return rc; |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 800 | } else { |
| 801 | /* record the time we transmitted this PDU */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 802 | osmo_gettimeofday(&time_now, NULL); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 803 | fc->time_last_pdu = time_now; |
| 804 | return fc->out_cb(priv, msg, llc_pdu_len, NULL); |
| 805 | } |
| 806 | } |
| 807 | |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 808 | |
| 809 | /* Initialize the Flow Control structure */ |
| 810 | void bssgp_fc_init(struct bssgp_flow_control *fc, |
| 811 | uint32_t bucket_size_max, uint32_t bucket_leak_rate, |
| 812 | uint32_t max_queue_depth, |
| 813 | int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg, |
| 814 | uint32_t llc_pdu_len, void *priv)) |
| 815 | { |
| 816 | fc->out_cb = out_cb; |
| 817 | fc->bucket_size_max = bucket_size_max; |
| 818 | fc->bucket_leak_rate = bucket_leak_rate; |
| 819 | fc->max_queue_depth = max_queue_depth; |
| 820 | INIT_LLIST_HEAD(&fc->queue); |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 821 | osmo_gettimeofday(&fc->time_last_pdu, NULL); |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 824 | /* Initialize the Flow Control parameters for a new MS according to |
| 825 | * default values for the BVC specified by BVCI and NSEI */ |
| 826 | int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci, |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 827 | uint16_t nsei, uint32_t max_queue_depth) |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 828 | { |
| 829 | struct bssgp_bvc_ctx *ctx; |
| 830 | |
| 831 | ctx = btsctx_by_bvci_nsei(bvci, nsei); |
| 832 | if (!ctx) |
| 833 | return -ENODEV; |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 834 | |
| 835 | /* output call-back of per-MS FC is per-CTX FC */ |
| 836 | bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms, |
| 837 | max_queue_depth, bssgp_fc_in); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 842 | static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp, |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 843 | struct bssgp_bvc_ctx *bctx) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 844 | { |
Harald Welte | 27b2bb7 | 2013-06-22 09:44:00 +0200 | [diff] [blame] | 845 | uint32_t old_leak_rate = bctx->fc->bucket_leak_rate; |
| 846 | uint32_t old_r_def_ms = bctx->r_default_ms; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 847 | |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 848 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n", |
| 849 | bctx->bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 850 | |
| 851 | if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) || |
| 852 | !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) || |
| 853 | !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) || |
| 854 | !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 855 | !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) { |
| 856 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC " |
| 857 | "missing mandatory IE\n", bctx->bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 858 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 859 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 860 | |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 861 | /* 11.3.5 Bucket Size in 100 octets unit */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 862 | bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE); |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 863 | /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 864 | bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8; |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 865 | /* 11.3.2 in octets */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 866 | bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS); |
Harald Welte | bb82622 | 2012-09-07 10:22:01 +0200 | [diff] [blame] | 867 | /* 11.3.32 Bucket Leak rate in 100bits/sec unit */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 868 | bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8; |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 869 | |
Harald Welte | 27b2bb7 | 2013-06-22 09:44:00 +0200 | [diff] [blame] | 870 | if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0) |
| 871 | LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak " |
| 872 | "rate of 0, stopping all DL GPRS!\n"); |
| 873 | else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0) |
| 874 | LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak " |
| 875 | "rate of != 0, restarting all DL GPRS!\n"); |
| 876 | |
| 877 | if (old_r_def_ms != 0 && bctx->r_default_ms == 0) |
| 878 | LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default " |
| 879 | "bucket leak rate of 0, stopping DL GPRS!\n"); |
| 880 | else if (old_r_def_ms == 0 && bctx->r_default_ms != 0) |
| 881 | LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default " |
| 882 | "bucket leak rate != 0, restarting DL GPRS!\n"); |
| 883 | |
| 884 | /* reconfigure the timer for flow control based on new values */ |
| 885 | fc_queue_timer_cfg(bctx->fc); |
| 886 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 887 | /* Send FLOW_CONTROL_BVC_ACK */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 888 | return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG), |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 889 | msgb_bvci(msg)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 890 | } |
Harald Welte | 3fddf3c | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 891 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 892 | /* Receive a BSSGP PDU from a BSS on a PTP BVCI */ |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 893 | static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, |
| 894 | struct bssgp_bvc_ctx *bctx) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 895 | { |
Harald Welte | ec19c10 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 896 | struct bssgp_normal_hdr *bgph = |
| 897 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 898 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 899 | int rc = 0; |
| 900 | |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 901 | OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS); |
| 902 | |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 903 | /* If traffic is received on a BVC that is marked as blocked, the |
| 904 | * received PDU shall not be accepted and a STATUS PDU (Cause value: |
| 905 | * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 906 | if (bctx->state & BVC_S_BLOCKED) { |
Harald Welte | 58e65c9 | 2010-05-13 21:45:23 +0200 | [diff] [blame] | 907 | uint16_t bvci = msgb_bvci(msg); |
| 908 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); |
| 909 | } |
| 910 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 911 | switch (pdu_type) { |
| 912 | case BSSGP_PDUT_UL_UNITDATA: |
| 913 | /* some LLC data from the MS */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 914 | rc = bssgp_rx_ul_ud(msg, tp, bctx); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 915 | break; |
| 916 | case BSSGP_PDUT_RA_CAPABILITY: |
| 917 | /* BSS requests RA capability or IMSI */ |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 918 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n", |
| 919 | bctx->bvci); |
Harald Welte | 6b7cf25 | 2010-05-13 19:41:31 +0200 | [diff] [blame] | 920 | /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */ |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 921 | /* FIXME: send RA_CAPA_UPDATE_ACK */ |
| 922 | break; |
| 923 | case BSSGP_PDUT_RADIO_STATUS: |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 924 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 925 | /* BSS informs us of some exception */ |
Harald Welte | 6b7cf25 | 2010-05-13 19:41:31 +0200 | [diff] [blame] | 926 | /* FIXME: send GMM_RADIO_STATUS.ind to GMM */ |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 927 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 928 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
| 929 | /* BSS informs us of available bandwidth in Gb interface */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 930 | rc = bssgp_rx_fc_bvc(msg, tp, bctx); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 931 | break; |
| 932 | case BSSGP_PDUT_FLOW_CONTROL_MS: |
| 933 | /* BSS informs us of available bandwidth to one MS */ |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 934 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n", |
| 935 | bctx->bvci); |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 936 | /* FIXME: actually implement flow control */ |
| 937 | /* FIXME: Send FLOW_CONTROL_MS_ACK */ |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 938 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 939 | case BSSGP_PDUT_STATUS: |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 940 | /* This is already handled in bssgp_rcvmsg() */ |
Jacob Erlbeck | 49ed9be | 2015-03-17 10:21:16 +0100 | [diff] [blame] | 941 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 942 | case BSSGP_PDUT_DOWNLOAD_BSS_PFC: |
| 943 | case BSSGP_PDUT_CREATE_BSS_PFC_ACK: |
| 944 | case BSSGP_PDUT_CREATE_BSS_PFC_NACK: |
| 945 | case BSSGP_PDUT_MODIFY_BSS_PFC: |
| 946 | case BSSGP_PDUT_DELETE_BSS_PFC_ACK: |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 947 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] " |
| 948 | "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 949 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 950 | break; |
| 951 | /* those only exist in the SGSN -> BSS direction */ |
| 952 | case BSSGP_PDUT_DL_UNITDATA: |
| 953 | case BSSGP_PDUT_PAGING_PS: |
| 954 | case BSSGP_PDUT_PAGING_CS: |
| 955 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 956 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 957 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 958 | DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n", |
| 959 | bctx->bvci, bssgp_pdu_str(pdu_type)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 960 | bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 961 | rc = -EINVAL; |
| 962 | break; |
| 963 | default: |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 964 | DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n", |
| 965 | bctx->bvci, bssgp_pdu_str(pdu_type)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 966 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 967 | break; |
| 968 | } |
| 969 | |
Holger Hans Peter Freyther | d30cefa | 2010-05-23 21:12:15 +0800 | [diff] [blame] | 970 | return rc; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */ |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 974 | static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp, |
| 975 | struct bssgp_bvc_ctx *bctx) |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 976 | { |
| 977 | struct bssgp_normal_hdr *bgph = |
| 978 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 979 | uint8_t pdu_type = bgph->pdu_type; |
| 980 | int rc = 0; |
| 981 | uint16_t ns_bvci = msgb_bvci(msg); |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 982 | uint16_t bvci = bctx ? bctx->bvci : ns_bvci; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 983 | |
| 984 | switch (bgph->pdu_type) { |
| 985 | case BSSGP_PDUT_SUSPEND: |
| 986 | /* MS wants to suspend */ |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 987 | rc = bssgp_rx_suspend(msg, tp); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 988 | break; |
| 989 | case BSSGP_PDUT_RESUME: |
| 990 | /* MS wants to resume */ |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 991 | rc = bssgp_rx_resume(msg, tp); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 992 | break; |
| 993 | case BSSGP_PDUT_FLUSH_LL_ACK: |
| 994 | /* BSS informs us it has performed LL FLUSH */ |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 995 | DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 996 | /* FIXME: send NM_FLUSH_LL.res to NM */ |
| 997 | break; |
| 998 | case BSSGP_PDUT_LLC_DISCARD: |
| 999 | /* BSS informs that some LLC PDU's have been discarded */ |
Jacob Erlbeck | b43baf2 | 2014-09-10 12:43:28 +0200 | [diff] [blame] | 1000 | if (!bctx) { |
| 1001 | LOGP(DBSSGP, LOGL_ERROR, |
| 1002 | "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n"); |
| 1003 | goto err_mand_ie; |
| 1004 | } |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1005 | rc = bssgp_rx_llc_disc(msg, tp, bctx); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1006 | break; |
| 1007 | case BSSGP_PDUT_BVC_BLOCK: |
| 1008 | /* BSS tells us that BVC shall be blocked */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1009 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1010 | !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) { |
| 1011 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK " |
| 1012 | "missing mandatory IE\n"); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1013 | goto err_mand_ie; |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1014 | } |
Harald Welte | 2677ea5 | 2010-05-31 17:16:36 +0200 | [diff] [blame] | 1015 | rc = bssgp_rx_bvc_block(msg, tp); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1016 | break; |
| 1017 | case BSSGP_PDUT_BVC_UNBLOCK: |
| 1018 | /* BSS tells us that BVC shall be unblocked */ |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1019 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) { |
| 1020 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK " |
| 1021 | "missing mandatory IE\n"); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1022 | goto err_mand_ie; |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1023 | } |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1024 | rc = bssgp_rx_bvc_unblock(msg, tp); |
| 1025 | break; |
Max | 590c402 | 2017-06-28 14:29:24 +0200 | [diff] [blame] | 1026 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 1027 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci); |
| 1028 | break; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1029 | case BSSGP_PDUT_BVC_RESET: |
| 1030 | /* BSS tells us that BVC init is required */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1031 | if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) || |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1032 | !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) { |
| 1033 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET " |
| 1034 | "missing mandatory IE\n"); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1035 | goto err_mand_ie; |
Harald Welte | e9686b6 | 2010-05-31 18:07:17 +0200 | [diff] [blame] | 1036 | } |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1037 | rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci); |
| 1038 | break; |
| 1039 | case BSSGP_PDUT_STATUS: |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 1040 | /* This is already handled in bssgp_rcvmsg() */ |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1041 | break; |
| 1042 | /* those only exist in the SGSN -> BSS direction */ |
| 1043 | case BSSGP_PDUT_PAGING_PS: |
| 1044 | case BSSGP_PDUT_PAGING_CS: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1045 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1046 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1047 | case BSSGP_PDUT_RESUME_ACK: |
| 1048 | case BSSGP_PDUT_RESUME_NACK: |
Harald Welte | 6b7cf25 | 2010-05-13 19:41:31 +0200 | [diff] [blame] | 1049 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1050 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1051 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1052 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 1053 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n", |
| 1054 | bvci, bssgp_pdu_str(pdu_type)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1055 | bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1056 | rc = -EINVAL; |
| 1057 | break; |
| 1058 | default: |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 1059 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n", |
| 1060 | bvci, bssgp_pdu_str(pdu_type)); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1061 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | return rc; |
| 1066 | err_mand_ie: |
| 1067 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 1068 | } |
| 1069 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1070 | /* We expect msgb_bssgph() to point to the BSSGP header */ |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1071 | int bssgp_rcvmsg(struct msgb *msg) |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1072 | { |
| 1073 | struct bssgp_normal_hdr *bgph = |
| 1074 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 1075 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 1076 | struct tlv_parsed tp; |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 1077 | struct bssgp_bvc_ctx *bctx; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1078 | uint8_t pdu_type = bgph->pdu_type; |
| 1079 | uint16_t ns_bvci = msgb_bvci(msg); |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 1080 | uint16_t nsei = msgb_nsei(msg); |
Jacob Erlbeck | b83b838 | 2014-09-23 13:28:22 +0200 | [diff] [blame] | 1081 | uint16_t bvci = ns_bvci; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1082 | int data_len; |
| 1083 | int rc = 0; |
| 1084 | |
| 1085 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 1086 | |
| 1087 | /* UNITDATA BSSGP headers have TLLI in front */ |
| 1088 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && |
| 1089 | pdu_type != BSSGP_PDUT_DL_UNITDATA) { |
| 1090 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 1091 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 1092 | } else { |
| 1093 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 1094 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 1095 | } |
Stefan Sperling | 2b544b2 | 2018-06-25 12:20:43 +0200 | [diff] [blame] | 1096 | if (rc < 0) { |
| 1097 | LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n", |
| 1098 | bssgp_pdu_str(pdu_type), msgb_hexdump(msg)); |
Stefan Sperling | f1e13d6 | 2018-06-25 12:20:43 +0200 | [diff] [blame] | 1099 | if (pdu_type != BSSGP_PDUT_STATUS) |
| 1100 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Stefan Sperling | 2b544b2 | 2018-06-25 12:20:43 +0200 | [diff] [blame] | 1101 | return rc; |
| 1102 | } |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1103 | |
Jacob Erlbeck | b83b838 | 2014-09-23 13:28:22 +0200 | [diff] [blame] | 1104 | if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1105 | bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI); |
Jacob Erlbeck | b83b838 | 2014-09-23 13:28:22 +0200 | [diff] [blame] | 1106 | |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1107 | /* look-up or create the BTS context for this BVC */ |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 1108 | bctx = btsctx_by_bvci_nsei(bvci, nsei); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1109 | |
Harald Welte | 16c8dbb | 2010-05-17 23:30:01 +0200 | [diff] [blame] | 1110 | if (bctx) { |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1111 | log_set_context(LOG_CTX_GB_BVC, bctx); |
Harald Welte | 16c8dbb | 2010-05-17 23:30:01 +0200 | [diff] [blame] | 1112 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); |
| 1113 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], |
| 1114 | msgb_bssgp_len(msg)); |
| 1115 | } |
| 1116 | |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 1117 | /* Always handle STATUS PDUs, even if they contain an invalid BVCI or |
| 1118 | * are otherwise unexpected */ |
| 1119 | if (pdu_type == BSSGP_PDUT_STATUS) |
| 1120 | /* Some exception has occurred */ |
| 1121 | return bssgp_rx_status(msg, &tp, bvci, bctx); |
| 1122 | |
| 1123 | /* Only a RESET PDU can create a new BVC context, otherwise it must be |
| 1124 | * registered if a BVCI is given. */ |
| 1125 | if (!bctx && bvci != BVCI_SIGNALLING && |
| 1126 | pdu_type != BSSGP_PDUT_BVC_RESET) { |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 1127 | LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci, |
Max | 2c34ab4 | 2016-03-17 15:42:26 +0100 | [diff] [blame] | 1128 | bssgp_pdu_str(pdu_type)); |
Jacob Erlbeck | 36153dc | 2015-03-17 10:21:17 +0100 | [diff] [blame] | 1129 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg); |
| 1130 | } |
| 1131 | |
Harald Welte | 61c0784 | 2010-05-18 11:57:08 +0200 | [diff] [blame] | 1132 | if (ns_bvci == BVCI_SIGNALLING) |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1133 | rc = bssgp_rx_sign(msg, &tp, bctx); |
Harald Welte | 61c0784 | 2010-05-18 11:57:08 +0200 | [diff] [blame] | 1134 | else if (ns_bvci == BVCI_PTM) |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1135 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Jacob Erlbeck | b535e39 | 2015-04-07 17:52:44 +0200 | [diff] [blame] | 1136 | else if (bctx) |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1137 | rc = bssgp_rx_ptp(msg, &tp, bctx); |
Jacob Erlbeck | b535e39 | 2015-04-07 17:52:44 +0200 | [diff] [blame] | 1138 | else |
| 1139 | LOGP(DBSSGP, LOGL_NOTICE, |
Max | 548caef | 2019-03-07 13:49:34 +0100 | [diff] [blame] | 1140 | "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci, |
| 1141 | bssgp_pdu_str(pdu_type), ns_bvci); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1142 | |
| 1143 | return rc; |
| 1144 | } |
| 1145 | |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1146 | int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime, |
| 1147 | struct bssgp_dl_ud_par *dup) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1148 | { |
Harald Welte | 8a52113 | 2010-05-17 22:59:29 +0200 | [diff] [blame] | 1149 | struct bssgp_bvc_ctx *bctx; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1150 | struct bssgp_ud_hdr *budh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1151 | uint8_t llc_pdu_tlv_hdr_len = 2; |
Harald Welte | 8ef54d1 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 1152 | uint8_t *llc_pdu_tlv; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1153 | uint16_t msg_len = msg->len; |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1154 | uint16_t bvci = msgb_bvci(msg); |
| 1155 | uint16_t nsei = msgb_nsei(msg); |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1156 | uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */ |
Harald Welte | 2f94683 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1157 | uint16_t drx_params; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1158 | |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1159 | OSMO_ASSERT(dup != NULL); |
| 1160 | |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1161 | /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */ |
Harald Welte | 61c0784 | 2010-05-18 11:57:08 +0200 | [diff] [blame] | 1162 | if (bvci <= BVCI_PTM ) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 1163 | LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n", |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1164 | bvci); |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 1165 | msgb_free(msg); |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1166 | return -EINVAL; |
| 1167 | } |
| 1168 | |
| 1169 | bctx = btsctx_by_bvci_nsei(bvci, nsei); |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1170 | if (!bctx) { |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 1171 | LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n", |
| 1172 | bvci); |
Holger Hans Peter Freyther | 10dd73c | 2014-10-10 17:24:34 +0200 | [diff] [blame] | 1173 | msgb_free(msg); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 1174 | return -ENODEV; |
Harald Welte | 25de811 | 2010-05-13 21:26:28 +0200 | [diff] [blame] | 1175 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1176 | |
| 1177 | if (msg->len > TVLV_MAX_ONEBYTE) |
| 1178 | llc_pdu_tlv_hdr_len += 1; |
| 1179 | |
| 1180 | /* prepend the tag and length of the LLC-PDU TLV */ |
| 1181 | llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len); |
| 1182 | llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU; |
| 1183 | if (llc_pdu_tlv_hdr_len > 2) { |
| 1184 | llc_pdu_tlv[1] = msg_len >> 8; |
| 1185 | llc_pdu_tlv[2] = msg_len & 0xff; |
| 1186 | } else { |
Sylvain Munaut | b00d1ad | 2010-06-09 21:13:13 +0200 | [diff] [blame] | 1187 | llc_pdu_tlv[1] = msg_len & 0x7f; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1188 | llc_pdu_tlv[1] |= 0x80; |
| 1189 | } |
| 1190 | |
Harald Welte | 2f94683 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1191 | /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */ |
| 1192 | |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1193 | /* Old TLLI to help BSS map from old->new */ |
| 1194 | if (dup->tlli) { |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1195 | uint32_t tlli = osmo_htonl(*dup->tlli); |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1196 | msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli); |
Harald Welte | 2f94683 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1197 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1198 | |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1199 | /* IMSI */ |
| 1200 | if (dup->imsi && strlen(dup->imsi)) { |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 1201 | uint8_t mi[GSM48_MID_MAX_SIZE]; |
| 1202 | /* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11, |
| 1203 | * but somehow gcc (8.2) is not smart enough to figure this out and claims that |
| 1204 | * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to |
| 1205 | * mi[131], which is wrong */ |
| 1206 | #pragma GCC diagnostic push |
| 1207 | #pragma GCC diagnostic ignored "-Warray-bounds" |
| 1208 | int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi); |
| 1209 | OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE); |
| 1210 | if (imsi_len > 2) |
| 1211 | msgb_tvlv_push(msg, BSSGP_IE_IMSI, |
| 1212 | imsi_len-2, mi+2); |
| 1213 | #pragma GCC diagnostic pop |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* DRX parameters */ |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1217 | drx_params = osmo_htons(dup->drx_parms); |
Jacob Erlbeck | c641591 | 2015-04-07 17:52:43 +0200 | [diff] [blame] | 1218 | msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2, |
| 1219 | (uint8_t *) &drx_params); |
| 1220 | |
| 1221 | /* FIXME: Priority */ |
| 1222 | |
| 1223 | /* MS Radio Access Capability */ |
| 1224 | if (dup->ms_ra_cap.len) |
| 1225 | msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP, |
| 1226 | dup->ms_ra_cap.len, dup->ms_ra_cap.v); |
| 1227 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1228 | /* prepend the pdu lifetime */ |
Harald Welte | 8ef54d1 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 1229 | msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1230 | |
| 1231 | /* prepend the QoS profile, TLLI and pdu type */ |
| 1232 | budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh)); |
Harald Welte | 8ef54d1 | 2012-06-17 09:31:16 +0800 | [diff] [blame] | 1233 | memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile)); |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1234 | budh->tlli = osmo_htonl(msgb_tlli(msg)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1235 | budh->pdu_type = BSSGP_PDUT_DL_UNITDATA; |
| 1236 | |
Harald Welte | 16c8dbb | 2010-05-17 23:30:01 +0200 | [diff] [blame] | 1237 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]); |
| 1238 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len); |
| 1239 | |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1240 | /* Identifiers down: BVCI, NSEI (in msgb->cb) */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1241 | |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 1242 | /* check if we have to go through per-ms flow control or can go |
| 1243 | * directly to the per-BSS flow control */ |
| 1244 | if (dup->fc) |
Harald Welte | d8b4769 | 2012-09-07 11:29:32 +0200 | [diff] [blame] | 1245 | return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc); |
Harald Welte | d11c059 | 2012-09-06 21:57:11 +0200 | [diff] [blame] | 1246 | else |
Harald Welte | d8b4769 | 2012-09-07 11:29:32 +0200 | [diff] [blame] | 1247 | return bssgp_fc_in(bctx->fc, msg, msg_len, NULL); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1248 | } |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1249 | |
| 1250 | /* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */ |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1251 | int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci, |
| 1252 | struct bssgp_paging_info *pinfo) |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1253 | { |
| 1254 | struct msgb *msg = bssgp_msgb_alloc(); |
| 1255 | struct bssgp_normal_hdr *bgph = |
| 1256 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1257 | uint16_t drx_params = osmo_htons(pinfo->drx_params); |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 1258 | uint8_t mi[GSM48_MID_MAX_SIZE]; |
| 1259 | int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 1260 | struct gsm48_ra_id ra; |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 1261 | |
| 1262 | if (imsi_len < 2) |
| 1263 | return -EINVAL; |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1264 | |
| 1265 | msgb_nsei(msg) = nsei; |
| 1266 | msgb_bvci(msg) = ns_bvci; |
| 1267 | |
| 1268 | if (pinfo->mode == BSSGP_PAGING_PS) |
| 1269 | bgph->pdu_type = BSSGP_PDUT_PAGING_PS; |
| 1270 | else |
| 1271 | bgph->pdu_type = BSSGP_PDUT_PAGING_CS; |
| 1272 | /* IMSI */ |
Harald Welte | a13fb75 | 2020-06-16 08:44:42 +0200 | [diff] [blame] | 1273 | /* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11, |
| 1274 | * but somehow gcc (8.2) is not smart enough to figure this out and claims that |
| 1275 | * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to |
| 1276 | * mi[131], which is wrong */ |
| 1277 | #pragma GCC diagnostic push |
| 1278 | #pragma GCC diagnostic ignored "-Warray-bounds" |
| 1279 | OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE); |
| 1280 | msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2); |
| 1281 | #pragma GCC diagnostic pop |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1282 | /* DRX Parameters */ |
| 1283 | msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2, |
| 1284 | (uint8_t *) &drx_params); |
| 1285 | /* Scope */ |
| 1286 | switch (pinfo->scope) { |
| 1287 | case BSSGP_PAGING_BSS_AREA: |
| 1288 | { |
| 1289 | uint8_t null = 0; |
| 1290 | msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null); |
| 1291 | } |
| 1292 | break; |
| 1293 | case BSSGP_PAGING_LOCATION_AREA: |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 1294 | gsm48_encode_ra(&ra, &pinfo->raid); |
| 1295 | msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra); |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1296 | break; |
| 1297 | case BSSGP_PAGING_ROUTEING_AREA: |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 1298 | bssgp_msgb_ra_put(msg, &pinfo->raid); |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1299 | break; |
| 1300 | case BSSGP_PAGING_BVCI: |
| 1301 | { |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1302 | uint16_t bvci = osmo_htons(pinfo->bvci); |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1303 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci); |
| 1304 | } |
| 1305 | break; |
| 1306 | } |
| 1307 | /* QoS profile mandatory for PS */ |
| 1308 | if (pinfo->mode == BSSGP_PAGING_PS) |
| 1309 | msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos); |
| 1310 | |
| 1311 | /* Optional (P-)TMSI */ |
| 1312 | if (pinfo->ptmsi) { |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1313 | uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi); |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1314 | msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi); |
| 1315 | } |
| 1316 | |
Alexander Couzens | 85a8fd3 | 2020-07-18 15:57:07 +0200 | [diff] [blame] | 1317 | return bssgp_ns_send(bssgp_ns_send_data, msg); |
Harald Welte | 68b4f03 | 2010-06-09 16:22:28 +0200 | [diff] [blame] | 1318 | } |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 1319 | |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 1320 | void bssgp_set_log_ss(int ss) |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 1321 | { |
| 1322 | DBSSGP = ss; |
| 1323 | } |
Alexander Couzens | acc0a07 | 2018-08-07 11:22:28 +0200 | [diff] [blame] | 1324 | |
| 1325 | /*! |
| 1326 | * \brief Flush the queue of the bssgp_flow_control |
| 1327 | * \param[in] The flow control object which holds the queue. |
| 1328 | */ |
| 1329 | void bssgp_fc_flush_queue(struct bssgp_flow_control *fc) |
| 1330 | { |
| 1331 | struct bssgp_fc_queue_element *element, *tmp; |
| 1332 | |
| 1333 | llist_for_each_entry_safe(element, tmp, &fc->queue, list) { |
| 1334 | msgb_free(element->msg); |
| 1335 | llist_del(&element->list); |
| 1336 | talloc_free(element); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | /*! |
| 1341 | * \brief Flush the queues of all BSSGP contexts. |
| 1342 | */ |
| 1343 | void bssgp_flush_all_queues() |
| 1344 | { |
| 1345 | struct bssgp_bvc_ctx *bctx; |
| 1346 | |
| 1347 | llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) { |
| 1348 | if (bctx->fc) |
| 1349 | bssgp_fc_flush_queue(bctx->fc); |
| 1350 | } |
| 1351 | } |