blob: 467e43ac518b057b99330ed9568ae0827d8fc534 [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>
72
Harald Welte9ba50052010-03-14 15:45:01 +080073static const struct tlv_definition ns_att_tlvdef = {
74 .def = {
75 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
76 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
77 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
78 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
79 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
80 },
81};
82
Harald Weltec51c23c2010-05-13 12:55:20 +020083enum ns_ctr {
84 NS_CTR_PKTS_IN,
85 NS_CTR_PKTS_OUT,
86 NS_CTR_BYTES_IN,
87 NS_CTR_BYTES_OUT,
88 NS_CTR_BLOCKED,
89 NS_CTR_DEAD,
90};
91
Harald Welte144e0292010-05-13 11:45:07 +020092static const struct rate_ctr_desc nsvc_ctr_description[] = {
93 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +020094 { "packets.out","Packets at NS Level (Out)" },
95 { "bytes.in", "Bytes at NS Level ( In)" },
96 { "bytes.out", "Bytes at NS Level (Out)" },
97 { "blocked", "NS-VC Block count " },
98 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +020099};
100
101static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200102 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200103 .group_description = "NSVC Peer Statistics",
104 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
105 .ctr_desc = nsvc_ctr_description,
106};
107
Harald Weltef030b212010-04-26 19:18:54 +0200108/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte90af1942010-05-15 23:02:24 +0200109struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200110{
111 struct gprs_nsvc *nsvc;
112 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
113 if (nsvc->nsvci == nsvci)
114 return nsvc;
115 }
116 return NULL;
117}
118
Harald Welte24a655f2010-04-30 19:54:29 +0200119/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte144e0292010-05-13 11:45:07 +0200120struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200121{
122 struct gprs_nsvc *nsvc;
123 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
124 if (nsvc->nsei == nsei)
125 return nsvc;
126 }
127 return NULL;
128}
129
Harald Weltef030b212010-04-26 19:18:54 +0200130/* Lookup struct gprs_nsvc based on remote peer socket addr */
131static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
132 struct sockaddr_in *sin)
133{
134 struct gprs_nsvc *nsvc;
135 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000136 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
137 sin->sin_addr.s_addr &&
138 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200139 return nsvc;
140 }
141 return NULL;
142}
143
Harald Welte69a4cf22010-05-03 20:55:10 +0200144static void gprs_ns_timer_cb(void *data);
145
Harald Welte144e0292010-05-13 11:45:07 +0200146struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200147{
148 struct gprs_nsvc *nsvc;
149
150 nsvc = talloc_zero(nsi, struct gprs_nsvc);
151 nsvc->nsvci = nsvci;
152 /* before RESET procedure: BLOCKED and DEAD */
153 nsvc->state = NSE_S_BLOCKED;
154 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200155 nsvc->timer.cb = gprs_ns_timer_cb;
156 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200157 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200158
Harald Weltef030b212010-04-26 19:18:54 +0200159 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
160
161 return nsvc;
162}
Harald Welte9ba50052010-03-14 15:45:01 +0800163
Harald Welte144e0292010-05-13 11:45:07 +0200164void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000165{
166 if (bsc_timer_pending(&nsvc->timer))
167 bsc_del_timer(&nsvc->timer);
168 llist_del(&nsvc->list);
169 talloc_free(nsvc);
170}
171
Harald Welte2fc725d2010-05-11 10:15:26 +0200172static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200173 uint8_t cause)
174{
175 struct ns_signal_data nssd;
176
177 nssd.nsvc = nsvc;
178 nssd.cause = cause;
179
180 dispatch_signal(SS_NS, signal, &nssd);
181}
182
Harald Welte9ba50052010-03-14 15:45:01 +0800183/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200184static const struct value_string ns_cause_str[] = {
185 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
186 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
187 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
188 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
189 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
190 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
191 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
192 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200193 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200194 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
195 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
196 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800197};
198
Harald Welte2577d412010-05-02 09:37:45 +0200199const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800200{
Harald Welte2577d412010-05-02 09:37:45 +0200201 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800202}
203
Harald Welte24a655f2010-04-30 19:54:29 +0200204static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200205extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200206
Harald Welte24a655f2010-04-30 19:54:29 +0200207static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800208{
Harald Weltef030b212010-04-26 19:18:54 +0200209 int ret;
210
Harald Welte91f7f4b2010-05-15 23:52:02 +0200211 log_set_context(BSC_CTX_NSVC, nsvc);
212
Harald Welte144e0292010-05-13 11:45:07 +0200213 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200214 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
215 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200216
Harald Welteb3ee2652010-05-19 14:38:50 +0200217 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200218 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200219 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200220 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200221 case GPRS_NS_LL_FR_GRE:
222 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
223 break;
Harald Weltef030b212010-04-26 19:18:54 +0200224 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200225 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200226 msgb_free(msg);
227 ret = -EIO;
228 break;
229 }
230 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800231}
232
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200233static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800234{
Harald Welteba4c6662010-05-19 15:38:10 +0200235 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800236 struct gprs_ns_hdr *nsh;
237
Harald Welte91f7f4b2010-05-15 23:52:02 +0200238 log_set_context(BSC_CTX_NSVC, nsvc);
239
Harald Welte9ba50052010-03-14 15:45:01 +0800240 if (!msg)
241 return -ENOMEM;
242
Harald Welte144e0292010-05-13 11:45:07 +0200243 msg->l2h = msgb_put(msg, sizeof(*nsh));
244 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800245
246 nsh->pdu_type = pdu_type;
247
Harald Welte24a655f2010-04-30 19:54:29 +0200248 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800249}
250
Harald Welte834f26d2010-05-11 06:20:54 +0200251int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200252{
Harald Welteba4c6662010-05-19 15:38:10 +0200253 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200254 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200255 uint16_t nsvci = htons(nsvc->nsvci);
256 uint16_t nsei = htons(nsvc->nsei);
257
Harald Welte91f7f4b2010-05-15 23:52:02 +0200258 log_set_context(BSC_CTX_NSVC, nsvc);
259
Harald Welte69a4cf22010-05-03 20:55:10 +0200260 if (!msg)
261 return -ENOMEM;
262
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000263 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
264 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
265
Harald Welte144e0292010-05-13 11:45:07 +0200266 msg->l2h = msgb_put(msg, sizeof(*nsh));
267 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200268 nsh->pdu_type = NS_PDUT_RESET;
269
270 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
271 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
272 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
273
274 return gprs_ns_tx(nsvc, msg);
275
276}
277
Harald Weltec1402a62010-05-12 11:48:44 +0200278int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
279 uint16_t bvci, struct msgb *orig_msg)
280{
Harald Welteba4c6662010-05-19 15:38:10 +0200281 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200282 struct gprs_ns_hdr *nsh;
283 uint16_t nsvci = htons(nsvc->nsvci);
284
Harald Welte91f7f4b2010-05-15 23:52:02 +0200285 log_set_context(BSC_CTX_NSVC, nsvc);
286
Harald Weltec1402a62010-05-12 11:48:44 +0200287 bvci = htons(bvci);
288
289 if (!msg)
290 return -ENOMEM;
291
Harald Welte90af1942010-05-15 23:02:24 +0200292 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000293 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
294
Harald Welte144e0292010-05-13 11:45:07 +0200295 msg->l2h = msgb_put(msg, sizeof(*nsh));
296 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200297 nsh->pdu_type = NS_PDUT_STATUS;
298
299 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
300
301 /* Section 9.2.7.1: Static conditions for NS-VCI */
302 if (cause == NS_CAUSE_NSVC_BLOCKED ||
303 cause == NS_CAUSE_NSVC_UNKNOWN)
304 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
305
306 /* Section 9.2.7.2: Static conditions for NS PDU */
307 switch (cause) {
308 case NS_CAUSE_SEM_INCORR_PDU:
309 case NS_CAUSE_PDU_INCOMP_PSTATE:
310 case NS_CAUSE_PROTO_ERR_UNSPEC:
311 case NS_CAUSE_INVAL_ESSENT_IE:
312 case NS_CAUSE_MISSING_ESSENT_IE:
313 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
314 orig_msg->l2h);
315 break;
316 default:
317 break;
318 }
319
320 /* Section 9.2.7.3: Static conditions for BVCI */
321 if (cause == NS_CAUSE_BVCI_UNKNOWN)
322 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
323
324 return gprs_ns_tx(nsvc, msg);
325}
326
Harald Welte834f26d2010-05-11 06:20:54 +0200327int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
328{
Harald Welteba4c6662010-05-19 15:38:10 +0200329 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200330 struct gprs_ns_hdr *nsh;
331 uint16_t nsvci = htons(nsvc->nsvci);
332
Harald Welte91f7f4b2010-05-15 23:52:02 +0200333 log_set_context(BSC_CTX_NSVC, nsvc);
334
Harald Welte834f26d2010-05-11 06:20:54 +0200335 if (!msg)
336 return -ENOMEM;
337
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000338 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
339 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
340
Harald Welte834f26d2010-05-11 06:20:54 +0200341 /* be conservative and mark it as blocked even now! */
342 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200343 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200344
Harald Welte144e0292010-05-13 11:45:07 +0200345 msg->l2h = msgb_put(msg, sizeof(*nsh));
346 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200347 nsh->pdu_type = NS_PDUT_BLOCK;
348
349 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
350 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
351
352 return gprs_ns_tx(nsvc, msg);
353}
354
355int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
356{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200357 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000358 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
359 nsvc->nsei, nsvc->nsvci);
360
Harald Welte834f26d2010-05-11 06:20:54 +0200361 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
362}
363
Harald Welteb983c312010-05-12 12:11:45 +0000364int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
365{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200366 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000367 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
368 nsvc->nsei, nsvc->nsvci);
369
370 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
371}
372
373int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
374{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200375 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000376 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
377 nsvc->nsei, nsvc->nsvci);
378
379 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
380}
381
Harald Weltefe4ab902010-05-12 17:19:53 +0000382static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
383 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
384 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
385 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200386};
387
Harald Welte7c24b9e2010-05-12 12:21:52 +0000388static const struct value_string timer_mode_strs[] = {
389 { NSVC_TIMER_TNS_RESET, "tns-reset" },
390 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
391 { NSVC_TIMER_TNS_TEST, "tns-test" },
392 { 0, NULL }
393};
394
Harald Welte69a4cf22010-05-03 20:55:10 +0200395static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
396{
Harald Weltefe4ab902010-05-12 17:19:53 +0000397 enum ns_timeout tout = timer_mode_tout[mode];
398 unsigned int seconds = nsvc->nsi->timeout[tout];
399
Harald Welte91f7f4b2010-05-15 23:52:02 +0200400 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000401 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
402 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000403 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000404
Harald Welte69a4cf22010-05-03 20:55:10 +0200405 if (bsc_timer_pending(&nsvc->timer))
406 bsc_del_timer(&nsvc->timer);
407
408 nsvc->timer_mode = mode;
Harald Weltefe4ab902010-05-12 17:19:53 +0000409 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200410}
411
Harald Welte80405452010-05-03 20:16:13 +0200412static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800413{
414 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000415 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
416 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800417
Harald Welte91f7f4b2010-05-15 23:52:02 +0200418 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000419 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
420 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000421 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000422
Harald Welte80405452010-05-03 20:16:13 +0200423 switch (nsvc->timer_mode) {
424 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800425 /* Tns-alive case: we expired without response ! */
426 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000427 if (nsvc->alive_retries >
428 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800429 /* mark as dead and blocked */
430 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200431 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
432 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200433 LOGP(DNS, LOGL_NOTICE,
434 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200435 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000436 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welte4941b352010-05-12 13:28:25 +0000437 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200438 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800439 return;
440 }
Harald Welteb983c312010-05-12 12:11:45 +0000441 /* Tns-test case: send NS-ALIVE PDU */
442 gprs_ns_tx_alive(nsvc);
443 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200444 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200445 break;
446 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800447 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000448 gprs_ns_tx_alive(nsvc);
449 /* start Tns-alive timer (transition into faster
450 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000451 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200452 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
453 break;
454 case NSVC_TIMER_TNS_RESET:
455 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200456 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200457 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200458 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200459 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200460 case _NSVC_TIMER_NR:
461 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800462 }
Harald Welte9ba50052010-03-14 15:45:01 +0800463}
464
465/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800466static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800467{
Harald Welteba4c6662010-05-19 15:38:10 +0200468 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800469 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200470 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800471
Harald Welte91f7f4b2010-05-15 23:52:02 +0200472 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800473 if (!msg)
474 return -ENOMEM;
475
Harald Weltec5478482010-03-18 00:01:43 +0800476 nsvci = htons(nsvc->nsvci);
477 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800478
Harald Welte144e0292010-05-13 11:45:07 +0200479 msg->l2h = msgb_put(msg, sizeof(*nsh));
480 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800481
482 nsh->pdu_type = NS_PDUT_RESET_ACK;
483
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200484 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200485 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800486
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200487 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
488 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800489
Harald Welte24a655f2010-04-30 19:54:29 +0200490 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800491}
492
Harald Welte24a655f2010-04-30 19:54:29 +0200493/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
494int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800495{
Harald Welte24a655f2010-04-30 19:54:29 +0200496 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800497 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200498 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200499
500 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
501 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200502 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200503 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200504 return -EINVAL;
505 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200506 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800507
Harald Welte69a4cf22010-05-03 20:55:10 +0200508 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200509 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200510 nsvc->nsei);
511 return -EBUSY;
512 }
513 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200514 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200515 nsvc->nsei);
516 return -EBUSY;
517 }
518
Harald Welteec20ba42010-05-13 12:18:49 +0200519 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
520 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800521 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200522 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800523 return -EIO;
524 }
525
526 nsh->pdu_type = NS_PDUT_UNITDATA;
527 /* spare octet in data[0] */
528 nsh->data[1] = bvci >> 8;
529 nsh->data[2] = bvci & 0xff;
530
Harald Welte24a655f2010-04-30 19:54:29 +0200531 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800532}
533
534/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200535static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800536{
537 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200538 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800539
Harald Weltec1402a62010-05-12 11:48:44 +0200540 if (nsvc->state & NSE_S_BLOCKED)
541 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
542 0, msg);
543
Harald Welte9ba50052010-03-14 15:45:01 +0800544 /* spare octet in data[0] */
545 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200546 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200547 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800548
549 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200550 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800551}
552
553/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200554static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800555{
Harald Weltef030b212010-04-26 19:18:54 +0200556 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800557 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200558 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800559 int rc;
560
Harald Welte41337832010-05-16 00:24:26 +0200561 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800562
563 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
564
565 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200566 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800567 return -EINVAL;
568 }
569
570 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200571 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800572
573 return 0;
574}
575
576/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200577static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800578{
579 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800580 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200581 uint8_t *cause;
582 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800583 int rc;
584
Harald Welte9ba50052010-03-14 15:45:01 +0800585 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
586
587 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
588 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
589 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200590 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200591 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800592 return -EINVAL;
593 }
594
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200595 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
596 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
597 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800598
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200599 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200600 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
601
Harald Weltec1402a62010-05-12 11:48:44 +0200602 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800603 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200604
Harald Weltec5478482010-03-18 00:01:43 +0800605 nsvc->nsei = ntohs(*nsei);
606 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800607
Harald Welte9ba50052010-03-14 15:45:01 +0800608 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200609 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800610
Harald Welte834f26d2010-05-11 06:20:54 +0200611 /* inform interested parties about the fact that this NSVC
612 * has received RESET */
Harald Welte2fc725d2010-05-11 10:15:26 +0200613 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200614
Harald Weltec5478482010-03-18 00:01:43 +0800615 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800616}
617
Harald Welte834f26d2010-05-11 06:20:54 +0200618static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
619{
620 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
621 struct tlv_parsed tp;
622 uint8_t *cause;
623 int rc;
624
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200625 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200626
627 nsvc->state |= NSE_S_BLOCKED;
628
629 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
630
631 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
632 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200633 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200634 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200635 return -EINVAL;
636 }
637
638 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
639 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
640
Harald Welte2fc725d2010-05-11 10:15:26 +0200641 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200642 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200643
644 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
645}
646
Harald Welte9ba50052010-03-14 15:45:01 +0800647/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200648int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200649 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800650{
651 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200652 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800653 int rc = 0;
654
Harald Weltef030b212010-04-26 19:18:54 +0200655 /* look up the NSVC based on source address */
656 nsvc = nsvc_by_rem_addr(nsi, saddr);
657 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200658 struct tlv_parsed tp;
659 uint16_t nsei;
Harald Welte1100a9d2010-05-19 16:01:39 +0200660 if (nsh->pdu_type == NS_PDUT_STATUS) {
661 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
662 "for non-existing NS-VC\n",
663 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
664 return 0;
665 }
Harald Weltef030b212010-04-26 19:18:54 +0200666 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200667 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200668 /* Since we have no NSVC, we have to use a fake */
669 nsvc = nsi->unknown_nsvc;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200670 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200671 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200672 "from %s:%u for non-existing NS-VC\n",
673 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
674 ntohs(saddr->sin_port));
Harald Weltedd1c83c2010-05-13 13:58:08 +0200675 nsvc->nsvci = nsvc->nsei = 0xfffe;
676 nsvc->ip.bts_addr = *saddr;
677 nsvc->state = NSE_S_ALIVE;
Harald Welteb3ee2652010-05-19 14:38:50 +0200678 nsvc->ll = ll;
Harald Weltee0c42d12010-05-18 14:32:29 +0200679#if 0
680 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
681#else
Harald Weltedd1c83c2010-05-13 13:58:08 +0200682 return gprs_ns_tx_status(nsvc,
Harald Welte803647e2010-05-12 13:51:08 +0000683 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
684 msg);
Harald Weltee0c42d12010-05-18 14:32:29 +0200685#endif
Harald Weltebbc9fac2010-05-03 18:54:12 +0200686 }
Harald Welte9a392932010-05-11 18:30:37 +0200687 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
688 msgb_l2len(msg), 0, 0);
689 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
690 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
691 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200692 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200693 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
694 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200695 return -EINVAL;
696 }
697 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
698 /* Check if we already know this NSEI, the remote end might
699 * simply have changed addresses, or it is a SGSN */
700 nsvc = nsvc_by_nsei(nsi, nsei);
701 if (!nsvc) {
Harald Welte91f7f4b2010-05-15 23:52:02 +0200702 nsvc = nsvc_create(nsi, 0xffff);
Harald Welteb3ee2652010-05-19 14:38:50 +0200703 nsvc->ll = ll;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200704 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9a392932010-05-11 18:30:37 +0200705 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
706 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Welte9a392932010-05-11 18:30:37 +0200707 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200708 /* Update the remote peer IP address/port */
709 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200710 } else
711 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800712
Harald Welte91f7f4b2010-05-15 23:52:02 +0200713 log_set_context(BSC_CTX_NSVC, nsvc);
714
Harald Welte144e0292010-05-13 11:45:07 +0200715 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200716 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200717 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200718
Harald Welte9ba50052010-03-14 15:45:01 +0800719 switch (nsh->pdu_type) {
720 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000721 /* If we're dead and blocked and suddenly receive a
722 * NS-ALIVE out of the blue, we might have been re-started
723 * and should send a NS-RESET to make sure everything recovers
724 * fine. */
725 if (nsvc->state == NSE_S_BLOCKED)
726 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
727 else
728 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800729 break;
730 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200731 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200732 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200733 if (nsvc->remote_end_is_sgsn) {
734 /* FIXME: this should be one level higher */
735 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200736 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200737 }
Harald Welte9ba50052010-03-14 15:45:01 +0800738 break;
739 case NS_PDUT_UNITDATA:
740 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200741 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800742 break;
743 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200744 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800745 break;
746 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200747 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800748 break;
749 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200750 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200751 /* mark NS-VC as blocked + active */
752 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200753 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200754 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000755 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200756 /* stop RESET timer */
757 bsc_del_timer(&nsvc->timer);
758 }
Harald Welte90af1942010-05-15 23:02:24 +0200759 /* Initiate TEST proc.: Send ALIVE and start timer */
760 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
761 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800762 break;
763 case NS_PDUT_UNBLOCK:
764 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200765 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800766 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200767 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800768 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800769 break;
770 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200771 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200772 /* mark NS-VC as unblocked + active */
773 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200774 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef88dc002010-05-12 14:18:46 +0000775 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800776 break;
777 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200778 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800779 break;
780 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200781 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200782 /* mark remote NS-VC as blocked + active */
783 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800784 break;
785 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200786 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200787 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800788 rc = -EINVAL;
789 break;
790 }
791 return rc;
792}
793
Harald Weltef030b212010-04-26 19:18:54 +0200794struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
795{
796 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
797
798 nsi->cb = cb;
799 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000800 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
801 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
802 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
803 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
804 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
805 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
806 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200807
Harald Welte60cc6ac2010-05-13 14:20:56 +0200808 /* Create the dummy NSVC that we use for sending
809 * messages to non-existant/unknown NS-VC's */
Harald Weltedd1c83c2010-05-13 13:58:08 +0200810 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +0200811 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200812
Harald Welte3771d092010-04-30 20:26:32 +0200813 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200814}
815
816void gprs_ns_destroy(struct gprs_ns_inst *nsi)
817{
818 /* FIXME: clear all timers */
819
820 /* recursively free the NSI and all its NSVCs */
821 talloc_free(nsi);
822}
823
824
825/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
826 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
827
828/* Read a single NS-over-IP message */
829static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
830 struct sockaddr_in *saddr)
831{
Harald Welteba4c6662010-05-19 15:38:10 +0200832 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +0200833 int ret = 0;
834 socklen_t saddr_len = sizeof(*saddr);
835
836 if (!msg) {
837 *error = -ENOMEM;
838 return NULL;
839 }
840
841 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
842 (struct sockaddr *)saddr, &saddr_len);
843 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200844 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200845 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200846 msgb_free(msg);
847 *error = ret;
848 return NULL;
849 } else if (ret == 0) {
850 msgb_free(msg);
851 *error = ret;
852 return NULL;
853 }
854
855 msg->l2h = msg->data;
856 msgb_put(msg, ret);
857
858 return msg;
859}
860
861static int handle_nsip_read(struct bsc_fd *bfd)
862{
863 int error;
864 struct sockaddr_in saddr;
865 struct gprs_ns_inst *nsi = bfd->data;
866 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
867
868 if (!msg)
869 return error;
870
Harald Welteb3ee2652010-05-19 14:38:50 +0200871 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +0000872
873 msgb_free(msg);
874
875 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200876}
877
878static int handle_nsip_write(struct bsc_fd *bfd)
879{
880 /* FIXME: actually send the data here instead of nsip_sendmsg() */
881 return -EIO;
882}
883
Harald Welteb3ee2652010-05-19 14:38:50 +0200884static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200885{
886 int rc;
887 struct gprs_ns_inst *nsi = nsvc->nsi;
888 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
889
890 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
891 (struct sockaddr *)daddr, sizeof(*daddr));
892
893 talloc_free(msg);
894
895 return rc;
896}
897
898/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
899static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
900{
901 int rc = 0;
902
903 if (what & BSC_FD_READ)
904 rc = handle_nsip_read(bfd);
905 if (what & BSC_FD_WRITE)
906 rc = handle_nsip_write(bfd);
907
908 return rc;
909}
910
Harald Weltef030b212010-04-26 19:18:54 +0200911/* Listen for incoming GPRS packets */
Harald Welte7fb05232010-05-19 15:09:09 +0200912int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +0200913{
914 int ret;
915
Harald Welte7fb05232010-05-19 15:09:09 +0200916 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
917 nsi->nsip.local_port, nsip_fd_cb);
Harald Weltef030b212010-04-26 19:18:54 +0200918 if (ret < 0)
919 return ret;
920
Harald Weltef030b212010-04-26 19:18:54 +0200921 nsi->nsip.fd.data = nsi;
922
923 return ret;
924}
Harald Welte3771d092010-04-30 20:26:32 +0200925
Harald Welte731d1fc2010-05-14 11:53:08 +0000926/* Initiate a RESET procedure */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800927void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +0000928{
929 /* Mark NS-VC locally as blocked and dead */
930 nsvc->state = NSE_S_BLOCKED;
931 /* Send NS-RESET PDU */
932 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
933 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
934 nsvc->nsei);
935 }
936 /* Start Tns-reset */
937 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +0000938}
939
Harald Welte3771d092010-04-30 20:26:32 +0200940/* Establish a connection (from the BSS) to the SGSN */
941struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200942 struct sockaddr_in *dest, uint16_t nsei,
943 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200944{
945 struct gprs_nsvc *nsvc;
946
947 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +0000948 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +0200949 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +0000950 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +0200951 nsvc->nsei = nsei;
952 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200953 nsvc->remote_end_is_sgsn = 1;
954
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800955 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
956 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +0200957}