Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */ |
| 2 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 75bb820 | 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 |
| 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 |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 25 | |
| 26 | #include <netinet/in.h> |
| 27 | |
| 28 | #include <osmocore/msgb.h> |
| 29 | #include <osmocore/tlv.h> |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
| 31 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
| 33 | #include <openbsc/gsm_data.h> |
| 34 | #include <openbsc/gsm_04_08_gprs.h> |
| 35 | #include <openbsc/gprs_bssgp.h> |
| 36 | #include <openbsc/gprs_llc.h> |
| 37 | #include <openbsc/gprs_ns.h> |
| 38 | |
| 39 | /* global pointer to the gsm network data structure */ |
| 40 | /* FIXME: this must go! */ |
| 41 | extern struct gsm_network *bsc_gsmnet; |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 42 | struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 43 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 44 | void *bssgp_tall_ctx = NULL; |
| 45 | |
Harald Welte | 4ddcf9b | 2010-05-02 09:34:20 +0200 | [diff] [blame] | 46 | /* BSSGP Protocol specific, not implementation specific */ |
| 47 | /* FIXME: This needs to go into libosmocore after finished */ |
| 48 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 49 | /* Chapter 11.3.9 / Table 11.10: Cause coding */ |
Harald Welte | 4ddcf9b | 2010-05-02 09:34:20 +0200 | [diff] [blame] | 50 | static const struct value_string bssgp_cause_strings[] = { |
| 51 | { BSSGP_CAUSE_PROC_OVERLOAD, "Processor overload" }, |
| 52 | { BSSGP_CAUSE_EQUIP_FAIL, "Equipment Failure" }, |
| 53 | { BSSGP_CAUSE_TRASIT_NET_FAIL, "Transit netowkr service failure" }, |
| 54 | { BSSGP_CAUSE_CAPA_GREATER_0KPBS,"Transmission capacity modified" }, |
| 55 | { BSSGP_CAUSE_UNKNOWN_MS, "Unknown MS" }, |
| 56 | { BSSGP_CAUSE_UNKNOWN_BVCI, "Unknown BVCI" }, |
| 57 | { BSSGP_CAUSE_CELL_TRAF_CONG, "Cell traffic congestion" }, |
| 58 | { BSSGP_CAUSE_SGSN_CONG, "SGSN congestion" }, |
| 59 | { BSSGP_CAUSE_OML_INTERV, "O&M intervention" }, |
| 60 | { BSSGP_CAUSE_BVCI_BLOCKED, "BVCI blocked" }, |
| 61 | { BSSGP_CAUSE_PFC_CREATE_FAIL, "PFC create failure" }, |
| 62 | { BSSGP_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 63 | { BSSGP_CAUSE_INV_MAND_INF, "Invalid mandatory information" }, |
| 64 | { BSSGP_CAUSE_MISSING_MAND_IE, "Missing mandatory IE" }, |
| 65 | { BSSGP_CAUSE_MISSING_COND_IE, "Missing conditional IE" }, |
| 66 | { BSSGP_CAUSE_UNEXP_COND_IE, "Unexpected conditional IE" }, |
| 67 | { BSSGP_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 68 | { BSSGP_CAUSE_PDU_INCOMP_STATE, "PDU incompatible with protocol state" }, |
| 69 | { BSSGP_CAUSE_PROTO_ERR_UNSPEC, "Protocol error - unspecified" }, |
| 70 | { BSSGP_CAUSE_PDU_INCOMP_FEAT, "PDU not compatible with feature set" }, |
| 71 | { 0, NULL }, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 72 | }; |
| 73 | |
Harald Welte | 4ddcf9b | 2010-05-02 09:34:20 +0200 | [diff] [blame] | 74 | const char *bssgp_cause_str(enum gprs_bssgp_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 75 | { |
Harald Welte | 4ddcf9b | 2010-05-02 09:34:20 +0200 | [diff] [blame] | 76 | return get_value_string(bssgp_cause_strings, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Harald Welte | 4ddcf9b | 2010-05-02 09:34:20 +0200 | [diff] [blame] | 79 | |
| 80 | /* Our actual implementation */ |
| 81 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 82 | #define BVC_F_BLOCKED 0x0001 |
| 83 | |
| 84 | /* The per-BTS context that we keep on the SGSN side of the BSSGP link */ |
| 85 | struct bssgp_bts_ctx { |
| 86 | struct llist_head list; |
| 87 | |
| 88 | /* parsed RA ID and Cell ID of the remote BTS */ |
| 89 | struct gprs_ra_id ra_id; |
| 90 | uint16_t cell_id; |
| 91 | |
| 92 | /* NSEI and BVCI of underlying Gb link. Together they |
| 93 | * uniquely identify a link to a BTS (5.4.4) */ |
| 94 | uint16_t bvci; |
| 95 | uint16_t nsei; |
| 96 | |
| 97 | uint32_t bvc_state; |
| 98 | |
| 99 | /* we might want to add this as a shortcut later, avoiding the NSVC |
| 100 | * lookup for every packet, similar to a routing cache */ |
| 101 | //struct gprs_nsvc *nsvc; |
| 102 | }; |
| 103 | LLIST_HEAD(bts_ctxts); |
| 104 | |
| 105 | /* Find a BTS Context based on parsed RA ID and Cell ID */ |
| 106 | struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid) |
| 107 | { |
| 108 | struct bssgp_bts_ctx *bctx; |
| 109 | |
| 110 | llist_for_each_entry(bctx, &bts_ctxts, list) { |
| 111 | if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) && |
| 112 | bctx->cell_id == cid) |
| 113 | return bctx; |
| 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | /* Find a BTS context based on BVCI+NSEI tuple */ |
| 119 | struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei) |
| 120 | { |
| 121 | struct bssgp_bts_ctx *bctx; |
| 122 | |
| 123 | llist_for_each_entry(bctx, &bts_ctxts, list) { |
| 124 | if (bctx->nsei == nsei && bctx->bvci == bvci) |
| 125 | return bctx; |
| 126 | } |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | struct bssgp_btx_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei) |
| 131 | { |
| 132 | struct bssgp_bts_ctx *ctx; |
| 133 | |
| 134 | ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx); |
| 135 | if (!ctx) |
| 136 | return NULL; |
| 137 | ctx->bvci = bvci; |
| 138 | ctx->nsei = nsei; |
| 139 | llist_add(&ctx->list, &bts_ctxts); |
| 140 | |
| 141 | return ctx; |
| 142 | } |
| 143 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 144 | static inline struct msgb *bssgp_msgb_alloc(void) |
| 145 | { |
| 146 | return msgb_alloc_headroom(4096, 128, "BSSGP"); |
| 147 | } |
| 148 | |
| 149 | /* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 150 | static int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei, |
| 151 | uint16_t bvci, uint16_t ns_bvci) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 152 | { |
| 153 | struct msgb *msg = bssgp_msgb_alloc(); |
| 154 | struct bssgp_normal_hdr *bgph = |
| 155 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 156 | uint16_t _bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 157 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 158 | msgb_nsei(msg) = nsei; |
| 159 | msgb_bvci(msg) = ns_bvci; |
| 160 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 161 | bgph->pdu_type = pdu_type; |
| 162 | _bvci = htons(bvci); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 163 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 164 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 165 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Chapter 10.4.5: Flow Control BVC ACK */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 169 | static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 170 | { |
| 171 | struct msgb *msg = bssgp_msgb_alloc(); |
| 172 | struct bssgp_normal_hdr *bgph = |
| 173 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 174 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 175 | msgb_nsei(msg) = nsei; |
| 176 | msgb_bvci(msg) = ns_bvci; |
| 177 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 178 | bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK; |
| 179 | msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag); |
| 180 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 181 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Chapter 10.4.14: Status */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 185 | int bssgp_tx_status(uint8_t cause, uint16_t *bvci, struct msgb *orig_msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 186 | { |
| 187 | struct msgb *msg = bssgp_msgb_alloc(); |
| 188 | struct bssgp_normal_hdr *bgph = |
| 189 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 190 | |
| 191 | DEBUGPC(DGPRS, "BSSGP: TX STATUS, cause=%s\n", bssgp_cause_str(cause)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 192 | msgb_nsei(msg) = msgb_nsei(orig_msg); |
| 193 | msgb_bvci(msg) = 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 194 | |
| 195 | bgph->pdu_type = BSSGP_PDUT_STATUS; |
| 196 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 197 | if (bvci) { |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 198 | uint16_t _bvci = htons(*bvci); |
| 199 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 200 | } |
| 201 | if (orig_msg) |
| 202 | msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR, |
Harald Welte | 8637cb0 | 2010-05-02 21:29:36 +0200 | [diff] [blame] | 203 | msgb_bssgp_len(orig_msg), msgb_bssgph(orig_msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 204 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 205 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 208 | uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf) |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 209 | { |
| 210 | /* 6 octets RAC */ |
| 211 | gsm48_parse_ra(raid, buf); |
| 212 | /* 2 octets CID */ |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 213 | return ntohs(*(uint16_t *) (buf+6)); |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 216 | /* Chapter 8.4 BVC-Reset Procedure */ |
| 217 | static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp, |
| 218 | uint16_t ns_bvci) |
| 219 | { |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 220 | struct bssgp_bts_ctx *bctx; |
| 221 | uint16_t nsei = msgb_nsei(msg); |
| 222 | uint16_t bvci; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 223 | int rc; |
| 224 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 225 | bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI)); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 226 | DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci, |
| 227 | bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE))); |
| 228 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 229 | /* look-up or create the BTS context for this BVC */ |
| 230 | bctx = btsctx_by_bvci_nsei(bvci, nsei); |
| 231 | if (!bctx) |
| 232 | bctx = btsctx_alloc(bvci, nsei); |
| 233 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 234 | /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS |
| 235 | * informs us about its RAC + Cell ID, so we can create a mapping */ |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 236 | if (bvci != 0 && bvci != 1) { |
| 237 | if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) { |
| 238 | LOGP(DGPRS, LOGL_ERROR, "BSSGP RESET BVCI=%u " |
| 239 | "missing mandatory IE\n", bvci); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | /* actually extract RAC / CID */ |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 243 | bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id, |
| 244 | TLVP_VAL(tp, BSSGP_IE_CELL_ID)); |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 245 | LOGP(DGPRS, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n", |
| 246 | bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac, |
| 247 | bctx->ra_id.rac, bctx->cell_id, bvci); |
| 248 | } |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 249 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 250 | /* Acknowledge the RESET to the BTS */ |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 251 | rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 252 | nsei, bvci, ns_bvci); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 256 | /* Uplink unit-data */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 257 | static int bssgp_rx_ul_ud(struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 258 | { |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 259 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
Harald Welte | 8637cb0 | 2010-05-02 21:29:36 +0200 | [diff] [blame] | 260 | int data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 261 | struct tlv_parsed tp; |
| 262 | int rc; |
| 263 | |
| 264 | DEBUGP(DGPRS, "BSSGP UL-UD\n"); |
| 265 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 266 | /* extract TLLI and parse TLV IEs */ |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 267 | msgb_tlli(msg) = ntohl(budh->tlli); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 268 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 269 | |
| 270 | /* Cell ID and LLC_PDU are the only mandatory IE */ |
| 271 | if (!TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID) || |
| 272 | !TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU)) |
| 273 | return -EIO; |
| 274 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 275 | /* FIXME: lookup bssgp_bts_ctx based on BVCI + NSEI */ |
| 276 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 277 | /* store pointer to LLC header and CELL ID in msgb->cb */ |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 278 | msgb_llch(msg) = TLVP_VAL(&tp, BSSGP_IE_LLC_PDU); |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 279 | msgb_bcid(msg) = TLVP_VAL(&tp, BSSGP_IE_CELL_ID); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 280 | |
| 281 | return gprs_llc_rcvmsg(msg, &tp); |
| 282 | } |
| 283 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 284 | static int bssgp_rx_suspend(struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 285 | { |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 286 | struct bssgp_normal_hdr *bgph = |
| 287 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 8637cb0 | 2010-05-02 21:29:36 +0200 | [diff] [blame] | 288 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 289 | struct tlv_parsed tp; |
| 290 | int rc; |
| 291 | |
| 292 | DEBUGP(DGPRS, "BSSGP SUSPEND\n"); |
| 293 | |
| 294 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 295 | if (rc < 0) |
| 296 | return rc; |
| 297 | |
| 298 | if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) || |
| 299 | !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) |
| 300 | return -EIO; |
| 301 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 302 | /* FIXME: pass the SUSPEND request to GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 303 | /* SEND SUSPEND_ACK or SUSPEND_NACK */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 306 | static int bssgp_rx_resume(struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 307 | { |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 308 | struct bssgp_normal_hdr *bgph = |
| 309 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 8637cb0 | 2010-05-02 21:29:36 +0200 | [diff] [blame] | 310 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 311 | struct tlv_parsed tp; |
| 312 | int rc; |
| 313 | |
| 314 | DEBUGP(DGPRS, "BSSGP RESUME\n"); |
| 315 | |
| 316 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 317 | if (rc < 0) |
| 318 | return rc; |
| 319 | |
| 320 | if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) || |
| 321 | !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA) || |
| 322 | !TLVP_PRESENT(&tp, BSSGP_IE_SUSPEND_REF_NR)) |
| 323 | return -EIO; |
| 324 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 325 | /* FIXME: pass the RESUME request to GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 326 | /* SEND RESUME_ACK or RESUME_NACK */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 329 | static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 330 | { |
| 331 | |
| 332 | DEBUGP(DGPRS, "BSSGP FC BVC\n"); |
| 333 | |
| 334 | if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) || |
| 335 | !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) || |
| 336 | !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) || |
| 337 | !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) || |
| 338 | !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) |
| 339 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 340 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 341 | /* FIXME: actually implement flow control */ |
| 342 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 343 | /* Send FLOW_CONTROL_BVC_ACK */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 344 | return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG), |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 345 | msgb_bvci(msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 346 | } |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 347 | |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 348 | /* We expect msgb_bssgph() to point to the BSSGP header */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 349 | int gprs_bssgp_rcvmsg(struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 350 | { |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 351 | struct bssgp_normal_hdr *bgph = |
| 352 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 353 | struct tlv_parsed tp; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 354 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | 8637cb0 | 2010-05-02 21:29:36 +0200 | [diff] [blame] | 355 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 356 | uint16_t bvci; /* PTP BVCI */ |
| 357 | uint16_t ns_bvci = msgb_bvci(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 358 | int rc = 0; |
| 359 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 360 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 361 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 362 | /* UNITDATA BSSGP headers have TLLI in front */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 363 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && |
| 364 | pdu_type != BSSGP_PDUT_DL_UNITDATA) |
| 365 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 366 | |
| 367 | switch (pdu_type) { |
| 368 | case BSSGP_PDUT_UL_UNITDATA: |
| 369 | /* some LLC data from the MS */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 370 | rc = bssgp_rx_ul_ud(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 371 | break; |
| 372 | case BSSGP_PDUT_RA_CAPABILITY: |
| 373 | /* BSS requests RA capability or IMSI */ |
| 374 | DEBUGP(DGPRS, "BSSGP RA CAPABILITY UPDATE\n"); |
| 375 | /* FIXME: send RA_CAPA_UPDATE_ACK */ |
| 376 | break; |
| 377 | case BSSGP_PDUT_RADIO_STATUS: |
| 378 | DEBUGP(DGPRS, "BSSGP RADIO STATUS\n"); |
| 379 | /* BSS informs us of some exception */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 380 | /* FIXME: notify GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 381 | break; |
| 382 | case BSSGP_PDUT_SUSPEND: |
| 383 | /* MS wants to suspend */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 384 | rc = bssgp_rx_suspend(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 385 | break; |
| 386 | case BSSGP_PDUT_RESUME: |
| 387 | /* MS wants to resume */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 388 | rc = bssgp_rx_resume(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 389 | break; |
| 390 | case BSSGP_PDUT_FLUSH_LL: |
| 391 | /* BSS informs MS has moved to one cell to other cell */ |
| 392 | DEBUGP(DGPRS, "BSSGP FLUSH LL\n"); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 393 | /* FIXME: notify GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 394 | /* Send FLUSH_LL_ACK */ |
| 395 | break; |
| 396 | case BSSGP_PDUT_LLC_DISCARD: |
| 397 | /* BSS informs that some LLC PDU's have been discarded */ |
| 398 | DEBUGP(DGPRS, "BSSGP LLC DISCARDED\n"); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 399 | /* FIXME: notify GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 400 | break; |
| 401 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
| 402 | /* BSS informs us of available bandwidth in Gb interface */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 403 | rc = bssgp_rx_fc_bvc(msg, &tp); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 404 | break; |
| 405 | case BSSGP_PDUT_FLOW_CONTROL_MS: |
| 406 | /* BSS informs us of available bandwidth to one MS */ |
| 407 | DEBUGP(DGPRS, "BSSGP FC MS\n"); |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 408 | /* FIXME: actually implement flow control */ |
| 409 | /* FIXME: Send FLOW_CONTROL_MS_ACK */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 410 | break; |
| 411 | case BSSGP_PDUT_BVC_BLOCK: |
| 412 | /* BSS tells us that BVC shall be blocked */ |
| 413 | DEBUGP(DGPRS, "BSSGP BVC BLOCK "); |
| 414 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) || |
| 415 | !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) |
| 416 | goto err_mand_ie; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 417 | bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 418 | DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci, |
| 419 | bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE))); |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 420 | /* We always acknowledge the BLOCKing */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 421 | rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 422 | msgb_nsei(msg), bvci, ns_bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | break; |
| 424 | case BSSGP_PDUT_BVC_UNBLOCK: |
| 425 | /* BSS tells us that BVC shall be unblocked */ |
| 426 | DEBUGP(DGPRS, "BSSGP BVC UNBLOCK "); |
| 427 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) |
| 428 | goto err_mand_ie; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 429 | bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 430 | DEBUGPC(DGPRS, "BVCI=%u\n", bvci); |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 431 | /* We always acknowledge the unBLOCKing */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 432 | rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 433 | msgb_nsei(msg), bvci, ns_bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 434 | break; |
| 435 | case BSSGP_PDUT_BVC_RESET: |
| 436 | /* BSS tells us that BVC init is required */ |
| 437 | DEBUGP(DGPRS, "BSSGP BVC RESET "); |
| 438 | if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) || |
| 439 | !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) |
| 440 | goto err_mand_ie; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 441 | rc = bssgp_rx_bvc_reset(msg, &tp, ns_bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 442 | break; |
| 443 | case BSSGP_PDUT_STATUS: |
| 444 | /* Some exception has occurred */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 445 | /* FIXME: notify GMM */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | case BSSGP_PDUT_DOWNLOAD_BSS_PFC: |
| 447 | case BSSGP_PDUT_CREATE_BSS_PFC_ACK: |
| 448 | case BSSGP_PDUT_CREATE_BSS_PFC_NACK: |
| 449 | case BSSGP_PDUT_MODIFY_BSS_PFC: |
| 450 | case BSSGP_PDUT_DELETE_BSS_PFC_ACK: |
| 451 | DEBUGP(DGPRS, "BSSGP PDU type 0x%02x not [yet] implemented\n", |
| 452 | pdu_type); |
| 453 | break; |
| 454 | /* those only exist in the SGSN -> BSS direction */ |
| 455 | case BSSGP_PDUT_DL_UNITDATA: |
| 456 | case BSSGP_PDUT_PAGING_PS: |
| 457 | case BSSGP_PDUT_PAGING_CS: |
| 458 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
| 459 | case BSSGP_PDUT_SUSPEND_ACK: |
| 460 | case BSSGP_PDUT_SUSPEND_NACK: |
| 461 | case BSSGP_PDUT_RESUME_ACK: |
| 462 | case BSSGP_PDUT_RESUME_NACK: |
| 463 | case BSSGP_PDUT_FLUSH_LL_ACK: |
| 464 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 465 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
| 466 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 467 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 468 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 469 | DEBUGP(DGPRS, "BSSGP PDU type 0x%02x only exists in DL\n", |
| 470 | pdu_type); |
| 471 | rc = -EINVAL; |
| 472 | break; |
| 473 | default: |
| 474 | DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type); |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | return rc; |
| 479 | err_mand_ie: |
| 480 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 481 | } |
| 482 | |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 483 | /* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 484 | * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */ |
| 485 | int gprs_bssgp_tx_dl_ud(struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 486 | { |
Harald Welte | f897265 | 2010-05-02 09:23:16 +0200 | [diff] [blame] | 487 | struct bssgp_bts_ctx *bctx; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 488 | struct bssgp_ud_hdr *budh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 489 | uint8_t llc_pdu_tlv_hdr_len = 2; |
| 490 | uint8_t *llc_pdu_tlv, *qos_profile; |
| 491 | uint16_t pdu_lifetime = 1000; /* centi-seconds */ |
| 492 | uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 }; |
| 493 | uint16_t msg_len = msg->len; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 494 | uint16_t bvci = msgb_bvci(msg); |
| 495 | uint16_t nsei = msgb_nsei(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 496 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 497 | /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */ |
| 498 | if (bvci < 2) { |
| 499 | LOGP(DGPRS, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n", |
| 500 | bvci); |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
| 504 | bctx = btsctx_by_bvci_nsei(bvci, nsei); |
| 505 | if (!bctx) |
| 506 | bctx = btsctx_alloc(bvci, nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 507 | |
| 508 | if (msg->len > TVLV_MAX_ONEBYTE) |
| 509 | llc_pdu_tlv_hdr_len += 1; |
| 510 | |
| 511 | /* prepend the tag and length of the LLC-PDU TLV */ |
| 512 | llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len); |
| 513 | llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU; |
| 514 | if (llc_pdu_tlv_hdr_len > 2) { |
| 515 | llc_pdu_tlv[1] = msg_len >> 8; |
| 516 | llc_pdu_tlv[2] = msg_len & 0xff; |
| 517 | } else { |
| 518 | llc_pdu_tlv[1] = msg_len & 0x3f; |
| 519 | llc_pdu_tlv[1] |= 0x80; |
| 520 | } |
| 521 | |
| 522 | /* FIXME: optional elements */ |
| 523 | |
| 524 | /* prepend the pdu lifetime */ |
| 525 | pdu_lifetime = htons(pdu_lifetime); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 526 | msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 527 | |
| 528 | /* prepend the QoS profile, TLLI and pdu type */ |
| 529 | budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh)); |
| 530 | memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default)); |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 531 | budh->tlli = htonl(msgb_tlli(msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 532 | budh->pdu_type = BSSGP_PDUT_DL_UNITDATA; |
| 533 | |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 534 | /* Identifiers down: BVCI, NSEI (in msgb->cb) */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 535 | |
| 536 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 537 | } |