blob: 4ccea4405f47a207cac84312f9e148a508e37da8 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* 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
24struct sgsn_instance *sgsn;
25void *tall_bsc_ctx;
26struct bssgp_bvc_ctx *bctx = btsctx_alloc(BVCI, NSEI);
27
28int 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;
67 gprs_rlcmac_segment_llc_pdu(tbf);
68}
69
70/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
71int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
72{
73 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
74 uint8_t pdu_type = bgph->pdu_type;
75 unsigned rc = 0;
76
77 /* If traffic is received on a BVC that is marked as blocked, the
78 * received PDU shall not be accepted and a STATUS PDU (Cause value:
79 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
80 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
81 {
82 uint16_t bvci = msgb_bvci(msg);
83 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
84 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
85 }
86
87 switch (pdu_type) {
88 case BSSGP_PDUT_DL_UNITDATA:
89 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_DL_UNITDATA\n");
90 gprs_bssgp_pcu_rx_dl_ud(msg);
91 break;
92 case BSSGP_PDUT_PAGING_PS:
93 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n");
94 break;
95 case BSSGP_PDUT_PAGING_CS:
96 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n");
97 break;
98 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
99 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n");
100 break;
101 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
102 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n");
103 break;
104 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
105 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n");
106 break;
107 default:
108 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type);
109 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
110 break;
111 }
112 return rc;
113}
114
115/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
116int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
117{
118 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
119 int rc = 0;
120 switch (bgph->pdu_type) {
121 case BSSGP_PDUT_STATUS:
122 /* Some exception has occurred */
123 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
124 /* FIXME: send NM_STATUS.ind to NM */
125 break;
126 case BSSGP_PDUT_SUSPEND_ACK:
127 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n");
128 break;
129 case BSSGP_PDUT_SUSPEND_NACK:
130 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_NACK\n");
131 break;
132 case BSSGP_PDUT_BVC_RESET_ACK:
133 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_RESET_ACK\n");
134 break;
135 case BSSGP_PDUT_PAGING_PS:
136 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n");
137 break;
138 case BSSGP_PDUT_PAGING_CS:
139 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n");
140 break;
141 case BSSGP_PDUT_RESUME_ACK:
142 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_ACK\n");
143 break;
144 case BSSGP_PDUT_RESUME_NACK:
145 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_NACK\n");
146 break;
147 case BSSGP_PDUT_FLUSH_LL:
148 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLUSH_LL\n");
149 break;
150 case BSSGP_PDUT_BVC_BLOCK_ACK:
151 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n");
152 break;
153 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
154 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n");
155 break;
156 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
157 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n");
158 break;
159 default:
160 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n", bctx->bvci, bgph->pdu_type);
161 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
162 break;
163 }
164 return rc;
165}
166
167int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
168{
169 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
170 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
171 struct tlv_parsed tp;
172 uint8_t pdu_type = bgph->pdu_type;
173 uint16_t ns_bvci = msgb_bvci(msg);
174 int data_len;
175 int rc = 0;
176
177 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
178
179 /* UNITDATA BSSGP headers have TLLI in front */
180 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
181 {
182 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
183 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
184 }
185 else
186 {
187 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
188 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
189 }
190
191 /* look-up or create the BTS context for this BVC */
192 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
193
194 /* Only a RESET PDU can create a new BVC context */
195 if (!bctx)
196 {
197 bctx = btsctx_alloc(ns_bvci, msgb_nsei(msg));
198 }
199
200 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET_ACK)
201 {
202 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
203 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
204 pdu_type);
205 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
206 }
207
208 if (bctx)
209 {
210 log_set_context(BSC_CTX_BVC, bctx);
211 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
212 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg));
213 }
214
215 if (ns_bvci == BVCI_SIGNALLING)
216 {
217 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
218 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
219 }
220 else if (ns_bvci == BVCI_PTM)
221 {
222 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTM bssgp_tx_status\n");
223 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
224 }
225 else
226 {
227 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n");
228 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
229 }
230 return rc;
231}