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