blob: a8f97e2a20303856c55e045209805f959118b73d [file] [log] [blame]
Harald Weltea9f23c82011-11-23 15:01:31 +01001/* GPRS Networks Service (NS) messages on the Gb interface
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Weltef030b212010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Weltee4cbb3f2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee4cbb3f2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Weltee4cbb3f2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080020 *
21 */
22
Harald Weltea9f23c82011-11-23 15:01:31 +010023/*! \addtogroup libgb
24 * @{
25 */
26
27/*! \file gprs_ns.c */
28
29/*!
30 * GPRS Networks Service (NS) messages on the Gb interface
31 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
32 *
33 * Some introduction into NS: NS is used typically on top of frame relay,
Harald Welte9ba50052010-03-14 15:45:01 +080034 * but in the ip.access world it is encapsulated in UDP packets. It serves
35 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
36 * do much, apart from providing congestion notification and status indication.
37 *
38 * Terms:
39 * NS Network Service
40 * NSVC NS Virtual Connection
41 * NSEI NS Entity Identifier
42 * NSVL NS Virtual Link
43 * NSVLI NS Virtual Link Identifier
44 * BVC BSSGP Virtual Connection
45 * BVCI BSSGP Virtual Connection Identifier
46 * NSVCG NS Virtual Connection Goup
47 * Blocked NS-VC cannot be used for user traffic
48 * Alive Ability of a NS-VC to provide communication
49 *
50 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
51 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
52 * NS then has to firgure out which NSVC's are responsible for this BVCI.
53 * Those mappings are administratively configured.
54 */
55
Harald Weltedd1c83c2010-05-13 13:58:08 +020056/* This implementation has the following limitations:
57 * o Only one NS-VC for each NSE: No load-sharing function
58 * o NSVCI 65535 and 65534 are reserved for internal use
59 * o Only UDP is supported as of now, no frame relay support
60 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
61 * o There are no BLOCK and UNBLOCK timers (yet?)
62 */
63
Harald Welte9ba50052010-03-14 15:45:01 +080064#include <stdlib.h>
65#include <unistd.h>
66#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020067#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080068
69#include <arpa/inet.h>
70
71#include <openbsc/gsm_data.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010072#include <osmocom/core/msgb.h>
73#include <osmocom/gsm/tlv.h>
74#include <osmocom/core/talloc.h>
75#include <osmocom/core/select.h>
76#include <osmocom/core/rate_ctr.h>
Harald Welte9ba50052010-03-14 15:45:01 +080077#include <openbsc/debug.h>
Harald Welte834f26d2010-05-11 06:20:54 +020078#include <openbsc/signal.h>
Harald Welte9ba50052010-03-14 15:45:01 +080079#include <openbsc/gprs_ns.h>
80#include <openbsc/gprs_bssgp.h>
Holger Hans Peter Freytheree59fe42010-05-23 21:23:44 +080081#include <openbsc/gprs_ns_frgre.h>
Holger Hans Peter Freythere75a34f2010-05-23 21:19:55 +080082#include <openbsc/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080083
Harald Welte9ba50052010-03-14 15:45:01 +080084static const struct tlv_definition ns_att_tlvdef = {
85 .def = {
86 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
87 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
88 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
89 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
90 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
91 },
92};
93
Harald Weltec51c23c2010-05-13 12:55:20 +020094enum ns_ctr {
95 NS_CTR_PKTS_IN,
96 NS_CTR_PKTS_OUT,
97 NS_CTR_BYTES_IN,
98 NS_CTR_BYTES_OUT,
99 NS_CTR_BLOCKED,
100 NS_CTR_DEAD,
101};
102
Harald Welte144e0292010-05-13 11:45:07 +0200103static const struct rate_ctr_desc nsvc_ctr_description[] = {
104 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +0200105 { "packets.out","Packets at NS Level (Out)" },
106 { "bytes.in", "Bytes at NS Level ( In)" },
107 { "bytes.out", "Bytes at NS Level (Out)" },
108 { "blocked", "NS-VC Block count " },
109 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +0200110};
111
112static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200113 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200114 .group_description = "NSVC Peer Statistics",
115 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
116 .ctr_desc = nsvc_ctr_description,
117};
118
Harald Weltea9f23c82011-11-23 15:01:31 +0100119/*! \brief Lookup struct gprs_nsvc based on NSVCI
120 * \param[in] nsi NS instance in which to search
121 * \param[in] nsvci NSVCI to be searched
122 * \returns gprs_nsvc of respective NSVCI
123 */
Harald Welte90af1942010-05-15 23:02:24 +0200124struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200125{
126 struct gprs_nsvc *nsvc;
127 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
128 if (nsvc->nsvci == nsvci)
129 return nsvc;
130 }
131 return NULL;
132}
133
Harald Weltea9f23c82011-11-23 15:01:31 +0100134/*! \brief Lookup struct gprs_nsvc based on NSEI
135 * \param[in] nsi NS instance in which to search
136 * \param[in] nsei NSEI to be searched
137 * \returns gprs_nsvc of respective NSEI
138 */
Harald Welte144e0292010-05-13 11:45:07 +0200139struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200140{
141 struct gprs_nsvc *nsvc;
142 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
143 if (nsvc->nsei == nsei)
144 return nsvc;
145 }
146 return NULL;
147}
148
Harald Weltef030b212010-04-26 19:18:54 +0200149/* Lookup struct gprs_nsvc based on remote peer socket addr */
150static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
151 struct sockaddr_in *sin)
152{
153 struct gprs_nsvc *nsvc;
154 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000155 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
156 sin->sin_addr.s_addr &&
157 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200158 return nsvc;
159 }
160 return NULL;
161}
162
Harald Welte69a4cf22010-05-03 20:55:10 +0200163static void gprs_ns_timer_cb(void *data);
164
Harald Welte144e0292010-05-13 11:45:07 +0200165struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200166{
167 struct gprs_nsvc *nsvc;
168
Harald Welteb1020d52010-05-25 22:17:30 +0200169 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
170
Harald Weltef030b212010-04-26 19:18:54 +0200171 nsvc = talloc_zero(nsi, struct gprs_nsvc);
172 nsvc->nsvci = nsvci;
173 /* before RESET procedure: BLOCKED and DEAD */
174 nsvc->state = NSE_S_BLOCKED;
175 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200176 nsvc->timer.cb = gprs_ns_timer_cb;
177 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200178 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200179
Harald Weltef030b212010-04-26 19:18:54 +0200180 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
181
182 return nsvc;
183}
Harald Welte9ba50052010-03-14 15:45:01 +0800184
Harald Weltea9f23c82011-11-23 15:01:31 +0100185/*! \brief Delete given NS-VC
186 * \param[in] nsvc gprs_nsvc to be deleted
187 */
Harald Welte144e0292010-05-13 11:45:07 +0200188void nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000189{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200190 if (osmo_timer_pending(&nsvc->timer))
191 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000192 llist_del(&nsvc->list);
193 talloc_free(nsvc);
194}
195
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200196static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200197 uint8_t cause)
198{
199 struct ns_signal_data nssd;
200
201 nssd.nsvc = nsvc;
202 nssd.cause = cause;
203
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200204 osmo_signal_dispatch(SS_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200205}
206
Harald Welte9ba50052010-03-14 15:45:01 +0800207/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200208static const struct value_string ns_cause_str[] = {
209 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
210 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
211 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
212 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
213 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
214 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
215 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
216 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200217 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200218 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
219 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
220 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800221};
222
Harald Weltea9f23c82011-11-23 15:01:31 +0100223/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200224const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800225{
Harald Welte2577d412010-05-02 09:37:45 +0200226 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800227}
228
Harald Welte24a655f2010-04-30 19:54:29 +0200229static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200230extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200231
Harald Welte24a655f2010-04-30 19:54:29 +0200232static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800233{
Harald Weltef030b212010-04-26 19:18:54 +0200234 int ret;
235
Harald Welte91f7f4b2010-05-15 23:52:02 +0200236 log_set_context(BSC_CTX_NSVC, nsvc);
237
Harald Welte144e0292010-05-13 11:45:07 +0200238 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200239 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
240 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200241
Harald Welteb3ee2652010-05-19 14:38:50 +0200242 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200243 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200244 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200245 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200246 case GPRS_NS_LL_FR_GRE:
247 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
248 break;
Harald Weltef030b212010-04-26 19:18:54 +0200249 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200250 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200251 msgb_free(msg);
252 ret = -EIO;
253 break;
254 }
255 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800256}
257
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200258static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800259{
Harald Welteba4c6662010-05-19 15:38:10 +0200260 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800261 struct gprs_ns_hdr *nsh;
262
Harald Welte91f7f4b2010-05-15 23:52:02 +0200263 log_set_context(BSC_CTX_NSVC, nsvc);
264
Harald Welte9ba50052010-03-14 15:45:01 +0800265 if (!msg)
266 return -ENOMEM;
267
Harald Welte144e0292010-05-13 11:45:07 +0200268 msg->l2h = msgb_put(msg, sizeof(*nsh));
269 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800270
271 nsh->pdu_type = pdu_type;
272
Harald Welte24a655f2010-04-30 19:54:29 +0200273 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800274}
275
Harald Weltea9f23c82011-11-23 15:01:31 +0100276/*! \brief Transmit a NS-RESET on a given NSVC
277 * \param[in] nsvc NS-VC used for transmission
278 * \paam[in] cause Numeric NS cause value
279 */
Harald Welte834f26d2010-05-11 06:20:54 +0200280int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200281{
Harald Welteba4c6662010-05-19 15:38:10 +0200282 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200283 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200284 uint16_t nsvci = htons(nsvc->nsvci);
285 uint16_t nsei = htons(nsvc->nsei);
286
Harald Welte91f7f4b2010-05-15 23:52:02 +0200287 log_set_context(BSC_CTX_NSVC, nsvc);
288
Harald Welte69a4cf22010-05-03 20:55:10 +0200289 if (!msg)
290 return -ENOMEM;
291
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000292 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
293 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
294
Harald Welte144e0292010-05-13 11:45:07 +0200295 msg->l2h = msgb_put(msg, sizeof(*nsh));
296 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200297 nsh->pdu_type = NS_PDUT_RESET;
298
299 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
300 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
301 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
302
303 return gprs_ns_tx(nsvc, msg);
304
305}
306
Harald Weltea9f23c82011-11-23 15:01:31 +0100307/*! \brief Transmit a NS-STATUS on a given NSVC
308 * \param[in] nsvc NS-VC to be used for transmission
309 * \param[in] cause Numeric NS cause value
310 * \param[in] bvci BVCI to be reset within NSVC
311 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200312int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
313 uint16_t bvci, struct msgb *orig_msg)
314{
Harald Welteba4c6662010-05-19 15:38:10 +0200315 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200316 struct gprs_ns_hdr *nsh;
317 uint16_t nsvci = htons(nsvc->nsvci);
318
Harald Welte91f7f4b2010-05-15 23:52:02 +0200319 log_set_context(BSC_CTX_NSVC, nsvc);
320
Harald Weltec1402a62010-05-12 11:48:44 +0200321 bvci = htons(bvci);
322
323 if (!msg)
324 return -ENOMEM;
325
Harald Welte90af1942010-05-15 23:02:24 +0200326 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000327 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
328
Harald Welte144e0292010-05-13 11:45:07 +0200329 msg->l2h = msgb_put(msg, sizeof(*nsh));
330 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200331 nsh->pdu_type = NS_PDUT_STATUS;
332
333 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
334
335 /* Section 9.2.7.1: Static conditions for NS-VCI */
336 if (cause == NS_CAUSE_NSVC_BLOCKED ||
337 cause == NS_CAUSE_NSVC_UNKNOWN)
338 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
339
340 /* Section 9.2.7.2: Static conditions for NS PDU */
341 switch (cause) {
342 case NS_CAUSE_SEM_INCORR_PDU:
343 case NS_CAUSE_PDU_INCOMP_PSTATE:
344 case NS_CAUSE_PROTO_ERR_UNSPEC:
345 case NS_CAUSE_INVAL_ESSENT_IE:
346 case NS_CAUSE_MISSING_ESSENT_IE:
347 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
348 orig_msg->l2h);
349 break;
350 default:
351 break;
352 }
353
354 /* Section 9.2.7.3: Static conditions for BVCI */
355 if (cause == NS_CAUSE_BVCI_UNKNOWN)
356 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
357
358 return gprs_ns_tx(nsvc, msg);
359}
360
Harald Weltea9f23c82011-11-23 15:01:31 +0100361/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
362 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
363 * \param[in] cause Numeric NS Cause value
364 * \returns 0 in case of success
365 */
Harald Welte834f26d2010-05-11 06:20:54 +0200366int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
367{
Harald Welteba4c6662010-05-19 15:38:10 +0200368 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200369 struct gprs_ns_hdr *nsh;
370 uint16_t nsvci = htons(nsvc->nsvci);
371
Harald Welte91f7f4b2010-05-15 23:52:02 +0200372 log_set_context(BSC_CTX_NSVC, nsvc);
373
Harald Welte834f26d2010-05-11 06:20:54 +0200374 if (!msg)
375 return -ENOMEM;
376
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000377 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
378 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
379
Harald Welte834f26d2010-05-11 06:20:54 +0200380 /* be conservative and mark it as blocked even now! */
381 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200382 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200383
Harald Welte144e0292010-05-13 11:45:07 +0200384 msg->l2h = msgb_put(msg, sizeof(*nsh));
385 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200386 nsh->pdu_type = NS_PDUT_BLOCK;
387
388 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
389 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
390
391 return gprs_ns_tx(nsvc, msg);
392}
393
Harald Weltea9f23c82011-11-23 15:01:31 +0100394/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
395 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
396 * \returns 0 in case of success
397 */
Harald Welte834f26d2010-05-11 06:20:54 +0200398int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
399{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200400 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000401 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
402 nsvc->nsei, nsvc->nsvci);
403
Harald Welte834f26d2010-05-11 06:20:54 +0200404 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
405}
406
Harald Weltea9f23c82011-11-23 15:01:31 +0100407/*! \brief Transmit a NS-ALIVE on a given NS-VC
408 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
409 * \returns 0 in case of success
410 */
Harald Welteb983c312010-05-12 12:11:45 +0000411int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
412{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200413 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000414 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
415 nsvc->nsei, nsvc->nsvci);
416
417 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
418}
419
Harald Weltea9f23c82011-11-23 15:01:31 +0100420/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
421 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
422 * \returns 0 in case of success
423 */
Harald Welteb983c312010-05-12 12:11:45 +0000424int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
425{
Harald Welte91f7f4b2010-05-15 23:52:02 +0200426 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000427 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
428 nsvc->nsei, nsvc->nsvci);
429
430 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
431}
432
Harald Weltefe4ab902010-05-12 17:19:53 +0000433static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
434 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
435 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
436 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200437};
438
Harald Welte7c24b9e2010-05-12 12:21:52 +0000439static const struct value_string timer_mode_strs[] = {
440 { NSVC_TIMER_TNS_RESET, "tns-reset" },
441 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
442 { NSVC_TIMER_TNS_TEST, "tns-test" },
443 { 0, NULL }
444};
445
Harald Welte69a4cf22010-05-03 20:55:10 +0200446static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
447{
Harald Weltefe4ab902010-05-12 17:19:53 +0000448 enum ns_timeout tout = timer_mode_tout[mode];
449 unsigned int seconds = nsvc->nsi->timeout[tout];
450
Harald Welte91f7f4b2010-05-15 23:52:02 +0200451 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000452 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
453 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000454 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000455
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200456 if (osmo_timer_pending(&nsvc->timer))
457 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200458
459 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200460 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200461}
462
Harald Welte80405452010-05-03 20:16:13 +0200463static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800464{
465 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000466 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
467 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800468
Harald Welte91f7f4b2010-05-15 23:52:02 +0200469 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000470 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
471 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000472 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000473
Harald Welte80405452010-05-03 20:16:13 +0200474 switch (nsvc->timer_mode) {
475 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800476 /* Tns-alive case: we expired without response ! */
477 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000478 if (nsvc->alive_retries >
479 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800480 /* mark as dead and blocked */
481 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200482 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
483 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200484 LOGP(DNS, LOGL_NOTICE,
485 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200486 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000487 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200488 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
489 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800490 return;
491 }
Harald Welteb983c312010-05-12 12:11:45 +0000492 /* Tns-test case: send NS-ALIVE PDU */
493 gprs_ns_tx_alive(nsvc);
494 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200495 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200496 break;
497 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800498 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000499 gprs_ns_tx_alive(nsvc);
500 /* start Tns-alive timer (transition into faster
501 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000502 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200503 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
504 break;
505 case NSVC_TIMER_TNS_RESET:
506 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200507 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200508 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200509 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200510 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200511 case _NSVC_TIMER_NR:
512 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800513 }
Harald Welte9ba50052010-03-14 15:45:01 +0800514}
515
516/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800517static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800518{
Harald Welteba4c6662010-05-19 15:38:10 +0200519 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800520 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200521 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800522
Harald Welte91f7f4b2010-05-15 23:52:02 +0200523 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800524 if (!msg)
525 return -ENOMEM;
526
Harald Weltec5478482010-03-18 00:01:43 +0800527 nsvci = htons(nsvc->nsvci);
528 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800529
Harald Welte144e0292010-05-13 11:45:07 +0200530 msg->l2h = msgb_put(msg, sizeof(*nsh));
531 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800532
533 nsh->pdu_type = NS_PDUT_RESET_ACK;
534
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200535 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200536 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800537
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200538 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
539 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800540
Harald Welte24a655f2010-04-30 19:54:29 +0200541 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800542}
543
Harald Weltea9f23c82011-11-23 15:01:31 +0100544/*! \brief High-level function for transmitting a NS-UNITDATA messsage
545 * \param[in] nsi NS-instance on which we shall transmit
546 * \param[in] msg struct msgb to be trasnmitted
547 *
548 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
549 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
550 * header for the NS-UNITDATA message type and sends it off.
551 *
552 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
553 */
Harald Welte24a655f2010-04-30 19:54:29 +0200554int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800555{
Harald Welte24a655f2010-04-30 19:54:29 +0200556 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800557 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200558 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200559
560 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
561 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200562 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200563 "to NS-VC!\n", msgb_nsei(msg));
Harald Weltef47df642010-08-09 21:15:40 +0800564 msgb_free(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200565 return -EINVAL;
566 }
Harald Welte91f7f4b2010-05-15 23:52:02 +0200567 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800568
Harald Welte69a4cf22010-05-03 20:55:10 +0200569 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200570 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200571 nsvc->nsei);
Harald Weltef47df642010-08-09 21:15:40 +0800572 msgb_free(msg);
Harald Welte69a4cf22010-05-03 20:55:10 +0200573 return -EBUSY;
574 }
575 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200576 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200577 nsvc->nsei);
Harald Weltef47df642010-08-09 21:15:40 +0800578 msgb_free(msg);
Harald Welte69a4cf22010-05-03 20:55:10 +0200579 return -EBUSY;
580 }
581
Harald Welteec20ba42010-05-13 12:18:49 +0200582 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
583 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800584 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200585 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800586 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800587 return -EIO;
588 }
589
590 nsh->pdu_type = NS_PDUT_UNITDATA;
591 /* spare octet in data[0] */
592 nsh->data[1] = bvci >> 8;
593 nsh->data[2] = bvci & 0xff;
594
Harald Welte24a655f2010-04-30 19:54:29 +0200595 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800596}
597
598/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200599static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800600{
601 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200602 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800603
Harald Weltec1402a62010-05-12 11:48:44 +0200604 if (nsvc->state & NSE_S_BLOCKED)
605 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
606 0, msg);
607
Harald Welte9ba50052010-03-14 15:45:01 +0800608 /* spare octet in data[0] */
609 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200610 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200611 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800612
613 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200614 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800615}
616
617/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200618static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800619{
Harald Weltef030b212010-04-26 19:18:54 +0200620 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800621 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200622 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800623 int rc;
624
Harald Welte41337832010-05-16 00:24:26 +0200625 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800626
Harald Weltebd33f3d2010-05-30 17:19:38 +0200627 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
628 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200629 if (rc < 0) {
630 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
631 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
632 "Error during TLV Parse\n", nsvc->nsei);
633 return rc;
634 }
Harald Welte9ba50052010-03-14 15:45:01 +0800635
636 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200637 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800638 return -EINVAL;
639 }
640
641 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200642 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800643
644 return 0;
645}
646
647/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200648static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800649{
650 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800651 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200652 uint8_t *cause;
653 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800654 int rc;
655
Harald Weltebd33f3d2010-05-30 17:19:38 +0200656 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
657 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200658 if (rc < 0) {
659 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
660 "Error during TLV Parse\n", nsvc->nsei);
661 return rc;
662 }
Harald Welte9ba50052010-03-14 15:45:01 +0800663
664 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
665 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
666 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200667 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200668 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800669 return -EINVAL;
670 }
671
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200672 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
673 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
674 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800675
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200676 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200677 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
678
Harald Weltec1402a62010-05-12 11:48:44 +0200679 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800680 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200681
Harald Weltec5478482010-03-18 00:01:43 +0800682 nsvc->nsei = ntohs(*nsei);
683 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800684
Harald Welte9ba50052010-03-14 15:45:01 +0800685 /* start the test procedure */
Harald Weltec9531202010-05-28 09:58:27 +0200686 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
687 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800688
Harald Welte834f26d2010-05-11 06:20:54 +0200689 /* inform interested parties about the fact that this NSVC
690 * has received RESET */
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200691 ns_osmo_signal_dispatch(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200692
Harald Weltec5478482010-03-18 00:01:43 +0800693 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800694}
695
Harald Welte834f26d2010-05-11 06:20:54 +0200696static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
697{
698 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
699 struct tlv_parsed tp;
700 uint8_t *cause;
701 int rc;
702
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200703 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200704
705 nsvc->state |= NSE_S_BLOCKED;
706
Harald Weltebd33f3d2010-05-30 17:19:38 +0200707 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
708 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200709 if (rc < 0) {
710 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
711 "Error during TLV Parse\n", nsvc->nsei);
712 return rc;
713 }
Harald Welte834f26d2010-05-11 06:20:54 +0200714
715 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
716 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200717 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200718 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200719 return -EINVAL;
720 }
721
722 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
723 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
724
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200725 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200726 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200727
728 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
729}
730
Harald Weltea9f23c82011-11-23 15:01:31 +0100731/*! \brief Receive incoming NS message from underlying transport layer
732 * \param nsi NS instance to which the data belongs
733 * \param[in] msg message buffer containing newly-received data
734 * \param[in] saddr socketaddr from which data was received
735 * \param[in] ll link-layer type in which data was received
736 * \returns 0 in case of success, < 0 in case of error
737 *
738 * This is the main entry point int othe NS imlementation where frames
739 * from the underlying transport (normally UDP) enter.
740 */
Harald Weltef030b212010-04-26 19:18:54 +0200741int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200742 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800743{
744 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200745 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800746 int rc = 0;
747
Harald Weltef030b212010-04-26 19:18:54 +0200748 /* look up the NSVC based on source address */
749 nsvc = nsvc_by_rem_addr(nsi, saddr);
750 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200751 struct tlv_parsed tp;
752 uint16_t nsei;
Harald Welte1100a9d2010-05-19 16:01:39 +0200753 if (nsh->pdu_type == NS_PDUT_STATUS) {
754 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
755 "for non-existing NS-VC\n",
756 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
757 return 0;
758 }
Harald Weltef030b212010-04-26 19:18:54 +0200759 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200760 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200761 /* Since we have no NSVC, we have to use a fake */
762 nsvc = nsi->unknown_nsvc;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200763 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200764 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200765 "from %s:%u for non-existing NS-VC\n",
766 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
767 ntohs(saddr->sin_port));
Harald Weltedd1c83c2010-05-13 13:58:08 +0200768 nsvc->nsvci = nsvc->nsei = 0xfffe;
769 nsvc->ip.bts_addr = *saddr;
770 nsvc->state = NSE_S_ALIVE;
Harald Welteb3ee2652010-05-19 14:38:50 +0200771 nsvc->ll = ll;
Harald Weltee0c42d12010-05-18 14:32:29 +0200772#if 0
773 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
774#else
Harald Weltedd1c83c2010-05-13 13:58:08 +0200775 return gprs_ns_tx_status(nsvc,
Harald Welte803647e2010-05-12 13:51:08 +0000776 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
777 msg);
Harald Weltee0c42d12010-05-18 14:32:29 +0200778#endif
Harald Weltebbc9fac2010-05-03 18:54:12 +0200779 }
Harald Welte9a392932010-05-11 18:30:37 +0200780 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
Harald Weltebd33f3d2010-05-30 17:19:38 +0200781 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200782 if (rc < 0) {
Harald Weltebd33f3d2010-05-30 17:19:38 +0200783 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
784 "TLV Parse\n", rc);
Harald Welte36250382010-05-28 10:08:14 +0200785 return rc;
786 }
Harald Welte9a392932010-05-11 18:30:37 +0200787 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
788 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
789 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200790 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200791 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
792 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200793 return -EINVAL;
794 }
795 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
796 /* Check if we already know this NSEI, the remote end might
797 * simply have changed addresses, or it is a SGSN */
798 nsvc = nsvc_by_nsei(nsi, nsei);
799 if (!nsvc) {
Harald Welte91f7f4b2010-05-15 23:52:02 +0200800 nsvc = nsvc_create(nsi, 0xffff);
Harald Welteb3ee2652010-05-19 14:38:50 +0200801 nsvc->ll = ll;
Harald Welte91f7f4b2010-05-15 23:52:02 +0200802 log_set_context(BSC_CTX_NSVC, nsvc);
Harald Welte9a392932010-05-11 18:30:37 +0200803 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
804 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Welte9a392932010-05-11 18:30:37 +0200805 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200806 /* Update the remote peer IP address/port */
807 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200808 } else
809 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800810
Harald Welte91f7f4b2010-05-15 23:52:02 +0200811 log_set_context(BSC_CTX_NSVC, nsvc);
812
Harald Welte144e0292010-05-13 11:45:07 +0200813 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200814 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200815 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200816
Harald Welte9ba50052010-03-14 15:45:01 +0800817 switch (nsh->pdu_type) {
818 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000819 /* If we're dead and blocked and suddenly receive a
820 * NS-ALIVE out of the blue, we might have been re-started
821 * and should send a NS-RESET to make sure everything recovers
822 * fine. */
823 if (nsvc->state == NSE_S_BLOCKED)
824 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
825 else
826 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800827 break;
828 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200829 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200830 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200831 if (nsvc->remote_end_is_sgsn) {
832 /* FIXME: this should be one level higher */
833 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200834 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200835 }
Harald Welte9ba50052010-03-14 15:45:01 +0800836 break;
837 case NS_PDUT_UNITDATA:
838 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200839 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800840 break;
841 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200842 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800843 break;
844 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200845 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800846 break;
847 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200848 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200849 /* mark NS-VC as blocked + active */
850 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200851 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200852 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000853 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200854 /* stop RESET timer */
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200855 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200856 }
Harald Welte90af1942010-05-15 23:02:24 +0200857 /* Initiate TEST proc.: Send ALIVE and start timer */
858 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltec9531202010-05-28 09:58:27 +0200859 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800860 break;
861 case NS_PDUT_UNBLOCK:
862 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200863 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800864 nsvc->state &= ~NSE_S_BLOCKED;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200865 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800866 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800867 break;
868 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200869 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200870 /* mark NS-VC as unblocked + active */
871 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200872 nsvc->remote_state = NSE_S_ALIVE;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200873 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800874 break;
875 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200876 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800877 break;
878 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200879 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200880 /* mark remote NS-VC as blocked + active */
881 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800882 break;
883 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200884 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200885 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800886 rc = -EINVAL;
887 break;
888 }
889 return rc;
890}
891
Harald Weltea9f23c82011-11-23 15:01:31 +0100892/*! \brief Create a new GPRS NS instance
893 * \param[in] cb Call-back function for incoming BSSGP data
894 * \returns dynamically allocated gprs_ns_inst
895 */
Harald Weltef030b212010-04-26 19:18:54 +0200896struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
897{
898 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
899
900 nsi->cb = cb;
901 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000902 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
903 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
904 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
905 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
906 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
907 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
908 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200909
Harald Welte60cc6ac2010-05-13 14:20:56 +0200910 /* Create the dummy NSVC that we use for sending
911 * messages to non-existant/unknown NS-VC's */
Harald Weltedd1c83c2010-05-13 13:58:08 +0200912 nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +0200913 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200914
Harald Welte3771d092010-04-30 20:26:32 +0200915 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200916}
917
Harald Weltea9f23c82011-11-23 15:01:31 +0100918/*! \brief Destroy an entire NS instance
919 * \param nsi gprs_ns_inst that is to be destroyed
920 *
921 * This function SHOULD release all resources associated with the
922 * NS-instance but is actually not completely implemented!
923 */
Harald Weltef030b212010-04-26 19:18:54 +0200924void gprs_ns_destroy(struct gprs_ns_inst *nsi)
925{
926 /* FIXME: clear all timers */
927
928 /* recursively free the NSI and all its NSVCs */
929 talloc_free(nsi);
930}
931
932
933/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
934 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
935
936/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200937static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +0200938 struct sockaddr_in *saddr)
939{
Harald Welteba4c6662010-05-19 15:38:10 +0200940 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +0200941 int ret = 0;
942 socklen_t saddr_len = sizeof(*saddr);
943
944 if (!msg) {
945 *error = -ENOMEM;
946 return NULL;
947 }
948
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +0800949 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +0200950 (struct sockaddr *)saddr, &saddr_len);
951 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200952 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200953 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200954 msgb_free(msg);
955 *error = ret;
956 return NULL;
957 } else if (ret == 0) {
958 msgb_free(msg);
959 *error = ret;
960 return NULL;
961 }
962
963 msg->l2h = msg->data;
964 msgb_put(msg, ret);
965
966 return msg;
967}
968
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200969static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +0200970{
971 int error;
972 struct sockaddr_in saddr;
973 struct gprs_ns_inst *nsi = bfd->data;
974 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
975
976 if (!msg)
977 return error;
978
Harald Welteb3ee2652010-05-19 14:38:50 +0200979 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +0000980
981 msgb_free(msg);
982
983 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200984}
985
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200986static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +0200987{
988 /* FIXME: actually send the data here instead of nsip_sendmsg() */
989 return -EIO;
990}
991
Harald Welteb3ee2652010-05-19 14:38:50 +0200992static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200993{
994 int rc;
995 struct gprs_ns_inst *nsi = nsvc->nsi;
996 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
997
998 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
999 (struct sockaddr *)daddr, sizeof(*daddr));
1000
1001 talloc_free(msg);
1002
1003 return rc;
1004}
1005
1006/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001007static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001008{
1009 int rc = 0;
1010
1011 if (what & BSC_FD_READ)
1012 rc = handle_nsip_read(bfd);
1013 if (what & BSC_FD_WRITE)
1014 rc = handle_nsip_write(bfd);
1015
1016 return rc;
1017}
1018
Harald Weltea9f23c82011-11-23 15:01:31 +01001019/*! \brief Create a listening socket for GPRS NS/UDP/IP
1020 * \param[in] nsi NS protocol instance to listen
1021 * \returns >=0 (fd) in case of success, negative in case of error
1022 *
1023 * A call to this function will create a UDP socket bound to the port
1024 * number and IP address specified in the NS protocol instance. The
1025 * file descriptor of the socket will be stored in nsi->nsip.fd.
1026 */
Harald Welte7fb05232010-05-19 15:09:09 +02001027int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001028{
1029 int ret;
1030
Harald Welte7fb05232010-05-19 15:09:09 +02001031 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip,
Pablo Neira Ayusoc0d04312011-04-05 18:33:24 +02001032 nsi->nsip.local_port, 0, nsip_fd_cb, NULL);
Harald Weltef030b212010-04-26 19:18:54 +02001033 if (ret < 0)
1034 return ret;
1035
Harald Weltef030b212010-04-26 19:18:54 +02001036 nsi->nsip.fd.data = nsi;
1037
1038 return ret;
1039}
Harald Welte3771d092010-04-30 20:26:32 +02001040
Harald Weltea9f23c82011-11-23 15:01:31 +01001041/*! \brief Initiate a RESET procedure
1042 * \param[in] nsvc NS-VC in which to start the procedure
1043 * \param[in] cause Numeric NS cause value
1044 *
1045 * This is a high-level function initiating a NS-RESET procedure. It
1046 * will not only send a NS-RESET, but also set the state to BLOCKED and
1047 * start the Tns-reset timer.
1048 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001049void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001050{
Harald Welteb1020d52010-05-25 22:17:30 +02001051 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1052 nsvc->nsei);
1053
Harald Welte731d1fc2010-05-14 11:53:08 +00001054 /* Mark NS-VC locally as blocked and dead */
1055 nsvc->state = NSE_S_BLOCKED;
1056 /* Send NS-RESET PDU */
1057 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1058 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1059 nsvc->nsei);
1060 }
1061 /* Start Tns-reset */
1062 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001063}
1064
Harald Weltea9f23c82011-11-23 15:01:31 +01001065/*! \brief Establish a NS connection (from the BSS) to the SGSN
1066 * \param nsi NS-instance
1067 * \param[in] dest Destination IP/Port
1068 * \param[in] nsei NSEI of the to-be-established NS-VC
1069 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1070 * \returns struct gprs_nsvc representing the new NS-VC
1071 *
1072 * This function will establish a single NS/UDP/IP connection in uplink
1073 * (BSS to SGSN) direction.
1074 */
Harald Welte3771d092010-04-30 20:26:32 +02001075struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001076 struct sockaddr_in *dest, uint16_t nsei,
1077 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001078{
1079 struct gprs_nsvc *nsvc;
1080
1081 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001082 if (!nsvc)
Harald Welte3771d092010-04-30 20:26:32 +02001083 nsvc = nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001084 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001085 nsvc->nsei = nsei;
1086 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001087 nsvc->remote_end_is_sgsn = 1;
1088
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001089 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1090 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001091}
Harald Weltea9f23c82011-11-23 15:01:31 +01001092
1093/*! }@ */