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 | |
| 28 | int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg) |
| 29 | { |
| 30 | struct bssgp_ud_hdr *budh; |
| 31 | int tfi; |
| 32 | int data_index = 0; |
| 33 | int i = 0; |
| 34 | int pdu_index = 0; |
| 35 | |
| 36 | budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg); |
| 37 | struct gprs_rlcmac_tbf *tbf; |
| 38 | // Create new TBF |
| 39 | tfi = tfi_alloc(); |
| 40 | if (tfi < 0) { |
| 41 | return tfi; |
| 42 | } |
| 43 | tbf = tbf_alloc(tfi); |
| 44 | tbf->direction = GPRS_RLCMAC_DL_TBF; |
| 45 | tbf->state = GPRS_RLCMAC_WAIT_DATA_SEQ_START; |
| 46 | tbf->tlli = ntohl(budh->tlli); |
| 47 | |
| 48 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP TLLI=0x%08x \n", ntohl(budh->tlli)); |
| 49 | for (i = 4; i < MAX_LEN_PDU; i++) |
| 50 | { |
| 51 | //LOGP(DBSSGP, LOGL_NOTICE, "SERCH data = -0x%02x\n", budh ->data[i]); |
| 52 | if(budh->data[i] == IE_PDU) |
| 53 | { |
| 54 | pdu_index = i + 2; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | for (i = pdu_index; i < pdu_index + (budh->data[pdu_index-1]&0x7f); i++) |
| 59 | { |
| 60 | //LOGP(DBSSGP, LOGL_NOTICE, "-0x%02x\n", budh ->data[i]); |
| 61 | tbf->rlc_data[data_index] = budh->data[i]; |
| 62 | data_index++; |
| 63 | } |
| 64 | DEBUGP(DBSSGP, "BSSGP Catch from SGSN=%u octets. Send it to OpenBTS.\n", data_index); |
| 65 | gsmtap_send_llc(tbf->rlc_data,data_index); |
| 66 | tbf->data_index = data_index; |
Ivan Kluchnikov | c320d86 | 2012-03-18 15:04:48 +0400 | [diff] [blame] | 67 | |
| 68 | gprs_rlcmac_downlink_assignment(tbf); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Receive a BSSGP PDU from a BSS on a PTP BVCI */ |
| 72 | int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
| 73 | { |
| 74 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 75 | uint8_t pdu_type = bgph->pdu_type; |
| 76 | unsigned rc = 0; |
| 77 | |
| 78 | /* If traffic is received on a BVC that is marked as blocked, the |
| 79 | * received PDU shall not be accepted and a STATUS PDU (Cause value: |
| 80 | * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ |
| 81 | if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) |
| 82 | { |
| 83 | uint16_t bvci = msgb_bvci(msg); |
| 84 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n"); |
| 85 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); |
| 86 | } |
| 87 | |
| 88 | switch (pdu_type) { |
| 89 | case BSSGP_PDUT_DL_UNITDATA: |
| 90 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_DL_UNITDATA\n"); |
| 91 | gprs_bssgp_pcu_rx_dl_ud(msg); |
| 92 | break; |
| 93 | case BSSGP_PDUT_PAGING_PS: |
| 94 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); |
| 95 | break; |
| 96 | case BSSGP_PDUT_PAGING_CS: |
| 97 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); |
| 98 | break; |
| 99 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
| 100 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n"); |
| 101 | break; |
| 102 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 103 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n"); |
| 104 | break; |
| 105 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
| 106 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n"); |
| 107 | break; |
| 108 | default: |
| 109 | DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type); |
| 110 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 111 | break; |
| 112 | } |
| 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | /* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */ |
| 117 | int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
| 118 | { |
| 119 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 120 | int rc = 0; |
| 121 | switch (bgph->pdu_type) { |
| 122 | case BSSGP_PDUT_STATUS: |
| 123 | /* Some exception has occurred */ |
| 124 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci); |
| 125 | /* FIXME: send NM_STATUS.ind to NM */ |
| 126 | break; |
| 127 | case BSSGP_PDUT_SUSPEND_ACK: |
| 128 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 129 | break; |
| 130 | case BSSGP_PDUT_SUSPEND_NACK: |
| 131 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_NACK\n"); |
| 132 | break; |
| 133 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 134 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_RESET_ACK\n"); |
| 135 | break; |
| 136 | case BSSGP_PDUT_PAGING_PS: |
| 137 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); |
| 138 | break; |
| 139 | case BSSGP_PDUT_PAGING_CS: |
| 140 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); |
| 141 | break; |
| 142 | case BSSGP_PDUT_RESUME_ACK: |
| 143 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_ACK\n"); |
| 144 | break; |
| 145 | case BSSGP_PDUT_RESUME_NACK: |
| 146 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_NACK\n"); |
| 147 | break; |
| 148 | case BSSGP_PDUT_FLUSH_LL: |
| 149 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLUSH_LL\n"); |
| 150 | break; |
| 151 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 152 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 153 | break; |
| 154 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 155 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n"); |
| 156 | break; |
| 157 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 158 | LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n"); |
| 159 | break; |
| 160 | default: |
| 161 | DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n", bctx->bvci, bgph->pdu_type); |
| 162 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 163 | break; |
| 164 | } |
| 165 | return rc; |
| 166 | } |
| 167 | |
| 168 | int gprs_bssgp_pcu_rcvmsg(struct msgb *msg) |
| 169 | { |
| 170 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 171 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 172 | struct tlv_parsed tp; |
| 173 | uint8_t pdu_type = bgph->pdu_type; |
| 174 | uint16_t ns_bvci = msgb_bvci(msg); |
| 175 | int data_len; |
| 176 | int rc = 0; |
| 177 | |
| 178 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 179 | |
| 180 | /* UNITDATA BSSGP headers have TLLI in front */ |
| 181 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA) |
| 182 | { |
| 183 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 184 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 189 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 190 | } |
| 191 | |
| 192 | /* look-up or create the BTS context for this BVC */ |
| 193 | bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg)); |
| 194 | |
| 195 | /* Only a RESET PDU can create a new BVC context */ |
| 196 | if (!bctx) |
| 197 | { |
| 198 | bctx = btsctx_alloc(ns_bvci, msgb_nsei(msg)); |
| 199 | } |
| 200 | |
| 201 | if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET_ACK) |
| 202 | { |
| 203 | LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU " |
| 204 | "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci, |
| 205 | pdu_type); |
| 206 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
| 207 | } |
| 208 | |
| 209 | if (bctx) |
| 210 | { |
| 211 | log_set_context(BSC_CTX_BVC, bctx); |
| 212 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); |
| 213 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg)); |
| 214 | } |
| 215 | |
| 216 | if (ns_bvci == BVCI_SIGNALLING) |
| 217 | { |
| 218 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); |
| 219 | rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx); |
| 220 | } |
| 221 | else if (ns_bvci == BVCI_PTM) |
| 222 | { |
| 223 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTM bssgp_tx_status\n"); |
| 224 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n"); |
| 229 | rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx); |
| 230 | } |
| 231 | return rc; |
| 232 | } |