blob: 3db1d67fe9362a65b04b5de97a06255fb9901561 [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>
Holger Hans Peter Freyther2fbf12d2010-05-23 21:23:44 +080072#include <openbsc/gprs_ns_frgre.h>
Holger Hans Peter Freythercf4e9c82010-05-23 21:19:55 +080073#include <openbsc/socket.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080074
Harald Welte9b455bf2010-03-14 15:45:01 +080075static const struct tlv_definition ns_att_tlvdef = {
76 .def = {
77 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
78 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
79 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
80 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
81 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
82 },
83};
84
Harald 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
Harald Welte4f1e8152010-05-25 22:17:30 +0200152 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
153
Harald Weltecb991632010-04-26 19:18:54 +0200154 nsvc = talloc_zero(nsi, struct gprs_nsvc);
155 nsvc->nsvci = nsvci;
156 /* before RESET procedure: BLOCKED and DEAD */
157 nsvc->state = NSE_S_BLOCKED;
158 nsvc->nsi = nsi;
Harald Welte199d9df2010-05-03 20:55:10 +0200159 nsvc->timer.cb = gprs_ns_timer_cb;
160 nsvc->timer.data = nsvc;
Harald Weltef2b4cd72010-05-13 11:45:07 +0200161 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte199d9df2010-05-03 20:55:10 +0200162
Harald Weltecb991632010-04-26 19:18:54 +0200163 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
164
165 return nsvc;
166}
Harald Welte9b455bf2010-03-14 15:45:01 +0800167
Harald Weltef2b4cd72010-05-13 11:45:07 +0200168void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte1194b582010-05-12 15:55:23 +0000169{
170 if (bsc_timer_pending(&nsvc->timer))
171 bsc_del_timer(&nsvc->timer);
172 llist_del(&nsvc->list);
173 talloc_free(nsvc);
174}
175
Harald Welte1389ac72010-05-11 10:15:26 +0200176static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200177 uint8_t cause)
178{
179 struct ns_signal_data nssd;
180
181 nssd.nsvc = nsvc;
182 nssd.cause = cause;
183
184 dispatch_signal(SS_NS, signal, &nssd);
185}
186
Harald Welte9b455bf2010-03-14 15:45:01 +0800187/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-05-02 09:37:45 +0200188static const struct value_string ns_cause_str[] = {
189 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
190 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
191 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
192 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
193 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
194 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
195 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
196 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte199d9df2010-05-03 20:55:10 +0200197 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-05-02 09:37:45 +0200198 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
199 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
200 { 0, NULL }
Harald Welte9b455bf2010-03-14 15:45:01 +0800201};
202
Harald Welte345223e2010-05-02 09:37:45 +0200203const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800204{
Harald Welte345223e2010-05-02 09:37:45 +0200205 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800206}
207
Harald Welte44f1c272010-04-30 19:54:29 +0200208static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welte5540c4c2010-05-19 14:38:50 +0200209extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200210
Harald Welte44f1c272010-04-30 19:54:29 +0200211static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800212{
Harald Weltecb991632010-04-26 19:18:54 +0200213 int ret;
214
Harald Welte8be8c8f2010-05-15 23:52:02 +0200215 log_set_context(BSC_CTX_NSVC, nsvc);
216
Harald Weltef2b4cd72010-05-13 11:45:07 +0200217 /* Increment number of Uplink bytes */
Harald Weltec1919862010-05-13 12:55:20 +0200218 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
219 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Weltef2b4cd72010-05-13 11:45:07 +0200220
Harald Welte5540c4c2010-05-19 14:38:50 +0200221 switch (nsvc->ll) {
Harald Weltecb991632010-04-26 19:18:54 +0200222 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200223 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200224 break;
Harald Welte5540c4c2010-05-19 14:38:50 +0200225 case GPRS_NS_LL_FR_GRE:
226 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
227 break;
Harald Weltecb991632010-04-26 19:18:54 +0200228 default:
Harald Welte5540c4c2010-05-19 14:38:50 +0200229 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200230 msgb_free(msg);
231 ret = -EIO;
232 break;
233 }
234 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800235}
236
Harald Welteeaa614c2010-05-02 11:26:34 +0200237static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800238{
Harald Weltee4860d72010-05-19 15:38:10 +0200239 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +0800240 struct gprs_ns_hdr *nsh;
241
Harald Welte8be8c8f2010-05-15 23:52:02 +0200242 log_set_context(BSC_CTX_NSVC, nsvc);
243
Harald Welte9b455bf2010-03-14 15:45:01 +0800244 if (!msg)
245 return -ENOMEM;
246
Harald Weltef2b4cd72010-05-13 11:45:07 +0200247 msg->l2h = msgb_put(msg, sizeof(*nsh));
248 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800249
250 nsh->pdu_type = pdu_type;
251
Harald Welte44f1c272010-04-30 19:54:29 +0200252 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800253}
254
Harald Welte99e32482010-05-11 06:20:54 +0200255int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200256{
Harald Weltee4860d72010-05-19 15:38:10 +0200257 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte199d9df2010-05-03 20:55:10 +0200258 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-05-03 20:55:10 +0200259 uint16_t nsvci = htons(nsvc->nsvci);
260 uint16_t nsei = htons(nsvc->nsei);
261
Harald Welte8be8c8f2010-05-15 23:52:02 +0200262 log_set_context(BSC_CTX_NSVC, nsvc);
263
Harald Welte199d9df2010-05-03 20:55:10 +0200264 if (!msg)
265 return -ENOMEM;
266
Harald Welte34caeb32010-05-12 12:01:33 +0000267 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
268 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
269
Harald Weltef2b4cd72010-05-13 11:45:07 +0200270 msg->l2h = msgb_put(msg, sizeof(*nsh));
271 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte199d9df2010-05-03 20:55:10 +0200272 nsh->pdu_type = NS_PDUT_RESET;
273
274 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
275 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
276 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
277
278 return gprs_ns_tx(nsvc, msg);
279
280}
281
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200282int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
283 uint16_t bvci, struct msgb *orig_msg)
284{
Harald Weltee4860d72010-05-19 15:38:10 +0200285 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200286 struct gprs_ns_hdr *nsh;
287 uint16_t nsvci = htons(nsvc->nsvci);
288
Harald Welte8be8c8f2010-05-15 23:52:02 +0200289 log_set_context(BSC_CTX_NSVC, nsvc);
290
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200291 bvci = htons(bvci);
292
293 if (!msg)
294 return -ENOMEM;
295
Harald Welteff56d612010-05-15 23:02:24 +0200296 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte34caeb32010-05-12 12:01:33 +0000297 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
298
Harald Weltef2b4cd72010-05-13 11:45:07 +0200299 msg->l2h = msgb_put(msg, sizeof(*nsh));
300 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200301 nsh->pdu_type = NS_PDUT_STATUS;
302
303 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
304
305 /* Section 9.2.7.1: Static conditions for NS-VCI */
306 if (cause == NS_CAUSE_NSVC_BLOCKED ||
307 cause == NS_CAUSE_NSVC_UNKNOWN)
308 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
309
310 /* Section 9.2.7.2: Static conditions for NS PDU */
311 switch (cause) {
312 case NS_CAUSE_SEM_INCORR_PDU:
313 case NS_CAUSE_PDU_INCOMP_PSTATE:
314 case NS_CAUSE_PROTO_ERR_UNSPEC:
315 case NS_CAUSE_INVAL_ESSENT_IE:
316 case NS_CAUSE_MISSING_ESSENT_IE:
317 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
318 orig_msg->l2h);
319 break;
320 default:
321 break;
322 }
323
324 /* Section 9.2.7.3: Static conditions for BVCI */
325 if (cause == NS_CAUSE_BVCI_UNKNOWN)
326 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
327
328 return gprs_ns_tx(nsvc, msg);
329}
330
Harald Welte99e32482010-05-11 06:20:54 +0200331int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
332{
Harald Weltee4860d72010-05-19 15:38:10 +0200333 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte99e32482010-05-11 06:20:54 +0200334 struct gprs_ns_hdr *nsh;
335 uint16_t nsvci = htons(nsvc->nsvci);
336
Harald Welte8be8c8f2010-05-15 23:52:02 +0200337 log_set_context(BSC_CTX_NSVC, nsvc);
338
Harald Welte99e32482010-05-11 06:20:54 +0200339 if (!msg)
340 return -ENOMEM;
341
Harald Welte34caeb32010-05-12 12:01:33 +0000342 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
343 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
344
Harald Welte99e32482010-05-11 06:20:54 +0200345 /* be conservative and mark it as blocked even now! */
346 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200347 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200348
Harald Weltef2b4cd72010-05-13 11:45:07 +0200349 msg->l2h = msgb_put(msg, sizeof(*nsh));
350 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte99e32482010-05-11 06:20:54 +0200351 nsh->pdu_type = NS_PDUT_BLOCK;
352
353 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
354 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
355
356 return gprs_ns_tx(nsvc, msg);
357}
358
359int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
360{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200361 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte34caeb32010-05-12 12:01:33 +0000362 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
363 nsvc->nsei, nsvc->nsvci);
364
Harald Welte99e32482010-05-11 06:20:54 +0200365 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
366}
367
Harald Weltebca900d2010-05-12 12:11:45 +0000368int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
369{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200370 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000371 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
372 nsvc->nsei, nsvc->nsvci);
373
374 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
375}
376
377int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
378{
Harald Welte8be8c8f2010-05-15 23:52:02 +0200379 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltebca900d2010-05-12 12:11:45 +0000380 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
381 nsvc->nsei, nsvc->nsvci);
382
383 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
384}
385
Harald Welteea4647d2010-05-12 17:19:53 +0000386static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
387 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
388 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
389 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte199d9df2010-05-03 20:55:10 +0200390};
391
Harald Welte811c4972010-05-12 12:21:52 +0000392static const struct value_string timer_mode_strs[] = {
393 { NSVC_TIMER_TNS_RESET, "tns-reset" },
394 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
395 { NSVC_TIMER_TNS_TEST, "tns-test" },
396 { 0, NULL }
397};
398
Harald Welte199d9df2010-05-03 20:55:10 +0200399static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
400{
Harald Welteea4647d2010-05-12 17:19:53 +0000401 enum ns_timeout tout = timer_mode_tout[mode];
402 unsigned int seconds = nsvc->nsi->timeout[tout];
403
Harald Welte8be8c8f2010-05-15 23:52:02 +0200404 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000405 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
406 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000407 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000408
Harald Welte199d9df2010-05-03 20:55:10 +0200409 if (bsc_timer_pending(&nsvc->timer))
410 bsc_del_timer(&nsvc->timer);
411
412 nsvc->timer_mode = mode;
Harald Welteea4647d2010-05-12 17:19:53 +0000413 bsc_schedule_timer(&nsvc->timer, seconds, 0);
Harald Welte199d9df2010-05-03 20:55:10 +0200414}
415
Harald Welte05b320a2010-05-03 20:16:13 +0200416static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800417{
418 struct gprs_nsvc *nsvc = data;
Harald Welteea4647d2010-05-12 17:19:53 +0000419 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
420 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9b455bf2010-03-14 15:45:01 +0800421
Harald Welte8be8c8f2010-05-15 23:52:02 +0200422 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welted4eaf802010-05-12 13:55:36 +0000423 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
424 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Welteea4647d2010-05-12 17:19:53 +0000425 seconds);
Harald Welte811c4972010-05-12 12:21:52 +0000426
Harald Welte05b320a2010-05-03 20:16:13 +0200427 switch (nsvc->timer_mode) {
428 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-03-14 15:45:01 +0800429 /* Tns-alive case: we expired without response ! */
430 nsvc->alive_retries++;
Harald Welteea4647d2010-05-12 17:19:53 +0000431 if (nsvc->alive_retries >
432 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800433 /* mark as dead and blocked */
434 nsvc->state = NSE_S_BLOCKED;
Harald Weltec1919862010-05-13 12:55:20 +0200435 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
436 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200437 LOGP(DNS, LOGL_NOTICE,
438 "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200439 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Welteea4647d2010-05-12 17:19:53 +0000440 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Harald Welteb778d2c2010-05-12 13:28:25 +0000441 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200442 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800443 return;
444 }
Harald Weltebca900d2010-05-12 12:11:45 +0000445 /* Tns-test case: send NS-ALIVE PDU */
446 gprs_ns_tx_alive(nsvc);
447 /* start Tns-alive timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200448 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200449 break;
450 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800451 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltebca900d2010-05-12 12:11:45 +0000452 gprs_ns_tx_alive(nsvc);
453 /* start Tns-alive timer (transition into faster
454 * alive retransmissions) */
Harald Welte811c4972010-05-12 12:21:52 +0000455 nsvc->alive_retries = 0;
Harald Welte199d9df2010-05-03 20:55:10 +0200456 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
457 break;
458 case NSVC_TIMER_TNS_RESET:
459 /* Chapter 7.3: Re-send the RESET */
Harald Welte90de93e2010-05-03 21:05:24 +0200460 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200461 /* Re-start Tns-reset timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200462 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200463 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200464 case _NSVC_TIMER_NR:
465 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800466 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800467}
468
469/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800470static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800471{
Harald Weltee4860d72010-05-19 15:38:10 +0200472 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +0800473 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200474 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800475
Harald Welte8be8c8f2010-05-15 23:52:02 +0200476 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800477 if (!msg)
478 return -ENOMEM;
479
Harald Welte5434d7e2010-03-18 00:01:43 +0800480 nsvci = htons(nsvc->nsvci);
481 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800482
Harald Weltef2b4cd72010-05-13 11:45:07 +0200483 msg->l2h = msgb_put(msg, sizeof(*nsh));
484 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800485
486 nsh->pdu_type = NS_PDUT_RESET_ACK;
487
Harald Weltefcc4cc92010-05-12 00:16:57 +0200488 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200489 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800490
Harald Welteeaa614c2010-05-02 11:26:34 +0200491 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
492 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800493
Harald Welte44f1c272010-04-30 19:54:29 +0200494 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800495}
496
Harald Welte44f1c272010-04-30 19:54:29 +0200497/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
498int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800499{
Harald Welte44f1c272010-04-30 19:54:29 +0200500 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800501 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200502 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200503
504 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
505 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200506 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200507 "to NS-VC!\n", msgb_nsei(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);
515 return -EBUSY;
516 }
517 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200518 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200519 nsvc->nsei);
520 return -EBUSY;
521 }
522
Harald Weltee1833452010-05-13 12:18:49 +0200523 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
524 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800525 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200526 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800527 return -EIO;
528 }
529
530 nsh->pdu_type = NS_PDUT_UNITDATA;
531 /* spare octet in data[0] */
532 nsh->data[1] = bvci >> 8;
533 nsh->data[2] = bvci & 0xff;
534
Harald Welte44f1c272010-04-30 19:54:29 +0200535 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800536}
537
538/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200539static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800540{
541 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200542 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800543
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200544 if (nsvc->state & NSE_S_BLOCKED)
545 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
546 0, msg);
547
Harald Welte9b455bf2010-03-14 15:45:01 +0800548 /* spare octet in data[0] */
549 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200550 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200551 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800552
553 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200554 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800555}
556
557/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200558static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800559{
Harald Weltecb991632010-04-26 19:18:54 +0200560 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800561 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200562 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800563 int rc;
564
Harald Welte7363e922010-05-16 00:24:26 +0200565 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800566
Harald Welted75d71e2010-05-30 17:19:38 +0200567 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
568 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200569 if (rc < 0) {
570 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
571 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
572 "Error during TLV Parse\n", nsvc->nsei);
573 return rc;
574 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800575
576 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltefcc4cc92010-05-12 00:16:57 +0200577 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800578 return -EINVAL;
579 }
580
581 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte7363e922010-05-16 00:24:26 +0200582 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800583
584 return 0;
585}
586
587/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200588static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800589{
590 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800591 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200592 uint8_t *cause;
593 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800594 int rc;
595
Harald Welted75d71e2010-05-30 17:19:38 +0200596 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
597 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200598 if (rc < 0) {
599 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
600 "Error during TLV Parse\n", nsvc->nsei);
601 return rc;
602 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800603
604 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
605 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
606 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200607 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200608 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800609 return -EINVAL;
610 }
611
Harald Welteeaa614c2010-05-02 11:26:34 +0200612 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
613 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
614 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800615
Harald Weltefcc4cc92010-05-12 00:16:57 +0200616 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200617 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
618
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200619 /* Mark NS-VC as blocked and alive */
Harald Welte5434d7e2010-03-18 00:01:43 +0800620 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200621
Harald Welte5434d7e2010-03-18 00:01:43 +0800622 nsvc->nsei = ntohs(*nsei);
623 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800624
Harald Welte9b455bf2010-03-14 15:45:01 +0800625 /* start the test procedure */
Harald Weltedccf5552010-05-28 09:58:27 +0200626 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
627 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9b455bf2010-03-14 15:45:01 +0800628
Harald Welte99e32482010-05-11 06:20:54 +0200629 /* inform interested parties about the fact that this NSVC
630 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200631 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200632
Harald Welte5434d7e2010-03-18 00:01:43 +0800633 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800634}
635
Harald Welte99e32482010-05-11 06:20:54 +0200636static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
637{
638 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
639 struct tlv_parsed tp;
640 uint8_t *cause;
641 int rc;
642
Harald Weltefcc4cc92010-05-12 00:16:57 +0200643 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte99e32482010-05-11 06:20:54 +0200644
645 nsvc->state |= NSE_S_BLOCKED;
646
Harald Welted75d71e2010-05-30 17:19:38 +0200647 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
648 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200649 if (rc < 0) {
650 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
651 "Error during TLV Parse\n", nsvc->nsei);
652 return rc;
653 }
Harald Welte99e32482010-05-11 06:20:54 +0200654
655 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
656 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte99e32482010-05-11 06:20:54 +0200657 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200658 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte99e32482010-05-11 06:20:54 +0200659 return -EINVAL;
660 }
661
662 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
663 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
664
Harald Welte1389ac72010-05-11 10:15:26 +0200665 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Weltec1919862010-05-13 12:55:20 +0200666 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte99e32482010-05-11 06:20:54 +0200667
668 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
669}
670
Harald Welte9b455bf2010-03-14 15:45:01 +0800671/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200672int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welte5540c4c2010-05-19 14:38:50 +0200673 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9b455bf2010-03-14 15:45:01 +0800674{
675 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200676 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800677 int rc = 0;
678
Harald Weltecb991632010-04-26 19:18:54 +0200679 /* look up the NSVC based on source address */
680 nsvc = nsvc_by_rem_addr(nsi, saddr);
681 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200682 struct tlv_parsed tp;
683 uint16_t nsei;
Harald Weltee72c61c2010-05-19 16:01:39 +0200684 if (nsh->pdu_type == NS_PDUT_STATUS) {
685 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
686 "for non-existing NS-VC\n",
687 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
688 return 0;
689 }
Harald Weltecb991632010-04-26 19:18:54 +0200690 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200691 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte9aa97fc2010-05-13 13:58:08 +0200692 /* Since we have no NSVC, we have to use a fake */
693 nsvc = nsi->unknown_nsvc;
Harald Welte8be8c8f2010-05-15 23:52:02 +0200694 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200695 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Welteef1226e2010-05-11 18:38:36 +0200696 "from %s:%u for non-existing NS-VC\n",
697 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
698 ntohs(saddr->sin_port));
Harald Welte9aa97fc2010-05-13 13:58:08 +0200699 nsvc->nsvci = nsvc->nsei = 0xfffe;
700 nsvc->ip.bts_addr = *saddr;
701 nsvc->state = NSE_S_ALIVE;
Harald Welte5540c4c2010-05-19 14:38:50 +0200702 nsvc->ll = ll;
Harald Welte77289c22010-05-18 14:32:29 +0200703#if 0
704 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
705#else
Harald Welte9aa97fc2010-05-13 13:58:08 +0200706 return gprs_ns_tx_status(nsvc,
Harald Welte8c2440e2010-05-12 13:51:08 +0000707 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
708 msg);
Harald Welte77289c22010-05-18 14:32:29 +0200709#endif
Harald Welte239cf052010-05-03 18:54:12 +0200710 }
Harald Weltee5117da2010-05-11 18:30:37 +0200711 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
Harald Welted75d71e2010-05-30 17:19:38 +0200712 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welteea985a42010-05-28 10:08:14 +0200713 if (rc < 0) {
Harald Welted75d71e2010-05-30 17:19:38 +0200714 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
715 "TLV Parse\n", rc);
Harald Welteea985a42010-05-28 10:08:14 +0200716 return rc;
717 }
Harald Weltee5117da2010-05-11 18:30:37 +0200718 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
719 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
720 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Weltee5117da2010-05-11 18:30:37 +0200721 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200722 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
723 msg);
Harald Weltee5117da2010-05-11 18:30:37 +0200724 return -EINVAL;
725 }
726 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
727 /* Check if we already know this NSEI, the remote end might
728 * simply have changed addresses, or it is a SGSN */
729 nsvc = nsvc_by_nsei(nsi, nsei);
730 if (!nsvc) {
Harald Welte8be8c8f2010-05-15 23:52:02 +0200731 nsvc = nsvc_create(nsi, 0xffff);
Harald Welte5540c4c2010-05-19 14:38:50 +0200732 nsvc->ll = ll;
Harald Welte8be8c8f2010-05-15 23:52:02 +0200733 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltee5117da2010-05-11 18:30:37 +0200734 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
735 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltee5117da2010-05-11 18:30:37 +0200736 }
Harald Welte7c209eb2010-05-11 18:40:45 +0200737 /* Update the remote peer IP address/port */
738 nsvc->ip.bts_addr = *saddr;
Harald Welte239cf052010-05-03 18:54:12 +0200739 } else
740 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800741
Harald Welte8be8c8f2010-05-15 23:52:02 +0200742 log_set_context(BSC_CTX_NSVC, nsvc);
743
Harald Weltef2b4cd72010-05-13 11:45:07 +0200744 /* Increment number of Incoming bytes */
Harald Weltec1919862010-05-13 12:55:20 +0200745 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec1919862010-05-13 12:55:20 +0200746 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Weltef2b4cd72010-05-13 11:45:07 +0200747
Harald Welte9b455bf2010-03-14 15:45:01 +0800748 switch (nsh->pdu_type) {
749 case NS_PDUT_ALIVE:
Harald Welte1194b582010-05-12 15:55:23 +0000750 /* If we're dead and blocked and suddenly receive a
751 * NS-ALIVE out of the blue, we might have been re-started
752 * and should send a NS-RESET to make sure everything recovers
753 * fine. */
754 if (nsvc->state == NSE_S_BLOCKED)
755 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
756 else
757 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800758 break;
759 case NS_PDUT_ALIVE_ACK:
Harald Welteff56d612010-05-15 23:02:24 +0200760 /* stop Tns-alive and start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200761 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200762 if (nsvc->remote_end_is_sgsn) {
763 /* FIXME: this should be one level higher */
764 if (nsvc->state & NSE_S_BLOCKED)
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200765 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte24b31312010-05-03 21:11:22 +0200766 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800767 break;
768 case NS_PDUT_UNITDATA:
769 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200770 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800771 break;
772 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200773 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800774 break;
775 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200776 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800777 break;
778 case NS_PDUT_RESET_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200779 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200780 /* mark NS-VC as blocked + active */
781 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200782 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec1919862010-05-13 12:55:20 +0200783 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte1ccbf442010-05-14 11:53:08 +0000784 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte199d9df2010-05-03 20:55:10 +0200785 /* stop RESET timer */
786 bsc_del_timer(&nsvc->timer);
787 }
Harald Welteff56d612010-05-15 23:02:24 +0200788 /* Initiate TEST proc.: Send ALIVE and start timer */
789 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltedccf5552010-05-28 09:58:27 +0200790 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9b455bf2010-03-14 15:45:01 +0800791 break;
792 case NS_PDUT_UNBLOCK:
793 /* Section 7.2: unblocking procedure */
Harald Weltefcc4cc92010-05-12 00:16:57 +0200794 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800795 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200796 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800797 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800798 break;
799 case NS_PDUT_UNBLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200800 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200801 /* mark NS-VC as unblocked + active */
802 nsvc->state = NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200803 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef6d67c02010-05-12 14:18:46 +0000804 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800805 break;
806 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200807 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800808 break;
809 case NS_PDUT_BLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200810 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200811 /* mark remote NS-VC as blocked + active */
812 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800813 break;
814 default:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200815 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200816 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800817 rc = -EINVAL;
818 break;
819 }
820 return rc;
821}
822
Harald Weltecb991632010-04-26 19:18:54 +0200823struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
824{
825 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
826
827 nsi->cb = cb;
828 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Welteea4647d2010-05-12 17:19:53 +0000829 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
830 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
831 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
832 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
833 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
834 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
835 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltecb991632010-04-26 19:18:54 +0200836
Harald Welte3863e042010-05-13 14:20:56 +0200837 /* Create the dummy NSVC that we use for sending
838 * messages to non-existant/unknown NS-VC's */
Harald Welte9aa97fc2010-05-13 13:58:08 +0200839 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte3863e042010-05-13 14:20:56 +0200840 llist_del(&nsi->unknown_nsvc->list);
Harald Welte9aa97fc2010-05-13 13:58:08 +0200841
Harald Welte9f75c352010-04-30 20:26:32 +0200842 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200843}
844
845void gprs_ns_destroy(struct gprs_ns_inst *nsi)
846{
847 /* FIXME: clear all timers */
848
849 /* recursively free the NSI and all its NSVCs */
850 talloc_free(nsi);
851}
852
853
854/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
855 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
856
857/* Read a single NS-over-IP message */
858static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
859 struct sockaddr_in *saddr)
860{
Harald Weltee4860d72010-05-19 15:38:10 +0200861 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltecb991632010-04-26 19:18:54 +0200862 int ret = 0;
863 socklen_t saddr_len = sizeof(*saddr);
864
865 if (!msg) {
866 *error = -ENOMEM;
867 return NULL;
868 }
869
Holger Hans Peter Freyther900aeaf2010-05-28 03:25:36 +0800870 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltecb991632010-04-26 19:18:54 +0200871 (struct sockaddr *)saddr, &saddr_len);
872 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200873 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200874 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200875 msgb_free(msg);
876 *error = ret;
877 return NULL;
878 } else if (ret == 0) {
879 msgb_free(msg);
880 *error = ret;
881 return NULL;
882 }
883
884 msg->l2h = msg->data;
885 msgb_put(msg, ret);
886
887 return msg;
888}
889
890static int handle_nsip_read(struct bsc_fd *bfd)
891{
892 int error;
893 struct sockaddr_in saddr;
894 struct gprs_ns_inst *nsi = bfd->data;
895 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
896
897 if (!msg)
898 return error;
899
Harald Welte5540c4c2010-05-19 14:38:50 +0200900 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte8272c772010-05-12 18:38:45 +0000901
902 msgb_free(msg);
903
904 return error;
Harald Weltecb991632010-04-26 19:18:54 +0200905}
906
907static int handle_nsip_write(struct bsc_fd *bfd)
908{
909 /* FIXME: actually send the data here instead of nsip_sendmsg() */
910 return -EIO;
911}
912
Harald Welte5540c4c2010-05-19 14:38:50 +0200913static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200914{
915 int rc;
916 struct gprs_ns_inst *nsi = nsvc->nsi;
917 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
918
919 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
920 (struct sockaddr *)daddr, sizeof(*daddr));
921
922 talloc_free(msg);
923
924 return rc;
925}
926
927/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
928static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
929{
930 int rc = 0;
931
932 if (what & BSC_FD_READ)
933 rc = handle_nsip_read(bfd);
934 if (what & BSC_FD_WRITE)
935 rc = handle_nsip_write(bfd);
936
937 return rc;
938}
939
Harald Weltecb991632010-04-26 19:18:54 +0200940/* Listen for incoming GPRS packets */
Harald Welteff3bde82010-05-19 15:09:09 +0200941int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltecb991632010-04-26 19:18:54 +0200942{
943 int ret;
944
Harald Welteff3bde82010-05-19 15:09:09 +0200945 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
946 nsi->nsip.local_port, nsip_fd_cb);
Harald Weltecb991632010-04-26 19:18:54 +0200947 if (ret < 0)
948 return ret;
949
Harald Weltecb991632010-04-26 19:18:54 +0200950 nsi->nsip.fd.data = nsi;
951
952 return ret;
953}
Harald Welte9f75c352010-04-30 20:26:32 +0200954
Harald Welte1ccbf442010-05-14 11:53:08 +0000955/* Initiate a RESET procedure */
Holger Hans Peter Freyther83e0b3f2010-05-23 21:18:01 +0800956void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte1ccbf442010-05-14 11:53:08 +0000957{
Harald Welte4f1e8152010-05-25 22:17:30 +0200958 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
959 nsvc->nsei);
960
Harald Welte1ccbf442010-05-14 11:53:08 +0000961 /* Mark NS-VC locally as blocked and dead */
962 nsvc->state = NSE_S_BLOCKED;
963 /* Send NS-RESET PDU */
964 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
965 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
966 nsvc->nsei);
967 }
968 /* Start Tns-reset */
969 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte1ccbf442010-05-14 11:53:08 +0000970}
971
Harald Welte9f75c352010-04-30 20:26:32 +0200972/* Establish a connection (from the BSS) to the SGSN */
973struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200974 struct sockaddr_in *dest, uint16_t nsei,
975 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200976{
977 struct gprs_nsvc *nsvc;
978
979 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Weltecad83012010-05-12 11:56:39 +0000980 if (!nsvc)
Harald Welte9f75c352010-04-30 20:26:32 +0200981 nsvc = nsvc_create(nsi, nsvci);
Harald Weltecad83012010-05-12 11:56:39 +0000982 nsvc->ip.bts_addr = *dest;
Harald Welteb77c6972010-05-01 11:28:43 +0200983 nsvc->nsei = nsei;
984 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200985 nsvc->remote_end_is_sgsn = 1;
986
Holger Hans Peter Freyther83e0b3f2010-05-23 21:18:01 +0800987 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
988 return nsvc;
Harald Welte9f75c352010-04-30 20:26:32 +0200989}