blob: cdcf36e146a8cdaa6c6520a9dcdda3d12ef44240 [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 Welte605ac5d2012-06-16 16:09:52 +08004/* (C) 2009-2012 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
Holger Hans Peter Freyther3c16de22012-07-12 14:13:41 +020069#include <sys/types.h>
70#include <sys/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080071#include <arpa/inet.h>
72
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010073#include <osmocom/core/msgb.h>
74#include <osmocom/gsm/tlv.h>
75#include <osmocom/core/talloc.h>
76#include <osmocom/core/select.h>
77#include <osmocom/core/rate_ctr.h>
Harald Welte73952e32012-06-16 14:59:56 +080078#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080079#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080080#include <osmocom/gprs/gprs_ns.h>
81#include <osmocom/gprs/gprs_bssgp.h>
82#include <osmocom/gprs/gprs_ns_frgre.h>
83
Harald Weltecca49632012-06-16 17:45:59 +080084#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080085
Harald Welte9ba50052010-03-14 15:45:01 +080086static const struct tlv_definition ns_att_tlvdef = {
87 .def = {
88 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
89 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
90 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
91 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
93 },
94};
95
Harald Weltec51c23c2010-05-13 12:55:20 +020096enum ns_ctr {
97 NS_CTR_PKTS_IN,
98 NS_CTR_PKTS_OUT,
99 NS_CTR_BYTES_IN,
100 NS_CTR_BYTES_OUT,
101 NS_CTR_BLOCKED,
102 NS_CTR_DEAD,
103};
104
Harald Welte144e0292010-05-13 11:45:07 +0200105static const struct rate_ctr_desc nsvc_ctr_description[] = {
106 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +0200107 { "packets.out","Packets at NS Level (Out)" },
108 { "bytes.in", "Bytes at NS Level ( In)" },
109 { "bytes.out", "Bytes at NS Level (Out)" },
110 { "blocked", "NS-VC Block count " },
111 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +0200112};
113
114static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200115 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200116 .group_description = "NSVC Peer Statistics",
117 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
118 .ctr_desc = nsvc_ctr_description,
119};
120
Harald Weltea9f23c82011-11-23 15:01:31 +0100121/*! \brief Lookup struct gprs_nsvc based on NSVCI
122 * \param[in] nsi NS instance in which to search
123 * \param[in] nsvci NSVCI to be searched
124 * \returns gprs_nsvc of respective NSVCI
125 */
Harald Weltef5430362012-06-17 12:25:53 +0800126struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200127{
128 struct gprs_nsvc *nsvc;
129 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
130 if (nsvc->nsvci == nsvci)
131 return nsvc;
132 }
133 return NULL;
134}
135
Harald Weltea9f23c82011-11-23 15:01:31 +0100136/*! \brief Lookup struct gprs_nsvc based on NSEI
137 * \param[in] nsi NS instance in which to search
138 * \param[in] nsei NSEI to be searched
139 * \returns gprs_nsvc of respective NSEI
140 */
Harald Weltef5430362012-06-17 12:25:53 +0800141struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200142{
143 struct gprs_nsvc *nsvc;
144 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
145 if (nsvc->nsei == nsei)
146 return nsvc;
147 }
148 return NULL;
149}
150
Harald Weltef030b212010-04-26 19:18:54 +0200151/* Lookup struct gprs_nsvc based on remote peer socket addr */
152static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
153 struct sockaddr_in *sin)
154{
155 struct gprs_nsvc *nsvc;
156 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000157 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
158 sin->sin_addr.s_addr &&
159 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200160 return nsvc;
161 }
162 return NULL;
163}
164
Harald Welte69a4cf22010-05-03 20:55:10 +0200165static void gprs_ns_timer_cb(void *data);
166
Harald Weltef5430362012-06-17 12:25:53 +0800167struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200168{
169 struct gprs_nsvc *nsvc;
170
Harald Welteb1020d52010-05-25 22:17:30 +0200171 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
172
Harald Weltef030b212010-04-26 19:18:54 +0200173 nsvc = talloc_zero(nsi, struct gprs_nsvc);
174 nsvc->nsvci = nsvci;
175 /* before RESET procedure: BLOCKED and DEAD */
176 nsvc->state = NSE_S_BLOCKED;
177 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200178 nsvc->timer.cb = gprs_ns_timer_cb;
179 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200180 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200181
Harald Weltef030b212010-04-26 19:18:54 +0200182 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
183
184 return nsvc;
185}
Harald Welte9ba50052010-03-14 15:45:01 +0800186
Harald Weltea9f23c82011-11-23 15:01:31 +0100187/*! \brief Delete given NS-VC
188 * \param[in] nsvc gprs_nsvc to be deleted
189 */
Harald Weltef5430362012-06-17 12:25:53 +0800190void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000191{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200192 if (osmo_timer_pending(&nsvc->timer))
193 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000194 llist_del(&nsvc->list);
195 talloc_free(nsvc);
196}
197
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200198static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200199 uint8_t cause)
200{
201 struct ns_signal_data nssd;
202
203 nssd.nsvc = nsvc;
204 nssd.cause = cause;
205
Harald Welte4fcdd762012-06-16 16:40:42 +0800206 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200207}
208
Harald Welte9ba50052010-03-14 15:45:01 +0800209/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200210static const struct value_string ns_cause_str[] = {
211 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
212 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
213 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
214 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
215 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
216 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
217 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
218 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200219 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200220 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
221 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
222 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800223};
224
Harald Weltea9f23c82011-11-23 15:01:31 +0100225/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200226const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800227{
Harald Welte2577d412010-05-02 09:37:45 +0200228 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800229}
230
Harald Welte24a655f2010-04-30 19:54:29 +0200231static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200232extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200233
Harald Welte24a655f2010-04-30 19:54:29 +0200234static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800235{
Harald Weltef030b212010-04-26 19:18:54 +0200236 int ret;
237
Harald Weltecca49632012-06-16 17:45:59 +0800238 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200239
Harald Welte144e0292010-05-13 11:45:07 +0200240 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200241 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
242 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200243
Harald Welteb3ee2652010-05-19 14:38:50 +0200244 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200245 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200246 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200247 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200248 case GPRS_NS_LL_FR_GRE:
249 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
250 break;
Harald Weltef030b212010-04-26 19:18:54 +0200251 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200252 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200253 msgb_free(msg);
254 ret = -EIO;
255 break;
256 }
257 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800258}
259
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200260static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800261{
Harald Welteba4c6662010-05-19 15:38:10 +0200262 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800263 struct gprs_ns_hdr *nsh;
264
Harald Weltecca49632012-06-16 17:45:59 +0800265 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200266
Harald Welte9ba50052010-03-14 15:45:01 +0800267 if (!msg)
268 return -ENOMEM;
269
Harald Welte144e0292010-05-13 11:45:07 +0200270 msg->l2h = msgb_put(msg, sizeof(*nsh));
271 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800272
273 nsh->pdu_type = pdu_type;
274
Harald Welte24a655f2010-04-30 19:54:29 +0200275 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800276}
277
Harald Weltea9f23c82011-11-23 15:01:31 +0100278/*! \brief Transmit a NS-RESET on a given NSVC
279 * \param[in] nsvc NS-VC used for transmission
280 * \paam[in] cause Numeric NS cause value
281 */
Harald Welte834f26d2010-05-11 06:20:54 +0200282int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200283{
Harald Welteba4c6662010-05-19 15:38:10 +0200284 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200285 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200286 uint16_t nsvci = htons(nsvc->nsvci);
287 uint16_t nsei = htons(nsvc->nsei);
288
Harald Weltecca49632012-06-16 17:45:59 +0800289 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200290
Harald Welte69a4cf22010-05-03 20:55:10 +0200291 if (!msg)
292 return -ENOMEM;
293
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000294 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
295 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
296
Harald Welte144e0292010-05-13 11:45:07 +0200297 msg->l2h = msgb_put(msg, sizeof(*nsh));
298 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200299 nsh->pdu_type = NS_PDUT_RESET;
300
301 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
302 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
303 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
304
305 return gprs_ns_tx(nsvc, msg);
306
307}
308
Harald Weltea9f23c82011-11-23 15:01:31 +0100309/*! \brief Transmit a NS-STATUS on a given NSVC
310 * \param[in] nsvc NS-VC to be used for transmission
311 * \param[in] cause Numeric NS cause value
312 * \param[in] bvci BVCI to be reset within NSVC
313 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200314int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
315 uint16_t bvci, struct msgb *orig_msg)
316{
Harald Welteba4c6662010-05-19 15:38:10 +0200317 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200318 struct gprs_ns_hdr *nsh;
319 uint16_t nsvci = htons(nsvc->nsvci);
320
Harald Weltecca49632012-06-16 17:45:59 +0800321 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200322
Harald Weltec1402a62010-05-12 11:48:44 +0200323 bvci = htons(bvci);
324
325 if (!msg)
326 return -ENOMEM;
327
Harald Welte90af1942010-05-15 23:02:24 +0200328 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000329 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
330
Harald Welte144e0292010-05-13 11:45:07 +0200331 msg->l2h = msgb_put(msg, sizeof(*nsh));
332 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200333 nsh->pdu_type = NS_PDUT_STATUS;
334
335 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
336
337 /* Section 9.2.7.1: Static conditions for NS-VCI */
338 if (cause == NS_CAUSE_NSVC_BLOCKED ||
339 cause == NS_CAUSE_NSVC_UNKNOWN)
340 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
341
342 /* Section 9.2.7.2: Static conditions for NS PDU */
343 switch (cause) {
344 case NS_CAUSE_SEM_INCORR_PDU:
345 case NS_CAUSE_PDU_INCOMP_PSTATE:
346 case NS_CAUSE_PROTO_ERR_UNSPEC:
347 case NS_CAUSE_INVAL_ESSENT_IE:
348 case NS_CAUSE_MISSING_ESSENT_IE:
349 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
350 orig_msg->l2h);
351 break;
352 default:
353 break;
354 }
355
356 /* Section 9.2.7.3: Static conditions for BVCI */
357 if (cause == NS_CAUSE_BVCI_UNKNOWN)
358 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
359
360 return gprs_ns_tx(nsvc, msg);
361}
362
Harald Weltea9f23c82011-11-23 15:01:31 +0100363/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
364 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
365 * \param[in] cause Numeric NS Cause value
366 * \returns 0 in case of success
367 */
Harald Welte834f26d2010-05-11 06:20:54 +0200368int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
369{
Harald Welteba4c6662010-05-19 15:38:10 +0200370 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200371 struct gprs_ns_hdr *nsh;
372 uint16_t nsvci = htons(nsvc->nsvci);
373
Harald Weltecca49632012-06-16 17:45:59 +0800374 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200375
Harald Welte834f26d2010-05-11 06:20:54 +0200376 if (!msg)
377 return -ENOMEM;
378
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000379 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
380 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
381
Harald Welte834f26d2010-05-11 06:20:54 +0200382 /* be conservative and mark it as blocked even now! */
383 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200384 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200385
Harald Welte144e0292010-05-13 11:45:07 +0200386 msg->l2h = msgb_put(msg, sizeof(*nsh));
387 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200388 nsh->pdu_type = NS_PDUT_BLOCK;
389
390 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
391 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
392
393 return gprs_ns_tx(nsvc, msg);
394}
395
Harald Weltea9f23c82011-11-23 15:01:31 +0100396/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
397 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
398 * \returns 0 in case of success
399 */
Harald Welte834f26d2010-05-11 06:20:54 +0200400int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
401{
Harald Weltecca49632012-06-16 17:45:59 +0800402 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000403 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
404 nsvc->nsei, nsvc->nsvci);
405
Harald Welte834f26d2010-05-11 06:20:54 +0200406 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
407}
408
Harald Weltea9f23c82011-11-23 15:01:31 +0100409/*! \brief Transmit a NS-ALIVE on a given NS-VC
410 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
411 * \returns 0 in case of success
412 */
Harald Welteb983c312010-05-12 12:11:45 +0000413int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
414{
Harald Weltecca49632012-06-16 17:45:59 +0800415 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000416 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
417 nsvc->nsei, nsvc->nsvci);
418
419 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
420}
421
Harald Weltea9f23c82011-11-23 15:01:31 +0100422/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
423 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
424 * \returns 0 in case of success
425 */
Harald Welteb983c312010-05-12 12:11:45 +0000426int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
427{
Harald Weltecca49632012-06-16 17:45:59 +0800428 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000429 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
430 nsvc->nsei, nsvc->nsvci);
431
432 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
433}
434
Harald Weltefe4ab902010-05-12 17:19:53 +0000435static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
436 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
437 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
438 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200439};
440
Harald Welte7c24b9e2010-05-12 12:21:52 +0000441static const struct value_string timer_mode_strs[] = {
442 { NSVC_TIMER_TNS_RESET, "tns-reset" },
443 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
444 { NSVC_TIMER_TNS_TEST, "tns-test" },
445 { 0, NULL }
446};
447
Harald Welte69a4cf22010-05-03 20:55:10 +0200448static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
449{
Harald Weltefe4ab902010-05-12 17:19:53 +0000450 enum ns_timeout tout = timer_mode_tout[mode];
451 unsigned int seconds = nsvc->nsi->timeout[tout];
452
Harald Weltecca49632012-06-16 17:45:59 +0800453 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000454 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
455 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000456 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000457
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200458 if (osmo_timer_pending(&nsvc->timer))
459 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200460
461 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200462 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200463}
464
Harald Welte80405452010-05-03 20:16:13 +0200465static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800466{
467 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000468 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
469 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800470
Harald Weltecca49632012-06-16 17:45:59 +0800471 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000472 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
473 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000474 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000475
Harald Welte80405452010-05-03 20:16:13 +0200476 switch (nsvc->timer_mode) {
477 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800478 /* Tns-alive case: we expired without response ! */
479 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000480 if (nsvc->alive_retries >
481 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800482 /* mark as dead and blocked */
483 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200484 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
485 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200486 LOGP(DNS, LOGL_NOTICE,
487 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200488 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000489 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200490 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
491 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800492 return;
493 }
Harald Welteb983c312010-05-12 12:11:45 +0000494 /* Tns-test case: send NS-ALIVE PDU */
495 gprs_ns_tx_alive(nsvc);
496 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200497 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200498 break;
499 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800500 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000501 gprs_ns_tx_alive(nsvc);
502 /* start Tns-alive timer (transition into faster
503 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000504 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200505 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
506 break;
507 case NSVC_TIMER_TNS_RESET:
508 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200509 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200510 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200511 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200512 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200513 case _NSVC_TIMER_NR:
514 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800515 }
Harald Welte9ba50052010-03-14 15:45:01 +0800516}
517
518/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800519static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800520{
Harald Welteba4c6662010-05-19 15:38:10 +0200521 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800522 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200523 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800524
Harald Weltecca49632012-06-16 17:45:59 +0800525 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800526 if (!msg)
527 return -ENOMEM;
528
Harald Weltec5478482010-03-18 00:01:43 +0800529 nsvci = htons(nsvc->nsvci);
530 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800531
Harald Welte144e0292010-05-13 11:45:07 +0200532 msg->l2h = msgb_put(msg, sizeof(*nsh));
533 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800534
535 nsh->pdu_type = NS_PDUT_RESET_ACK;
536
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200537 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200538 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800539
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200540 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
541 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800542
Harald Welte24a655f2010-04-30 19:54:29 +0200543 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800544}
545
Harald Weltea9f23c82011-11-23 15:01:31 +0100546/*! \brief High-level function for transmitting a NS-UNITDATA messsage
547 * \param[in] nsi NS-instance on which we shall transmit
548 * \param[in] msg struct msgb to be trasnmitted
549 *
550 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
551 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
552 * header for the NS-UNITDATA message type and sends it off.
553 *
554 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
555 */
Harald Welte24a655f2010-04-30 19:54:29 +0200556int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800557{
Harald Welte24a655f2010-04-30 19:54:29 +0200558 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800559 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200560 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200561
Harald Weltef5430362012-06-17 12:25:53 +0800562 nsvc = gprs_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200563 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200564 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200565 "to NS-VC!\n", msgb_nsei(msg));
Harald Weltef47df642010-08-09 21:15:40 +0800566 msgb_free(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200567 return -EINVAL;
568 }
Harald Weltecca49632012-06-16 17:45:59 +0800569 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800570
Harald Welte69a4cf22010-05-03 20:55:10 +0200571 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200572 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200573 nsvc->nsei);
Harald Weltef47df642010-08-09 21:15:40 +0800574 msgb_free(msg);
Harald Welte69a4cf22010-05-03 20:55:10 +0200575 return -EBUSY;
576 }
577 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200578 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200579 nsvc->nsei);
Harald Weltef47df642010-08-09 21:15:40 +0800580 msgb_free(msg);
Harald Welte69a4cf22010-05-03 20:55:10 +0200581 return -EBUSY;
582 }
583
Harald Welteec20ba42010-05-13 12:18:49 +0200584 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
585 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800586 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200587 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800588 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800589 return -EIO;
590 }
591
592 nsh->pdu_type = NS_PDUT_UNITDATA;
593 /* spare octet in data[0] */
594 nsh->data[1] = bvci >> 8;
595 nsh->data[2] = bvci & 0xff;
596
Harald Welte24a655f2010-04-30 19:54:29 +0200597 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800598}
599
600/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200601static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800602{
603 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200604 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800605
Harald Weltec1402a62010-05-12 11:48:44 +0200606 if (nsvc->state & NSE_S_BLOCKED)
607 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
608 0, msg);
609
Harald Welte9ba50052010-03-14 15:45:01 +0800610 /* spare octet in data[0] */
611 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200612 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200613 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800614
615 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200616 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800617}
618
619/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200620static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800621{
Harald Weltef030b212010-04-26 19:18:54 +0200622 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800623 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200624 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800625 int rc;
626
Harald Welte41337832010-05-16 00:24:26 +0200627 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800628
Harald Weltebd33f3d2010-05-30 17:19:38 +0200629 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
630 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200631 if (rc < 0) {
632 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
633 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
634 "Error during TLV Parse\n", nsvc->nsei);
635 return rc;
636 }
Harald Welte9ba50052010-03-14 15:45:01 +0800637
638 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200639 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800640 return -EINVAL;
641 }
642
643 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200644 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800645
646 return 0;
647}
648
649/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200650static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800651{
652 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800653 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200654 uint8_t *cause;
655 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800656 int rc;
657
Harald Weltebd33f3d2010-05-30 17:19:38 +0200658 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
659 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200660 if (rc < 0) {
661 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
662 "Error during TLV Parse\n", nsvc->nsei);
663 return rc;
664 }
Harald Welte9ba50052010-03-14 15:45:01 +0800665
666 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
667 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
668 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200669 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200670 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800671 return -EINVAL;
672 }
673
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200674 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
675 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
676 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800677
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200678 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200679 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
680
Harald Weltec1402a62010-05-12 11:48:44 +0200681 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800682 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200683
Harald Weltec5478482010-03-18 00:01:43 +0800684 nsvc->nsei = ntohs(*nsei);
685 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800686
Harald Welte9ba50052010-03-14 15:45:01 +0800687 /* start the test procedure */
Harald Weltec9531202010-05-28 09:58:27 +0200688 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
689 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800690
Harald Welte834f26d2010-05-11 06:20:54 +0200691 /* inform interested parties about the fact that this NSVC
692 * has received RESET */
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200693 ns_osmo_signal_dispatch(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200694
Harald Weltec5478482010-03-18 00:01:43 +0800695 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800696}
697
Harald Welte834f26d2010-05-11 06:20:54 +0200698static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
699{
700 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
701 struct tlv_parsed tp;
702 uint8_t *cause;
703 int rc;
704
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200705 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200706
707 nsvc->state |= NSE_S_BLOCKED;
708
Harald Weltebd33f3d2010-05-30 17:19:38 +0200709 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
710 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200711 if (rc < 0) {
712 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
713 "Error during TLV Parse\n", nsvc->nsei);
714 return rc;
715 }
Harald Welte834f26d2010-05-11 06:20:54 +0200716
717 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
718 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200719 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200720 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200721 return -EINVAL;
722 }
723
724 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
725 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
726
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200727 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200728 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200729
730 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
731}
732
Harald Weltea9f23c82011-11-23 15:01:31 +0100733/*! \brief Receive incoming NS message from underlying transport layer
734 * \param nsi NS instance to which the data belongs
735 * \param[in] msg message buffer containing newly-received data
736 * \param[in] saddr socketaddr from which data was received
737 * \param[in] ll link-layer type in which data was received
738 * \returns 0 in case of success, < 0 in case of error
739 *
740 * This is the main entry point int othe NS imlementation where frames
741 * from the underlying transport (normally UDP) enter.
742 */
Harald Weltef030b212010-04-26 19:18:54 +0200743int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200744 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800745{
746 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200747 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800748 int rc = 0;
749
Harald Weltef030b212010-04-26 19:18:54 +0200750 /* look up the NSVC based on source address */
751 nsvc = nsvc_by_rem_addr(nsi, saddr);
752 if (!nsvc) {
Harald Welte9a392932010-05-11 18:30:37 +0200753 struct tlv_parsed tp;
754 uint16_t nsei;
Harald Welte1100a9d2010-05-19 16:01:39 +0200755 if (nsh->pdu_type == NS_PDUT_STATUS) {
756 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u "
757 "for non-existing NS-VC\n",
758 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
759 return 0;
760 }
Harald Weltef030b212010-04-26 19:18:54 +0200761 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200762 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Weltedd1c83c2010-05-13 13:58:08 +0200763 /* Since we have no NSVC, we have to use a fake */
764 nsvc = nsi->unknown_nsvc;
Harald Weltecca49632012-06-16 17:45:59 +0800765 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200766 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
Harald Weltebba902d2010-05-11 18:38:36 +0200767 "from %s:%u for non-existing NS-VC\n",
768 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
769 ntohs(saddr->sin_port));
Harald Weltedd1c83c2010-05-13 13:58:08 +0200770 nsvc->nsvci = nsvc->nsei = 0xfffe;
771 nsvc->ip.bts_addr = *saddr;
772 nsvc->state = NSE_S_ALIVE;
Harald Welteb3ee2652010-05-19 14:38:50 +0200773 nsvc->ll = ll;
Harald Weltee0c42d12010-05-18 14:32:29 +0200774#if 0
775 return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
776#else
Harald Weltedd1c83c2010-05-13 13:58:08 +0200777 return gprs_ns_tx_status(nsvc,
Harald Welte803647e2010-05-12 13:51:08 +0000778 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
779 msg);
Harald Weltee0c42d12010-05-18 14:32:29 +0200780#endif
Harald Weltebbc9fac2010-05-03 18:54:12 +0200781 }
Harald Welte9a392932010-05-11 18:30:37 +0200782 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
Harald Weltebd33f3d2010-05-30 17:19:38 +0200783 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200784 if (rc < 0) {
Harald Weltebd33f3d2010-05-30 17:19:38 +0200785 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
786 "TLV Parse\n", rc);
Harald Welte36250382010-05-28 10:08:14 +0200787 return rc;
788 }
Harald Welte9a392932010-05-11 18:30:37 +0200789 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
790 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
791 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte9a392932010-05-11 18:30:37 +0200792 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200793 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
794 msg);
Harald Welte9a392932010-05-11 18:30:37 +0200795 return -EINVAL;
796 }
797 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
798 /* Check if we already know this NSEI, the remote end might
799 * simply have changed addresses, or it is a SGSN */
Harald Weltef5430362012-06-17 12:25:53 +0800800 nsvc = gprs_nsvc_by_nsei(nsi, nsei);
Harald Welte9a392932010-05-11 18:30:37 +0200801 if (!nsvc) {
Harald Weltef5430362012-06-17 12:25:53 +0800802 nsvc = gprs_nsvc_create(nsi, 0xffff);
Harald Welteb3ee2652010-05-19 14:38:50 +0200803 nsvc->ll = ll;
Harald Weltecca49632012-06-16 17:45:59 +0800804 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9a392932010-05-11 18:30:37 +0200805 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
806 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Welte9a392932010-05-11 18:30:37 +0200807 }
Harald Welte2b7d4652010-05-11 18:40:45 +0200808 /* Update the remote peer IP address/port */
809 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200810 } else
811 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800812
Harald Weltecca49632012-06-16 17:45:59 +0800813 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200814
Harald Welte144e0292010-05-13 11:45:07 +0200815 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200816 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200817 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200818
Harald Welte9ba50052010-03-14 15:45:01 +0800819 switch (nsh->pdu_type) {
820 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000821 /* If we're dead and blocked and suddenly receive a
822 * NS-ALIVE out of the blue, we might have been re-started
823 * and should send a NS-RESET to make sure everything recovers
824 * fine. */
825 if (nsvc->state == NSE_S_BLOCKED)
826 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
827 else
828 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800829 break;
830 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200831 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200832 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200833 if (nsvc->remote_end_is_sgsn) {
834 /* FIXME: this should be one level higher */
835 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200836 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200837 }
Harald Welte9ba50052010-03-14 15:45:01 +0800838 break;
839 case NS_PDUT_UNITDATA:
840 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200841 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800842 break;
843 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200844 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800845 break;
846 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200847 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800848 break;
849 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200850 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200851 /* mark NS-VC as blocked + active */
852 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200853 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200854 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000855 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200856 /* stop RESET timer */
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200857 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200858 }
Harald Welte90af1942010-05-15 23:02:24 +0200859 /* Initiate TEST proc.: Send ALIVE and start timer */
860 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltec9531202010-05-28 09:58:27 +0200861 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800862 break;
863 case NS_PDUT_UNBLOCK:
864 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200865 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800866 nsvc->state &= ~NSE_S_BLOCKED;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200867 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800868 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800869 break;
870 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200871 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200872 /* mark NS-VC as unblocked + active */
873 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200874 nsvc->remote_state = NSE_S_ALIVE;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200875 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800876 break;
877 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200878 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800879 break;
880 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200881 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200882 /* mark remote NS-VC as blocked + active */
883 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800884 break;
885 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200886 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200887 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800888 rc = -EINVAL;
889 break;
890 }
891 return rc;
892}
893
Harald Weltea9f23c82011-11-23 15:01:31 +0100894/*! \brief Create a new GPRS NS instance
895 * \param[in] cb Call-back function for incoming BSSGP data
896 * \returns dynamically allocated gprs_ns_inst
897 */
Harald Welte4fcdd762012-06-16 16:40:42 +0800898struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +0200899{
Harald Welte4fcdd762012-06-16 16:40:42 +0800900 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +0200901
902 nsi->cb = cb;
903 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000904 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
905 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
906 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
907 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
908 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
909 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
910 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +0200911
Harald Welte60cc6ac2010-05-13 14:20:56 +0200912 /* Create the dummy NSVC that we use for sending
913 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +0800914 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +0200915 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +0200916
Harald Welte3771d092010-04-30 20:26:32 +0200917 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200918}
919
Harald Weltea9f23c82011-11-23 15:01:31 +0100920/*! \brief Destroy an entire NS instance
921 * \param nsi gprs_ns_inst that is to be destroyed
922 *
Andreas Eversbergaaccdac2012-09-28 10:11:25 +0200923 * This function releases all resources associated with the
924 * NS-instance.
Harald Weltea9f23c82011-11-23 15:01:31 +0100925 */
Harald Weltef030b212010-04-26 19:18:54 +0200926void gprs_ns_destroy(struct gprs_ns_inst *nsi)
927{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +0200928 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +0200929
Andreas Eversbergaaccdac2012-09-28 10:11:25 +0200930 /* delete all NSVCs and clear their timers */
931 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
932 gprs_nsvc_delete(nsvc);
933
934 /* close socket and unregister */
935 if (nsi->nsip.fd.data) {
936 close(nsi->nsip.fd.fd);
937 osmo_fd_unregister(&nsi->nsip.fd);
938 }
939
940 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +0200941 talloc_free(nsi);
942}
943
944
945/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
946 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
947
948/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200949static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +0200950 struct sockaddr_in *saddr)
951{
Harald Welteba4c6662010-05-19 15:38:10 +0200952 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +0200953 int ret = 0;
954 socklen_t saddr_len = sizeof(*saddr);
955
956 if (!msg) {
957 *error = -ENOMEM;
958 return NULL;
959 }
960
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +0800961 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +0200962 (struct sockaddr *)saddr, &saddr_len);
963 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200964 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200965 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200966 msgb_free(msg);
967 *error = ret;
968 return NULL;
969 } else if (ret == 0) {
970 msgb_free(msg);
971 *error = ret;
972 return NULL;
973 }
974
975 msg->l2h = msg->data;
976 msgb_put(msg, ret);
977
978 return msg;
979}
980
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200981static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +0200982{
983 int error;
984 struct sockaddr_in saddr;
985 struct gprs_ns_inst *nsi = bfd->data;
986 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
987
988 if (!msg)
989 return error;
990
Harald Welteb3ee2652010-05-19 14:38:50 +0200991 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +0000992
993 msgb_free(msg);
994
995 return error;
Harald Weltef030b212010-04-26 19:18:54 +0200996}
997
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200998static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +0200999{
1000 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1001 return -EIO;
1002}
1003
Harald Welteb3ee2652010-05-19 14:38:50 +02001004static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001005{
1006 int rc;
1007 struct gprs_ns_inst *nsi = nsvc->nsi;
1008 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1009
1010 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1011 (struct sockaddr *)daddr, sizeof(*daddr));
1012
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001013 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001014
1015 return rc;
1016}
1017
1018/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001019static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001020{
1021 int rc = 0;
1022
1023 if (what & BSC_FD_READ)
1024 rc = handle_nsip_read(bfd);
1025 if (what & BSC_FD_WRITE)
1026 rc = handle_nsip_write(bfd);
1027
1028 return rc;
1029}
1030
Harald Weltea9f23c82011-11-23 15:01:31 +01001031/*! \brief Create a listening socket for GPRS NS/UDP/IP
1032 * \param[in] nsi NS protocol instance to listen
1033 * \returns >=0 (fd) in case of success, negative in case of error
1034 *
1035 * A call to this function will create a UDP socket bound to the port
1036 * number and IP address specified in the NS protocol instance. The
1037 * file descriptor of the socket will be stored in nsi->nsip.fd.
1038 */
Harald Welte7fb05232010-05-19 15:09:09 +02001039int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001040{
Harald Welte73952e32012-06-16 14:59:56 +08001041 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001042 int ret;
1043
Harald Welte73952e32012-06-16 14:59:56 +08001044 in.s_addr = htonl(nsi->nsip.local_ip);
1045
1046 nsi->nsip.fd.cb = nsip_fd_cb;
1047 nsi->nsip.fd.data = nsi;
1048 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1049 IPPROTO_UDP, inet_ntoa(in),
1050 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001051 if (ret < 0)
1052 return ret;
1053
Harald Weltef030b212010-04-26 19:18:54 +02001054
1055 return ret;
1056}
Harald Welte3771d092010-04-30 20:26:32 +02001057
Harald Weltea9f23c82011-11-23 15:01:31 +01001058/*! \brief Initiate a RESET procedure
1059 * \param[in] nsvc NS-VC in which to start the procedure
1060 * \param[in] cause Numeric NS cause value
1061 *
1062 * This is a high-level function initiating a NS-RESET procedure. It
1063 * will not only send a NS-RESET, but also set the state to BLOCKED and
1064 * start the Tns-reset timer.
1065 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001066void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001067{
Harald Welteb1020d52010-05-25 22:17:30 +02001068 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1069 nsvc->nsei);
1070
Harald Welte731d1fc2010-05-14 11:53:08 +00001071 /* Mark NS-VC locally as blocked and dead */
1072 nsvc->state = NSE_S_BLOCKED;
1073 /* Send NS-RESET PDU */
1074 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1075 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1076 nsvc->nsei);
1077 }
1078 /* Start Tns-reset */
1079 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001080}
1081
Harald Weltea9f23c82011-11-23 15:01:31 +01001082/*! \brief Establish a NS connection (from the BSS) to the SGSN
1083 * \param nsi NS-instance
1084 * \param[in] dest Destination IP/Port
1085 * \param[in] nsei NSEI of the to-be-established NS-VC
1086 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1087 * \returns struct gprs_nsvc representing the new NS-VC
1088 *
1089 * This function will establish a single NS/UDP/IP connection in uplink
1090 * (BSS to SGSN) direction.
1091 */
Harald Weltef5430362012-06-17 12:25:53 +08001092struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001093 struct sockaddr_in *dest, uint16_t nsei,
1094 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001095{
1096 struct gprs_nsvc *nsvc;
1097
1098 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001099 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001100 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001101 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001102 nsvc->nsei = nsei;
1103 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001104 nsvc->remote_end_is_sgsn = 1;
1105
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001106 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1107 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001108}
Harald Weltea9f23c82011-11-23 15:01:31 +01001109
Harald Weltecca49632012-06-16 17:45:59 +08001110void gprs_ns_set_log_ss(int ss)
1111{
1112 DNS = ss;
1113}
1114
Harald Weltea9f23c82011-11-23 15:01:31 +01001115/*! }@ */