blob: a686a22aaff2e2b720225c6cebb36fdb074e4cd1 [file] [log] [blame]
Harald Welte9ba50052010-03-14 15:45:01 +08001/* GPRS Networks Service (NS) messages on the Gb interface
2 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
4/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/* Some introduction into NS: NS is used typically on top of frame relay,
25 * but in the ip.access world it is encapsulated in UDP packets. It serves
26 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
27 * do much, apart from providing congestion notification and status indication.
28 *
29 * Terms:
30 * NS Network Service
31 * NSVC NS Virtual Connection
32 * NSEI NS Entity Identifier
33 * NSVL NS Virtual Link
34 * NSVLI NS Virtual Link Identifier
35 * BVC BSSGP Virtual Connection
36 * BVCI BSSGP Virtual Connection Identifier
37 * NSVCG NS Virtual Connection Goup
38 * Blocked NS-VC cannot be used for user traffic
39 * Alive Ability of a NS-VC to provide communication
40 *
41 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
42 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
43 * NS then has to firgure out which NSVC's are responsible for this BVCI.
44 * Those mappings are administratively configured.
45 */
46
47#include <stdlib.h>
48#include <unistd.h>
49#include <errno.h>
50#include <sys/types.h>
51
52#include <arpa/inet.h>
53
54#include <openbsc/gsm_data.h>
55#include <osmocore/msgb.h>
56#include <osmocore/tlv.h>
57#include <osmocore/talloc.h>
58#include <openbsc/debug.h>
59#include <openbsc/gprs_ns.h>
60#include <openbsc/gprs_bssgp.h>
61
62#define NS_ALLOC_SIZE 1024
63
64static const struct tlv_definition ns_att_tlvdef = {
65 .def = {
66 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
67 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
68 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
69 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
70 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
71 },
72};
73
74#define NSE_S_BLOCKED 0x0001
75#define NSE_S_ALIVE 0x0002
76
77struct gprs_nsvc {
78 struct llist_head list;
79
80 u_int16_t nsei; /* end-to-end significance */
81 u_int16_t nsvci; /* uniquely identifies NS-VC at SGSN */
82
83 u_int32_t state;
84
85 struct timer_list alive_timer;
86 int timer_is_tns_alive;
87 int alive_retries;
88};
89
90/* FIXME: dynamically search for the matching NSVC */
91static struct gprs_nsvc dummy_nsvc = { .state = NSE_S_BLOCKED | NSE_S_ALIVE };
92
93/* Section 10.3.2, Table 13 */
94static const char *ns_cause_str[] = {
95 [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure",
96 [NS_CAUSE_OM_INTERVENTION] = "O&M intervention",
97 [NS_CAUSE_EQUIP_FAIL] = "Equipment failure",
98 [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked",
99 [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown",
100 [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown",
101 [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU",
102 [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state",
103 [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified",
104 [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE",
105 [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE",
106};
107
108static const char *gprs_ns_cause_str(enum ns_cause cause)
109{
110 if (cause >= ARRAY_SIZE(ns_cause_str))
111 return "undefined";
112
113 if (ns_cause_str[cause])
114 return ns_cause_str[cause];
115
116 return "undefined";
117}
118
119static int gprs_ns_tx(struct msgb *msg)
120{
121 return ipac_gprs_send(msg);
122}
123
124static int gprs_ns_tx_simple(struct gprs_ns_link *link, u_int8_t pdu_type)
125{
126 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
127 struct gprs_ns_hdr *nsh;
128
129 if (!msg)
130 return -ENOMEM;
131
132 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
133
134 nsh->pdu_type = pdu_type;
135
136 return gprs_ns_tx(msg);
137}
138
139#define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */
140#define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */
141#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
142
143static void gprs_ns_alive_cb(void *data)
144{
145 struct gprs_nsvc *nsvc = data;
146
147 if (nsvc->timer_is_tns_alive) {
148 /* Tns-alive case: we expired without response ! */
149 nsvc->alive_retries++;
150 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
151 /* mark as dead and blocked */
152 nsvc->state = NSE_S_BLOCKED;
153 DEBUGP(DGPRS, "Tns-alive more then %u retries, "
154 " blocking NS-VC\n", NS_ALIVE_RETRIES);
155 /* FIXME: inform higher layers */
156 return;
157 }
158 } else {
159 /* Tns-test case: send NS-ALIVE PDU */
160 gprs_ns_tx_simple(NULL, NS_PDUT_ALIVE);
161 /* start Tns-alive timer */
162 nsvc->timer_is_tns_alive = 1;
163 }
164 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
165}
166
167/* Section 9.2.6 */
168static int gprs_ns_tx_reset_ack(u_int16_t nsvci, u_int16_t nsei)
169{
170 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
171 struct gprs_ns_hdr *nsh;
172
173 if (!msg)
174 return -ENOMEM;
175
176 nsvci = htons(nsvci);
177 nsei = htons(nsei);
178
179 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
180
181 nsh->pdu_type = NS_PDUT_RESET_ACK;
182
183 msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci);
184 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei);
185
186 return gprs_ns_tx(msg);
187}
188
189/* Section 9.2.10: transmit side */
190int gprs_ns_sendmsg(struct gprs_ns_link *link, u_int16_t bvci,
191 struct msgb *msg)
192{
193 struct gprs_ns_hdr *nsh;
194
195 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
196 if (!nsh) {
197 DEBUGP(DGPRS, "Not enough headroom for NS header\n");
198 return -EIO;
199 }
200
201 nsh->pdu_type = NS_PDUT_UNITDATA;
202 /* spare octet in data[0] */
203 nsh->data[1] = bvci >> 8;
204 nsh->data[2] = bvci & 0xff;
205
206 return gprs_ns_tx(msg);
207}
208
209/* Section 9.2.10: receive side */
210static int gprs_ns_rx_unitdata(struct msgb *msg)
211{
212 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
213 u_int16_t bvci;
214
215 /* spare octet in data[0] */
216 bvci = nsh->data[1] << 8 | nsh->data[2];
217 msg->l3h = &nsh->data[3];
218
219 /* call upper layer (BSSGP) */
220 return gprs_bssgp_rcvmsg(msg, bvci);
221}
222
223/* Section 9.2.7 */
224static int gprs_ns_rx_status(struct msgb *msg)
225{
226 struct gprs_ns_hdr *nsh = msg->l2h;
227 struct tlv_parsed tp;
228 u_int8_t cause;
229 int rc;
230
231 DEBUGP(DGPRS, "NS STATUS ");
232
233 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
234
235 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
236 DEBUGPC(DGPRS, "missing cause IE\n");
237 return -EINVAL;
238 }
239
240 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
241 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
242
243 return 0;
244}
245
246/* Section 7.3 */
247static int gprs_ns_rx_reset(struct msgb *msg)
248{
249 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
250 struct gprs_nsvc *nsvc = &dummy_nsvc;
251 struct tlv_parsed tp;
252 u_int8_t *cause;
253 u_int16_t *nsvci, *nsei;
254 int rc;
255
256 DEBUGP(DGPRS, "NS RESET ");
257
258 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
259
260 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
261 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
262 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
263 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
264 DEBUGPC(DGPRS, "Missing mandatory IE\n");
265 return -EINVAL;
266 }
267
268 cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
269 nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI);
270 nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
271
272 *nsvci = ntohs(*nsvci);
273 *nsei = ntohs(*nsei);
274
275 DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n",
276 gprs_ns_cause_str(*cause), *nsvci, *nsei);
277
278 /* mark the NS-VC as blocked and alive */
279 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
280 nsvc->nsei = *nsei;
281 nsvc->nsvci = *nsvci;
282
283 /* start the test procedure */
284 nsvc->alive_timer.cb = gprs_ns_alive_cb;
285 nsvc->alive_timer.data = nsvc;
286 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
287
288 return gprs_ns_tx_reset_ack(*nsvci, *nsei);
289}
290
291/* main entry point, here incoming NS frames enter */
292int gprs_ns_rcvmsg(struct msgb *msg)
293{
294 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
295 struct gprs_nsvc *nsvc = &dummy_nsvc;
296 int rc = 0;
297
298 switch (nsh->pdu_type) {
299 case NS_PDUT_ALIVE:
300 /* remote end inquires whether we're still alive,
301 * we need to respond with ALIVE_ACK */
302 rc = gprs_ns_tx_simple(NULL, NS_PDUT_ALIVE_ACK);
303 break;
304 case NS_PDUT_ALIVE_ACK:
305 /* stop Tns-alive */
306 bsc_del_timer(&nsvc->alive_timer);
307 /* start Tns-test */
308 nsvc->timer_is_tns_alive = 0;
309 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST);
310 break;
311 case NS_PDUT_UNITDATA:
312 /* actual user data */
313 rc = gprs_ns_rx_unitdata(msg);
314 break;
315 case NS_PDUT_STATUS:
316 rc = gprs_ns_rx_status(msg);
317 break;
318 case NS_PDUT_RESET:
319 rc = gprs_ns_rx_reset(msg);
320 break;
321 case NS_PDUT_RESET_ACK:
322 /* FIXME: mark remote NS-VC as blocked + active */
323 break;
324 case NS_PDUT_UNBLOCK:
325 /* Section 7.2: unblocking procedure */
326 DEBUGP(DGPRS, "NS UNBLOCK\n");
327 nsvc->state &= ~NSE_S_BLOCKED;
328 rc = gprs_ns_tx_simple(NULL, NS_PDUT_UNBLOCK_ACK);
329 break;
330 case NS_PDUT_UNBLOCK_ACK:
331 /* FIXME: mark remote NS-VC as unblocked + active */
332 break;
333 case NS_PDUT_BLOCK:
334 DEBUGP(DGPRS, "NS BLOCK\n");
335 nsvc->state |= NSE_S_BLOCKED;
336 rc = gprs_ns_tx_simple(NULL, NS_PDUT_UNBLOCK_ACK);
337 break;
338 case NS_PDUT_BLOCK_ACK:
339 /* FIXME: mark remote NS-VC as blocked + active */
340 break;
341 default:
342 DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type);
343 rc = -EINVAL;
344 break;
345 }
346 return rc;
347}
348