blob: 8924e5b253b97701e9ec4fc6bddcefde33cef1ed [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;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040031 int i = 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040032
33 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040034
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040035 struct gprs_rlcmac_tbf *tbf;
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040036 tbf = tbf_alloc(GPRS_RLCMAC_DL_TBF, ntohl(budh->tlli));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040037
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040038 if (!tbf)
39 {
40 return -1;
41 }
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040042 /* LLC_PDU is mandatory IE */
43 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040044 {
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040045 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tbf->tlli);
46 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
47 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +040048
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040049 uint16_t imsi_len = 0;
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040050 uint8_t *imsi = NULL;
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040051 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
52 {
53 imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI);
54 imsi = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_IMSI);
55
56 LOGPC(DBSSGP, LOGL_NOTICE, " IMSI = ");
57 for (i = 0; i < imsi_len; i++)
58 {
59 LOGPC(DBSSGP, LOGL_NOTICE, "%02x", imsi[i]);
60 }
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040061 LOGP(DBSSGP, LOGL_NOTICE, "\n");
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040062 }
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040063
64 tbf_dl_establish(tbf, imsi);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040065
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040066 uint8_t *llc_pdu = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
67 uint16_t llc_pdu_len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
68
69 tbf_dl_data_transfer(tbf, llc_pdu, llc_pdu_len);
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +040070}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040071
72
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040073/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
74int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
75{
76 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
77 uint8_t pdu_type = bgph->pdu_type;
78 unsigned rc = 0;
79
80 /* If traffic is received on a BVC that is marked as blocked, the
81 * received PDU shall not be accepted and a STATUS PDU (Cause value:
82 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
83 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
84 {
85 uint16_t bvci = msgb_bvci(msg);
86 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
87 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
88 }
89
90 switch (pdu_type) {
91 case BSSGP_PDUT_DL_UNITDATA:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040092 LOGP(DBSSGP, LOGL_NOTICE, "RX: [SGSN->PCU] BSSGP_PDUT_DL_UNITDATA\n");
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +040093 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040094 break;
95 case BSSGP_PDUT_PAGING_PS:
96 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n");
97 break;
98 case BSSGP_PDUT_PAGING_CS:
99 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n");
100 break;
101 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
102 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n");
103 break;
104 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
105 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n");
106 break;
107 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
108 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n");
109 break;
110 default:
111 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type);
112 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
113 break;
114 }
115 return rc;
116}
117
118/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
119int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
120{
121 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
122 int rc = 0;
123 switch (bgph->pdu_type) {
124 case BSSGP_PDUT_STATUS:
125 /* Some exception has occurred */
126 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
127 /* FIXME: send NM_STATUS.ind to NM */
128 break;
129 case BSSGP_PDUT_SUSPEND_ACK:
130 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n");
131 break;
132 case BSSGP_PDUT_SUSPEND_NACK:
133 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_NACK\n");
134 break;
135 case BSSGP_PDUT_BVC_RESET_ACK:
136 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_RESET_ACK\n");
137 break;
138 case BSSGP_PDUT_PAGING_PS:
139 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_PS\n");
140 break;
141 case BSSGP_PDUT_PAGING_CS:
142 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_PAGING_CS\n");
143 break;
144 case BSSGP_PDUT_RESUME_ACK:
145 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_ACK\n");
146 break;
147 case BSSGP_PDUT_RESUME_NACK:
148 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_RESUME_NACK\n");
149 break;
150 case BSSGP_PDUT_FLUSH_LL:
151 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_FLUSH_LL\n");
152 break;
153 case BSSGP_PDUT_BVC_BLOCK_ACK:
154 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SUSPEND_ACK\n");
155 break;
156 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
157 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n");
158 break;
159 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
160 LOGP(DBSSGP, LOGL_NOTICE, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n");
161 break;
162 default:
163 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n", bctx->bvci, bgph->pdu_type);
164 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
165 break;
166 }
167 return rc;
168}
169
170int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
171{
172 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
173 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
174 struct tlv_parsed tp;
175 uint8_t pdu_type = bgph->pdu_type;
176 uint16_t ns_bvci = msgb_bvci(msg);
177 int data_len;
178 int rc = 0;
179
180 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
181
182 /* UNITDATA BSSGP headers have TLLI in front */
183 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
184 {
185 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
186 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
187 }
188 else
189 {
190 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
191 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
192 }
193
194 /* look-up or create the BTS context for this BVC */
195 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
196
197 /* Only a RESET PDU can create a new BVC context */
198 if (!bctx)
199 {
200 bctx = btsctx_alloc(ns_bvci, msgb_nsei(msg));
201 }
202
203 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET_ACK)
204 {
205 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
206 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
207 pdu_type);
208 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
209 }
210
211 if (bctx)
212 {
213 log_set_context(BSC_CTX_BVC, bctx);
214 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
215 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg));
216 }
217
218 if (ns_bvci == BVCI_SIGNALLING)
219 {
220 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
221 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
222 }
223 else if (ns_bvci == BVCI_PTM)
224 {
225 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTM bssgp_tx_status\n");
226 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
227 }
228 else
229 {
230 LOGP(DBSSGP, LOGL_NOTICE, "rx BVCI_PTP gprs_bssgp_rx_ptp\n");
231 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
232 }
233 return rc;
234}