blob: e83aee8e6addb7a7e3daac182ff8f1dadb2410b1 [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 Welte144e0292010-05-13 11:45:07 +020059#include <osmocore/rate_ctr.h>
Harald Welte9ba50052010-03-14 15:45:01 +080060#include <openbsc/debug.h>
Harald Welte834f26d2010-05-11 06:20:54 +020061#include <openbsc/signal.h>
Harald Welte9ba50052010-03-14 15:45:01 +080062#include <openbsc/gprs_ns.h>
63#include <openbsc/gprs_bssgp.h>
64
65#define NS_ALLOC_SIZE 1024
66
67static const struct tlv_definition ns_att_tlvdef = {
68 .def = {
69 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
70 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
71 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
72 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
73 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
74 },
75};
76
Harald Welte144e0292010-05-13 11:45:07 +020077static const struct rate_ctr_desc nsvc_ctr_description[] = {
78 { "packets.in", "Packets at NS Level ( In)" },
79 { "packets.out", "Packets at NS Level (Out)" },
80 { "bytes.in", "Bytes at NS Level ( In)" },
81 { "bytes.out", "Bytes at NS Level (Out)" },
82};
83
84static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
85 .group_prefix_fmt = "ns.nsvc%u",
86 .group_description = "NSVC Peer Statistics",
87 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
88 .ctr_desc = nsvc_ctr_description,
89};
90
Harald Weltef030b212010-04-26 19:18:54 +020091/* Lookup struct gprs_nsvc based on NSVCI */
92static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020093 uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +020094{
95 struct gprs_nsvc *nsvc;
96 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
97 if (nsvc->nsvci == nsvci)
98 return nsvc;
99 }
100 return NULL;
101}
102
Harald Welte24a655f2010-04-30 19:54:29 +0200103/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte144e0292010-05-13 11:45:07 +0200104struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200105{
106 struct gprs_nsvc *nsvc;
107 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
108 if (nsvc->nsei == nsei)
109 return nsvc;
110 }
111 return NULL;
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 Welte144e0292010-05-13 11:45:07 +0200130struct 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;
Harald Welte144e0292010-05-13 11:45:07 +0200141 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200142
Harald Weltef030b212010-04-26 19:18:54 +0200143 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
144
145 return nsvc;
146}
Harald Welte9ba50052010-03-14 15:45:01 +0800147
Harald Welte144e0292010-05-13 11:45:07 +0200148void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000149{
150 if (bsc_timer_pending(&nsvc->timer))
151 bsc_del_timer(&nsvc->timer);
152 llist_del(&nsvc->list);
153 talloc_free(nsvc);
154}
155
Harald Welte2fc725d2010-05-11 10:15:26 +0200156static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200157 uint8_t cause)
158{
159 struct ns_signal_data nssd;
160
161 nssd.nsvc = nsvc;
162 nssd.cause = cause;
163
164 dispatch_signal(SS_NS, signal, &nssd);
165}
166
Harald Welte9ba50052010-03-14 15:45:01 +0800167/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200168static const struct value_string ns_cause_str[] = {
169 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
170 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
171 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
172 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
173 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
174 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
175 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
176 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200177 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200178 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
179 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
180 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800181};
182
Harald Welte2577d412010-05-02 09:37:45 +0200183const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800184{
Harald Welte2577d412010-05-02 09:37:45 +0200185 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800186}
187
Harald Welte24a655f2010-04-30 19:54:29 +0200188static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200189
Harald Welte24a655f2010-04-30 19:54:29 +0200190static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800191{
Harald Weltef030b212010-04-26 19:18:54 +0200192 int ret;
193
Harald Welte144e0292010-05-13 11:45:07 +0200194 /* Increment number of Uplink bytes */
195 rate_ctr_inc(&nsvc->ctrg->ctr[1]);
196 rate_ctr_add(&nsvc->ctrg->ctr[3], msgb_l2len(msg));
197
Harald Weltef030b212010-04-26 19:18:54 +0200198 switch (nsvc->nsi->ll) {
199 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200200 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200201 break;
202 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200203 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200204 msgb_free(msg);
205 ret = -EIO;
206 break;
207 }
208 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800209}
210
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200211static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800212{
213 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
214 struct gprs_ns_hdr *nsh;
215
216 if (!msg)
217 return -ENOMEM;
218
Harald Welte144e0292010-05-13 11:45:07 +0200219 msg->l2h = msgb_put(msg, sizeof(*nsh));
220 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800221
222 nsh->pdu_type = pdu_type;
223
Harald Welte24a655f2010-04-30 19:54:29 +0200224 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800225}
226
Harald Welte834f26d2010-05-11 06:20:54 +0200227int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200228{
229 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
230 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200231 uint16_t nsvci = htons(nsvc->nsvci);
232 uint16_t nsei = htons(nsvc->nsei);
233
234 if (!msg)
235 return -ENOMEM;
236
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000237 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
238 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
239
Harald Welte144e0292010-05-13 11:45:07 +0200240 msg->l2h = msgb_put(msg, sizeof(*nsh));
241 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200242 nsh->pdu_type = NS_PDUT_RESET;
243
244 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
245 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
246 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
247
248 return gprs_ns_tx(nsvc, msg);
249
250}
251
Harald Weltec1402a62010-05-12 11:48:44 +0200252int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
253 uint16_t bvci, struct msgb *orig_msg)
254{
255 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
256 struct gprs_ns_hdr *nsh;
257 uint16_t nsvci = htons(nsvc->nsvci);
258
259 bvci = htons(bvci);
260
261 if (!msg)
262 return -ENOMEM;
263
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000264 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
265 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
266
Harald Welte144e0292010-05-13 11:45:07 +0200267 msg->l2h = msgb_put(msg, sizeof(*nsh));
268 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200269 nsh->pdu_type = NS_PDUT_STATUS;
270
271 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
272
273 /* Section 9.2.7.1: Static conditions for NS-VCI */
274 if (cause == NS_CAUSE_NSVC_BLOCKED ||
275 cause == NS_CAUSE_NSVC_UNKNOWN)
276 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
277
278 /* Section 9.2.7.2: Static conditions for NS PDU */
279 switch (cause) {
280 case NS_CAUSE_SEM_INCORR_PDU:
281 case NS_CAUSE_PDU_INCOMP_PSTATE:
282 case NS_CAUSE_PROTO_ERR_UNSPEC:
283 case NS_CAUSE_INVAL_ESSENT_IE:
284 case NS_CAUSE_MISSING_ESSENT_IE:
285 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
286 orig_msg->l2h);
287 break;
288 default:
289 break;
290 }
291
292 /* Section 9.2.7.3: Static conditions for BVCI */
293 if (cause == NS_CAUSE_BVCI_UNKNOWN)
294 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
295
296 return gprs_ns_tx(nsvc, msg);
297}
298
Harald Welte834f26d2010-05-11 06:20:54 +0200299int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
300{
301 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
302 struct gprs_ns_hdr *nsh;
303 uint16_t nsvci = htons(nsvc->nsvci);
304
305 if (!msg)
306 return -ENOMEM;
307
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000308 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
309 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
310
Harald Welte834f26d2010-05-11 06:20:54 +0200311 /* be conservative and mark it as blocked even now! */
312 nsvc->state |= NSE_S_BLOCKED;
313
Harald Welte144e0292010-05-13 11:45:07 +0200314 msg->l2h = msgb_put(msg, sizeof(*nsh));
315 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200316 nsh->pdu_type = NS_PDUT_BLOCK;
317
318 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
319 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
320
321 return gprs_ns_tx(nsvc, msg);
322}
323
324int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
325{
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000326 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
327 nsvc->nsei, nsvc->nsvci);
328
Harald Welte834f26d2010-05-11 06:20:54 +0200329 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
330}
331
Harald Welteb983c312010-05-12 12:11:45 +0000332int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
333{
334 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
335 nsvc->nsei, nsvc->nsvci);
336
337 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
338}
339
340int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
341{
342 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
343 nsvc->nsei, nsvc->nsvci);
344
345 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
346}
347
Harald Weltefe4ab902010-05-12 17:19:53 +0000348static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
349 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
350 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
351 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200352};
353
Harald Welte7c24b9e2010-05-12 12:21:52 +0000354static const struct value_string timer_mode_strs[] = {
355 { NSVC_TIMER_TNS_RESET, "tns-reset" },
356 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
357 { NSVC_TIMER_TNS_TEST, "tns-test" },
358 { 0, NULL }
359};
360
Harald Welte69a4cf22010-05-03 20:55:10 +0200361static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
362{
Harald Weltefe4ab902010-05-12 17:19:53 +0000363 enum ns_timeout tout = timer_mode_tout[mode];
364 unsigned int seconds = nsvc->nsi->timeout[tout];
365
Harald Welte4e187c62010-05-12 13:55:36 +0000366 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
367 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000368 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000369
Harald Welte69a4cf22010-05-03 20:55:10 +0200370 if (bsc_timer_pending(&nsvc->timer))
371 bsc_del_timer(&nsvc->timer);
372
373 nsvc->timer_mode = mode;
Harald Weltefe4ab902010-05-12 17:19:53 +0000374 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200375}
376
Harald Welte80405452010-05-03 20:16:13 +0200377static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800378{
379 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000380 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
381 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800382
Harald Welte4e187c62010-05-12 13:55:36 +0000383 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
384 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000385 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000386
Harald Welte80405452010-05-03 20:16:13 +0200387 switch (nsvc->timer_mode) {
388 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800389 /* Tns-alive case: we expired without response ! */
390 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000391 if (nsvc->alive_retries >
392 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800393 /* mark as dead and blocked */
394 nsvc->state = NSE_S_BLOCKED;
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200395 LOGP(DNS, LOGL_NOTICE,
396 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200397 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000398 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welte4941b352010-05-12 13:28:25 +0000399 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200400 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800401 return;
402 }
Harald Welteb983c312010-05-12 12:11:45 +0000403 /* Tns-test case: send NS-ALIVE PDU */
404 gprs_ns_tx_alive(nsvc);
405 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200406 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200407 break;
408 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800409 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000410 gprs_ns_tx_alive(nsvc);
411 /* start Tns-alive timer (transition into faster
412 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000413 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200414 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
415 break;
416 case NSVC_TIMER_TNS_RESET:
417 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200418 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200419 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200420 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200421 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200422 case _NSVC_TIMER_NR:
423 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800424 }
Harald Welte9ba50052010-03-14 15:45:01 +0800425}
426
427/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800428static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800429{
430 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
431 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200432 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800433
434 if (!msg)
435 return -ENOMEM;
436
Harald Weltec5478482010-03-18 00:01:43 +0800437 nsvci = htons(nsvc->nsvci);
438 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800439
Harald Welte144e0292010-05-13 11:45:07 +0200440 msg->l2h = msgb_put(msg, sizeof(*nsh));
441 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800442
443 nsh->pdu_type = NS_PDUT_RESET_ACK;
444
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200445 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200446 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800447
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200448 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
449 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800450
Harald Welte24a655f2010-04-30 19:54:29 +0200451 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800452}
453
Harald Welte24a655f2010-04-30 19:54:29 +0200454/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
455int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800456{
Harald Welte24a655f2010-04-30 19:54:29 +0200457 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800458 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200459 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200460
461 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
462 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200463 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200464 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200465 return -EINVAL;
466 }
Harald Welte9ba50052010-03-14 15:45:01 +0800467
Harald Welte69a4cf22010-05-03 20:55:10 +0200468 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200469 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200470 nsvc->nsei);
471 return -EBUSY;
472 }
473 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200474 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200475 nsvc->nsei);
476 return -EBUSY;
477 }
478
Harald Welte9ba50052010-03-14 15:45:01 +0800479 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
480 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200481 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800482 return -EIO;
483 }
484
485 nsh->pdu_type = NS_PDUT_UNITDATA;
486 /* spare octet in data[0] */
487 nsh->data[1] = bvci >> 8;
488 nsh->data[2] = bvci & 0xff;
489
Harald Welte24a655f2010-04-30 19:54:29 +0200490 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800491}
492
493/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200494static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800495{
496 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200497 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800498
Harald Weltec1402a62010-05-12 11:48:44 +0200499 if (nsvc->state & NSE_S_BLOCKED)
500 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
501 0, msg);
502
Harald Welte9ba50052010-03-14 15:45:01 +0800503 /* spare octet in data[0] */
504 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200505 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200506 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800507
508 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200509 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800510}
511
512/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200513static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800514{
Harald Weltef030b212010-04-26 19:18:54 +0200515 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800516 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200517 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800518 int rc;
519
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200520 LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800521
522 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
523
524 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200525 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800526 return -EINVAL;
527 }
528
529 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200530 LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800531
532 return 0;
533}
534
535/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200536static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800537{
538 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800539 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200540 uint8_t *cause;
541 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800542 int rc;
543
Harald Welte9ba50052010-03-14 15:45:01 +0800544 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
545
546 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
547 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
548 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200549 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200550 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800551 return -EINVAL;
552 }
553
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200554 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
555 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
556 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800557
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200558 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200559 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
560
Harald Weltec1402a62010-05-12 11:48:44 +0200561 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800562 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200563
Harald Weltec5478482010-03-18 00:01:43 +0800564 nsvc->nsei = ntohs(*nsei);
565 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800566
Harald Welte9ba50052010-03-14 15:45:01 +0800567 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200568 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800569
Harald Welte834f26d2010-05-11 06:20:54 +0200570 /* inform interested parties about the fact that this NSVC
571 * has received RESET */
Harald Welte2fc725d2010-05-11 10:15:26 +0200572 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200573
Harald Weltec5478482010-03-18 00:01:43 +0800574 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800575}
576
Harald Welte834f26d2010-05-11 06:20:54 +0200577static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
578{
579 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
580 struct tlv_parsed tp;
581 uint8_t *cause;
582 int rc;
583
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200584 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200585
586 nsvc->state |= NSE_S_BLOCKED;
587
588 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
589
590 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
591 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200592 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200593 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200594 return -EINVAL;
595 }
596
597 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
598 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
599
Harald Welte2fc725d2010-05-11 10:15:26 +0200600 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200601
602 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
603}
604
Harald Welte9ba50052010-03-14 15:45:01 +0800605/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200606int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
607 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800608{
609 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200610 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800611 int rc = 0;
612
Harald Welte144e0292010-05-13 11:45:07 +0200613 DEBUGP(DNS, "gprs_ns_rcvmsg(%d)\n", msgb_l2len(msg));
614
Harald Weltef030b212010-04-26 19:18:54 +0200615 /* look up the NSVC based on source address */
616 nsvc = nsvc_by_rem_addr(nsi, saddr);
617 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200618 struct tlv_parsed tp;
619 uint16_t nsei;
Harald Weltef030b212010-04-26 19:18:54 +0200620 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200621 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte803647e2010-05-12 13:51:08 +0000622 struct gprs_nsvc fake_nsvc;
Harald Welteb8a6a832010-05-11 05:54:22 +0200623 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200624 "from %s:%u for non-existing NS-VC\n",
625 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
626 ntohs(saddr->sin_port));
Harald Welte803647e2010-05-12 13:51:08 +0000627 /* Since we have no NSVC, we have to create a fake */
628 fake_nsvc.nsvci = fake_nsvc.nsei = 0;
629 fake_nsvc.nsi = nsi;
630 fake_nsvc.ip.bts_addr = *saddr;
631 fake_nsvc.state = NSE_S_ALIVE;
Harald Welte803647e2010-05-12 13:51:08 +0000632 return gprs_ns_tx_status(&fake_nsvc,
633 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
634 msg);
Harald Weltebbc9fac2010-05-03 18:54:12 +0200635 }
Harald Welte9a392932010-05-11 18:30:37 +0200636 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
637 msgb_l2len(msg), 0, 0);
638 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
639 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
640 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200641 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200642 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
643 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200644 return -EINVAL;
645 }
646 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
647 /* Check if we already know this NSEI, the remote end might
648 * simply have changed addresses, or it is a SGSN */
649 nsvc = nsvc_by_nsei(nsi, nsei);
650 if (!nsvc) {
651 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
652 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
653 nsvc = nsvc_create(nsi, 0xffff);
Harald Welte9a392932010-05-11 18:30:37 +0200654 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200655 /* Update the remote peer IP address/port */
656 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200657 } else
658 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800659
Harald Welte144e0292010-05-13 11:45:07 +0200660 /* Increment number of Incoming bytes */
661 rate_ctr_inc(&nsvc->ctrg->ctr[2]);
662 rate_ctr_add(&nsvc->ctrg->ctr[0], msgb_l2len(msg));
663
Harald Welte9ba50052010-03-14 15:45:01 +0800664 switch (nsh->pdu_type) {
665 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000666 /* If we're dead and blocked and suddenly receive a
667 * NS-ALIVE out of the blue, we might have been re-started
668 * and should send a NS-RESET to make sure everything recovers
669 * fine. */
670 if (nsvc->state == NSE_S_BLOCKED)
671 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
672 else
673 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800674 break;
675 case NS_PDUT_ALIVE_ACK:
676 /* stop Tns-alive */
Harald Welte80405452010-05-03 20:16:13 +0200677 bsc_del_timer(&nsvc->timer);
Harald Welte9ba50052010-03-14 15:45:01 +0800678 /* start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200679 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200680 if (nsvc->remote_end_is_sgsn) {
681 /* FIXME: this should be one level higher */
682 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200683 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200684 }
Harald Welte9ba50052010-03-14 15:45:01 +0800685 break;
686 case NS_PDUT_UNITDATA:
687 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200688 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800689 break;
690 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200691 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800692 break;
693 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200694 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800695 break;
696 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200697 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200698 /* mark NS-VC as blocked + active */
699 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200700 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte69a4cf22010-05-03 20:55:10 +0200701 if (nsvc->remote_end_is_sgsn) {
702 /* stop RESET timer */
703 bsc_del_timer(&nsvc->timer);
Harald Weltec1402a62010-05-12 11:48:44 +0200704 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte570fb832010-05-03 21:05:24 +0200705 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
706 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte69a4cf22010-05-03 20:55:10 +0200707 }
Harald Welte9ba50052010-03-14 15:45:01 +0800708 break;
709 case NS_PDUT_UNBLOCK:
710 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200711 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800712 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200713 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800714 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800715 break;
716 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200717 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200718 /* mark NS-VC as unblocked + active */
719 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200720 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef88dc002010-05-12 14:18:46 +0000721 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800722 break;
723 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200724 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800725 break;
726 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200727 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200728 /* mark remote NS-VC as blocked + active */
729 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800730 break;
731 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200732 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200733 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800734 rc = -EINVAL;
735 break;
736 }
737 return rc;
738}
739
Harald Weltef030b212010-04-26 19:18:54 +0200740struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
741{
742 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
743
744 nsi->cb = cb;
745 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000746 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
747 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
748 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
749 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
750 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
751 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
752 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200753
Harald Welte3771d092010-04-30 20:26:32 +0200754 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200755}
756
757void gprs_ns_destroy(struct gprs_ns_inst *nsi)
758{
759 /* FIXME: clear all timers */
760
761 /* recursively free the NSI and all its NSVCs */
762 talloc_free(nsi);
763}
764
765
766/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
767 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
768
769/* Read a single NS-over-IP message */
770static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
771 struct sockaddr_in *saddr)
772{
773 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
774 int ret = 0;
775 socklen_t saddr_len = sizeof(*saddr);
776
777 if (!msg) {
778 *error = -ENOMEM;
779 return NULL;
780 }
781
782 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
783 (struct sockaddr *)saddr, &saddr_len);
784 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200785 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200786 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200787 msgb_free(msg);
788 *error = ret;
789 return NULL;
790 } else if (ret == 0) {
791 msgb_free(msg);
792 *error = ret;
793 return NULL;
794 }
795
796 msg->l2h = msg->data;
797 msgb_put(msg, ret);
798
799 return msg;
800}
801
802static int handle_nsip_read(struct bsc_fd *bfd)
803{
804 int error;
805 struct sockaddr_in saddr;
806 struct gprs_ns_inst *nsi = bfd->data;
807 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
808
809 if (!msg)
810 return error;
811
Harald Welte91813cf2010-05-12 18:38:45 +0000812 error = gprs_ns_rcvmsg(nsi, msg, &saddr);
813
814 msgb_free(msg);
815
816 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200817}
818
819static int handle_nsip_write(struct bsc_fd *bfd)
820{
821 /* FIXME: actually send the data here instead of nsip_sendmsg() */
822 return -EIO;
823}
824
Harald Welte24a655f2010-04-30 19:54:29 +0200825int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200826{
827 int rc;
828 struct gprs_ns_inst *nsi = nsvc->nsi;
829 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
830
831 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
832 (struct sockaddr *)daddr, sizeof(*daddr));
833
834 talloc_free(msg);
835
836 return rc;
837}
838
839/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
840static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
841{
842 int rc = 0;
843
844 if (what & BSC_FD_READ)
845 rc = handle_nsip_read(bfd);
846 if (what & BSC_FD_WRITE)
847 rc = handle_nsip_write(bfd);
848
849 return rc;
850}
851
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200852extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltef030b212010-04-26 19:18:54 +0200853 int (*cb)(struct bsc_fd *fd, unsigned int what));
854
855/* Listen for incoming GPRS packets */
856int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
857{
858 int ret;
859
860 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
861 if (ret < 0)
862 return ret;
863
864 nsi->ll = GPRS_NS_LL_UDP;
865 nsi->nsip.fd.data = nsi;
866
867 return ret;
868}
Harald Welte3771d092010-04-30 20:26:32 +0200869
870/* Establish a connection (from the BSS) to the SGSN */
871struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200872 struct sockaddr_in *dest, uint16_t nsei,
873 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200874{
875 struct gprs_nsvc *nsvc;
876
877 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +0000878 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +0200879 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +0000880 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +0200881 nsvc->nsei = nsei;
882 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200883 nsvc->remote_end_is_sgsn = 1;
884
885 /* Initiate a RESET procedure */
Harald Weltec1402a62010-05-12 11:48:44 +0200886 /* Mark NS-VC locally as blocked and dead */
887 nsvc->state = NSE_S_BLOCKED;
888 /* Send NS-RESET PDU */
Harald Welte570fb832010-05-03 21:05:24 +0200889 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200890 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200891 nsei);
892 }
Harald Weltec1402a62010-05-12 11:48:44 +0200893 /* Start Tns-reset */
Harald Welte69a4cf22010-05-03 20:55:10 +0200894 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte3771d092010-04-30 20:26:32 +0200895
896 return nsvc;
897}
Harald Welte2bffac52010-05-12 15:55:23 +0000898
Harald Welte2bffac52010-05-12 15:55:23 +0000899