blob: 1b9d7c6d5b343069703de2ce319275dd4c8b51c9 [file] [log] [blame]
Harald Welte24a655f2010-04-30 19:54:29 +02001/* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Weltef030b212010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
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>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020050#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080051
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>
Harald Weltef030b212010-04-26 19:18:54 +020058#include <osmocore/select.h>
Harald Welte9ba50052010-03-14 15:45:01 +080059#include <openbsc/debug.h>
Harald Welte834f26d2010-05-11 06:20:54 +020060#include <openbsc/signal.h>
Harald Welte9ba50052010-03-14 15:45:01 +080061#include <openbsc/gprs_ns.h>
62#include <openbsc/gprs_bssgp.h>
63
64#define NS_ALLOC_SIZE 1024
65
Harald Weltefe4ab902010-05-12 17:19:53 +000066/* FIXME: this should go to some common file as it is copied
67 * in vty_interface.c of the BSC */
68static const struct value_string gprs_ns_timer_strs[] = {
69 { 0, "tns-block" },
70 { 1, "tns-block-retries" },
71 { 2, "tns-reset" },
72 { 3, "tns-reset-retries" },
73 { 4, "tns-test" },
74 { 5, "tns-alive" },
75 { 6, "tns-alive-retries" },
76 { 0, NULL }
77};
78
Harald Welte9ba50052010-03-14 15:45:01 +080079static const struct tlv_definition ns_att_tlvdef = {
80 .def = {
81 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
82 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
83 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
84 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
85 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
86 },
87};
88
Harald Weltef030b212010-04-26 19:18:54 +020089/* Lookup struct gprs_nsvc based on NSVCI */
90static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020091 uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +020092{
93 struct gprs_nsvc *nsvc;
94 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
95 if (nsvc->nsvci == nsvci)
96 return nsvc;
97 }
98 return NULL;
99}
100
Harald Welte24a655f2010-04-30 19:54:29 +0200101/* Lookup struct gprs_nsvc based on NSVCI */
102static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200103 uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200104{
105 struct gprs_nsvc *nsvc;
106 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
107 if (nsvc->nsei == nsei)
108 return nsvc;
109 }
110 return NULL;
111}
112
113
Harald Weltef030b212010-04-26 19:18:54 +0200114/* Lookup struct gprs_nsvc based on remote peer socket addr */
115static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
116 struct sockaddr_in *sin)
117{
118 struct gprs_nsvc *nsvc;
119 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000120 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
121 sin->sin_addr.s_addr &&
122 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200123 return nsvc;
124 }
125 return NULL;
126}
127
Harald Welte69a4cf22010-05-03 20:55:10 +0200128static void gprs_ns_timer_cb(void *data);
129
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200130static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200131{
132 struct gprs_nsvc *nsvc;
133
134 nsvc = talloc_zero(nsi, struct gprs_nsvc);
135 nsvc->nsvci = nsvci;
136 /* before RESET procedure: BLOCKED and DEAD */
137 nsvc->state = NSE_S_BLOCKED;
138 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200139 nsvc->timer.cb = gprs_ns_timer_cb;
140 nsvc->timer.data = nsvc;
141
Harald Weltef030b212010-04-26 19:18:54 +0200142 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
143
144 return nsvc;
145}
Harald Welte9ba50052010-03-14 15:45:01 +0800146
Harald Welte2bffac52010-05-12 15:55:23 +0000147static void nsvc_delete(struct gprs_nsvc *nsvc)
148{
149 if (bsc_timer_pending(&nsvc->timer))
150 bsc_del_timer(&nsvc->timer);
151 llist_del(&nsvc->list);
152 talloc_free(nsvc);
153}
154
Harald Welte2fc725d2010-05-11 10:15:26 +0200155static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200156 uint8_t cause)
157{
158 struct ns_signal_data nssd;
159
160 nssd.nsvc = nsvc;
161 nssd.cause = cause;
162
163 dispatch_signal(SS_NS, signal, &nssd);
164}
165
Harald Welte9ba50052010-03-14 15:45:01 +0800166/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200167static const struct value_string ns_cause_str[] = {
168 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
169 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
170 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
171 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
172 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
173 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
174 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
175 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200176 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200177 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
178 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
179 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800180};
181
Harald Welte2577d412010-05-02 09:37:45 +0200182const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800183{
Harald Welte2577d412010-05-02 09:37:45 +0200184 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800185}
186
Harald Welte24a655f2010-04-30 19:54:29 +0200187static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200188
Harald Welte24a655f2010-04-30 19:54:29 +0200189static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800190{
Harald Weltef030b212010-04-26 19:18:54 +0200191 int ret;
192
193 switch (nsvc->nsi->ll) {
194 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200195 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200196 break;
197 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200198 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200199 msgb_free(msg);
200 ret = -EIO;
201 break;
202 }
203 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800204}
205
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200206static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800207{
208 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
209 struct gprs_ns_hdr *nsh;
210
211 if (!msg)
212 return -ENOMEM;
213
214 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
215
216 nsh->pdu_type = pdu_type;
217
Harald Welte24a655f2010-04-30 19:54:29 +0200218 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800219}
220
Harald Welte834f26d2010-05-11 06:20:54 +0200221int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200222{
223 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
224 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200225 uint16_t nsvci = htons(nsvc->nsvci);
226 uint16_t nsei = htons(nsvc->nsei);
227
228 if (!msg)
229 return -ENOMEM;
230
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000231 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
232 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
233
Harald Welte69a4cf22010-05-03 20:55:10 +0200234 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
235 nsh->pdu_type = NS_PDUT_RESET;
236
237 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
238 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
239 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
240
241 return gprs_ns_tx(nsvc, msg);
242
243}
244
Harald Weltec1402a62010-05-12 11:48:44 +0200245int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
246 uint16_t bvci, struct msgb *orig_msg)
247{
248 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
249 struct gprs_ns_hdr *nsh;
250 uint16_t nsvci = htons(nsvc->nsvci);
251
252 bvci = htons(bvci);
253
254 if (!msg)
255 return -ENOMEM;
256
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000257 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
258 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
259
Harald Weltec1402a62010-05-12 11:48:44 +0200260 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
261 nsh->pdu_type = NS_PDUT_STATUS;
262
263 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
264
265 /* Section 9.2.7.1: Static conditions for NS-VCI */
266 if (cause == NS_CAUSE_NSVC_BLOCKED ||
267 cause == NS_CAUSE_NSVC_UNKNOWN)
268 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
269
270 /* Section 9.2.7.2: Static conditions for NS PDU */
271 switch (cause) {
272 case NS_CAUSE_SEM_INCORR_PDU:
273 case NS_CAUSE_PDU_INCOMP_PSTATE:
274 case NS_CAUSE_PROTO_ERR_UNSPEC:
275 case NS_CAUSE_INVAL_ESSENT_IE:
276 case NS_CAUSE_MISSING_ESSENT_IE:
277 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
278 orig_msg->l2h);
279 break;
280 default:
281 break;
282 }
283
284 /* Section 9.2.7.3: Static conditions for BVCI */
285 if (cause == NS_CAUSE_BVCI_UNKNOWN)
286 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
287
288 return gprs_ns_tx(nsvc, msg);
289}
290
Harald Welte834f26d2010-05-11 06:20:54 +0200291int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
292{
293 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
294 struct gprs_ns_hdr *nsh;
295 uint16_t nsvci = htons(nsvc->nsvci);
296
297 if (!msg)
298 return -ENOMEM;
299
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000300 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
301 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
302
Harald Welte834f26d2010-05-11 06:20:54 +0200303 /* be conservative and mark it as blocked even now! */
304 nsvc->state |= NSE_S_BLOCKED;
305
306 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
307 nsh->pdu_type = NS_PDUT_BLOCK;
308
309 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
310 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
311
312 return gprs_ns_tx(nsvc, msg);
313}
314
315int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
316{
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000317 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
318 nsvc->nsei, nsvc->nsvci);
319
Harald Welte834f26d2010-05-11 06:20:54 +0200320 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
321}
322
Harald Welteb983c312010-05-12 12:11:45 +0000323int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
324{
325 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
326 nsvc->nsei, nsvc->nsvci);
327
328 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
329}
330
331int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
332{
333 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
334 nsvc->nsei, nsvc->nsvci);
335
336 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
337}
338
Harald Weltefe4ab902010-05-12 17:19:53 +0000339static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
340 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
341 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
342 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200343};
344
Harald Welte7c24b9e2010-05-12 12:21:52 +0000345static const struct value_string timer_mode_strs[] = {
346 { NSVC_TIMER_TNS_RESET, "tns-reset" },
347 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
348 { NSVC_TIMER_TNS_TEST, "tns-test" },
349 { 0, NULL }
350};
351
Harald Welte69a4cf22010-05-03 20:55:10 +0200352static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
353{
Harald Weltefe4ab902010-05-12 17:19:53 +0000354 enum ns_timeout tout = timer_mode_tout[mode];
355 unsigned int seconds = nsvc->nsi->timeout[tout];
356
Harald Welte4e187c62010-05-12 13:55:36 +0000357 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
358 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000359 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000360
Harald Welte69a4cf22010-05-03 20:55:10 +0200361 if (bsc_timer_pending(&nsvc->timer))
362 bsc_del_timer(&nsvc->timer);
363
364 nsvc->timer_mode = mode;
Harald Weltefe4ab902010-05-12 17:19:53 +0000365 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200366}
367
Harald Welte80405452010-05-03 20:16:13 +0200368static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800369{
370 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000371 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
372 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800373
Harald Welte4e187c62010-05-12 13:55:36 +0000374 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
375 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000376 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000377
Harald Welte80405452010-05-03 20:16:13 +0200378 switch (nsvc->timer_mode) {
379 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800380 /* Tns-alive case: we expired without response ! */
381 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000382 if (nsvc->alive_retries >
383 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800384 /* mark as dead and blocked */
385 nsvc->state = NSE_S_BLOCKED;
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200386 LOGP(DNS, LOGL_NOTICE,
387 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200388 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000389 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welte4941b352010-05-12 13:28:25 +0000390 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200391 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800392 return;
393 }
Harald Welteb983c312010-05-12 12:11:45 +0000394 /* Tns-test case: send NS-ALIVE PDU */
395 gprs_ns_tx_alive(nsvc);
396 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200397 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200398 break;
399 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800400 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000401 gprs_ns_tx_alive(nsvc);
402 /* start Tns-alive timer (transition into faster
403 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000404 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200405 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
406 break;
407 case NSVC_TIMER_TNS_RESET:
408 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200409 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200410 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200411 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200412 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200413 case _NSVC_TIMER_NR:
414 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800415 }
Harald Welte9ba50052010-03-14 15:45:01 +0800416}
417
418/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800419static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800420{
421 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
422 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200423 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800424
425 if (!msg)
426 return -ENOMEM;
427
Harald Weltec5478482010-03-18 00:01:43 +0800428 nsvci = htons(nsvc->nsvci);
429 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800430
431 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
432
433 nsh->pdu_type = NS_PDUT_RESET_ACK;
434
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200435 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200436 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800437
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200438 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
439 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800440
Harald Welte24a655f2010-04-30 19:54:29 +0200441 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800442}
443
Harald Welte24a655f2010-04-30 19:54:29 +0200444/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
445int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800446{
Harald Welte24a655f2010-04-30 19:54:29 +0200447 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800448 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200449 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200450
451 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
452 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200453 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200454 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200455 return -EINVAL;
456 }
Harald Welte9ba50052010-03-14 15:45:01 +0800457
Harald Welte69a4cf22010-05-03 20:55:10 +0200458 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200459 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200460 nsvc->nsei);
461 return -EBUSY;
462 }
463 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200464 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200465 nsvc->nsei);
466 return -EBUSY;
467 }
468
Harald Welte9ba50052010-03-14 15:45:01 +0800469 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
470 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200471 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800472 return -EIO;
473 }
474
475 nsh->pdu_type = NS_PDUT_UNITDATA;
476 /* spare octet in data[0] */
477 nsh->data[1] = bvci >> 8;
478 nsh->data[2] = bvci & 0xff;
479
Harald Welte24a655f2010-04-30 19:54:29 +0200480 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800481}
482
483/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200484static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800485{
486 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200487 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800488
Harald Weltec1402a62010-05-12 11:48:44 +0200489 if (nsvc->state & NSE_S_BLOCKED)
490 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
491 0, msg);
492
Harald Welte9ba50052010-03-14 15:45:01 +0800493 /* spare octet in data[0] */
494 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200495 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200496 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800497
498 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200499 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800500}
501
502/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200503static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800504{
Harald Weltef030b212010-04-26 19:18:54 +0200505 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800506 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200507 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800508 int rc;
509
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200510 LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800511
512 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
513
514 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200515 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800516 return -EINVAL;
517 }
518
519 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200520 LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800521
522 return 0;
523}
524
525/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200526static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800527{
528 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800529 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200530 uint8_t *cause;
531 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800532 int rc;
533
Harald Welte9ba50052010-03-14 15:45:01 +0800534 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
535
536 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
537 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
538 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200539 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200540 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800541 return -EINVAL;
542 }
543
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200544 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
545 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
546 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800547
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200548 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200549 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
550
Harald Weltec1402a62010-05-12 11:48:44 +0200551 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800552 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200553
Harald Weltec5478482010-03-18 00:01:43 +0800554 nsvc->nsei = ntohs(*nsei);
555 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800556
Harald Welte9ba50052010-03-14 15:45:01 +0800557 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200558 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800559
Harald Welte834f26d2010-05-11 06:20:54 +0200560 /* inform interested parties about the fact that this NSVC
561 * has received RESET */
Harald Welte2fc725d2010-05-11 10:15:26 +0200562 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200563
Harald Weltec5478482010-03-18 00:01:43 +0800564 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800565}
566
Harald Welte834f26d2010-05-11 06:20:54 +0200567static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
568{
569 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
570 struct tlv_parsed tp;
571 uint8_t *cause;
572 int rc;
573
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200574 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200575
576 nsvc->state |= NSE_S_BLOCKED;
577
578 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
579
580 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
581 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200582 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200583 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200584 return -EINVAL;
585 }
586
587 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
588 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
589
Harald Welte2fc725d2010-05-11 10:15:26 +0200590 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200591
592 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
593}
594
Harald Welte9ba50052010-03-14 15:45:01 +0800595/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200596int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
597 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800598{
599 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200600 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800601 int rc = 0;
602
Harald Weltef030b212010-04-26 19:18:54 +0200603 /* look up the NSVC based on source address */
604 nsvc = nsvc_by_rem_addr(nsi, saddr);
605 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200606 struct tlv_parsed tp;
607 uint16_t nsei;
Harald Weltef030b212010-04-26 19:18:54 +0200608 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200609 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte803647e2010-05-12 13:51:08 +0000610 struct gprs_nsvc fake_nsvc;
Harald Welteb8a6a832010-05-11 05:54:22 +0200611 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200612 "from %s:%u for non-existing NS-VC\n",
613 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
614 ntohs(saddr->sin_port));
Harald Welte803647e2010-05-12 13:51:08 +0000615 /* Since we have no NSVC, we have to create a fake */
616 fake_nsvc.nsvci = fake_nsvc.nsei = 0;
617 fake_nsvc.nsi = nsi;
618 fake_nsvc.ip.bts_addr = *saddr;
619 fake_nsvc.state = NSE_S_ALIVE;
Harald Welte803647e2010-05-12 13:51:08 +0000620 return gprs_ns_tx_status(&fake_nsvc,
621 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
622 msg);
Harald Weltebbc9fac2010-05-03 18:54:12 +0200623 }
Harald Welte9a392932010-05-11 18:30:37 +0200624 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
625 msgb_l2len(msg), 0, 0);
626 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
627 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
628 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200629 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200630 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
631 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200632 return -EINVAL;
633 }
634 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
635 /* Check if we already know this NSEI, the remote end might
636 * simply have changed addresses, or it is a SGSN */
637 nsvc = nsvc_by_nsei(nsi, nsei);
638 if (!nsvc) {
639 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
640 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
641 nsvc = nsvc_create(nsi, 0xffff);
Harald Welte9a392932010-05-11 18:30:37 +0200642 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200643 /* Update the remote peer IP address/port */
644 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200645 } else
646 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800647
Harald Welte9ba50052010-03-14 15:45:01 +0800648 switch (nsh->pdu_type) {
649 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000650 /* If we're dead and blocked and suddenly receive a
651 * NS-ALIVE out of the blue, we might have been re-started
652 * and should send a NS-RESET to make sure everything recovers
653 * fine. */
654 if (nsvc->state == NSE_S_BLOCKED)
655 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
656 else
657 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800658 break;
659 case NS_PDUT_ALIVE_ACK:
660 /* stop Tns-alive */
Harald Welte80405452010-05-03 20:16:13 +0200661 bsc_del_timer(&nsvc->timer);
Harald Welte9ba50052010-03-14 15:45:01 +0800662 /* start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200663 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200664 if (nsvc->remote_end_is_sgsn) {
665 /* FIXME: this should be one level higher */
666 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200667 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200668 }
Harald Welte9ba50052010-03-14 15:45:01 +0800669 break;
670 case NS_PDUT_UNITDATA:
671 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200672 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800673 break;
674 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200675 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800676 break;
677 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200678 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800679 break;
680 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200681 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200682 /* mark NS-VC as blocked + active */
683 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200684 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte69a4cf22010-05-03 20:55:10 +0200685 if (nsvc->remote_end_is_sgsn) {
686 /* stop RESET timer */
687 bsc_del_timer(&nsvc->timer);
Harald Weltec1402a62010-05-12 11:48:44 +0200688 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte570fb832010-05-03 21:05:24 +0200689 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
690 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte69a4cf22010-05-03 20:55:10 +0200691 }
Harald Welte9ba50052010-03-14 15:45:01 +0800692 break;
693 case NS_PDUT_UNBLOCK:
694 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200695 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800696 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200697 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800698 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800699 break;
700 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200701 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200702 /* mark NS-VC as unblocked + active */
703 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200704 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef88dc002010-05-12 14:18:46 +0000705 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800706 break;
707 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200708 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800709 break;
710 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200711 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200712 /* mark remote NS-VC as blocked + active */
713 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800714 break;
715 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200716 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200717 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800718 rc = -EINVAL;
719 break;
720 }
721 return rc;
722}
723
Harald Weltef030b212010-04-26 19:18:54 +0200724struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
725{
726 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
727
728 nsi->cb = cb;
729 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000730 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
731 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
732 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
733 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
734 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
735 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
736 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200737
Harald Welte3771d092010-04-30 20:26:32 +0200738 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200739}
740
741void gprs_ns_destroy(struct gprs_ns_inst *nsi)
742{
743 /* FIXME: clear all timers */
744
745 /* recursively free the NSI and all its NSVCs */
746 talloc_free(nsi);
747}
748
749
750/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
751 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
752
753/* Read a single NS-over-IP message */
754static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
755 struct sockaddr_in *saddr)
756{
757 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
758 int ret = 0;
759 socklen_t saddr_len = sizeof(*saddr);
760
761 if (!msg) {
762 *error = -ENOMEM;
763 return NULL;
764 }
765
766 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
767 (struct sockaddr *)saddr, &saddr_len);
768 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200769 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200770 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200771 msgb_free(msg);
772 *error = ret;
773 return NULL;
774 } else if (ret == 0) {
775 msgb_free(msg);
776 *error = ret;
777 return NULL;
778 }
779
780 msg->l2h = msg->data;
781 msgb_put(msg, ret);
782
783 return msg;
784}
785
786static int handle_nsip_read(struct bsc_fd *bfd)
787{
788 int error;
789 struct sockaddr_in saddr;
790 struct gprs_ns_inst *nsi = bfd->data;
791 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
792
793 if (!msg)
794 return error;
795
796 return gprs_ns_rcvmsg(nsi, msg, &saddr);
797}
798
799static int handle_nsip_write(struct bsc_fd *bfd)
800{
801 /* FIXME: actually send the data here instead of nsip_sendmsg() */
802 return -EIO;
803}
804
Harald Welte24a655f2010-04-30 19:54:29 +0200805int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200806{
807 int rc;
808 struct gprs_ns_inst *nsi = nsvc->nsi;
809 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
810
811 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
812 (struct sockaddr *)daddr, sizeof(*daddr));
813
814 talloc_free(msg);
815
816 return rc;
817}
818
819/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
820static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
821{
822 int rc = 0;
823
824 if (what & BSC_FD_READ)
825 rc = handle_nsip_read(bfd);
826 if (what & BSC_FD_WRITE)
827 rc = handle_nsip_write(bfd);
828
829 return rc;
830}
831
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200832extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltef030b212010-04-26 19:18:54 +0200833 int (*cb)(struct bsc_fd *fd, unsigned int what));
834
835/* Listen for incoming GPRS packets */
836int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
837{
838 int ret;
839
840 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
841 if (ret < 0)
842 return ret;
843
844 nsi->ll = GPRS_NS_LL_UDP;
845 nsi->nsip.fd.data = nsi;
846
847 return ret;
848}
Harald Welte3771d092010-04-30 20:26:32 +0200849
850/* Establish a connection (from the BSS) to the SGSN */
851struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200852 struct sockaddr_in *dest, uint16_t nsei,
853 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200854{
855 struct gprs_nsvc *nsvc;
856
857 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +0000858 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +0200859 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +0000860 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +0200861 nsvc->nsei = nsei;
862 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200863 nsvc->remote_end_is_sgsn = 1;
864
865 /* Initiate a RESET procedure */
Harald Weltec1402a62010-05-12 11:48:44 +0200866 /* Mark NS-VC locally as blocked and dead */
867 nsvc->state = NSE_S_BLOCKED;
868 /* Send NS-RESET PDU */
Harald Welte570fb832010-05-03 21:05:24 +0200869 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200870 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200871 nsei);
872 }
Harald Weltec1402a62010-05-12 11:48:44 +0200873 /* Start Tns-reset */
Harald Welte69a4cf22010-05-03 20:55:10 +0200874 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte3771d092010-04-30 20:26:32 +0200875
876 return nsvc;
877}
Harald Welte2bffac52010-05-12 15:55:23 +0000878
879#include <vty/vty.h>
880#include <vty/command.h>
881
882static struct gprs_ns_inst *vty_nsi = NULL;
883
884static struct cmd_node ns_node = {
885 NS_NODE,
886 "%s(ns)#",
887 1,
888};
889
890static int config_write_ns(struct vty *vty)
891{
892 struct gprs_nsvc *nsvc;
Harald Weltefe4ab902010-05-12 17:19:53 +0000893 unsigned int i;
Harald Welte2bffac52010-05-12 15:55:23 +0000894
895 vty_out(vty, "ns%s", VTY_NEWLINE);
896
897 llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) {
898 if (!nsvc->persistent)
899 continue;
900 vty_out(vty, " nse %u nsvci %u%s",
901 nsvc->nsei, nsvc->nsvci, VTY_NEWLINE);
902 vty_out(vty, " nse %u remote-role %s%s",
903 nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss",
904 VTY_NEWLINE);
905 if (nsvc->nsi->ll == GPRS_NS_LL_UDP) {
906 vty_out(vty, " nse %u remote-ip %s%s",
907 nsvc->nsei,
908 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
909 VTY_NEWLINE);
910 vty_out(vty, " nse %u remote-port %u%s",
911 nsvc->nsei, ntohs(nsvc->ip.bts_addr.sin_port),
912 VTY_NEWLINE);
913 }
Harald Welte2bffac52010-05-12 15:55:23 +0000914 }
915
Harald Weltefe4ab902010-05-12 17:19:53 +0000916 for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++)
917 vty_out(vty, " timer %s %u%s",
918 get_value_string(gprs_ns_timer_strs, i),
919 vty_nsi->timeout[i], VTY_NEWLINE);
920
Harald Welte2bffac52010-05-12 15:55:23 +0000921 return CMD_SUCCESS;
922}
923
924DEFUN(cfg_ns, cfg_ns_cmd,
925 "ns",
926 "Configure the GPRS Network Service")
927{
928 vty->node = NS_NODE;
929 return CMD_SUCCESS;
930}
931
932DEFUN(show_ns, show_ns_cmd, "show ns",
933 SHOW_STR "Display information about the NS protocol")
934{
935 struct gprs_ns_inst *nsi = vty_nsi;
936 struct gprs_nsvc *nsvc;
937
938 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
939 vty_out(vty, "NSEI %5u, NS-VC %5u, Remote: %-4s, %5s %9s",
940 nsvc->nsei, nsvc->nsvci,
941 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
942 nsvc->state & NSE_S_ALIVE ? "ALIVE" : "DEAD",
943 nsvc->state & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED");
944 if (nsvc->nsi->ll == GPRS_NS_LL_UDP)
945 vty_out(vty, ", %15s:%u",
946 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
947 ntohs(nsvc->ip.bts_addr.sin_port));
948 vty_out(vty, "%s", VTY_NEWLINE);
949 }
950
951 return CMD_SUCCESS;
952}
953
954
955#define NSE_CMD_STR "NS Entity\n" "NS Entity ID (NSEI)\n"
956
957DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
958 "nse <0-65535> nsvci <0-65534>",
959 NSE_CMD_STR
960 "NS Virtual Connection\n"
961 "NS Virtual Connection ID (NSVCI)\n"
962 )
963{
964 uint16_t nsei = atoi(argv[0]);
965 uint16_t nsvci = atoi(argv[1]);
966 struct gprs_nsvc *nsvc;
967
968 nsvc = nsvc_by_nsei(vty_nsi, nsei);
969 if (!nsvc) {
970 nsvc = nsvc_create(vty_nsi, nsvci);
971 nsvc->nsei = nsei;
972 }
973 nsvc->nsvci = nsvci;
974 /* All NSVCs that are explicitly configured by VTY are
975 * marked as persistent so we can write them to the config
976 * file at some later point */
977 nsvc->persistent = 1;
978
979 return CMD_SUCCESS;
980}
981
982DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
983 "nse <0-65535> remote-ip A.B.C.D",
984 NSE_CMD_STR
985 "Remote IP Address\n"
986 "Remote IP Address\n")
987{
988 uint16_t nsei = atoi(argv[0]);
989 struct gprs_nsvc *nsvc;
990
991 nsvc = nsvc_by_nsei(vty_nsi, nsei);
992 if (!nsvc) {
993 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
994 return CMD_WARNING;
995 }
996 inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr);
997
998 return CMD_SUCCESS;
999
1000}
1001
1002DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
1003 "nse <0-65535> remote-port <0-65535>",
1004 NSE_CMD_STR
1005 "Remote UDP Port\n"
1006 "Remote UDP Port Number\n")
1007{
1008 uint16_t nsei = atoi(argv[0]);
1009 uint16_t port = atoi(argv[1]);
1010 struct gprs_nsvc *nsvc;
1011
1012 nsvc = nsvc_by_nsei(vty_nsi, nsei);
1013 if (!nsvc) {
1014 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
1015 return CMD_WARNING;
1016 }
1017
1018 nsvc->ip.bts_addr.sin_port = htons(port);
1019
1020 return CMD_SUCCESS;
1021}
1022
1023DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
1024 "nse <0-65535> remote-role (sgsn|bss)",
1025 NSE_CMD_STR
1026 "Remote NSE Role\n"
1027 "Remote Peer is SGSN\n"
1028 "Remote Peer is BSS\n")
1029{
1030 uint16_t nsei = atoi(argv[0]);
1031 struct gprs_nsvc *nsvc;
1032
1033 nsvc = nsvc_by_nsei(vty_nsi, nsei);
1034 if (!nsvc) {
1035 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
1036 return CMD_WARNING;
1037 }
1038
1039 if (!strcmp(argv[1], "sgsn"))
1040 nsvc->remote_end_is_sgsn = 1;
1041 else
1042 nsvc->remote_end_is_sgsn = 0;
1043
1044 return CMD_SUCCESS;
1045}
1046
1047DEFUN(cfg_no_nse, cfg_no_nse_cmd,
1048 "no nse <0-65535>",
1049 "Delete NS Entity\n"
1050 "Delete " NSE_CMD_STR)
1051{
1052 uint16_t nsei = atoi(argv[0]);
1053 struct gprs_nsvc *nsvc;
1054
1055 nsvc = nsvc_by_nsei(vty_nsi, nsei);
1056 if (!nsvc) {
1057 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
1058 return CMD_WARNING;
1059 }
1060
1061 nsvc_delete(nsvc);
1062
1063 return CMD_SUCCESS;
1064}
1065
Harald Weltefe4ab902010-05-12 17:19:53 +00001066DEFUN(cfg_ns_timer, cfg_ns_timer_cmd,
1067 "timer " NS_TIMERS " <0-65535>",
1068 "Network Service Timer\n"
1069 NS_TIMERS_HELP "Timer Value\n")
1070{
1071 int idx = get_string_value(gprs_ns_timer_strs, argv[0]);
1072 int val = atoi(argv[1]);
1073
1074 if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout))
1075 return CMD_WARNING;
1076
1077 vty_nsi->timeout[idx] = val;
1078
1079 return CMD_SUCCESS;
1080}
1081
Harald Welte2bffac52010-05-12 15:55:23 +00001082int gprs_ns_vty_init(struct gprs_ns_inst *nsi)
1083{
1084 vty_nsi = nsi;
1085
Harald Welteef5324f2010-05-12 16:50:52 +00001086 install_element_ve(&show_ns_cmd);
Harald Welte2bffac52010-05-12 15:55:23 +00001087
1088 install_element(CONFIG_NODE, &cfg_ns_cmd);
1089 install_node(&ns_node, config_write_ns);
1090 install_default(NS_NODE);
1091 install_element(NS_NODE, &cfg_nse_nsvci_cmd);
1092 install_element(NS_NODE, &cfg_nse_remoteip_cmd);
1093 install_element(NS_NODE, &cfg_nse_remoteport_cmd);
1094 install_element(NS_NODE, &cfg_nse_remoterole_cmd);
1095 install_element(NS_NODE, &cfg_no_nse_cmd);
Harald Weltefe4ab902010-05-12 17:19:53 +00001096 install_element(NS_NODE, &cfg_ns_timer_cmd);
Harald Welte2bffac52010-05-12 15:55:23 +00001097
1098 return 0;
1099}