Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* gprs_bssgp_pcu.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <gprs_rlcmac.h> |
| 21 | #include <gprs_bssgp_pcu.h> |
| 22 | #include <pcu_l1_if.h> |
| 23 | |
| 24 | struct sgsn_instance *sgsn; |
| 25 | void *tall_bsc_ctx; |
| 26 | struct bssgp_bvc_ctx *bctx = btsctx_alloc(BVCI, NSEI); |
| 27 | |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 28 | int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 29 | { |
| 30 | struct bssgp_ud_hdr *budh; |
| 31 | int tfi; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 32 | uint32_t tlli; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 33 | int i = 0; |
Andreas Eversberg | 7d7cf54 | 2012-06-25 09:26:15 +0200 | [diff] [blame] | 34 | uint8_t trx, ts; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 35 | uint8_t *data; |
| 36 | uint16_t len; |
| 37 | struct gprs_rlcmac_tbf *tbf; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 38 | |
| 39 | budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 40 | tlli = ntohl(budh->tlli); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 41 | |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 42 | /* LLC_PDU is mandatory IE */ |
| 43 | if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 44 | { |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 45 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 46 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 47 | } |
Ivan Kluchnikov | b172b1b | 2012-06-07 01:51:49 +0400 | [diff] [blame] | 48 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 49 | data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 50 | len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU); |
| 51 | if (len > sizeof(tbf->llc_frame)) |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 52 | { |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 53 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD IE_LLC_PDU too large\n", tlli); |
| 54 | return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 55 | } |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 56 | LOGP(DBSSGP, LOGL_NOTICE, "LLC PDU = (TLLI=0x%08x) %s\n", tlli, osmo_hexdump(data, len)); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 57 | |
| 58 | uint16_t imsi_len = 0; |
| 59 | uint8_t *imsi; |
| 60 | if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) |
| 61 | { |
| 62 | imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 63 | imsi = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 64 | |
| 65 | LOGPC(DBSSGP, LOGL_NOTICE, " IMSI = "); |
| 66 | for (i = 0; i < imsi_len; i++) |
| 67 | { |
| 68 | LOGPC(DBSSGP, LOGL_NOTICE, "%02x", imsi[i]); |
| 69 | } |
| 70 | LOGPC(DBSSGP, LOGL_NOTICE, "\n"); |
| 71 | } |
| 72 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 73 | /* check for existing TBF */ |
| 74 | if ((tbf = tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF))) { |
| 75 | LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [DOWNLINK] APPEND TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli); |
| 76 | if (tbf->state == GPRS_RLCMAC_WAIT_RELEASE) { |
| 77 | LOGP(DRLCMAC, LOGL_NOTICE, "TBF in WAIT RELEASE state " |
| 78 | "(T3193), so reuse TBF\n"); |
| 79 | memcpy(tbf->llc_frame, data, len); |
| 80 | tbf->llc_length = len; |
| 81 | memset(&tbf->dir.dl, 0, sizeof(tbf->dir.dl)); /* reset |
| 82 | rlc states */ |
| 83 | gprs_rlcmac_trigger_downlink_assignment(tbf, 1); |
| 84 | } else { |
| 85 | /* the TBF exists, so we must write it in the queue */ |
| 86 | struct msgb *llc_msg = msgb_alloc(len, "llc_pdu_queue"); |
| 87 | if (!llc_msg) |
| 88 | return -ENOMEM; |
| 89 | memcpy(msgb_put(llc_msg, len), data, len); |
| 90 | msgb_enqueue(&tbf->llc_queue, llc_msg); |
| 91 | } |
| 92 | } else { |
| 93 | // Create new TBF |
| 94 | tfi = tfi_alloc(&trx, &ts); |
| 95 | if (tfi < 0) { |
| 96 | LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n"); |
| 97 | /* FIXME: send reject */ |
| 98 | return -EBUSY; |
| 99 | } |
| 100 | tbf = tbf_alloc(tfi, trx, ts); |
| 101 | tbf->direction = GPRS_RLCMAC_DL_TBF; |
| 102 | tbf->tlli = tlli; |
| 103 | tbf->tlli_valid = 1; |
| 104 | |
| 105 | LOGP(DRLCMAC, LOGL_NOTICE, "TBF: [DOWNLINK] START TFI: %u TLLI: 0x%08x \n", tbf->tfi, tbf->tlli); |
| 106 | |
| 107 | /* new TBF, so put first frame */ |
| 108 | memcpy(tbf->llc_frame, data, len); |
| 109 | tbf->llc_length = len; |
| 110 | |
| 111 | /* trigger downlink assignment and set state to ASSIGN. |
| 112 | * we don't use old_downlink, so the possible uplink is used |
| 113 | * to trigger downlink assignment. if there is no uplink, |
| 114 | * AGCH is used. */ |
| 115 | gprs_rlcmac_trigger_downlink_assignment(tbf, 0); |
| 116 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 117 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 118 | return 0; |
Ivan Kluchnikov | b172b1b | 2012-06-07 01:51:49 +0400 | [diff] [blame] | 119 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 120 | /* Receive a BSSGP PDU from a BSS on a PTP BVCI */ |
| 121 | int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
| 122 | { |
| 123 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 124 | uint8_t pdu_type = bgph->pdu_type; |
| 125 | unsigned rc = 0; |
| 126 | |
| 127 | /* If traffic is received on a BVC that is marked as blocked, the |
| 128 | * received PDU shall not be accepted and a STATUS PDU (Cause value: |
| 129 | * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ |
| 130 | if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) |
| 131 | { |
| 132 | uint16_t bvci = msgb_bvci(msg); |
| 133 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n"); |
| 134 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); |
| 135 | } |
| 136 | |
| 137 | switch (pdu_type) { |
| 138 | case BSSGP_PDUT_DL_UNITDATA: |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 139 | LOGP(DBSSGP, LOGL_NOTICE, "RX: [SGSN->PCU] BSSGP_PDUT_DL_UNITDATA\n"); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 140 | gprs_bssgp_pcu_rx_dl_ud(msg, tp); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 141 | break; |
| 142 | case BSSGP_PDUT_PAGING_PS: |
| 143 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); |
| 144 | break; |
| 145 | case BSSGP_PDUT_PAGING_CS: |
| 146 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); |
| 147 | break; |
| 148 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
| 149 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n"); |
| 150 | break; |
| 151 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 152 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n"); |
| 153 | break; |
| 154 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
| 155 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n"); |
| 156 | break; |
| 157 | default: |
| 158 | DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type); |
| 159 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 160 | break; |
| 161 | } |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | /* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */ |
| 166 | int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
| 167 | { |
| 168 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 169 | int rc = 0; |
| 170 | switch (bgph->pdu_type) { |
| 171 | case BSSGP_PDUT_STATUS: |
| 172 | /* Some exception has occurred */ |
| 173 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci); |
| 174 | /* FIXME: send NM_STATUS.ind to NM */ |
| 175 | break; |
| 176 | case BSSGP_PDUT_SUSPEND_ACK: |
| 177 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 178 | break; |
| 179 | case BSSGP_PDUT_SUSPEND_NACK: |
| 180 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_NACK\n"); |
| 181 | break; |
| 182 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 183 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_RESET_ACK\n"); |
| 184 | break; |
| 185 | case BSSGP_PDUT_PAGING_PS: |
| 186 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); |
| 187 | break; |
| 188 | case BSSGP_PDUT_PAGING_CS: |
| 189 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); |
| 190 | break; |
| 191 | case BSSGP_PDUT_RESUME_ACK: |
| 192 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_ACK\n"); |
| 193 | break; |
| 194 | case BSSGP_PDUT_RESUME_NACK: |
| 195 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_NACK\n"); |
| 196 | break; |
| 197 | case BSSGP_PDUT_FLUSH_LL: |
| 198 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLUSH_LL\n"); |
| 199 | break; |
| 200 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 201 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 202 | break; |
| 203 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 204 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n"); |
| 205 | break; |
| 206 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 207 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n"); |
| 208 | break; |
| 209 | default: |
| 210 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n", bctx->bvci, bgph->pdu_type); |
| 211 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 212 | break; |
| 213 | } |
| 214 | return rc; |
| 215 | } |
| 216 | |
| 217 | int gprs_bssgp_pcu_rcvmsg(struct msgb *msg) |
| 218 | { |
| 219 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 220 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 221 | struct tlv_parsed tp; |
| 222 | uint8_t pdu_type = bgph->pdu_type; |
| 223 | uint16_t ns_bvci = msgb_bvci(msg); |
| 224 | int data_len; |
| 225 | int rc = 0; |
| 226 | |
| 227 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 228 | |
| 229 | /* UNITDATA BSSGP headers have TLLI in front */ |
| 230 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA) |
| 231 | { |
| 232 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 233 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 238 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 239 | } |
| 240 | |
| 241 | /* look-up or create the BTS context for this BVC */ |
| 242 | bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg)); |
| 243 | |
| 244 | /* Only a RESET PDU can create a new BVC context */ |
| 245 | if (!bctx) |
| 246 | { |
| 247 | bctx = btsctx_alloc(ns_bvci, msgb_nsei(msg)); |
| 248 | } |
| 249 | |
| 250 | if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET_ACK) |
| 251 | { |
| 252 | LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU " |
| 253 | "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci, |
| 254 | pdu_type); |
| 255 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
| 256 | } |
| 257 | |
| 258 | if (bctx) |
| 259 | { |
| 260 | log_set_context(BSC_CTX_BVC, bctx); |
| 261 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); |
| 262 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg)); |
| 263 | } |
| 264 | |
| 265 | if (ns_bvci == BVCI_SIGNALLING) |
| 266 | { |
| 267 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); |
| 268 | rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx); |
| 269 | } |
| 270 | else if (ns_bvci == BVCI_PTM) |
| 271 | { |
| 272 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTM bssgp_tx_status\n"); |
| 273 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n"); |
| 278 | rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx); |
| 279 | } |
| 280 | return rc; |
| 281 | } |
Harald Welte | d679009 | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 282 | |
| 283 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 284 | { |
| 285 | return 0; |
| 286 | } |