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