blob: 4846b64736db498d482639f360557f8c26ca03b7 [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 Freythere75a34f2010-05-23 21:19:55 +080072#include <openbsc/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080073
Harald Welte9ba50052010-03-14 15:45:01 +080074static const struct tlv_definition ns_att_tlvdef = {
75 .def = {
76 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
77 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
78 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
79 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
80 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
81 },
82};
83
Harald Weltec51c23c2010-05-13 12:55:20 +020084enum ns_ctr {
85 NS_CTR_PKTS_IN,
86 NS_CTR_PKTS_OUT,
87 NS_CTR_BYTES_IN,
88 NS_CTR_BYTES_OUT,
89 NS_CTR_BLOCKED,
90 NS_CTR_DEAD,
91};
92
Harald Welte144e0292010-05-13 11:45:07 +020093static const struct rate_ctr_desc nsvc_ctr_description[] = {
94 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +020095 { "packets.out","Packets at NS Level (Out)" },
96 { "bytes.in", "Bytes at NS Level ( In)" },
97 { "bytes.out", "Bytes at NS Level (Out)" },
98 { "blocked", "NS-VC Block count " },
99 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +0200100};
101
102static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200103 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200104 .group_description = "NSVC Peer Statistics",
105 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
106 .ctr_desc = nsvc_ctr_description,
107};
108
Harald Weltef030b212010-04-26 19:18:54 +0200109/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte90af1942010-05-15 23:02:24 +0200110struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200111{
112 struct gprs_nsvc *nsvc;
113 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
114 if (nsvc->nsvci == nsvci)
115 return nsvc;
116 }
117 return NULL;
118}
119
Harald Welte24a655f2010-04-30 19:54:29 +0200120/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welte144e0292010-05-13 11:45:07 +0200121struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200122{
123 struct gprs_nsvc *nsvc;
124 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
125 if (nsvc->nsei == nsei)
126 return nsvc;
127 }
128 return NULL;
129}
130
Harald Weltef030b212010-04-26 19:18:54 +0200131/* Lookup struct gprs_nsvc based on remote peer socket addr */
132static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
133 struct sockaddr_in *sin)
134{
135 struct gprs_nsvc *nsvc;
136 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000137 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
138 sin->sin_addr.s_addr &&
139 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200140 return nsvc;
141 }
142 return NULL;
143}
144
Harald Welte69a4cf22010-05-03 20:55:10 +0200145static void gprs_ns_timer_cb(void *data);
146
Harald Welte144e0292010-05-13 11:45:07 +0200147struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200148{
149 struct gprs_nsvc *nsvc;
150
151 nsvc = talloc_zero(nsi, struct gprs_nsvc);
152 nsvc->nsvci = nsvci;
153 /* before RESET procedure: BLOCKED and DEAD */
154 nsvc->state = NSE_S_BLOCKED;
155 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200156 nsvc->timer.cb = gprs_ns_timer_cb;
157 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200158 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200159
Harald Weltef030b212010-04-26 19:18:54 +0200160 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
161
162 return nsvc;
163}
Harald Welte9ba50052010-03-14 15:45:01 +0800164
Harald Welte144e0292010-05-13 11:45:07 +0200165void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000166{
167 if (bsc_timer_pending(&nsvc->timer))
168 bsc_del_timer(&nsvc->timer);
169 llist_del(&nsvc->list);
170 talloc_free(nsvc);
171}
172
Harald Welte2fc725d2010-05-11 10:15:26 +0200173static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200174 uint8_t cause)
175{
176 struct ns_signal_data nssd;
177
178 nssd.nsvc = nsvc;
179 nssd.cause = cause;
180
181 dispatch_signal(SS_NS, signal, &nssd);
182}
183
Harald Welte9ba50052010-03-14 15:45:01 +0800184/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200185static const struct value_string ns_cause_str[] = {
186 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
187 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
188 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
189 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
190 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
191 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
192 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
193 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200194 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200195 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
196 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
197 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800198};
199
Harald Welte2577d412010-05-02 09:37:45 +0200200const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800201{
Harald Welte2577d412010-05-02 09:37:45 +0200202 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800203}
204
Harald Welte24a655f2010-04-30 19:54:29 +0200205static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200206extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200207
Harald Welte24a655f2010-04-30 19:54:29 +0200208static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800209{
Harald Weltef030b212010-04-26 19:18:54 +0200210 int ret;
211
Harald Welte91f7f4b2010-05-15 23:52:02 +0200212 log_set_context(BSC_CTX_NSVC, nsvc);
213
Harald Welte144e0292010-05-13 11:45:07 +0200214 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200215 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
216 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200217
Harald Welteb3ee2652010-05-19 14:38:50 +0200218 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200219 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200220 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200221 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200222 case GPRS_NS_LL_FR_GRE:
223 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
224 break;
Harald Weltef030b212010-04-26 19:18:54 +0200225 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200226 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200227 msgb_free(msg);
228 ret = -EIO;
229 break;
230 }
231 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800232}
233
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200234static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800235{
Harald Welteba4c6662010-05-19 15:38:10 +0200236 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800237 struct gprs_ns_hdr *nsh;
238
Harald Welte91f7f4b2010-05-15 23:52:02 +0200239 log_set_context(BSC_CTX_NSVC, nsvc);
240
Harald Welte9ba50052010-03-14 15:45:01 +0800241 if (!msg)
242 return -ENOMEM;
243
Harald Welte144e0292010-05-13 11:45:07 +0200244 msg->l2h = msgb_put(msg, sizeof(*nsh));
245 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800246
247 nsh->pdu_type = pdu_type;
248
Harald Welte24a655f2010-04-30 19:54:29 +0200249 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800250}
251
Harald Welte834f26d2010-05-11 06:20:54 +0200252int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200253{
Harald Welteba4c6662010-05-19 15:38:10 +0200254 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200255 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200256 uint16_t nsvci = htons(nsvc->nsvci);
257 uint16_t nsei = htons(nsvc->nsei);
258
Harald Welte91f7f4b2010-05-15 23:52:02 +0200259 log_set_context(BSC_CTX_NSVC, nsvc);
260
Harald Welte69a4cf22010-05-03 20:55:10 +0200261 if (!msg)
262 return -ENOMEM;
263
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000264 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
265 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
266
Harald Welte144e0292010-05-13 11:45:07 +0200267 msg->l2h = msgb_put(msg, sizeof(*nsh));
268 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200269 nsh->pdu_type = NS_PDUT_RESET;
270
271 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
272 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
273 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
274
275 return gprs_ns_tx(nsvc, msg);
276
277}
278
Harald Weltec1402a62010-05-12 11:48:44 +0200279int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
280 uint16_t bvci, struct msgb *orig_msg)
281{
Harald Welteba4c6662010-05-19 15:38:10 +0200282 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200283 struct gprs_ns_hdr *nsh;
284 uint16_t nsvci = htons(nsvc->nsvci);
285
Harald Welte91f7f4b2010-05-15 23:52:02 +0200286 log_set_context(BSC_CTX_NSVC, nsvc);
287
Harald Weltec1402a62010-05-12 11:48:44 +0200288 bvci = htons(bvci);
289
290 if (!msg)
291 return -ENOMEM;
292
Harald Welte90af1942010-05-15 23:02:24 +0200293 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000294 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
295
Harald Welte144e0292010-05-13 11:45:07 +0200296 msg->l2h = msgb_put(msg, sizeof(*nsh));
297 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200298 nsh->pdu_type = NS_PDUT_STATUS;
299
300 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
301
302 /* Section 9.2.7.1: Static conditions for NS-VCI */
303 if (cause == NS_CAUSE_NSVC_BLOCKED ||
304 cause == NS_CAUSE_NSVC_UNKNOWN)
305 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
306
307 /* Section 9.2.7.2: Static conditions for NS PDU */
308 switch (cause) {
309 case NS_CAUSE_SEM_INCORR_PDU:
310 case NS_CAUSE_PDU_INCOMP_PSTATE:
311 case NS_CAUSE_PROTO_ERR_UNSPEC:
312 case NS_CAUSE_INVAL_ESSENT_IE:
313 case NS_CAUSE_MISSING_ESSENT_IE:
314 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
315 orig_msg->l2h);
316 break;
317 default:
318 break;
319 }
320
321 /* Section 9.2.7.3: Static conditions for BVCI */
322 if (cause == NS_CAUSE_BVCI_UNKNOWN)
323 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
324
325 return gprs_ns_tx(nsvc, msg);
326}
327
Harald Welte834f26d2010-05-11 06:20:54 +0200328int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
329{
Harald Welteba4c6662010-05-19 15:38:10 +0200330 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200331 struct gprs_ns_hdr *nsh;
332 uint16_t nsvci = htons(nsvc->nsvci);
333
Harald Welte91f7f4b2010-05-15 23:52:02 +0200334 log_set_context(BSC_CTX_NSVC, nsvc);
335
Harald Welte834f26d2010-05-11 06:20:54 +0200336 if (!msg)
337 return -ENOMEM;
338
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000339 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
340 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
341
Harald Welte834f26d2010-05-11 06:20:54 +0200342 /* be conservative and mark it as blocked even now! */
343 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200344 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200345
Harald Welte144e0292010-05-13 11:45:07 +0200346 msg->l2h = msgb_put(msg, sizeof(*nsh));
347 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200348 nsh->pdu_type = NS_PDUT_BLOCK;
349
350 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
351 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
352
353 return gprs_ns_tx(nsvc, msg);
354}
355
356int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
357{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200358 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000359 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
360 nsvc->nsei, nsvc->nsvci);
361
Harald Welte834f26d2010-05-11 06:20:54 +0200362 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
363}
364
Harald Welteb983c312010-05-12 12:11:45 +0000365int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
366{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200367 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000368 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
369 nsvc->nsei, nsvc->nsvci);
370
371 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
372}
373
374int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
375{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200376 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000377 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
378 nsvc->nsei, nsvc->nsvci);
379
380 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
381}
382
Harald Weltefe4ab902010-05-12 17:19:53 +0000383static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
384 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
385 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
386 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200387};
388
Harald Welte7c24b9e2010-05-12 12:21:52 +0000389static const struct value_string timer_mode_strs[] = {
390 { NSVC_TIMER_TNS_RESET, "tns-reset" },
391 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
392 { NSVC_TIMER_TNS_TEST, "tns-test" },
393 { 0, NULL }
394};
395
Harald Welte69a4cf22010-05-03 20:55:10 +0200396static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
397{
Harald Weltefe4ab902010-05-12 17:19:53 +0000398 enum ns_timeout tout = timer_mode_tout[mode];
399 unsigned int seconds = nsvc->nsi->timeout[tout];
400
Harald Welte91f7f4b2010-05-15 23:52:02 +0200401 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000402 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
403 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000404 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000405
Harald Welte69a4cf22010-05-03 20:55:10 +0200406 if (bsc_timer_pending(&nsvc->timer))
407 bsc_del_timer(&nsvc->timer);
408
409 nsvc->timer_mode = mode;
Harald Weltefe4ab902010-05-12 17:19:53 +0000410 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200411}
412
Harald Welte80405452010-05-03 20:16:13 +0200413static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800414{
415 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000416 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
417 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800418
Harald Welte91f7f4b2010-05-15 23:52:02 +0200419 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000420 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
421 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000422 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000423
Harald Welte80405452010-05-03 20:16:13 +0200424 switch (nsvc->timer_mode) {
425 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800426 /* Tns-alive case: we expired without response ! */
427 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000428 if (nsvc->alive_retries >
429 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800430 /* mark as dead and blocked */
431 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200432 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
433 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200434 LOGP(DNS, LOGL_NOTICE,
435 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200436 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000437 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welte4941b352010-05-12 13:28:25 +0000438 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200439 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800440 return;
441 }
Harald Welteb983c312010-05-12 12:11:45 +0000442 /* Tns-test case: send NS-ALIVE PDU */
443 gprs_ns_tx_alive(nsvc);
444 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200445 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200446 break;
447 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800448 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000449 gprs_ns_tx_alive(nsvc);
450 /* start Tns-alive timer (transition into faster
451 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000452 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200453 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
454 break;
455 case NSVC_TIMER_TNS_RESET:
456 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200457 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200458 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200459 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200460 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200461 case _NSVC_TIMER_NR:
462 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800463 }
Harald Welte9ba50052010-03-14 15:45:01 +0800464}
465
466/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800467static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800468{
Harald Welteba4c6662010-05-19 15:38:10 +0200469 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800470 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200471 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800472
Harald Welte91f7f4b2010-05-15 23:52:02 +0200473 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800474 if (!msg)
475 return -ENOMEM;
476
Harald Weltec5478482010-03-18 00:01:43 +0800477 nsvci = htons(nsvc->nsvci);
478 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800479
Harald Welte144e0292010-05-13 11:45:07 +0200480 msg->l2h = msgb_put(msg, sizeof(*nsh));
481 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800482
483 nsh->pdu_type = NS_PDUT_RESET_ACK;
484
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200485 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200486 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800487
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200488 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
489 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800490
Harald Welte24a655f2010-04-30 19:54:29 +0200491 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800492}
493
Harald Welte24a655f2010-04-30 19:54:29 +0200494/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
495int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800496{
Harald Welte24a655f2010-04-30 19:54:29 +0200497 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800498 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200499 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200500
501 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
502 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200503 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200504 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200505 return -EINVAL;
506 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200507 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800508
Harald Welte69a4cf22010-05-03 20:55:10 +0200509 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200510 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200511 nsvc->nsei);
512 return -EBUSY;
513 }
514 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200515 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200516 nsvc->nsei);
517 return -EBUSY;
518 }
519
Harald Welteec20ba42010-05-13 12:18:49 +0200520 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
521 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800522 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200523 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800524 return -EIO;
525 }
526
527 nsh->pdu_type = NS_PDUT_UNITDATA;
528 /* spare octet in data[0] */
529 nsh->data[1] = bvci >> 8;
530 nsh->data[2] = bvci & 0xff;
531
Harald Welte24a655f2010-04-30 19:54:29 +0200532 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800533}
534
535/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200536static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800537{
538 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200539 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800540
Harald Weltec1402a62010-05-12 11:48:44 +0200541 if (nsvc->state & NSE_S_BLOCKED)
542 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
543 0, msg);
544
Harald Welte9ba50052010-03-14 15:45:01 +0800545 /* spare octet in data[0] */
546 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200547 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200548 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800549
550 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200551 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800552}
553
554/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200555static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800556{
Harald Weltef030b212010-04-26 19:18:54 +0200557 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800558 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200559 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800560 int rc;
561
Harald Welte41337832010-05-16 00:24:26 +0200562 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800563
564 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
565
566 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200567 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800568 return -EINVAL;
569 }
570
571 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200572 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800573
574 return 0;
575}
576
577/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200578static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800579{
580 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800581 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200582 uint8_t *cause;
583 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800584 int rc;
585
Harald Welte9ba50052010-03-14 15:45:01 +0800586 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
587
588 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
589 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
590 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200591 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200592 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800593 return -EINVAL;
594 }
595
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200596 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
597 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
598 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800599
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200600 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200601 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
602
Harald Weltec1402a62010-05-12 11:48:44 +0200603 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800604 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200605
Harald Weltec5478482010-03-18 00:01:43 +0800606 nsvc->nsei = ntohs(*nsei);
607 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800608
Harald Welte9ba50052010-03-14 15:45:01 +0800609 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200610 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800611
Harald Welte834f26d2010-05-11 06:20:54 +0200612 /* inform interested parties about the fact that this NSVC
613 * has received RESET */
Harald Welte2fc725d2010-05-11 10:15:26 +0200614 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200615
Harald Weltec5478482010-03-18 00:01:43 +0800616 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800617}
618
Harald Welte834f26d2010-05-11 06:20:54 +0200619static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
620{
621 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
622 struct tlv_parsed tp;
623 uint8_t *cause;
624 int rc;
625
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200626 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200627
628 nsvc->state |= NSE_S_BLOCKED;
629
630 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
631
632 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
633 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200634 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200635 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200636 return -EINVAL;
637 }
638
639 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
640 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
641
Harald Welte2fc725d2010-05-11 10:15:26 +0200642 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200643 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200644
645 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
646}
647
Harald Welte9ba50052010-03-14 15:45:01 +0800648/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200649int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200650 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800651{
652 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200653 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800654 int rc = 0;
655
Harald Weltef030b212010-04-26 19:18:54 +0200656 /* look up the NSVC based on source address */
657 nsvc = nsvc_by_rem_addr(nsi, saddr);
658 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200659 struct tlv_parsed tp;
660 uint16_t nsei;
Harald Welte1100a9d2010-05-19 16:01:39 +0200661 if (nsh->pdu_type == NS_PDUT_STATUS) {
662 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
663 "for non-existing NS-VC\n",
664 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
665 return 0;
666 }
Harald Weltef030b212010-04-26 19:18:54 +0200667 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200668 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200669 /* Since we have no NSVC, we have to use a fake */
670 nsvc = nsi->unknown_nsvc;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200671 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200672 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200673 "from %s:%u for non-existing NS-VC\n",
674 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
675 ntohs(saddr->sin_port));
Harald Weltedd1c83c2010-05-13 13:58:08 +0200676 nsvc->nsvci = nsvc->nsei = 0xfffe;
677 nsvc->ip.bts_addr = *saddr;
678 nsvc->state = NSE_S_ALIVE;
Harald Welteb3ee2652010-05-19 14:38:50 +0200679 nsvc->ll = ll;
Harald Weltee0c42d12010-05-18 14:32:29 +0200680#if 0
681 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
682#else
Harald Weltedd1c83c2010-05-13 13:58:08 +0200683 return gprs_ns_tx_status(nsvc,
Harald Welte803647e2010-05-12 13:51:08 +0000684 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
685 msg);
Harald Weltee0c42d12010-05-18 14:32:29 +0200686#endif
Harald Weltebbc9fac2010-05-03 18:54:12 +0200687 }
Harald Welte9a392932010-05-11 18:30:37 +0200688 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
689 msgb_l2len(msg), 0, 0);
690 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
691 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
692 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200693 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200694 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
695 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200696 return -EINVAL;
697 }
698 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
699 /* Check if we already know this NSEI, the remote end might
700 * simply have changed addresses, or it is a SGSN */
701 nsvc = nsvc_by_nsei(nsi, nsei);
702 if (!nsvc) {
Harald Welte91f7f4b2010-05-15 23:52:02 +0200703 nsvc = nsvc_create(nsi, 0xffff);
Harald Welteb3ee2652010-05-19 14:38:50 +0200704 nsvc->ll = ll;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200705 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9a392932010-05-11 18:30:37 +0200706 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
707 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Welte9a392932010-05-11 18:30:37 +0200708 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200709 /* Update the remote peer IP address/port */
710 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200711 } else
712 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800713
Harald Welte91f7f4b2010-05-15 23:52:02 +0200714 log_set_context(BSC_CTX_NSVC, nsvc);
715
Harald Welte144e0292010-05-13 11:45:07 +0200716 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200717 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200718 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200719
Harald Welte9ba50052010-03-14 15:45:01 +0800720 switch (nsh->pdu_type) {
721 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000722 /* If we're dead and blocked and suddenly receive a
723 * NS-ALIVE out of the blue, we might have been re-started
724 * and should send a NS-RESET to make sure everything recovers
725 * fine. */
726 if (nsvc->state == NSE_S_BLOCKED)
727 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
728 else
729 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800730 break;
731 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200732 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200733 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200734 if (nsvc->remote_end_is_sgsn) {
735 /* FIXME: this should be one level higher */
736 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200737 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200738 }
Harald Welte9ba50052010-03-14 15:45:01 +0800739 break;
740 case NS_PDUT_UNITDATA:
741 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200742 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800743 break;
744 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200745 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800746 break;
747 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200748 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800749 break;
750 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200751 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200752 /* mark NS-VC as blocked + active */
753 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200754 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200755 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000756 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200757 /* stop RESET timer */
758 bsc_del_timer(&nsvc->timer);
759 }
Harald Welte90af1942010-05-15 23:02:24 +0200760 /* Initiate TEST proc.: Send ALIVE and start timer */
761 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
762 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800763 break;
764 case NS_PDUT_UNBLOCK:
765 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200766 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800767 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200768 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800769 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800770 break;
771 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200772 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200773 /* mark NS-VC as unblocked + active */
774 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200775 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef88dc002010-05-12 14:18:46 +0000776 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800777 break;
778 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200779 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800780 break;
781 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200782 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200783 /* mark remote NS-VC as blocked + active */
784 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800785 break;
786 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200787 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200788 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800789 rc = -EINVAL;
790 break;
791 }
792 return rc;
793}
794
Harald Weltef030b212010-04-26 19:18:54 +0200795struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
796{
797 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
798
799 nsi->cb = cb;
800 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000801 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
802 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
803 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
804 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
805 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
806 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
807 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200808
Harald Welte60cc6ac2010-05-13 14:20:56 +0200809 /* Create the dummy NSVC that we use for sending
810 * messages to non-existant/unknown NS-VC's */
Harald Weltedd1c83c2010-05-13 13:58:08 +0200811 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +0200812 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200813
Harald Welte3771d092010-04-30 20:26:32 +0200814 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200815}
816
817void gprs_ns_destroy(struct gprs_ns_inst *nsi)
818{
819 /* FIXME: clear all timers */
820
821 /* recursively free the NSI and all its NSVCs */
822 talloc_free(nsi);
823}
824
825
826/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
827 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
828
829/* Read a single NS-over-IP message */
830static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
831 struct sockaddr_in *saddr)
832{
Harald Welteba4c6662010-05-19 15:38:10 +0200833 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +0200834 int ret = 0;
835 socklen_t saddr_len = sizeof(*saddr);
836
837 if (!msg) {
838 *error = -ENOMEM;
839 return NULL;
840 }
841
842 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
843 (struct sockaddr *)saddr, &saddr_len);
844 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200845 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200846 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200847 msgb_free(msg);
848 *error = ret;
849 return NULL;
850 } else if (ret == 0) {
851 msgb_free(msg);
852 *error = ret;
853 return NULL;
854 }
855
856 msg->l2h = msg->data;
857 msgb_put(msg, ret);
858
859 return msg;
860}
861
862static int handle_nsip_read(struct bsc_fd *bfd)
863{
864 int error;
865 struct sockaddr_in saddr;
866 struct gprs_ns_inst *nsi = bfd->data;
867 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
868
869 if (!msg)
870 return error;
871
Harald Welteb3ee2652010-05-19 14:38:50 +0200872 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +0000873
874 msgb_free(msg);
875
876 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200877}
878
879static int handle_nsip_write(struct bsc_fd *bfd)
880{
881 /* FIXME: actually send the data here instead of nsip_sendmsg() */
882 return -EIO;
883}
884
Harald Welteb3ee2652010-05-19 14:38:50 +0200885static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200886{
887 int rc;
888 struct gprs_ns_inst *nsi = nsvc->nsi;
889 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
890
891 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
892 (struct sockaddr *)daddr, sizeof(*daddr));
893
894 talloc_free(msg);
895
896 return rc;
897}
898
899/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
900static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
901{
902 int rc = 0;
903
904 if (what & BSC_FD_READ)
905 rc = handle_nsip_read(bfd);
906 if (what & BSC_FD_WRITE)
907 rc = handle_nsip_write(bfd);
908
909 return rc;
910}
911
Harald Weltef030b212010-04-26 19:18:54 +0200912/* Listen for incoming GPRS packets */
Harald Welte7fb05232010-05-19 15:09:09 +0200913int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +0200914{
915 int ret;
916
Harald Welte7fb05232010-05-19 15:09:09 +0200917 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
918 nsi->nsip.local_port, nsip_fd_cb);
Harald Weltef030b212010-04-26 19:18:54 +0200919 if (ret < 0)
920 return ret;
921
Harald Weltef030b212010-04-26 19:18:54 +0200922 nsi->nsip.fd.data = nsi;
923
924 return ret;
925}
Harald Welte3771d092010-04-30 20:26:32 +0200926
Harald Welte731d1fc2010-05-14 11:53:08 +0000927/* Initiate a RESET procedure */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800928void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +0000929{
930 /* Mark NS-VC locally as blocked and dead */
931 nsvc->state = NSE_S_BLOCKED;
932 /* Send NS-RESET PDU */
933 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
934 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
935 nsvc->nsei);
936 }
937 /* Start Tns-reset */
938 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +0000939}
940
Harald Welte3771d092010-04-30 20:26:32 +0200941/* Establish a connection (from the BSS) to the SGSN */
942struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200943 struct sockaddr_in *dest, uint16_t nsei,
944 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200945{
946 struct gprs_nsvc *nsvc;
947
948 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +0000949 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +0200950 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +0000951 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +0200952 nsvc->nsei = nsei;
953 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200954 nsvc->remote_end_is_sgsn = 1;
955
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800956 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
957 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +0200958}