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