blob: 3d115c114bb85a375ccb4287d7a3a27c4625093a [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
Harald Weltedd1c83c2010-05-13 13:58:08 +020047/* This implementation has the following limitations:
48 * o Only one NS-VC for each NSE: No load-sharing function
49 * o NSVCI 65535 and 65534 are reserved for internal use
50 * o Only UDP is supported as of now, no frame relay support
51 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
52 * o There are no BLOCK and UNBLOCK timers (yet?)
53 */
54
Harald Welte9ba50052010-03-14 15:45:01 +080055#include <stdlib.h>
56#include <unistd.h>
57#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020058#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080059
60#include <arpa/inet.h>
61
62#include <openbsc/gsm_data.h>
63#include <osmocore/msgb.h>
64#include <osmocore/tlv.h>
65#include <osmocore/talloc.h>
Harald Weltef030b212010-04-26 19:18:54 +020066#include <osmocore/select.h>
Harald Welte144e0292010-05-13 11:45:07 +020067#include <osmocore/rate_ctr.h>
Harald Welte9ba50052010-03-14 15:45:01 +080068#include <openbsc/debug.h>
Harald Welte834f26d2010-05-11 06:20:54 +020069#include <openbsc/signal.h>
Harald Welte9ba50052010-03-14 15:45:01 +080070#include <openbsc/gprs_ns.h>
71#include <openbsc/gprs_bssgp.h>
Holger Hans Peter Freytheree59fe42010-05-23 21:23:44 +080072#include <openbsc/gprs_ns_frgre.h>
Holger Hans Peter Freythere75a34f2010-05-23 21:19:55 +080073#include <openbsc/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080074
Harald Welte9ba50052010-03-14 15:45:01 +080075static const struct tlv_definition ns_att_tlvdef = {
76 .def = {
77 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
78 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
79 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
80 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
81 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
82 },
83};
84
Harald Weltec51c23c2010-05-13 12:55:20 +020085enum ns_ctr {
86 NS_CTR_PKTS_IN,
87 NS_CTR_PKTS_OUT,
88 NS_CTR_BYTES_IN,
89 NS_CTR_BYTES_OUT,
90 NS_CTR_BLOCKED,
91 NS_CTR_DEAD,
92};
93
Harald Welte144e0292010-05-13 11:45:07 +020094static const struct rate_ctr_desc nsvc_ctr_description[] = {
95 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +020096 { "packets.out","Packets at NS Level (Out)" },
97 { "bytes.in", "Bytes at NS Level ( In)" },
98 { "bytes.out", "Bytes at NS Level (Out)" },
99 { "blocked", "NS-VC Block count " },
100 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +0200101};
102
103static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200104 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200105 .group_description = "NSVC Peer Statistics",
106 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
107 .ctr_desc = nsvc_ctr_description,
108};
109
Harald Weltef030b212010-04-26 19:18:54 +0200110/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte90af1942010-05-15 23:02:24 +0200111struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200112{
113 struct gprs_nsvc *nsvc;
114 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
115 if (nsvc->nsvci == nsvci)
116 return nsvc;
117 }
118 return NULL;
119}
120
Harald Welte24a655f2010-04-30 19:54:29 +0200121/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte144e0292010-05-13 11:45:07 +0200122struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200123{
124 struct gprs_nsvc *nsvc;
125 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
126 if (nsvc->nsei == nsei)
127 return nsvc;
128 }
129 return NULL;
130}
131
Harald Weltef030b212010-04-26 19:18:54 +0200132/* Lookup struct gprs_nsvc based on remote peer socket addr */
133static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
134 struct sockaddr_in *sin)
135{
136 struct gprs_nsvc *nsvc;
137 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000138 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
139 sin->sin_addr.s_addr &&
140 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200141 return nsvc;
142 }
143 return NULL;
144}
145
Harald Welte69a4cf22010-05-03 20:55:10 +0200146static void gprs_ns_timer_cb(void *data);
147
Harald Welte144e0292010-05-13 11:45:07 +0200148struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200149{
150 struct gprs_nsvc *nsvc;
151
152 nsvc = talloc_zero(nsi, struct gprs_nsvc);
153 nsvc->nsvci = nsvci;
154 /* before RESET procedure: BLOCKED and DEAD */
155 nsvc->state = NSE_S_BLOCKED;
156 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200157 nsvc->timer.cb = gprs_ns_timer_cb;
158 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200159 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200160
Harald Weltef030b212010-04-26 19:18:54 +0200161 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
162
163 return nsvc;
164}
Harald Welte9ba50052010-03-14 15:45:01 +0800165
Harald Welte144e0292010-05-13 11:45:07 +0200166void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000167{
168 if (bsc_timer_pending(&nsvc->timer))
169 bsc_del_timer(&nsvc->timer);
170 llist_del(&nsvc->list);
171 talloc_free(nsvc);
172}
173
Harald Welte2fc725d2010-05-11 10:15:26 +0200174static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200175 uint8_t cause)
176{
177 struct ns_signal_data nssd;
178
179 nssd.nsvc = nsvc;
180 nssd.cause = cause;
181
182 dispatch_signal(SS_NS, signal, &nssd);
183}
184
Harald Welte9ba50052010-03-14 15:45:01 +0800185/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200186static const struct value_string ns_cause_str[] = {
187 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
188 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
189 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
190 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
191 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
192 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
193 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
194 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200195 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200196 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
197 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
198 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800199};
200
Harald Welte2577d412010-05-02 09:37:45 +0200201const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800202{
Harald Welte2577d412010-05-02 09:37:45 +0200203 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800204}
205
Harald Welte24a655f2010-04-30 19:54:29 +0200206static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200207extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200208
Harald Welte24a655f2010-04-30 19:54:29 +0200209static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800210{
Harald Weltef030b212010-04-26 19:18:54 +0200211 int ret;
212
Harald Welte91f7f4b2010-05-15 23:52:02 +0200213 log_set_context(BSC_CTX_NSVC, nsvc);
214
Harald Welte144e0292010-05-13 11:45:07 +0200215 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200216 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
217 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200218
Harald Welteb3ee2652010-05-19 14:38:50 +0200219 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200220 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200221 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200222 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200223 case GPRS_NS_LL_FR_GRE:
224 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
225 break;
Harald Weltef030b212010-04-26 19:18:54 +0200226 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200227 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200228 msgb_free(msg);
229 ret = -EIO;
230 break;
231 }
232 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800233}
234
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200235static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800236{
Harald Welteba4c6662010-05-19 15:38:10 +0200237 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800238 struct gprs_ns_hdr *nsh;
239
Harald Welte91f7f4b2010-05-15 23:52:02 +0200240 log_set_context(BSC_CTX_NSVC, nsvc);
241
Harald Welte9ba50052010-03-14 15:45:01 +0800242 if (!msg)
243 return -ENOMEM;
244
Harald Welte144e0292010-05-13 11:45:07 +0200245 msg->l2h = msgb_put(msg, sizeof(*nsh));
246 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800247
248 nsh->pdu_type = pdu_type;
249
Harald Welte24a655f2010-04-30 19:54:29 +0200250 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800251}
252
Harald Welte834f26d2010-05-11 06:20:54 +0200253int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200254{
Harald Welteba4c6662010-05-19 15:38:10 +0200255 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200256 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200257 uint16_t nsvci = htons(nsvc->nsvci);
258 uint16_t nsei = htons(nsvc->nsei);
259
Harald Welte91f7f4b2010-05-15 23:52:02 +0200260 log_set_context(BSC_CTX_NSVC, nsvc);
261
Harald Welte69a4cf22010-05-03 20:55:10 +0200262 if (!msg)
263 return -ENOMEM;
264
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000265 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
266 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
267
Harald Welte144e0292010-05-13 11:45:07 +0200268 msg->l2h = msgb_put(msg, sizeof(*nsh));
269 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200270 nsh->pdu_type = NS_PDUT_RESET;
271
272 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
273 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
274 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
275
276 return gprs_ns_tx(nsvc, msg);
277
278}
279
Harald Weltec1402a62010-05-12 11:48:44 +0200280int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
281 uint16_t bvci, struct msgb *orig_msg)
282{
Harald Welteba4c6662010-05-19 15:38:10 +0200283 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200284 struct gprs_ns_hdr *nsh;
285 uint16_t nsvci = htons(nsvc->nsvci);
286
Harald Welte91f7f4b2010-05-15 23:52:02 +0200287 log_set_context(BSC_CTX_NSVC, nsvc);
288
Harald Weltec1402a62010-05-12 11:48:44 +0200289 bvci = htons(bvci);
290
291 if (!msg)
292 return -ENOMEM;
293
Harald Welte90af1942010-05-15 23:02:24 +0200294 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000295 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
296
Harald Welte144e0292010-05-13 11:45:07 +0200297 msg->l2h = msgb_put(msg, sizeof(*nsh));
298 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200299 nsh->pdu_type = NS_PDUT_STATUS;
300
301 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
302
303 /* Section 9.2.7.1: Static conditions for NS-VCI */
304 if (cause == NS_CAUSE_NSVC_BLOCKED ||
305 cause == NS_CAUSE_NSVC_UNKNOWN)
306 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
307
308 /* Section 9.2.7.2: Static conditions for NS PDU */
309 switch (cause) {
310 case NS_CAUSE_SEM_INCORR_PDU:
311 case NS_CAUSE_PDU_INCOMP_PSTATE:
312 case NS_CAUSE_PROTO_ERR_UNSPEC:
313 case NS_CAUSE_INVAL_ESSENT_IE:
314 case NS_CAUSE_MISSING_ESSENT_IE:
315 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
316 orig_msg->l2h);
317 break;
318 default:
319 break;
320 }
321
322 /* Section 9.2.7.3: Static conditions for BVCI */
323 if (cause == NS_CAUSE_BVCI_UNKNOWN)
324 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
325
326 return gprs_ns_tx(nsvc, msg);
327}
328
Harald Welte834f26d2010-05-11 06:20:54 +0200329int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
330{
Harald Welteba4c6662010-05-19 15:38:10 +0200331 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200332 struct gprs_ns_hdr *nsh;
333 uint16_t nsvci = htons(nsvc->nsvci);
334
Harald Welte91f7f4b2010-05-15 23:52:02 +0200335 log_set_context(BSC_CTX_NSVC, nsvc);
336
Harald Welte834f26d2010-05-11 06:20:54 +0200337 if (!msg)
338 return -ENOMEM;
339
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000340 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
341 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
342
Harald Welte834f26d2010-05-11 06:20:54 +0200343 /* be conservative and mark it as blocked even now! */
344 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200345 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200346
Harald Welte144e0292010-05-13 11:45:07 +0200347 msg->l2h = msgb_put(msg, sizeof(*nsh));
348 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200349 nsh->pdu_type = NS_PDUT_BLOCK;
350
351 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
352 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
353
354 return gprs_ns_tx(nsvc, msg);
355}
356
357int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
358{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200359 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000360 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
361 nsvc->nsei, nsvc->nsvci);
362
Harald Welte834f26d2010-05-11 06:20:54 +0200363 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
364}
365
Harald Welteb983c312010-05-12 12:11:45 +0000366int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
367{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200368 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000369 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
370 nsvc->nsei, nsvc->nsvci);
371
372 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
373}
374
375int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
376{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200377 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000378 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
379 nsvc->nsei, nsvc->nsvci);
380
381 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
382}
383
Harald Weltefe4ab902010-05-12 17:19:53 +0000384static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
385 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
386 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
387 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200388};
389
Harald Welte7c24b9e2010-05-12 12:21:52 +0000390static const struct value_string timer_mode_strs[] = {
391 { NSVC_TIMER_TNS_RESET, "tns-reset" },
392 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
393 { NSVC_TIMER_TNS_TEST, "tns-test" },
394 { 0, NULL }
395};
396
Harald Welte69a4cf22010-05-03 20:55:10 +0200397static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
398{
Harald Weltefe4ab902010-05-12 17:19:53 +0000399 enum ns_timeout tout = timer_mode_tout[mode];
400 unsigned int seconds = nsvc->nsi->timeout[tout];
401
Harald Welte91f7f4b2010-05-15 23:52:02 +0200402 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000403 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
404 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000405 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000406
Harald Welte69a4cf22010-05-03 20:55:10 +0200407 if (bsc_timer_pending(&nsvc->timer))
408 bsc_del_timer(&nsvc->timer);
409
410 nsvc->timer_mode = mode;
Harald Weltefe4ab902010-05-12 17:19:53 +0000411 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200412}
413
Harald Welte80405452010-05-03 20:16:13 +0200414static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800415{
416 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000417 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
418 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800419
Harald Welte91f7f4b2010-05-15 23:52:02 +0200420 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000421 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
422 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000423 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000424
Harald Welte80405452010-05-03 20:16:13 +0200425 switch (nsvc->timer_mode) {
426 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800427 /* Tns-alive case: we expired without response ! */
428 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000429 if (nsvc->alive_retries >
430 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800431 /* mark as dead and blocked */
432 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200433 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
434 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200435 LOGP(DNS, LOGL_NOTICE,
436 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200437 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000438 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welte4941b352010-05-12 13:28:25 +0000439 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200440 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800441 return;
442 }
Harald Welteb983c312010-05-12 12:11:45 +0000443 /* Tns-test case: send NS-ALIVE PDU */
444 gprs_ns_tx_alive(nsvc);
445 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200446 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200447 break;
448 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800449 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000450 gprs_ns_tx_alive(nsvc);
451 /* start Tns-alive timer (transition into faster
452 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000453 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200454 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
455 break;
456 case NSVC_TIMER_TNS_RESET:
457 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200458 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200459 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200460 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200461 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200462 case _NSVC_TIMER_NR:
463 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800464 }
Harald Welte9ba50052010-03-14 15:45:01 +0800465}
466
467/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800468static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800469{
Harald Welteba4c6662010-05-19 15:38:10 +0200470 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800471 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200472 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800473
Harald Welte91f7f4b2010-05-15 23:52:02 +0200474 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800475 if (!msg)
476 return -ENOMEM;
477
Harald Weltec5478482010-03-18 00:01:43 +0800478 nsvci = htons(nsvc->nsvci);
479 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800480
Harald Welte144e0292010-05-13 11:45:07 +0200481 msg->l2h = msgb_put(msg, sizeof(*nsh));
482 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800483
484 nsh->pdu_type = NS_PDUT_RESET_ACK;
485
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200486 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200487 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800488
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200489 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
490 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800491
Harald Welte24a655f2010-04-30 19:54:29 +0200492 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800493}
494
Harald Welte24a655f2010-04-30 19:54:29 +0200495/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
496int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800497{
Harald Welte24a655f2010-04-30 19:54:29 +0200498 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800499 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200500 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200501
502 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
503 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200504 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200505 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200506 return -EINVAL;
507 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200508 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800509
Harald Welte69a4cf22010-05-03 20:55:10 +0200510 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200511 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200512 nsvc->nsei);
513 return -EBUSY;
514 }
515 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200516 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200517 nsvc->nsei);
518 return -EBUSY;
519 }
520
Harald Welteec20ba42010-05-13 12:18:49 +0200521 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
522 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800523 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200524 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800525 return -EIO;
526 }
527
528 nsh->pdu_type = NS_PDUT_UNITDATA;
529 /* spare octet in data[0] */
530 nsh->data[1] = bvci >> 8;
531 nsh->data[2] = bvci & 0xff;
532
Harald Welte24a655f2010-04-30 19:54:29 +0200533 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800534}
535
536/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200537static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800538{
539 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200540 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800541
Harald Weltec1402a62010-05-12 11:48:44 +0200542 if (nsvc->state & NSE_S_BLOCKED)
543 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
544 0, msg);
545
Harald Welte9ba50052010-03-14 15:45:01 +0800546 /* spare octet in data[0] */
547 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200548 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200549 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800550
551 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200552 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800553}
554
555/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200556static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800557{
Harald Weltef030b212010-04-26 19:18:54 +0200558 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800559 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200560 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800561 int rc;
562
Harald Welte41337832010-05-16 00:24:26 +0200563 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800564
565 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
566
567 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200568 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800569 return -EINVAL;
570 }
571
572 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200573 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800574
575 return 0;
576}
577
578/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200579static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800580{
581 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800582 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200583 uint8_t *cause;
584 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800585 int rc;
586
Harald Welte9ba50052010-03-14 15:45:01 +0800587 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
588
589 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
590 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
591 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +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 Welte9ba50052010-03-14 15:45:01 +0800594 return -EINVAL;
595 }
596
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200597 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
598 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
599 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800600
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200601 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200602 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
603
Harald Weltec1402a62010-05-12 11:48:44 +0200604 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800605 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200606
Harald Weltec5478482010-03-18 00:01:43 +0800607 nsvc->nsei = ntohs(*nsei);
608 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800609
Harald Welte9ba50052010-03-14 15:45:01 +0800610 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200611 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800612
Harald Welte834f26d2010-05-11 06:20:54 +0200613 /* inform interested parties about the fact that this NSVC
614 * has received RESET */
Harald Welte2fc725d2010-05-11 10:15:26 +0200615 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200616
Harald Weltec5478482010-03-18 00:01:43 +0800617 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800618}
619
Harald Welte834f26d2010-05-11 06:20:54 +0200620static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
621{
622 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
623 struct tlv_parsed tp;
624 uint8_t *cause;
625 int rc;
626
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200627 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200628
629 nsvc->state |= NSE_S_BLOCKED;
630
631 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
632
633 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
634 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200635 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200636 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200637 return -EINVAL;
638 }
639
640 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
641 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
642
Harald Welte2fc725d2010-05-11 10:15:26 +0200643 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200644 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200645
646 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
647}
648
Harald Welte9ba50052010-03-14 15:45:01 +0800649/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200650int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200651 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800652{
653 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200654 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800655 int rc = 0;
656
Harald Weltef030b212010-04-26 19:18:54 +0200657 /* look up the NSVC based on source address */
658 nsvc = nsvc_by_rem_addr(nsi, saddr);
659 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200660 struct tlv_parsed tp;
661 uint16_t nsei;
Harald Welte1100a9d2010-05-19 16:01:39 +0200662 if (nsh->pdu_type == NS_PDUT_STATUS) {
663 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
664 "for non-existing NS-VC\n",
665 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
666 return 0;
667 }
Harald Weltef030b212010-04-26 19:18:54 +0200668 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200669 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200670 /* Since we have no NSVC, we have to use a fake */
671 nsvc = nsi->unknown_nsvc;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200672 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200673 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200674 "from %s:%u for non-existing NS-VC\n",
675 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
676 ntohs(saddr->sin_port));
Harald Weltedd1c83c2010-05-13 13:58:08 +0200677 nsvc->nsvci = nsvc->nsei = 0xfffe;
678 nsvc->ip.bts_addr = *saddr;
679 nsvc->state = NSE_S_ALIVE;
Harald Welteb3ee2652010-05-19 14:38:50 +0200680 nsvc->ll = ll;
Harald Weltee0c42d12010-05-18 14:32:29 +0200681#if 0
682 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
683#else
Harald Weltedd1c83c2010-05-13 13:58:08 +0200684 return gprs_ns_tx_status(nsvc,
Harald Welte803647e2010-05-12 13:51:08 +0000685 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
686 msg);
Harald Weltee0c42d12010-05-18 14:32:29 +0200687#endif
Harald Weltebbc9fac2010-05-03 18:54:12 +0200688 }
Harald Welte9a392932010-05-11 18:30:37 +0200689 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
690 msgb_l2len(msg), 0, 0);
691 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
692 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
693 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200694 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200695 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
696 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200697 return -EINVAL;
698 }
699 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
700 /* Check if we already know this NSEI, the remote end might
701 * simply have changed addresses, or it is a SGSN */
702 nsvc = nsvc_by_nsei(nsi, nsei);
703 if (!nsvc) {
Harald Welte91f7f4b2010-05-15 23:52:02 +0200704 nsvc = nsvc_create(nsi, 0xffff);
Harald Welteb3ee2652010-05-19 14:38:50 +0200705 nsvc->ll = ll;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200706 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9a392932010-05-11 18:30:37 +0200707 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
708 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Welte9a392932010-05-11 18:30:37 +0200709 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200710 /* Update the remote peer IP address/port */
711 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200712 } else
713 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800714
Harald Welte91f7f4b2010-05-15 23:52:02 +0200715 log_set_context(BSC_CTX_NSVC, nsvc);
716
Harald Welte144e0292010-05-13 11:45:07 +0200717 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200718 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200719 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200720
Harald Welte9ba50052010-03-14 15:45:01 +0800721 switch (nsh->pdu_type) {
722 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000723 /* If we're dead and blocked and suddenly receive a
724 * NS-ALIVE out of the blue, we might have been re-started
725 * and should send a NS-RESET to make sure everything recovers
726 * fine. */
727 if (nsvc->state == NSE_S_BLOCKED)
728 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
729 else
730 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800731 break;
732 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200733 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200734 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200735 if (nsvc->remote_end_is_sgsn) {
736 /* FIXME: this should be one level higher */
737 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200738 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200739 }
Harald Welte9ba50052010-03-14 15:45:01 +0800740 break;
741 case NS_PDUT_UNITDATA:
742 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200743 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800744 break;
745 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200746 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800747 break;
748 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200749 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800750 break;
751 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200752 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200753 /* mark NS-VC as blocked + active */
754 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200755 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200756 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000757 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200758 /* stop RESET timer */
759 bsc_del_timer(&nsvc->timer);
760 }
Harald Welte90af1942010-05-15 23:02:24 +0200761 /* Initiate TEST proc.: Send ALIVE and start timer */
762 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
763 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800764 break;
765 case NS_PDUT_UNBLOCK:
766 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200767 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800768 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200769 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800770 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800771 break;
772 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200773 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200774 /* mark NS-VC as unblocked + active */
775 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200776 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef88dc002010-05-12 14:18:46 +0000777 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800778 break;
779 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200780 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800781 break;
782 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200783 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200784 /* mark remote NS-VC as blocked + active */
785 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800786 break;
787 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200788 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200789 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800790 rc = -EINVAL;
791 break;
792 }
793 return rc;
794}
795
Harald Weltef030b212010-04-26 19:18:54 +0200796struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
797{
798 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
799
800 nsi->cb = cb;
801 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000802 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
803 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
804 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
805 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
806 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
807 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
808 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200809
Harald Welte60cc6ac2010-05-13 14:20:56 +0200810 /* Create the dummy NSVC that we use for sending
811 * messages to non-existant/unknown NS-VC's */
Harald Weltedd1c83c2010-05-13 13:58:08 +0200812 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +0200813 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200814
Harald Welte3771d092010-04-30 20:26:32 +0200815 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200816}
817
818void gprs_ns_destroy(struct gprs_ns_inst *nsi)
819{
820 /* FIXME: clear all timers */
821
822 /* recursively free the NSI and all its NSVCs */
823 talloc_free(nsi);
824}
825
826
827/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
828 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
829
830/* Read a single NS-over-IP message */
831static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
832 struct sockaddr_in *saddr)
833{
Harald Welteba4c6662010-05-19 15:38:10 +0200834 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +0200835 int ret = 0;
836 socklen_t saddr_len = sizeof(*saddr);
837
838 if (!msg) {
839 *error = -ENOMEM;
840 return NULL;
841 }
842
843 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
844 (struct sockaddr *)saddr, &saddr_len);
845 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200846 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200847 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200848 msgb_free(msg);
849 *error = ret;
850 return NULL;
851 } else if (ret == 0) {
852 msgb_free(msg);
853 *error = ret;
854 return NULL;
855 }
856
857 msg->l2h = msg->data;
858 msgb_put(msg, ret);
859
860 return msg;
861}
862
863static int handle_nsip_read(struct bsc_fd *bfd)
864{
865 int error;
866 struct sockaddr_in saddr;
867 struct gprs_ns_inst *nsi = bfd->data;
868 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
869
870 if (!msg)
871 return error;
872
Harald Welteb3ee2652010-05-19 14:38:50 +0200873 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +0000874
875 msgb_free(msg);
876
877 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200878}
879
880static int handle_nsip_write(struct bsc_fd *bfd)
881{
882 /* FIXME: actually send the data here instead of nsip_sendmsg() */
883 return -EIO;
884}
885
Harald Welteb3ee2652010-05-19 14:38:50 +0200886static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200887{
888 int rc;
889 struct gprs_ns_inst *nsi = nsvc->nsi;
890 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
891
892 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
893 (struct sockaddr *)daddr, sizeof(*daddr));
894
895 talloc_free(msg);
896
897 return rc;
898}
899
900/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
901static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
902{
903 int rc = 0;
904
905 if (what & BSC_FD_READ)
906 rc = handle_nsip_read(bfd);
907 if (what & BSC_FD_WRITE)
908 rc = handle_nsip_write(bfd);
909
910 return rc;
911}
912
Harald Weltef030b212010-04-26 19:18:54 +0200913/* Listen for incoming GPRS packets */
Harald Welte7fb05232010-05-19 15:09:09 +0200914int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +0200915{
916 int ret;
917
Harald Welte7fb05232010-05-19 15:09:09 +0200918 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
919 nsi->nsip.local_port, nsip_fd_cb);
Harald Weltef030b212010-04-26 19:18:54 +0200920 if (ret < 0)
921 return ret;
922
Harald Weltef030b212010-04-26 19:18:54 +0200923 nsi->nsip.fd.data = nsi;
924
925 return ret;
926}
Harald Welte3771d092010-04-30 20:26:32 +0200927
Harald Welte731d1fc2010-05-14 11:53:08 +0000928/* Initiate a RESET procedure */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800929void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +0000930{
931 /* Mark NS-VC locally as blocked and dead */
932 nsvc->state = NSE_S_BLOCKED;
933 /* Send NS-RESET PDU */
934 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
935 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
936 nsvc->nsei);
937 }
938 /* Start Tns-reset */
939 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +0000940}
941
Harald Welte3771d092010-04-30 20:26:32 +0200942/* Establish a connection (from the BSS) to the SGSN */
943struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200944 struct sockaddr_in *dest, uint16_t nsei,
945 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200946{
947 struct gprs_nsvc *nsvc;
948
949 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +0000950 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +0200951 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +0000952 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +0200953 nsvc->nsei = nsei;
954 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200955 nsvc->remote_end_is_sgsn = 1;
956
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800957 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
958 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +0200959}