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