blob: 6d028aba13d2170b3a149bf47c140608073212af [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
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 Welte9aa97fc2010-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 Welte9b455bf2010-03-14 15:45:01 +080055#include <stdlib.h>
56#include <unistd.h>
57#include <errno.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020058#include <stdint.h>
Harald Welte9b455bf2010-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 Weltecb991632010-04-26 19:18:54 +020066#include <osmocore/select.h>
Harald Weltef2b4cd72010-05-13 11:45:07 +020067#include <osmocore/rate_ctr.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080068#include <openbsc/debug.h>
Harald Welte99e32482010-05-11 06:20:54 +020069#include <openbsc/signal.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080070#include <openbsc/gprs_ns.h>
71#include <openbsc/gprs_bssgp.h>
72
73#define NS_ALLOC_SIZE 1024
74
75static const struct tlv_definition ns_att_tlvdef = {
76 .def = {
77 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
78 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
79 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
80 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
81 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
82 },
83};
84
Harald Weltec1919862010-05-13 12:55:20 +020085enum ns_ctr {
86 NS_CTR_PKTS_IN,
87 NS_CTR_PKTS_OUT,
88 NS_CTR_BYTES_IN,
89 NS_CTR_BYTES_OUT,
90 NS_CTR_BLOCKED,
91 NS_CTR_DEAD,
92};
93
Harald Weltef2b4cd72010-05-13 11:45:07 +020094static const struct rate_ctr_desc nsvc_ctr_description[] = {
95 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec1919862010-05-13 12:55:20 +020096 { "packets.out","Packets at NS Level (Out)" },
97 { "bytes.in", "Bytes at NS Level ( In)" },
98 { "bytes.out", "Bytes at NS Level (Out)" },
99 { "blocked", "NS-VC Block count " },
100 { "dead", "NS-VC gone dead count " },
Harald Weltef2b4cd72010-05-13 11:45:07 +0200101};
102
103static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec1919862010-05-13 12:55:20 +0200104 .group_name_prefix = "ns.nsvc",
Harald Weltef2b4cd72010-05-13 11:45:07 +0200105 .group_description = "NSVC Peer Statistics",
106 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
107 .ctr_desc = nsvc_ctr_description,
108};
109
Harald Weltecb991632010-04-26 19:18:54 +0200110/* Lookup struct gprs_nsvc based on NSVCI */
Harald Welteff56d612010-05-15 23:02:24 +0200111struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-04-26 19:18:54 +0200112{
113 struct gprs_nsvc *nsvc;
114 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
115 if (nsvc->nsvci == nsvci)
116 return nsvc;
117 }
118 return NULL;
119}
120
Harald Welte44f1c272010-04-30 19:54:29 +0200121/* Lookup struct gprs_nsvc based on NSVCI */
Harald Weltef2b4cd72010-05-13 11:45:07 +0200122struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte44f1c272010-04-30 19:54:29 +0200123{
124 struct gprs_nsvc *nsvc;
125 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
126 if (nsvc->nsei == nsei)
127 return nsvc;
128 }
129 return NULL;
130}
131
Harald Weltecb991632010-04-26 19:18:54 +0200132/* Lookup struct gprs_nsvc based on remote peer socket addr */
133static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
134 struct sockaddr_in *sin)
135{
136 struct gprs_nsvc *nsvc;
137 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Weltecad83012010-05-12 11:56:39 +0000138 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
139 sin->sin_addr.s_addr &&
140 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltecb991632010-04-26 19:18:54 +0200141 return nsvc;
142 }
143 return NULL;
144}
145
Harald Welte199d9df2010-05-03 20:55:10 +0200146static void gprs_ns_timer_cb(void *data);
147
Harald Weltef2b4cd72010-05-13 11:45:07 +0200148struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-04-26 19:18:54 +0200149{
150 struct gprs_nsvc *nsvc;
151
152 nsvc = talloc_zero(nsi, struct gprs_nsvc);
153 nsvc->nsvci = nsvci;
154 /* before RESET procedure: BLOCKED and DEAD */
155 nsvc->state = NSE_S_BLOCKED;
156 nsvc->nsi = nsi;
Harald Welte199d9df2010-05-03 20:55:10 +0200157 nsvc->timer.cb = gprs_ns_timer_cb;
158 nsvc->timer.data = nsvc;
Harald Weltef2b4cd72010-05-13 11:45:07 +0200159 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte199d9df2010-05-03 20:55:10 +0200160
Harald Weltecb991632010-04-26 19:18:54 +0200161 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
162
163 return nsvc;
164}
Harald Welte9b455bf2010-03-14 15:45:01 +0800165
Harald Weltef2b4cd72010-05-13 11:45:07 +0200166void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte1194b582010-05-12 15:55:23 +0000167{
168 if (bsc_timer_pending(&nsvc->timer))
169 bsc_del_timer(&nsvc->timer);
170 llist_del(&nsvc->list);
171 talloc_free(nsvc);
172}
173
Harald Welte1389ac72010-05-11 10:15:26 +0200174static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200175 uint8_t cause)
176{
177 struct ns_signal_data nssd;
178
179 nssd.nsvc = nsvc;
180 nssd.cause = cause;
181
182 dispatch_signal(SS_NS, signal, &nssd);
183}
184
Harald Welte9b455bf2010-03-14 15:45:01 +0800185/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-05-02 09:37:45 +0200186static const struct value_string ns_cause_str[] = {
187 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
188 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
189 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
190 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
191 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
192 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
193 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
194 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte199d9df2010-05-03 20:55:10 +0200195 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-05-02 09:37:45 +0200196 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
197 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
198 { 0, NULL }
Harald Welte9b455bf2010-03-14 15:45:01 +0800199};
200
Harald Welte345223e2010-05-02 09:37:45 +0200201const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800202{
Harald Welte345223e2010-05-02 09:37:45 +0200203 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800204}
205
Harald Welte44f1c272010-04-30 19:54:29 +0200206static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200207
Harald Welte44f1c272010-04-30 19:54:29 +0200208static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800209{
Harald Weltecb991632010-04-26 19:18:54 +0200210 int ret;
211
Harald Welte8be8c8f2010-05-15 23:52:02 +0200212 log_set_context(BSC_CTX_NSVC, nsvc);
213
Harald Weltef2b4cd72010-05-13 11:45:07 +0200214 /* Increment number of Uplink bytes */
Harald Weltec1919862010-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 Weltef2b4cd72010-05-13 11:45:07 +0200217
Harald Weltecb991632010-04-26 19:18:54 +0200218 switch (nsvc->nsi->ll) {
219 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200220 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200221 break;
222 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200223 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200224 msgb_free(msg);
225 ret = -EIO;
226 break;
227 }
228 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800229}
230
Harald Welteeaa614c2010-05-02 11:26:34 +0200231static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800232{
233 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
234 struct gprs_ns_hdr *nsh;
235
Harald Welte8be8c8f2010-05-15 23:52:02 +0200236 log_set_context(BSC_CTX_NSVC, nsvc);
237
Harald Welte9b455bf2010-03-14 15:45:01 +0800238 if (!msg)
239 return -ENOMEM;
240
Harald Weltef2b4cd72010-05-13 11:45:07 +0200241 msg->l2h = msgb_put(msg, sizeof(*nsh));
242 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800243
244 nsh->pdu_type = pdu_type;
245
Harald Welte44f1c272010-04-30 19:54:29 +0200246 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800247}
248
Harald Welte99e32482010-05-11 06:20:54 +0200249int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200250{
251 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
252 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-05-03 20:55:10 +0200253 uint16_t nsvci = htons(nsvc->nsvci);
254 uint16_t nsei = htons(nsvc->nsei);
255
Harald Welte8be8c8f2010-05-15 23:52:02 +0200256 log_set_context(BSC_CTX_NSVC, nsvc);
257
Harald Welte199d9df2010-05-03 20:55:10 +0200258 if (!msg)
259 return -ENOMEM;
260
Harald Welte34caeb32010-05-12 12:01:33 +0000261 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
262 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
263
Harald Weltef2b4cd72010-05-13 11:45:07 +0200264 msg->l2h = msgb_put(msg, sizeof(*nsh));
265 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte199d9df2010-05-03 20:55:10 +0200266 nsh->pdu_type = NS_PDUT_RESET;
267
268 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
269 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
270 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
271
272 return gprs_ns_tx(nsvc, msg);
273
274}
275
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200276int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
277 uint16_t bvci, struct msgb *orig_msg)
278{
279 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
280 struct gprs_ns_hdr *nsh;
281 uint16_t nsvci = htons(nsvc->nsvci);
282
Harald Welte8be8c8f2010-05-15 23:52:02 +0200283 log_set_context(BSC_CTX_NSVC, nsvc);
284
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200285 bvci = htons(bvci);
286
287 if (!msg)
288 return -ENOMEM;
289
Harald Welteff56d612010-05-15 23:02:24 +0200290 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte34caeb32010-05-12 12:01:33 +0000291 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
292
Harald Weltef2b4cd72010-05-13 11:45:07 +0200293 msg->l2h = msgb_put(msg, sizeof(*nsh));
294 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200295 nsh->pdu_type = NS_PDUT_STATUS;
296
297 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
298
299 /* Section 9.2.7.1: Static conditions for NS-VCI */
300 if (cause == NS_CAUSE_NSVC_BLOCKED ||
301 cause == NS_CAUSE_NSVC_UNKNOWN)
302 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
303
304 /* Section 9.2.7.2: Static conditions for NS PDU */
305 switch (cause) {
306 case NS_CAUSE_SEM_INCORR_PDU:
307 case NS_CAUSE_PDU_INCOMP_PSTATE:
308 case NS_CAUSE_PROTO_ERR_UNSPEC:
309 case NS_CAUSE_INVAL_ESSENT_IE:
310 case NS_CAUSE_MISSING_ESSENT_IE:
311 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
312 orig_msg->l2h);
313 break;
314 default:
315 break;
316 }
317
318 /* Section 9.2.7.3: Static conditions for BVCI */
319 if (cause == NS_CAUSE_BVCI_UNKNOWN)
320 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
321
322 return gprs_ns_tx(nsvc, msg);
323}
324
Harald Welte99e32482010-05-11 06:20:54 +0200325int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
326{
327 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
328 struct gprs_ns_hdr *nsh;
329 uint16_t nsvci = htons(nsvc->nsvci);
330
Harald Welte8be8c8f2010-05-15 23:52:02 +0200331 log_set_context(BSC_CTX_NSVC, nsvc);
332
Harald Welte99e32482010-05-11 06:20:54 +0200333 if (!msg)
334 return -ENOMEM;
335
Harald Welte34caeb32010-05-12 12:01:33 +0000336 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
337 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
338
Harald Welte99e32482010-05-11 06:20:54 +0200339 /* be conservative and mark it as blocked even now! */
340 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200341 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200342
Harald Weltef2b4cd72010-05-13 11:45:07 +0200343 msg->l2h = msgb_put(msg, sizeof(*nsh));
344 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte99e32482010-05-11 06:20:54 +0200345 nsh->pdu_type = NS_PDUT_BLOCK;
346
347 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
348 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
349
350 return gprs_ns_tx(nsvc, msg);
351}
352
353int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
354{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200355 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte34caeb32010-05-12 12:01:33 +0000356 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
357 nsvc->nsei, nsvc->nsvci);
358
Harald Welte99e32482010-05-11 06:20:54 +0200359 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
360}
361
Harald Weltebca900d2010-05-12 12:11:45 +0000362int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
363{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200364 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000365 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
366 nsvc->nsei, nsvc->nsvci);
367
368 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
369}
370
371int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
372{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200373 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000374 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
375 nsvc->nsei, nsvc->nsvci);
376
377 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
378}
379
Harald Welteea4647d2010-05-12 17:19:53 +0000380static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
381 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
382 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
383 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte199d9df2010-05-03 20:55:10 +0200384};
385
Harald Welte811c4972010-05-12 12:21:52 +0000386static const struct value_string timer_mode_strs[] = {
387 { NSVC_TIMER_TNS_RESET, "tns-reset" },
388 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
389 { NSVC_TIMER_TNS_TEST, "tns-test" },
390 { 0, NULL }
391};
392
Harald Welte199d9df2010-05-03 20:55:10 +0200393static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
394{
Harald Welteea4647d2010-05-12 17:19:53 +0000395 enum ns_timeout tout = timer_mode_tout[mode];
396 unsigned int seconds = nsvc->nsi->timeout[tout];
397
Harald Welte8be8c8f2010-05-15 23:52:02 +0200398 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000399 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
400 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000401 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000402
Harald Welte199d9df2010-05-03 20:55:10 +0200403 if (bsc_timer_pending(&nsvc->timer))
404 bsc_del_timer(&nsvc->timer);
405
406 nsvc->timer_mode = mode;
Harald Welteea4647d2010-05-12 17:19:53 +0000407 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte199d9df2010-05-03 20:55:10 +0200408}
409
Harald Welte05b320a2010-05-03 20:16:13 +0200410static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800411{
412 struct gprs_nsvc *nsvc = data;
Harald Welteea4647d2010-05-12 17:19:53 +0000413 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
414 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9b455bf2010-03-14 15:45:01 +0800415
Harald Welte8be8c8f2010-05-15 23:52:02 +0200416 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000417 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
418 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000419 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000420
Harald Welte05b320a2010-05-03 20:16:13 +0200421 switch (nsvc->timer_mode) {
422 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-03-14 15:45:01 +0800423 /* Tns-alive case: we expired without response ! */
424 nsvc->alive_retries++;
Harald Welteea4647d2010-05-12 17:19:53 +0000425 if (nsvc->alive_retries >
426 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800427 /* mark as dead and blocked */
428 nsvc->state = NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200429 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
430 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200431 LOGP(DNS, LOGL_NOTICE,
432 "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200433 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Welteea4647d2010-05-12 17:19:53 +0000434 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welteb778d2c2010-05-12 13:28:25 +0000435 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200436 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800437 return;
438 }
Harald Weltebca900d2010-05-12 12:11:45 +0000439 /* Tns-test case: send NS-ALIVE PDU */
440 gprs_ns_tx_alive(nsvc);
441 /* start Tns-alive timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200442 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200443 break;
444 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800445 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltebca900d2010-05-12 12:11:45 +0000446 gprs_ns_tx_alive(nsvc);
447 /* start Tns-alive timer (transition into faster
448 * alive retransmissions) */
Harald Welte811c4972010-05-12 12:21:52 +0000449 nsvc->alive_retries = 0;
Harald Welte199d9df2010-05-03 20:55:10 +0200450 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
451 break;
452 case NSVC_TIMER_TNS_RESET:
453 /* Chapter 7.3: Re-send the RESET */
Harald Welte90de93e2010-05-03 21:05:24 +0200454 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200455 /* Re-start Tns-reset timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200456 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200457 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200458 case _NSVC_TIMER_NR:
459 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800460 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800461}
462
463/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800464static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800465{
466 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
467 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200468 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800469
Harald Welte8be8c8f2010-05-15 23:52:02 +0200470 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800471 if (!msg)
472 return -ENOMEM;
473
Harald Welte5434d7e2010-03-18 00:01:43 +0800474 nsvci = htons(nsvc->nsvci);
475 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800476
Harald Weltef2b4cd72010-05-13 11:45:07 +0200477 msg->l2h = msgb_put(msg, sizeof(*nsh));
478 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800479
480 nsh->pdu_type = NS_PDUT_RESET_ACK;
481
Harald Weltefcc4cc92010-05-12 00:16:57 +0200482 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200483 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800484
Harald Welteeaa614c2010-05-02 11:26:34 +0200485 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
486 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800487
Harald Welte44f1c272010-04-30 19:54:29 +0200488 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800489}
490
Harald Welte44f1c272010-04-30 19:54:29 +0200491/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
492int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800493{
Harald Welte44f1c272010-04-30 19:54:29 +0200494 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800495 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200496 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200497
498 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
499 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200500 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200501 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte44f1c272010-04-30 19:54:29 +0200502 return -EINVAL;
503 }
Harald Welte8be8c8f2010-05-15 23:52:02 +0200504 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800505
Harald Welte199d9df2010-05-03 20:55:10 +0200506 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200507 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200508 nsvc->nsei);
509 return -EBUSY;
510 }
511 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200512 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200513 nsvc->nsei);
514 return -EBUSY;
515 }
516
Harald Weltee1833452010-05-13 12:18:49 +0200517 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
518 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800519 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200520 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800521 return -EIO;
522 }
523
524 nsh->pdu_type = NS_PDUT_UNITDATA;
525 /* spare octet in data[0] */
526 nsh->data[1] = bvci >> 8;
527 nsh->data[2] = bvci & 0xff;
528
Harald Welte44f1c272010-04-30 19:54:29 +0200529 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800530}
531
532/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200533static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800534{
535 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200536 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800537
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200538 if (nsvc->state & NSE_S_BLOCKED)
539 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
540 0, msg);
541
Harald Welte9b455bf2010-03-14 15:45:01 +0800542 /* spare octet in data[0] */
543 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200544 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200545 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800546
547 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200548 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800549}
550
551/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200552static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800553{
Harald Weltecb991632010-04-26 19:18:54 +0200554 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800555 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200556 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800557 int rc;
558
Harald Welte7363e922010-05-16 00:24:26 +0200559 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800560
561 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
562
563 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltefcc4cc92010-05-12 00:16:57 +0200564 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800565 return -EINVAL;
566 }
567
568 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte7363e922010-05-16 00:24:26 +0200569 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800570
571 return 0;
572}
573
574/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200575static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800576{
577 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800578 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200579 uint8_t *cause;
580 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800581 int rc;
582
Harald Welte9b455bf2010-03-14 15:45:01 +0800583 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
584
585 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
586 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
587 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200588 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200589 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800590 return -EINVAL;
591 }
592
Harald Welteeaa614c2010-05-02 11:26:34 +0200593 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
594 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
595 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800596
Harald Weltefcc4cc92010-05-12 00:16:57 +0200597 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200598 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
599
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200600 /* Mark NS-VC as blocked and alive */
Harald Welte5434d7e2010-03-18 00:01:43 +0800601 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200602
Harald Welte5434d7e2010-03-18 00:01:43 +0800603 nsvc->nsei = ntohs(*nsei);
604 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800605
Harald Welte9b455bf2010-03-14 15:45:01 +0800606 /* start the test procedure */
Harald Welte199d9df2010-05-03 20:55:10 +0200607 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800608
Harald Welte99e32482010-05-11 06:20:54 +0200609 /* inform interested parties about the fact that this NSVC
610 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200611 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200612
Harald Welte5434d7e2010-03-18 00:01:43 +0800613 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800614}
615
Harald Welte99e32482010-05-11 06:20:54 +0200616static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
617{
618 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
619 struct tlv_parsed tp;
620 uint8_t *cause;
621 int rc;
622
Harald Weltefcc4cc92010-05-12 00:16:57 +0200623 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte99e32482010-05-11 06:20:54 +0200624
625 nsvc->state |= NSE_S_BLOCKED;
626
627 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
628
629 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
630 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte99e32482010-05-11 06:20:54 +0200631 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200632 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte99e32482010-05-11 06:20:54 +0200633 return -EINVAL;
634 }
635
636 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
637 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
638
Harald Welte1389ac72010-05-11 10:15:26 +0200639 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec1919862010-05-13 12:55:20 +0200640 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200641
642 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
643}
644
Harald Welte9b455bf2010-03-14 15:45:01 +0800645/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200646int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
647 struct sockaddr_in *saddr)
Harald Welte9b455bf2010-03-14 15:45:01 +0800648{
649 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200650 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800651 int rc = 0;
652
Harald Weltecb991632010-04-26 19:18:54 +0200653 /* look up the NSVC based on source address */
654 nsvc = nsvc_by_rem_addr(nsi, saddr);
655 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200656 struct tlv_parsed tp;
657 uint16_t nsei;
Harald Weltecb991632010-04-26 19:18:54 +0200658 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200659 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte9aa97fc2010-05-13 13:58:08 +0200660 /* Since we have no NSVC, we have to use a fake */
661 nsvc = nsi->unknown_nsvc;
Harald Welte8be8c8f2010-05-15 23:52:02 +0200662 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200663 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Welteef1226e2010-05-11 18:38:36 +0200664 "from %s:%u for non-existing NS-VC\n",
665 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
666 ntohs(saddr->sin_port));
Harald Welte9aa97fc2010-05-13 13:58:08 +0200667 nsvc->nsvci = nsvc->nsei = 0xfffe;
668 nsvc->ip.bts_addr = *saddr;
669 nsvc->state = NSE_S_ALIVE;
Harald Welte77289c22010-05-18 14:32:29 +0200670#if 0
671 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
672#else
Harald Welte9aa97fc2010-05-13 13:58:08 +0200673 return gprs_ns_tx_status(nsvc,
Harald Welte8c2440e2010-05-12 13:51:08 +0000674 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
675 msg);
Harald Welte77289c22010-05-18 14:32:29 +0200676#endif
Harald Welte239cf052010-05-03 18:54:12 +0200677 }
Harald Weltee5117da2010-05-11 18:30:37 +0200678 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
679 msgb_l2len(msg), 0, 0);
680 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
681 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
682 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Weltee5117da2010-05-11 18:30:37 +0200683 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200684 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
685 msg);
Harald Weltee5117da2010-05-11 18:30:37 +0200686 return -EINVAL;
687 }
688 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
689 /* Check if we already know this NSEI, the remote end might
690 * simply have changed addresses, or it is a SGSN */
691 nsvc = nsvc_by_nsei(nsi, nsei);
692 if (!nsvc) {
Harald Welte8be8c8f2010-05-15 23:52:02 +0200693 nsvc = nsvc_create(nsi, 0xffff);
694 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltee5117da2010-05-11 18:30:37 +0200695 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
696 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltee5117da2010-05-11 18:30:37 +0200697 }
Harald Welte7c209eb2010-05-11 18:40:45 +0200698 /* Update the remote peer IP address/port */
699 nsvc->ip.bts_addr = *saddr;
Harald Welte239cf052010-05-03 18:54:12 +0200700 } else
701 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800702
Harald Welte8be8c8f2010-05-15 23:52:02 +0200703 log_set_context(BSC_CTX_NSVC, nsvc);
704
Harald Weltef2b4cd72010-05-13 11:45:07 +0200705 /* Increment number of Incoming bytes */
Harald Weltec1919862010-05-13 12:55:20 +0200706 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec1919862010-05-13 12:55:20 +0200707 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Weltef2b4cd72010-05-13 11:45:07 +0200708
Harald Welte9b455bf2010-03-14 15:45:01 +0800709 switch (nsh->pdu_type) {
710 case NS_PDUT_ALIVE:
Harald Welte1194b582010-05-12 15:55:23 +0000711 /* If we're dead and blocked and suddenly receive a
712 * NS-ALIVE out of the blue, we might have been re-started
713 * and should send a NS-RESET to make sure everything recovers
714 * fine. */
715 if (nsvc->state == NSE_S_BLOCKED)
716 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
717 else
718 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800719 break;
720 case NS_PDUT_ALIVE_ACK:
Harald Welteff56d612010-05-15 23:02:24 +0200721 /* stop Tns-alive and start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200722 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200723 if (nsvc->remote_end_is_sgsn) {
724 /* FIXME: this should be one level higher */
725 if (nsvc->state & NSE_S_BLOCKED)
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200726 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte24b31312010-05-03 21:11:22 +0200727 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800728 break;
729 case NS_PDUT_UNITDATA:
730 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200731 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800732 break;
733 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200734 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800735 break;
736 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200737 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800738 break;
739 case NS_PDUT_RESET_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200740 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200741 /* mark NS-VC as blocked + active */
742 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200743 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec1919862010-05-13 12:55:20 +0200744 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte1ccbf442010-05-14 11:53:08 +0000745 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte199d9df2010-05-03 20:55:10 +0200746 /* stop RESET timer */
747 bsc_del_timer(&nsvc->timer);
748 }
Harald Welteff56d612010-05-15 23:02:24 +0200749 /* Initiate TEST proc.: Send ALIVE and start timer */
750 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
751 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800752 break;
753 case NS_PDUT_UNBLOCK:
754 /* Section 7.2: unblocking procedure */
Harald Weltefcc4cc92010-05-12 00:16:57 +0200755 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800756 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200757 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800758 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800759 break;
760 case NS_PDUT_UNBLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200761 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200762 /* mark NS-VC as unblocked + active */
763 nsvc->state = NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200764 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef6d67c02010-05-12 14:18:46 +0000765 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800766 break;
767 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200768 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800769 break;
770 case NS_PDUT_BLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200771 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200772 /* mark remote NS-VC as blocked + active */
773 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800774 break;
775 default:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200776 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200777 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800778 rc = -EINVAL;
779 break;
780 }
781 return rc;
782}
783
Harald Weltecb991632010-04-26 19:18:54 +0200784struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
785{
786 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
787
788 nsi->cb = cb;
789 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Welteea4647d2010-05-12 17:19:53 +0000790 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
791 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
792 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
793 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
794 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
795 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
796 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltecb991632010-04-26 19:18:54 +0200797
Harald Welte3863e042010-05-13 14:20:56 +0200798 /* Create the dummy NSVC that we use for sending
799 * messages to non-existant/unknown NS-VC's */
Harald Welte9aa97fc2010-05-13 13:58:08 +0200800 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte3863e042010-05-13 14:20:56 +0200801 llist_del(&nsi->unknown_nsvc->list);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200802
Harald Welte9f75c352010-04-30 20:26:32 +0200803 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200804}
805
806void gprs_ns_destroy(struct gprs_ns_inst *nsi)
807{
808 /* FIXME: clear all timers */
809
810 /* recursively free the NSI and all its NSVCs */
811 talloc_free(nsi);
812}
813
814
815/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
816 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
817
818/* Read a single NS-over-IP message */
819static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
820 struct sockaddr_in *saddr)
821{
822 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
823 int ret = 0;
824 socklen_t saddr_len = sizeof(*saddr);
825
826 if (!msg) {
827 *error = -ENOMEM;
828 return NULL;
829 }
830
831 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
832 (struct sockaddr *)saddr, &saddr_len);
833 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200834 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200835 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200836 msgb_free(msg);
837 *error = ret;
838 return NULL;
839 } else if (ret == 0) {
840 msgb_free(msg);
841 *error = ret;
842 return NULL;
843 }
844
845 msg->l2h = msg->data;
846 msgb_put(msg, ret);
847
848 return msg;
849}
850
851static int handle_nsip_read(struct bsc_fd *bfd)
852{
853 int error;
854 struct sockaddr_in saddr;
855 struct gprs_ns_inst *nsi = bfd->data;
856 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
857
858 if (!msg)
859 return error;
860
Harald Welte8272c772010-05-12 18:38:45 +0000861 error = gprs_ns_rcvmsg(nsi, msg, &saddr);
862
863 msgb_free(msg);
864
865 return error;
Harald Weltecb991632010-04-26 19:18:54 +0200866}
867
868static int handle_nsip_write(struct bsc_fd *bfd)
869{
870 /* FIXME: actually send the data here instead of nsip_sendmsg() */
871 return -EIO;
872}
873
Harald Welte44f1c272010-04-30 19:54:29 +0200874int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200875{
876 int rc;
877 struct gprs_ns_inst *nsi = nsvc->nsi;
878 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
879
880 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
881 (struct sockaddr *)daddr, sizeof(*daddr));
882
883 talloc_free(msg);
884
885 return rc;
886}
887
888/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
889static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
890{
891 int rc = 0;
892
893 if (what & BSC_FD_READ)
894 rc = handle_nsip_read(bfd);
895 if (what & BSC_FD_WRITE)
896 rc = handle_nsip_write(bfd);
897
898 return rc;
899}
900
Harald Welteeaa614c2010-05-02 11:26:34 +0200901extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltecb991632010-04-26 19:18:54 +0200902 int (*cb)(struct bsc_fd *fd, unsigned int what));
903
904/* Listen for incoming GPRS packets */
905int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
906{
907 int ret;
908
909 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
910 if (ret < 0)
911 return ret;
912
913 nsi->ll = GPRS_NS_LL_UDP;
914 nsi->nsip.fd.data = nsi;
915
916 return ret;
917}
Harald Welte9f75c352010-04-30 20:26:32 +0200918
Harald Welte1ccbf442010-05-14 11:53:08 +0000919/* Initiate a RESET procedure */
920int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
921{
922 /* Mark NS-VC locally as blocked and dead */
923 nsvc->state = NSE_S_BLOCKED;
924 /* Send NS-RESET PDU */
925 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
926 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
927 nsvc->nsei);
928 }
929 /* Start Tns-reset */
930 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
931
932 return nsvc;
933}
934
Harald Welte9f75c352010-04-30 20:26:32 +0200935/* Establish a connection (from the BSS) to the SGSN */
936struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200937 struct sockaddr_in *dest, uint16_t nsei,
938 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200939{
940 struct gprs_nsvc *nsvc;
941
942 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Weltecad83012010-05-12 11:56:39 +0000943 if (!nsvc)
Harald Welte9f75c352010-04-30 20:26:32 +0200944 nsvc = nsvc_create(nsi, nsvci);
Harald Weltecad83012010-05-12 11:56:39 +0000945 nsvc->ip.bts_addr = *dest;
Harald Welteb77c6972010-05-01 11:28:43 +0200946 nsvc->nsei = nsei;
947 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200948 nsvc->remote_end_is_sgsn = 1;
949
Harald Welte1ccbf442010-05-14 11:53:08 +0000950 return gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9f75c352010-04-30 20:26:32 +0200951}