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