blob: 877a065453af2c5a19dd7cb90e08a8c4ad971ed5 [file] [log] [blame]
Harald Welte44f1c272010-04-30 19:54:29 +02001/* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg);
Harald Welte9b455bf2010-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 Weltecb991632010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080020 *
21 */
22
23/* Some introduction into NS: NS is used typically on top of frame relay,
24 * but in the ip.access world it is encapsulated in UDP packets. It serves
25 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
26 * do much, apart from providing congestion notification and status indication.
27 *
28 * Terms:
29 * NS Network Service
30 * NSVC NS Virtual Connection
31 * NSEI NS Entity Identifier
32 * NSVL NS Virtual Link
33 * NSVLI NS Virtual Link Identifier
34 * BVC BSSGP Virtual Connection
35 * BVCI BSSGP Virtual Connection Identifier
36 * NSVCG NS Virtual Connection Goup
37 * Blocked NS-VC cannot be used for user traffic
38 * Alive Ability of a NS-VC to provide communication
39 *
40 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
41 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
42 * NS then has to firgure out which NSVC's are responsible for this BVCI.
43 * Those mappings are administratively configured.
44 */
45
Harald Welte9aa97fc2010-05-13 13:58:08 +020046/* This implementation has the following limitations:
47 * o Only one NS-VC for each NSE: No load-sharing function
48 * o NSVCI 65535 and 65534 are reserved for internal use
49 * o Only UDP is supported as of now, no frame relay support
50 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
51 * o There are no BLOCK and UNBLOCK timers (yet?)
52 */
53
Harald Welte9b455bf2010-03-14 15:45:01 +080054#include <stdlib.h>
55#include <unistd.h>
56#include <errno.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020057#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080058
59#include <arpa/inet.h>
60
61#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/core/msgb.h>
63#include <osmocom/gsm/tlv.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/core/select.h>
66#include <osmocom/core/rate_ctr.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080067#include <openbsc/debug.h>
Harald Welte99e32482010-05-11 06:20:54 +020068#include <openbsc/signal.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080069#include <openbsc/gprs_ns.h>
70#include <openbsc/gprs_bssgp.h>
Holger Hans Peter Freyther2fbf12d2010-05-23 21:23:44 +080071#include <openbsc/gprs_ns_frgre.h>
Holger Hans Peter Freythercf4e9c82010-05-23 21:19:55 +080072#include <openbsc/socket.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080073
Harald Welte9b455bf2010-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 Weltec1919862010-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 Weltef2b4cd72010-05-13 11:45:07 +020093static const struct rate_ctr_desc nsvc_ctr_description[] = {
94 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec1919862010-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 Weltef2b4cd72010-05-13 11:45:07 +0200100};
101
102static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec1919862010-05-13 12:55:20 +0200103 .group_name_prefix = "ns.nsvc",
Harald Weltef2b4cd72010-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 Weltecb991632010-04-26 19:18:54 +0200109/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welteff56d612010-05-15 23:02:24 +0200110struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-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 Welte44f1c272010-04-30 19:54:29 +0200120/* Lookup struct gprs_nsvc based on NSVCI */
Harald Weltef2b4cd72010-05-13 11:45:07 +0200121struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte44f1c272010-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 Weltecb991632010-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 Weltecad83012010-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 Weltecb991632010-04-26 19:18:54 +0200140 return nsvc;
141 }
142 return NULL;
143}
144
Harald Welte199d9df2010-05-03 20:55:10 +0200145static void gprs_ns_timer_cb(void *data);
146
Harald Weltef2b4cd72010-05-13 11:45:07 +0200147struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-04-26 19:18:54 +0200148{
149 struct gprs_nsvc *nsvc;
150
Harald Welte4f1e8152010-05-25 22:17:30 +0200151 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
152
Harald Weltecb991632010-04-26 19:18:54 +0200153 nsvc = talloc_zero(nsi, struct gprs_nsvc);
154 nsvc->nsvci = nsvci;
155 /* before RESET procedure: BLOCKED and DEAD */
156 nsvc->state = NSE_S_BLOCKED;
157 nsvc->nsi = nsi;
Harald Welte199d9df2010-05-03 20:55:10 +0200158 nsvc->timer.cb = gprs_ns_timer_cb;
159 nsvc->timer.data = nsvc;
Harald Weltef2b4cd72010-05-13 11:45:07 +0200160 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte199d9df2010-05-03 20:55:10 +0200161
Harald Weltecb991632010-04-26 19:18:54 +0200162 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
163
164 return nsvc;
165}
Harald Welte9b455bf2010-03-14 15:45:01 +0800166
Harald Weltef2b4cd72010-05-13 11:45:07 +0200167void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte1194b582010-05-12 15:55:23 +0000168{
169 if (bsc_timer_pending(&nsvc->timer))
170 bsc_del_timer(&nsvc->timer);
171 llist_del(&nsvc->list);
172 talloc_free(nsvc);
173}
174
Harald Welte1389ac72010-05-11 10:15:26 +0200175static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200176 uint8_t cause)
177{
178 struct ns_signal_data nssd;
179
180 nssd.nsvc = nsvc;
181 nssd.cause = cause;
182
183 dispatch_signal(SS_NS, signal, &nssd);
184}
185
Harald Welte9b455bf2010-03-14 15:45:01 +0800186/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-05-02 09:37:45 +0200187static const struct value_string ns_cause_str[] = {
188 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
189 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
190 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
191 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
192 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
193 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
194 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
195 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte199d9df2010-05-03 20:55:10 +0200196 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-05-02 09:37:45 +0200197 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
198 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
199 { 0, NULL }
Harald Welte9b455bf2010-03-14 15:45:01 +0800200};
201
Harald Welte345223e2010-05-02 09:37:45 +0200202const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800203{
Harald Welte345223e2010-05-02 09:37:45 +0200204 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800205}
206
Harald Welte44f1c272010-04-30 19:54:29 +0200207static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welte5540c4c2010-05-19 14:38:50 +0200208extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200209
Harald Welte44f1c272010-04-30 19:54:29 +0200210static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800211{
Harald Weltecb991632010-04-26 19:18:54 +0200212 int ret;
213
Harald Welte8be8c8f2010-05-15 23:52:02 +0200214 log_set_context(BSC_CTX_NSVC, nsvc);
215
Harald Weltef2b4cd72010-05-13 11:45:07 +0200216 /* Increment number of Uplink bytes */
Harald Weltec1919862010-05-13 12:55:20 +0200217 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
218 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Weltef2b4cd72010-05-13 11:45:07 +0200219
Harald Welte5540c4c2010-05-19 14:38:50 +0200220 switch (nsvc->ll) {
Harald Weltecb991632010-04-26 19:18:54 +0200221 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200222 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200223 break;
Harald Welte5540c4c2010-05-19 14:38:50 +0200224 case GPRS_NS_LL_FR_GRE:
225 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
226 break;
Harald Weltecb991632010-04-26 19:18:54 +0200227 default:
Harald Welte5540c4c2010-05-19 14:38:50 +0200228 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200229 msgb_free(msg);
230 ret = -EIO;
231 break;
232 }
233 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800234}
235
Harald Welteeaa614c2010-05-02 11:26:34 +0200236static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800237{
Harald Weltee4860d72010-05-19 15:38:10 +0200238 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +0800239 struct gprs_ns_hdr *nsh;
240
Harald Welte8be8c8f2010-05-15 23:52:02 +0200241 log_set_context(BSC_CTX_NSVC, nsvc);
242
Harald Welte9b455bf2010-03-14 15:45:01 +0800243 if (!msg)
244 return -ENOMEM;
245
Harald Weltef2b4cd72010-05-13 11:45:07 +0200246 msg->l2h = msgb_put(msg, sizeof(*nsh));
247 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800248
249 nsh->pdu_type = pdu_type;
250
Harald Welte44f1c272010-04-30 19:54:29 +0200251 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800252}
253
Harald Welte99e32482010-05-11 06:20:54 +0200254int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200255{
Harald Weltee4860d72010-05-19 15:38:10 +0200256 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte199d9df2010-05-03 20:55:10 +0200257 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-05-03 20:55:10 +0200258 uint16_t nsvci = htons(nsvc->nsvci);
259 uint16_t nsei = htons(nsvc->nsei);
260
Harald Welte8be8c8f2010-05-15 23:52:02 +0200261 log_set_context(BSC_CTX_NSVC, nsvc);
262
Harald Welte199d9df2010-05-03 20:55:10 +0200263 if (!msg)
264 return -ENOMEM;
265
Harald Welte34caeb32010-05-12 12:01:33 +0000266 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
267 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
268
Harald Weltef2b4cd72010-05-13 11:45:07 +0200269 msg->l2h = msgb_put(msg, sizeof(*nsh));
270 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte199d9df2010-05-03 20:55:10 +0200271 nsh->pdu_type = NS_PDUT_RESET;
272
273 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
274 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
275 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
276
277 return gprs_ns_tx(nsvc, msg);
278
279}
280
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200281int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
282 uint16_t bvci, struct msgb *orig_msg)
283{
Harald Weltee4860d72010-05-19 15:38:10 +0200284 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200285 struct gprs_ns_hdr *nsh;
286 uint16_t nsvci = htons(nsvc->nsvci);
287
Harald Welte8be8c8f2010-05-15 23:52:02 +0200288 log_set_context(BSC_CTX_NSVC, nsvc);
289
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200290 bvci = htons(bvci);
291
292 if (!msg)
293 return -ENOMEM;
294
Harald Welteff56d612010-05-15 23:02:24 +0200295 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte34caeb32010-05-12 12:01:33 +0000296 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
297
Harald Weltef2b4cd72010-05-13 11:45:07 +0200298 msg->l2h = msgb_put(msg, sizeof(*nsh));
299 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200300 nsh->pdu_type = NS_PDUT_STATUS;
301
302 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
303
304 /* Section 9.2.7.1: Static conditions for NS-VCI */
305 if (cause == NS_CAUSE_NSVC_BLOCKED ||
306 cause == NS_CAUSE_NSVC_UNKNOWN)
307 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
308
309 /* Section 9.2.7.2: Static conditions for NS PDU */
310 switch (cause) {
311 case NS_CAUSE_SEM_INCORR_PDU:
312 case NS_CAUSE_PDU_INCOMP_PSTATE:
313 case NS_CAUSE_PROTO_ERR_UNSPEC:
314 case NS_CAUSE_INVAL_ESSENT_IE:
315 case NS_CAUSE_MISSING_ESSENT_IE:
316 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
317 orig_msg->l2h);
318 break;
319 default:
320 break;
321 }
322
323 /* Section 9.2.7.3: Static conditions for BVCI */
324 if (cause == NS_CAUSE_BVCI_UNKNOWN)
325 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
326
327 return gprs_ns_tx(nsvc, msg);
328}
329
Harald Welte99e32482010-05-11 06:20:54 +0200330int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
331{
Harald Weltee4860d72010-05-19 15:38:10 +0200332 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte99e32482010-05-11 06:20:54 +0200333 struct gprs_ns_hdr *nsh;
334 uint16_t nsvci = htons(nsvc->nsvci);
335
Harald Welte8be8c8f2010-05-15 23:52:02 +0200336 log_set_context(BSC_CTX_NSVC, nsvc);
337
Harald Welte99e32482010-05-11 06:20:54 +0200338 if (!msg)
339 return -ENOMEM;
340
Harald Welte34caeb32010-05-12 12:01:33 +0000341 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
342 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
343
Harald Welte99e32482010-05-11 06:20:54 +0200344 /* be conservative and mark it as blocked even now! */
345 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200346 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200347
Harald Weltef2b4cd72010-05-13 11:45:07 +0200348 msg->l2h = msgb_put(msg, sizeof(*nsh));
349 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte99e32482010-05-11 06:20:54 +0200350 nsh->pdu_type = NS_PDUT_BLOCK;
351
352 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
353 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
354
355 return gprs_ns_tx(nsvc, msg);
356}
357
358int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
359{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200360 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte34caeb32010-05-12 12:01:33 +0000361 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
362 nsvc->nsei, nsvc->nsvci);
363
Harald Welte99e32482010-05-11 06:20:54 +0200364 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
365}
366
Harald Weltebca900d2010-05-12 12:11:45 +0000367int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
368{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200369 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000370 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
371 nsvc->nsei, nsvc->nsvci);
372
373 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
374}
375
376int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
377{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200378 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000379 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
380 nsvc->nsei, nsvc->nsvci);
381
382 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
383}
384
Harald Welteea4647d2010-05-12 17:19:53 +0000385static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
386 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
387 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
388 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte199d9df2010-05-03 20:55:10 +0200389};
390
Harald Welte811c4972010-05-12 12:21:52 +0000391static const struct value_string timer_mode_strs[] = {
392 { NSVC_TIMER_TNS_RESET, "tns-reset" },
393 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
394 { NSVC_TIMER_TNS_TEST, "tns-test" },
395 { 0, NULL }
396};
397
Harald Welte199d9df2010-05-03 20:55:10 +0200398static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
399{
Harald Welteea4647d2010-05-12 17:19:53 +0000400 enum ns_timeout tout = timer_mode_tout[mode];
401 unsigned int seconds = nsvc->nsi->timeout[tout];
402
Harald Welte8be8c8f2010-05-15 23:52:02 +0200403 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000404 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
405 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000406 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000407
Harald Welte199d9df2010-05-03 20:55:10 +0200408 if (bsc_timer_pending(&nsvc->timer))
409 bsc_del_timer(&nsvc->timer);
410
411 nsvc->timer_mode = mode;
Harald Welteea4647d2010-05-12 17:19:53 +0000412 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte199d9df2010-05-03 20:55:10 +0200413}
414
Harald Welte05b320a2010-05-03 20:16:13 +0200415static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800416{
417 struct gprs_nsvc *nsvc = data;
Harald Welteea4647d2010-05-12 17:19:53 +0000418 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
419 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9b455bf2010-03-14 15:45:01 +0800420
Harald Welte8be8c8f2010-05-15 23:52:02 +0200421 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000422 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
423 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000424 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000425
Harald Welte05b320a2010-05-03 20:16:13 +0200426 switch (nsvc->timer_mode) {
427 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-03-14 15:45:01 +0800428 /* Tns-alive case: we expired without response ! */
429 nsvc->alive_retries++;
Harald Welteea4647d2010-05-12 17:19:53 +0000430 if (nsvc->alive_retries >
431 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800432 /* mark as dead and blocked */
433 nsvc->state = NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200434 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
435 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200436 LOGP(DNS, LOGL_NOTICE,
437 "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200438 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Welteea4647d2010-05-12 17:19:53 +0000439 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welteb778d2c2010-05-12 13:28:25 +0000440 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200441 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800442 return;
443 }
Harald Weltebca900d2010-05-12 12:11:45 +0000444 /* Tns-test case: send NS-ALIVE PDU */
445 gprs_ns_tx_alive(nsvc);
446 /* start Tns-alive timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200447 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200448 break;
449 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800450 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltebca900d2010-05-12 12:11:45 +0000451 gprs_ns_tx_alive(nsvc);
452 /* start Tns-alive timer (transition into faster
453 * alive retransmissions) */
Harald Welte811c4972010-05-12 12:21:52 +0000454 nsvc->alive_retries = 0;
Harald Welte199d9df2010-05-03 20:55:10 +0200455 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
456 break;
457 case NSVC_TIMER_TNS_RESET:
458 /* Chapter 7.3: Re-send the RESET */
Harald Welte90de93e2010-05-03 21:05:24 +0200459 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200460 /* Re-start Tns-reset timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200461 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200462 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200463 case _NSVC_TIMER_NR:
464 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800465 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800466}
467
468/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800469static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800470{
Harald Weltee4860d72010-05-19 15:38:10 +0200471 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +0800472 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200473 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800474
Harald Welte8be8c8f2010-05-15 23:52:02 +0200475 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800476 if (!msg)
477 return -ENOMEM;
478
Harald Welte5434d7e2010-03-18 00:01:43 +0800479 nsvci = htons(nsvc->nsvci);
480 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800481
Harald Weltef2b4cd72010-05-13 11:45:07 +0200482 msg->l2h = msgb_put(msg, sizeof(*nsh));
483 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800484
485 nsh->pdu_type = NS_PDUT_RESET_ACK;
486
Harald Weltefcc4cc92010-05-12 00:16:57 +0200487 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200488 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800489
Harald Welteeaa614c2010-05-02 11:26:34 +0200490 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
491 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800492
Harald Welte44f1c272010-04-30 19:54:29 +0200493 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800494}
495
Harald Welte44f1c272010-04-30 19:54:29 +0200496/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
497int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800498{
Harald Welte44f1c272010-04-30 19:54:29 +0200499 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800500 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200501 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200502
503 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
504 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200505 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200506 "to NS-VC!\n", msgb_nsei(msg));
Harald Weltec1fa7942010-08-09 21:15:40 +0800507 msgb_free(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200508 return -EINVAL;
509 }
Harald Welte8be8c8f2010-05-15 23:52:02 +0200510 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800511
Harald Welte199d9df2010-05-03 20:55:10 +0200512 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200513 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200514 nsvc->nsei);
Harald Weltec1fa7942010-08-09 21:15:40 +0800515 msgb_free(msg);
Harald Welte199d9df2010-05-03 20:55:10 +0200516 return -EBUSY;
517 }
518 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200519 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200520 nsvc->nsei);
Harald Weltec1fa7942010-08-09 21:15:40 +0800521 msgb_free(msg);
Harald Welte199d9df2010-05-03 20:55:10 +0200522 return -EBUSY;
523 }
524
Harald Weltee1833452010-05-13 12:18:49 +0200525 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
526 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800527 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200528 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltec1fa7942010-08-09 21:15:40 +0800529 msgb_free(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800530 return -EIO;
531 }
532
533 nsh->pdu_type = NS_PDUT_UNITDATA;
534 /* spare octet in data[0] */
535 nsh->data[1] = bvci >> 8;
536 nsh->data[2] = bvci & 0xff;
537
Harald Welte44f1c272010-04-30 19:54:29 +0200538 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800539}
540
541/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200542static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800543{
544 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200545 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800546
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200547 if (nsvc->state & NSE_S_BLOCKED)
548 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
549 0, msg);
550
Harald Welte9b455bf2010-03-14 15:45:01 +0800551 /* spare octet in data[0] */
552 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200553 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200554 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800555
556 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200557 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800558}
559
560/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200561static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800562{
Harald Weltecb991632010-04-26 19:18:54 +0200563 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800564 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200565 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800566 int rc;
567
Harald Welte7363e922010-05-16 00:24:26 +0200568 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800569
Harald Welted75d71e2010-05-30 17:19:38 +0200570 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
571 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200572 if (rc < 0) {
573 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
574 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
575 "Error during TLV Parse\n", nsvc->nsei);
576 return rc;
577 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800578
579 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltefcc4cc92010-05-12 00:16:57 +0200580 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800581 return -EINVAL;
582 }
583
584 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte7363e922010-05-16 00:24:26 +0200585 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800586
587 return 0;
588}
589
590/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200591static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800592{
593 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800594 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200595 uint8_t *cause;
596 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800597 int rc;
598
Harald Welted75d71e2010-05-30 17:19:38 +0200599 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
600 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200601 if (rc < 0) {
602 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
603 "Error during TLV Parse\n", nsvc->nsei);
604 return rc;
605 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800606
607 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
608 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
609 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200610 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200611 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800612 return -EINVAL;
613 }
614
Harald Welteeaa614c2010-05-02 11:26:34 +0200615 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
616 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
617 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800618
Harald Weltefcc4cc92010-05-12 00:16:57 +0200619 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200620 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
621
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200622 /* Mark NS-VC as blocked and alive */
Harald Welte5434d7e2010-03-18 00:01:43 +0800623 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200624
Harald Welte5434d7e2010-03-18 00:01:43 +0800625 nsvc->nsei = ntohs(*nsei);
626 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800627
Harald Welte9b455bf2010-03-14 15:45:01 +0800628 /* start the test procedure */
Harald Weltedccf5552010-05-28 09:58:27 +0200629 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
630 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9b455bf2010-03-14 15:45:01 +0800631
Harald Welte99e32482010-05-11 06:20:54 +0200632 /* inform interested parties about the fact that this NSVC
633 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200634 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200635
Harald Welte5434d7e2010-03-18 00:01:43 +0800636 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800637}
638
Harald Welte99e32482010-05-11 06:20:54 +0200639static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
640{
641 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
642 struct tlv_parsed tp;
643 uint8_t *cause;
644 int rc;
645
Harald Weltefcc4cc92010-05-12 00:16:57 +0200646 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte99e32482010-05-11 06:20:54 +0200647
648 nsvc->state |= NSE_S_BLOCKED;
649
Harald Welted75d71e2010-05-30 17:19:38 +0200650 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
651 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200652 if (rc < 0) {
653 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
654 "Error during TLV Parse\n", nsvc->nsei);
655 return rc;
656 }
Harald Welte99e32482010-05-11 06:20:54 +0200657
658 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
659 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte99e32482010-05-11 06:20:54 +0200660 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200661 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte99e32482010-05-11 06:20:54 +0200662 return -EINVAL;
663 }
664
665 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
666 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
667
Harald Welte1389ac72010-05-11 10:15:26 +0200668 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec1919862010-05-13 12:55:20 +0200669 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200670
671 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
672}
673
Harald Welte9b455bf2010-03-14 15:45:01 +0800674/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200675int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welte5540c4c2010-05-19 14:38:50 +0200676 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9b455bf2010-03-14 15:45:01 +0800677{
678 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200679 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800680 int rc = 0;
681
Harald Weltecb991632010-04-26 19:18:54 +0200682 /* look up the NSVC based on source address */
683 nsvc = nsvc_by_rem_addr(nsi, saddr);
684 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200685 struct tlv_parsed tp;
686 uint16_t nsei;
Harald Weltee72c61c2010-05-19 16:01:39 +0200687 if (nsh->pdu_type == NS_PDUT_STATUS) {
688 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
689 "for non-existing NS-VC\n",
690 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
691 return 0;
692 }
Harald Weltecb991632010-04-26 19:18:54 +0200693 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200694 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte9aa97fc2010-05-13 13:58:08 +0200695 /* Since we have no NSVC, we have to use a fake */
696 nsvc = nsi->unknown_nsvc;
Harald Welte8be8c8f2010-05-15 23:52:02 +0200697 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200698 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Welteef1226e2010-05-11 18:38:36 +0200699 "from %s:%u for non-existing NS-VC\n",
700 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
701 ntohs(saddr->sin_port));
Harald Welte9aa97fc2010-05-13 13:58:08 +0200702 nsvc->nsvci = nsvc->nsei = 0xfffe;
703 nsvc->ip.bts_addr = *saddr;
704 nsvc->state = NSE_S_ALIVE;
Harald Welte5540c4c2010-05-19 14:38:50 +0200705 nsvc->ll = ll;
Harald Welte77289c22010-05-18 14:32:29 +0200706#if 0
707 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
708#else
Harald Welte9aa97fc2010-05-13 13:58:08 +0200709 return gprs_ns_tx_status(nsvc,
Harald Welte8c2440e2010-05-12 13:51:08 +0000710 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
711 msg);
Harald Welte77289c22010-05-18 14:32:29 +0200712#endif
Harald Welte239cf052010-05-03 18:54:12 +0200713 }
Harald Weltee5117da2010-05-11 18:30:37 +0200714 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
Harald Welted75d71e2010-05-30 17:19:38 +0200715 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200716 if (rc < 0) {
Harald Welted75d71e2010-05-30 17:19:38 +0200717 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
718 "TLV Parse\n", rc);
Harald Welteea985a42010-05-28 10:08:14 +0200719 return rc;
720 }
Harald Weltee5117da2010-05-11 18:30:37 +0200721 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
722 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
723 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Weltee5117da2010-05-11 18:30:37 +0200724 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200725 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
726 msg);
Harald Weltee5117da2010-05-11 18:30:37 +0200727 return -EINVAL;
728 }
729 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
730 /* Check if we already know this NSEI, the remote end might
731 * simply have changed addresses, or it is a SGSN */
732 nsvc = nsvc_by_nsei(nsi, nsei);
733 if (!nsvc) {
Harald Welte8be8c8f2010-05-15 23:52:02 +0200734 nsvc = nsvc_create(nsi, 0xffff);
Harald Welte5540c4c2010-05-19 14:38:50 +0200735 nsvc->ll = ll;
Harald Welte8be8c8f2010-05-15 23:52:02 +0200736 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltee5117da2010-05-11 18:30:37 +0200737 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
738 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltee5117da2010-05-11 18:30:37 +0200739 }
Harald Welte7c209eb2010-05-11 18:40:45 +0200740 /* Update the remote peer IP address/port */
741 nsvc->ip.bts_addr = *saddr;
Harald Welte239cf052010-05-03 18:54:12 +0200742 } else
743 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800744
Harald Welte8be8c8f2010-05-15 23:52:02 +0200745 log_set_context(BSC_CTX_NSVC, nsvc);
746
Harald Weltef2b4cd72010-05-13 11:45:07 +0200747 /* Increment number of Incoming bytes */
Harald Weltec1919862010-05-13 12:55:20 +0200748 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec1919862010-05-13 12:55:20 +0200749 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Weltef2b4cd72010-05-13 11:45:07 +0200750
Harald Welte9b455bf2010-03-14 15:45:01 +0800751 switch (nsh->pdu_type) {
752 case NS_PDUT_ALIVE:
Harald Welte1194b582010-05-12 15:55:23 +0000753 /* If we're dead and blocked and suddenly receive a
754 * NS-ALIVE out of the blue, we might have been re-started
755 * and should send a NS-RESET to make sure everything recovers
756 * fine. */
757 if (nsvc->state == NSE_S_BLOCKED)
758 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
759 else
760 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800761 break;
762 case NS_PDUT_ALIVE_ACK:
Harald Welteff56d612010-05-15 23:02:24 +0200763 /* stop Tns-alive and start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200764 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200765 if (nsvc->remote_end_is_sgsn) {
766 /* FIXME: this should be one level higher */
767 if (nsvc->state & NSE_S_BLOCKED)
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200768 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte24b31312010-05-03 21:11:22 +0200769 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800770 break;
771 case NS_PDUT_UNITDATA:
772 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200773 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800774 break;
775 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200776 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800777 break;
778 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200779 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800780 break;
781 case NS_PDUT_RESET_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200782 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200783 /* mark NS-VC as blocked + active */
784 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200785 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec1919862010-05-13 12:55:20 +0200786 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte1ccbf442010-05-14 11:53:08 +0000787 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte199d9df2010-05-03 20:55:10 +0200788 /* stop RESET timer */
789 bsc_del_timer(&nsvc->timer);
790 }
Harald Welteff56d612010-05-15 23:02:24 +0200791 /* Initiate TEST proc.: Send ALIVE and start timer */
792 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltedccf5552010-05-28 09:58:27 +0200793 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9b455bf2010-03-14 15:45:01 +0800794 break;
795 case NS_PDUT_UNBLOCK:
796 /* Section 7.2: unblocking procedure */
Harald Weltefcc4cc92010-05-12 00:16:57 +0200797 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800798 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200799 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800800 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800801 break;
802 case NS_PDUT_UNBLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200803 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200804 /* mark NS-VC as unblocked + active */
805 nsvc->state = NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200806 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef6d67c02010-05-12 14:18:46 +0000807 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800808 break;
809 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200810 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800811 break;
812 case NS_PDUT_BLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200813 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200814 /* mark remote NS-VC as blocked + active */
815 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800816 break;
817 default:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200818 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200819 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800820 rc = -EINVAL;
821 break;
822 }
823 return rc;
824}
825
Harald Weltecb991632010-04-26 19:18:54 +0200826struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
827{
828 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
829
830 nsi->cb = cb;
831 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Welteea4647d2010-05-12 17:19:53 +0000832 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
833 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
834 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
835 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
836 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
837 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
838 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltecb991632010-04-26 19:18:54 +0200839
Harald Welte3863e042010-05-13 14:20:56 +0200840 /* Create the dummy NSVC that we use for sending
841 * messages to non-existant/unknown NS-VC's */
Harald Welte9aa97fc2010-05-13 13:58:08 +0200842 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte3863e042010-05-13 14:20:56 +0200843 llist_del(&nsi->unknown_nsvc->list);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200844
Harald Welte9f75c352010-04-30 20:26:32 +0200845 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200846}
847
848void gprs_ns_destroy(struct gprs_ns_inst *nsi)
849{
850 /* FIXME: clear all timers */
851
852 /* recursively free the NSI and all its NSVCs */
853 talloc_free(nsi);
854}
855
856
857/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
858 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
859
860/* Read a single NS-over-IP message */
861static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
862 struct sockaddr_in *saddr)
863{
Harald Weltee4860d72010-05-19 15:38:10 +0200864 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltecb991632010-04-26 19:18:54 +0200865 int ret = 0;
866 socklen_t saddr_len = sizeof(*saddr);
867
868 if (!msg) {
869 *error = -ENOMEM;
870 return NULL;
871 }
872
Holger Hans Peter Freyther900aeaf2010-05-28 03:25:36 +0800873 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltecb991632010-04-26 19:18:54 +0200874 (struct sockaddr *)saddr, &saddr_len);
875 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200876 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200877 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200878 msgb_free(msg);
879 *error = ret;
880 return NULL;
881 } else if (ret == 0) {
882 msgb_free(msg);
883 *error = ret;
884 return NULL;
885 }
886
887 msg->l2h = msg->data;
888 msgb_put(msg, ret);
889
890 return msg;
891}
892
893static int handle_nsip_read(struct bsc_fd *bfd)
894{
895 int error;
896 struct sockaddr_in saddr;
897 struct gprs_ns_inst *nsi = bfd->data;
898 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
899
900 if (!msg)
901 return error;
902
Harald Welte5540c4c2010-05-19 14:38:50 +0200903 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte8272c772010-05-12 18:38:45 +0000904
905 msgb_free(msg);
906
907 return error;
Harald Weltecb991632010-04-26 19:18:54 +0200908}
909
910static int handle_nsip_write(struct bsc_fd *bfd)
911{
912 /* FIXME: actually send the data here instead of nsip_sendmsg() */
913 return -EIO;
914}
915
Harald Welte5540c4c2010-05-19 14:38:50 +0200916static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200917{
918 int rc;
919 struct gprs_ns_inst *nsi = nsvc->nsi;
920 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
921
922 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
923 (struct sockaddr *)daddr, sizeof(*daddr));
924
925 talloc_free(msg);
926
927 return rc;
928}
929
930/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
931static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
932{
933 int rc = 0;
934
935 if (what & BSC_FD_READ)
936 rc = handle_nsip_read(bfd);
937 if (what & BSC_FD_WRITE)
938 rc = handle_nsip_write(bfd);
939
940 return rc;
941}
942
Harald Weltecb991632010-04-26 19:18:54 +0200943/* Listen for incoming GPRS packets */
Harald Welteff3bde82010-05-19 15:09:09 +0200944int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltecb991632010-04-26 19:18:54 +0200945{
946 int ret;
947
Harald Welteff3bde82010-05-19 15:09:09 +0200948 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
949 nsi->nsip.local_port, nsip_fd_cb);
Harald Weltecb991632010-04-26 19:18:54 +0200950 if (ret < 0)
951 return ret;
952
Harald Weltecb991632010-04-26 19:18:54 +0200953 nsi->nsip.fd.data = nsi;
954
955 return ret;
956}
Harald Welte9f75c352010-04-30 20:26:32 +0200957
Harald Welte1ccbf442010-05-14 11:53:08 +0000958/* Initiate a RESET procedure */
Holger Hans Peter Freyther83e0b3f2010-05-23 21:18:01 +0800959void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte1ccbf442010-05-14 11:53:08 +0000960{
Harald Welte4f1e8152010-05-25 22:17:30 +0200961 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
962 nsvc->nsei);
963
Harald Welte1ccbf442010-05-14 11:53:08 +0000964 /* Mark NS-VC locally as blocked and dead */
965 nsvc->state = NSE_S_BLOCKED;
966 /* Send NS-RESET PDU */
967 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
968 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
969 nsvc->nsei);
970 }
971 /* Start Tns-reset */
972 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte1ccbf442010-05-14 11:53:08 +0000973}
974
Harald Welte9f75c352010-04-30 20:26:32 +0200975/* Establish a connection (from the BSS) to the SGSN */
976struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200977 struct sockaddr_in *dest, uint16_t nsei,
978 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200979{
980 struct gprs_nsvc *nsvc;
981
982 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Weltecad83012010-05-12 11:56:39 +0000983 if (!nsvc)
Harald Welte9f75c352010-04-30 20:26:32 +0200984 nsvc = nsvc_create(nsi, nsvci);
Harald Weltecad83012010-05-12 11:56:39 +0000985 nsvc->ip.bts_addr = *dest;
Harald Welteb77c6972010-05-01 11:28:43 +0200986 nsvc->nsei = nsei;
987 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200988 nsvc->remote_end_is_sgsn = 1;
989
Holger Hans Peter Freyther83e0b3f2010-05-23 21:18:01 +0800990 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
991 return nsvc;
Harald Welte9f75c352010-04-30 20:26:32 +0200992}