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