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