From 5c2f9fbfa3e4391ab05279d9e710b1bcd46af3a8 Mon Sep 17 00:00:00 2001 From: Ivan Kluchnikov Date: Sun, 5 Feb 2012 02:27:17 +0400 Subject: [PATCH] Added RLC/MAC application. - Segmentation of upper layer PDUs into RLC data blocks and re-assembly of RLC data blocks into upper layer PDUs. - Segmentation of RLC/MAC control messages into RLC/MAC control blocks and re-assembly of RLC/MAC control messages from RLC/MAC control blocks. - NS and BSSGP protocol implementation for communication with OpenSGSN. - UDPSocket for communication with OpenBTS. --- GPRSSocket.cpp | 236 ++++++++++++++++++++++++++++++++ GPRSSocket.h | 33 +++++ Makefile.am | 41 +++++- RLCMAC.cpp | 26 ++++ bssgp.cpp | 363 +++++++++++++++++++++++++++++++++++++++++++++++++ bssgp.h | 53 ++++++++ 6 files changed, 748 insertions(+), 4 deletions(-) create mode 100644 GPRSSocket.cpp create mode 100644 GPRSSocket.h create mode 100644 RLCMAC.cpp create mode 100644 bssgp.cpp create mode 100644 bssgp.h diff --git a/GPRSSocket.cpp b/GPRSSocket.cpp new file mode 100644 index 00000000..f3634787 --- /dev/null +++ b/GPRSSocket.cpp @@ -0,0 +1,236 @@ +/*GPRSSocket.cpp + * + * Copyright (C) 2011 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include +#include "GPRSSocket.h" +#include "gsm_rlcmac.h" +#include "bssgp.h" + +#define MAX_UDP_LENGTH 1500 + +#define RLCMAC_DATA_BLOCK 0 +#define RLCMAC_CONTROL_BLOCK 1 + +// TODO: We should take ports and IP from config. +UDPSocket GPRSRLCMACSocket(5070, "127.0.0.1", 5934); + +void sendToOpenBTS(BitVector * vector) +{ + char buffer[MAX_UDP_LENGTH]; + int ofs = 0; + vector->pack((unsigned char*)&buffer[ofs]); + ofs += vector->size() >> 3; + COUT("Send to OpenBTS: " << *vector); + GPRSRLCMACSocket.write(buffer, ofs); +} + +void writePDassignment(BitVector * dest, uint8_t TFI, uint32_t TLLI) +{ + // TODO We should use our implementation of encode RLC/MAC Control messages. + unsigned wp = 0; + dest->writeField(wp,0x1,2); // Payload Type + dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber + dest->writeField(wp,0x1,1); // Suppl/Polling Bit + dest->writeField(wp,0x1,3); // Uplink state flag + dest->writeField(wp,0x2,6); // MESSAGE TYPE + dest->writeField(wp,0x0,2); // Page Mode + + dest->writeField(wp,0x0,1); // switch PERSIST_LEVEL: off + dest->writeField(wp,0x2,2); // switch TLLI : on + dest->writeField(wp,TLLI,32); // TLLI + + dest->writeField(wp,0x0,1); // Message escape + dest->writeField(wp,0x0,2); // Medium Access Method: Dynamic Allocation + dest->writeField(wp,0x0,1); // RLC acknowledged mode + + dest->writeField(wp,0x0,1); // the network establishes no new downlink TBF for the mobile station + dest->writeField(wp,0x1,8); // timeslot 7 + dest->writeField(wp,0x1,8); // TIMING_ADVANCE_INDEX + + dest->writeField(wp,0x0,1); // switch TIMING_ADVANCE_VALUE = off + dest->writeField(wp,0x1,1); // switch TIMING_ADVANCE_INDEX = on + dest->writeField(wp,0xC,4); // TIMING_ADVANCE_INDEX + dest->writeField(wp,0x7,3); // TIMING_ADVANCE_TIMESLOT_NUMBER + + dest->writeField(wp,0x0,1); // switch POWER CONTROL = off + dest->writeField(wp,0x1,1); // Frequency Parameters information elements = present + + dest->writeField(wp,0x2,3); // Training Sequence Code (TSC) = 2 + dest->writeField(wp,0x1,2); // Indirect encoding struct = present + dest->writeField(wp,0x0,6); // MAIO + dest->writeField(wp,0xE,4); // MA_Number + dest->writeField(wp,0x8,4); // CHANGE_MARK_1 CHANGE_MARK_2 + + dest->writeField(wp,0x1,1); // switch TFI : on + dest->writeField(wp,0x14,5);// TFI + + dest->writeField(wp,0x1,1); // Power Control Parameters IE = present + dest->writeField(wp,0x0,4); // ALPHA power control parameter + dest->writeField(wp,0x0,1); // switch GAMMA_TN0 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN1 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN2 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN3 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN4 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN5 = off + dest->writeField(wp,0x0,1); // switch GAMMA_TN6 = off + dest->writeField(wp,0x1,1); // switch GAMMA_TN7 = on + dest->writeField(wp,0x0,5); // GAMMA_TN7 + + dest->writeField(wp,0x0,1); // TBF Starting TIME IE not present + dest->writeField(wp,0x0,1); // Measurement Mapping struct not present +} + +void writePUack(BitVector * dest, uint8_t TFI, uint32_t TLLI, unsigned CV, unsigned BSN) +{ + // TODO We should use our implementation of encode RLC/MAC Control messages. + unsigned wp = 0; + dest->writeField(wp,0x1,2); // payload + dest->writeField(wp,0x0,2); // Uplink block with TDMA framenumber + if (CV == 0) dest->writeField(wp,0x1,1); // Suppl/Polling Bit + else dest->writeField(wp,0x0,1); //Suppl/Polling Bit + dest->writeField(wp,0x1,3); // Uplink state flag + + //dest->writeField(wp,0x0,1); // Reduced block sequence number + //dest->writeField(wp,BSN+6,5); // Radio transaction identifier + //dest->writeField(wp,0x1,1); // Final segment + //dest->writeField(wp,0x1,1); // Address control + + //dest->writeField(wp,0x0,2); // Power reduction: 0 + //dest->writeField(wp,TFI,5); // Temporary flow identifier + //dest->writeField(wp,0x1,1); // Direction + + dest->writeField(wp,0x09,6); // MESSAGE TYPE + dest->writeField(wp,0x0,2); // Page Mode + + dest->writeField(wp,0x0,2); + dest->writeField(wp,TFI,5); // Uplink TFI + dest->writeField(wp,0x0,1); + + dest->writeField(wp,0x0,2); // CS1 + if (CV == 0) dest->writeField(wp,0x1,1); // FINAL_ACK_INDICATION + else dest->writeField(wp,0x0,1); // FINAL_ACK_INDICATION + dest->writeField(wp,BSN+1,7); // STARTING_SEQUENCE_NUMBER + // RECEIVE_BLOCK_BITMAP + for (unsigned i=0; i<8; i++) { + dest->writeField(wp,0xff,8); + } + dest->writeField(wp,0x1,1); // CONTENTION_RESOLUTION_TLLI = present + dest->writeField(wp,TLLI,8*4); + dest->writeField(wp,0x00,4); //spare +} + +void RLCMACDispatchMessage(BitVector *vector) +{ + static uint8_t rlc_data[60]; + static uint8_t tfi = 0; + static uint32_t tlli = 0; + static unsigned dataIndex = 0; + static unsigned startDispatch = 0; + unsigned blockDataLen = 0; + unsigned readIndex = 0; + unsigned payload = vector->readField(readIndex, 2); + + switch (payload) { + case RLCMAC_DATA_BLOCK: + { + COUT("RLCMAC_DATA_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); + RlcMacUplinkDataBlock_t * dataBlock = (RlcMacUplinkDataBlock_t *)malloc(sizeof(RlcMacUplinkDataBlock_t)); + decode_gsm_rlcmac_uplink_data(vector, dataBlock); + COUT("RLCMAC_DATA_BLOCK_END------------------------------"); + //TODO Implement other cases. + if (dataBlock->BSN == 0) + { + startDispatch = 1; + } + if (startDispatch) + { + tfi = dataBlock->TFI; + if (dataBlock->E_1 == 0) // Extension octet follows immediately + { + blockDataLen = dataBlock->LENGTH_INDICATOR[0]; + } + else + { + blockDataLen = 20; + if(dataBlock->TI == 1) // TLLI field is present + { + tlli = dataBlock->TLLI; + blockDataLen -= 4; + if (dataBlock->PI == 1) // PFI is present if TI field indicates presence of TLLI + { + blockDataLen -= 1; + } + } + } + unsigned dataOctetNum = 0; + for (unsigned i = dataIndex; i < dataIndex + blockDataLen; i++) + { + rlc_data[i] = dataBlock->RLC_DATA[dataOctetNum]; + dataOctetNum++; + } + dataIndex += blockDataLen; + BitVector packetUplinkAck(23*8); + packetUplinkAck.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + writePUack(&packetUplinkAck, tfi, tlli, dataBlock->CV, dataBlock->BSN); + COUT("RLCMAC_CONTROL_BLOCK>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + RlcMacDownlink_t * pUA = (RlcMacDownlink_t *)malloc(sizeof(RlcMacUplink_t)); + decode_gsm_rlcmac_downlink(&packetUplinkAck, pUA); + free(pUA); + COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); + sendToOpenBTS(&packetUplinkAck); + } + if (dataBlock->CV == 0) + { + sendToSGSN(tfi, tlli, rlc_data, dataIndex); + dataIndex = 0; + startDispatch = 0; + } + free(dataBlock); + } + break; + case RLCMAC_CONTROL_BLOCK: + { + COUT("RLCMAC_CONTROL_BLOCK<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); + RlcMacUplink_t * controlBlock = (RlcMacUplink_t *)malloc(sizeof(RlcMacUplink_t)); + decode_gsm_rlcmac_uplink(vector, controlBlock); + free(controlBlock); + COUT("RLCMAC_CONTROL_BLOCK_END------------------------------"); + } + break; + default: + COUT("Unknown RLCMAC block payload\n"); + } +} + +void *RLCMACSocket(void *) +{ + BitVector *vector = new BitVector(23*8); + GPRSRLCMACSocket.nonblocking(); + while (1) { + char buf[MAX_UDP_LENGTH]; + int count = GPRSRLCMACSocket.read(buf, 3000); + if (count>0) { + vector->unpack((const unsigned char*)buf); + COUT("Recieve from OpenBTS (MS): " << *vector); + RLCMACDispatchMessage(vector); + } + } +} diff --git a/GPRSSocket.h b/GPRSSocket.h new file mode 100644 index 00000000..cd31f99e --- /dev/null +++ b/GPRSSocket.h @@ -0,0 +1,33 @@ +/*GPRSSocket.h + * + * Copyright (C) 2011 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef GPRSSOCKET_H +#define GPRSSOCKET_H + +#include + +void sendToOpenBTS(BitVector * vector); + +void writePUack(BitVector * dest, uint8_t TFI, uint32_t TLLI, unsigned CV, unsigned BSN); + +void RLCMACDispatchMessage(BitVector *vector); + +void *RLCMACSocket(void *); + +#endif // GPRSSOCKET_H diff --git a/Makefile.am b/Makefile.am index f479b5b9..3808f835 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,19 +27,52 @@ noinst_LTLIBRARIES = libgprs.la libgprs_la_SOURCES = \ csn1.cpp \ - gsm_rlcmac.cpp + gsm_rlcmac.cpp \ + bssgp.cpp \ + GPRSSocket.cpp noinst_PROGRAMS = \ - RLCMACTest + RLCMACTest \ + RLCMAC noinst_HEADERS = \ csn1.h \ - gsm_rlcmac.h + gsm_rlcmac.h \ + bssgp.h \ + GPRSSocket.h RLCMACTest_SOURCES = RLCMACTest.cpp RLCMACTest_LDADD = \ libgprs.la \ $(COMMON_LA) + +#OPENBSC_DIR = /path to OpenBSC dir +#OPENGGSN_DIR = /path to OpenGGGSN dir + +RLCMAC_SOURCES = RLCMAC.cpp +RLCMAC_LDADD = \ + libgprs.la \ + $(OPENBSC_DIR)/src/libgb/gprs_ns.o \ + $(OPENBSC_DIR)/src/libgb/gprs_bssgp.o \ + $(OPENBSC_DIR)/src/libgb/gprs_bssgp_bss.o \ + $(OPENBSC_DIR)/src/libgb/gprs_bssgp_util.o \ + $(OPENBSC_DIR)/src/libgb/gprs_ns_frgre.o \ + $(OPENGGSN_DIR)/gtp/queue.o \ + $(OPENGGSN_DIR)/gtp/lookupa.o \ + $(OPENGGSN_DIR)/gtp/pdp.o \ + $(OPENGGSN_DIR)/gtp/gtpie.o \ + $(OPENGGSN_DIR)/gtp/gtp.o \ + $(OPENBSC_DIR)/src/gprs/sgsn_libgtp.o \ + $(OPENBSC_DIR)/src/gprs/gprs_sndcp.o \ + $(OPENBSC_DIR)/src/gprs/crc24.o \ + $(OPENBSC_DIR)/src/gprs/gprs_llc.o \ + $(OPENBSC_DIR)/src/gprs/gprs_gmm.o \ + $(OPENBSC_DIR)/src/gprs/gprs_sgsn.o \ + $(OPENBSC_DIR)/src/libcommon/socket.o \ + $(OPENBSC_DIR)/src/libcommon/debug.o \ + -losmocore \ + -losmogsm \ + $(COMMON_LA) + #MOSTLYCLEANFILES += testSource testDestination - diff --git a/RLCMAC.cpp b/RLCMAC.cpp new file mode 100644 index 00000000..4a174685 --- /dev/null +++ b/RLCMAC.cpp @@ -0,0 +1,26 @@ +/* RLCMAC.cpp + * + * Copyright (C) 2011 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "bssgp.h" + +int main(int argc, char *argv[]) +{ + // Start RLC/MAC server for communication with OpenBTS and OsmoSGSN. + RLCMACServer(); +} diff --git a/bssgp.cpp b/bssgp.cpp new file mode 100644 index 00000000..dd773df4 --- /dev/null +++ b/bssgp.cpp @@ -0,0 +1,363 @@ +/* bssgp.cpp + * + * Copyright (C) 2011 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include "GPRSSocket.h" +#include "bssgp.h" + +// TODO: We should move this parameters to config file. +#define SGSN_IP "127.0.0.1" +#define SGSN_PORT 23000 +#define CELL_ID 3 +#define BVCI 7 +#define NSEI 3 +#define NSVCI 4 +#define MNC 1 +#define MCC 1 +#define LAC 1000 +#define RAC 1 + +#define QOS_PROFILE 0 +#define BSSGP_HDR_LEN 20 +#define NS_HDR_LEN 4 +#define MAX_LEN_PDU 100 +#define IE_PDU 14 +#define BLOCK_DATA_LEN 16 + +#define BLOCK_LEN 23 + +uint16_t bvci = BVCI; +uint16_t nsei = NSEI; +uint8_t TFI; +struct bssgp_bvc_ctx *bctx = btsctx_alloc(bvci, nsei); +struct gprs_nsvc *nsvc; +struct gprs_ns_inst *sgsn_nsi; +struct sgsn_instance *sgsn; +void *tall_bsc_ctx; + +// Send RLC data to OpenBTS. +void sendRLC(uint32_t tlli, uint8_t *pdu, unsigned startIndex, unsigned endIndex) +{ + unsigned wp = 0; + BitVector resultVector(BLOCK_LEN*8); + resultVector.unhex("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); + // TODO: Encode downlink RLC/MAC data block. + sendToOpenBTS(&resultVector); +} + +/* Receive a BSSGP PDU from a BSS on a PTP BVCI */ +int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) +{ + struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); + uint8_t pdu_type = bgph->pdu_type; + uint8_t pdu[MAX_LEN_PDU]; + unsigned rc = 0; + unsigned dataIndex = 0; + unsigned numBlocks = 0; + unsigned i = 0; + unsigned j = 0; + unsigned pduIndex = 0; + struct bssgp_ud_hdr *budh; + + /* If traffic is received on a BVC that is marked as blocked, the + * received PDU shall not be accepted and a STATUS PDU (Cause value: + * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ + if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) + { + uint16_t bvci = msgb_bvci(msg); + LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n"); + return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); + } + + switch (pdu_type) { + case BSSGP_PDUT_DL_UNITDATA: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_DL_UNITDATA\n"); + budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP TLLI=0x%08x \n", ntohl(budh->tlli)); + for (i = 4; i < MAX_LEN_PDU; i++) + { + LOGP(DBSSGP, LOGL_NOTICE, "SERCH data = -0x%02x\n", budh ->data[i]); + if(budh ->data[i] == IE_PDU) + { + pduIndex = i+2; + break; + } + } + for (i = pduIndex; i < pduIndex + (budh->data[pduIndex-1]&0x7f); i++) + { + LOGP(DBSSGP, LOGL_NOTICE, "-0x%02x\n", budh ->data[i]); + pdu[dataIndex] = budh ->data[i]; + dataIndex++; + } + DEBUGP(DBSSGP, "BSSGP Catch from SGSN=%u octets. Send it to OpenBTS.\n", dataIndex); + if (dataIndex > BLOCK_DATA_LEN) + { + int blockDataLen = BLOCK_DATA_LEN; + numBlocks = dataIndex/BLOCK_DATA_LEN; + int ost = dataIndex%BLOCK_DATA_LEN; + int startIndex = 0; + int endIndex = 0; + if (dataIndex%BLOCK_DATA_LEN > 0) + { + numBlocks++; + } + for (i = 0; i < numBlocks; i++) + { + if (i == numBlocks-1) + { + blockDataLen = ost; + } + endIndex = startIndex+blockDataLen; + sendRLC(ntohl(budh->tlli), pdu, startIndex, endIndex); + startIndex += blockDataLen; + } + } + else + { + sendRLC(ntohl(budh->tlli), pdu, 0, dataIndex); + } + break; + case BSSGP_PDUT_PAGING_PS: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); + break; + case BSSGP_PDUT_PAGING_CS: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); + break; + case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n"); + break; + case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n"); + break; + case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n"); + break; + default: + DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type); + rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); + break; + } + return rc; +} + +/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */ +int gprs_bssgp_bss_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) +{ + struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); + int rc = 0; + switch (bgph->pdu_type) { + case BSSGP_PDUT_STATUS: + /* Some exception has occurred */ + DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci); + /* FIXME: send NM_STATUS.ind to NM */ + break; + case BSSGP_PDUT_SUSPEND_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); + break; + case BSSGP_PDUT_SUSPEND_NACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_NACK\n"); + break; + case BSSGP_PDUT_BVC_RESET_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_RESET_ACK\n"); + break; + case BSSGP_PDUT_PAGING_PS: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n"); + break; + case BSSGP_PDUT_PAGING_CS: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n"); + break; + case BSSGP_PDUT_RESUME_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_ACK\n"); + break; + case BSSGP_PDUT_RESUME_NACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_NACK\n"); + break; + case BSSGP_PDUT_FLUSH_LL: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLUSH_LL\n"); + break; + case BSSGP_PDUT_BVC_BLOCK_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n"); + break; + case BSSGP_PDUT_BVC_UNBLOCK_ACK: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n"); + break; + case BSSGP_PDUT_SGSN_INVOKE_TRACE: + LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n"); + break; + default: + DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n", bctx->bvci, bgph->pdu_type); + rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); + break; + } + return rc; +} + +int gprs_bssgp_bss_rcvmsg(struct msgb *msg) +{ + struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); + struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); + struct tlv_parsed tp; + uint8_t pdu_type = bgph->pdu_type; + uint16_t ns_bvci = msgb_bvci(msg); + int data_len; + int rc = 0; + + /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ + + /* UNITDATA BSSGP headers have TLLI in front */ + if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA) + { + data_len = msgb_bssgp_len(msg) - sizeof(*bgph); + rc = bssgp_tlv_parse(&tp, bgph->data, data_len); + } + else + { + data_len = msgb_bssgp_len(msg) - sizeof(*budh); + rc = bssgp_tlv_parse(&tp, budh->data, data_len); + } + + /* look-up or create the BTS context for this BVC */ + bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg)); + + /* Only a RESET PDU can create a new BVC context */ + if (!bctx) + { + bctx = btsctx_alloc(ns_bvci, msgb_nsei(msg)); + } + + if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET_ACK) + { + LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU " + "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci, + pdu_type); + return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); + } + + if (bctx) + { + log_set_context(BSC_CTX_BVC, bctx); + rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); + rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg)); + } + + if (ns_bvci == BVCI_SIGNALLING) + { + LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); + rc = gprs_bssgp_bss_rx_sign(msg, &tp, bctx); + } + else if (ns_bvci == BVCI_PTM) + { + LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTM bssgp_tx_status\n"); + rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); + } + else + { + LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n"); + rc = gprs_bssgp_bss_rx_ptp(msg, &tp, bctx); + } + return rc; +} + + +int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci) +{ + int rc = 0; + switch (event) { + case GPRS_NS_EVT_UNIT_DATA: + /* hand the message into the BSSGP implementation */ + rc = gprs_bssgp_bss_rcvmsg(msg); + break; + default: + LOGP(DGPRS, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event); + if (msg) + talloc_free(msg); + rc = -EIO; + break; + } + return rc; +} + +// Send RLC data to SGSN. +void sendToSGSN(uint8_t tfi, uint32_t tlli, uint8_t * rlc_data, unsigned dataLen) +{ + const uint8_t qos_profile = QOS_PROFILE; + struct msgb *llc_pdu; + unsigned msgLen = NS_HDR_LEN + BSSGP_HDR_LEN + dataLen; + TFI = tfi; + bctx->cell_id = CELL_ID; + bctx->nsei = NSEI; + bctx->ra_id.mnc = MNC; + bctx->ra_id.mcc = MCC; + bctx->ra_id.lac = LAC; + bctx->ra_id.rac = RAC; + bctx->bvci = BVCI; + LOGP(DBSSGP, LOGL_DEBUG, "Data len %u TLLI 0x%08x , TFI 0x%02x", dataLen, tlli, tfi); + //for (unsigned i = 0; i < dataLen; i++) + // LOGP(DBSSGP, LOGL_DEBUG, " Data[%u] = %u", i, rlc_data[i]); + llc_pdu = msgb_alloc_headroom(msgLen, msgLen,"llc_pdu"); + msgb_tvlv_push(llc_pdu, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*dataLen, rlc_data); + bssgp_tx_ul_ud(bctx, tlli, &qos_profile, llc_pdu); +} + +void RLCMACServer() +{ + uint16_t nsvci = NSVCI; + + // Socket for reading BitVectors (RLC/MAC Frames) from OpenBTS application. + Thread RLCMACInterface; + RLCMACInterface.start(RLCMACSocket,NULL); + + osmo_init_logging(&log_info); + sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); + bssgp_nsi = sgsn_nsi; + + if (!bssgp_nsi) + { + LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); + exit(1); + } + + bctx->cell_id = CELL_ID; + bctx->nsei = NSEI; + bctx->ra_id.mnc = MNC; + bctx->ra_id.mcc = MCC; + bctx->ra_id.lac = LAC; + bctx->ra_id.rac = RAC; + bctx->bvci = BVCI; + uint8_t cause = 39; + gprs_ns_nsip_listen(sgsn_nsi); + + struct sockaddr_in dest; + dest.sin_family = AF_INET; + dest.sin_port = htons(SGSN_PORT); + inet_aton(SGSN_IP, &dest.sin_addr); + + nsvc = nsip_connect(sgsn_nsi, &dest, nsei, nsvci); + unsigned i = 0; + while (1) + { + osmo_select_main(0); + if (i == 7) + { + bssgp_tx_bvc_reset(bctx, nsvc, bvci, cause); + } + i++; + } +} diff --git a/bssgp.h b/bssgp.h new file mode 100644 index 00000000..b8beea9f --- /dev/null +++ b/bssgp.h @@ -0,0 +1,53 @@ +/* bssgp.h + * + * Copyright (C) 2011 Ivan Klyuchnikov + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef BSSGP_H +#define BSSGP_H + +extern "C" { +#include +#include +#include +#include +#include +#include +#include + +int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, struct gprs_nsvc * nsvc, uint16_t bvci, uint8_t cause); + +int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli, const uint8_t *qos_profile, struct msgb *llc_pdu); + +struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei); +} + +void sendRLC(uint32_t tlli, uint8_t *pdu, unsigned startIndex, unsigned endIndex); + +int gprs_bssgp_bss_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); + +int gprs_bssgp_bss_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx); + +int gprs_bssgp_bss_rcvmsg(struct msgb *msg); + +int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci); + +void sendToSGSN(uint8_t tfi, uint32_t tlli, uint8_t * rlc_data, unsigned dataLen); + +void RLCMACServer(); + +#endif // BSSGP_H