blob: b3bb77deb083d69ff78f9248999755b74b25434d [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
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200733int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
734 struct gprs_nsvc *fallback_nsvc,
735 struct gprs_nsvc **new_nsvc);
736
737int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
738 struct gprs_nsvc *nsvc);
739
Harald Weltea9f23c82011-11-23 15:01:31 +0100740/*! \brief Receive incoming NS message from underlying transport layer
741 * \param nsi NS instance to which the data belongs
742 * \param[in] msg message buffer containing newly-received data
743 * \param[in] saddr socketaddr from which data was received
744 * \param[in] ll link-layer type in which data was received
745 * \returns 0 in case of success, < 0 in case of error
746 *
747 * This is the main entry point int othe NS imlementation where frames
748 * from the underlying transport (normally UDP) enter.
749 */
Harald Weltef030b212010-04-26 19:18:54 +0200750int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200751 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800752{
Harald Weltef030b212010-04-26 19:18:54 +0200753 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800754 int rc = 0;
755
Harald Weltef030b212010-04-26 19:18:54 +0200756 /* look up the NSVC based on source address */
757 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200758
Harald Weltef030b212010-04-26 19:18:54 +0200759 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200760 struct gprs_nsvc *fallback_nsvc;
761
762 fallback_nsvc = nsi->unknown_nsvc;
763 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
764 fallback_nsvc->ip.bts_addr = *saddr;
765 fallback_nsvc->ll = ll;
766
767 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
768
769 switch (rc) {
770 case GPRS_NS_CS_CREATED:
771 case GPRS_NS_CS_FOUND:
Harald Welteb3ee2652010-05-19 14:38:50 +0200772 nsvc->ll = ll;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200773 break;
774 case GPRS_NS_CS_SKIPPED:
775 case GPRS_NS_CS_REJECTED:
776 break;
777 default:
Harald Welte36250382010-05-28 10:08:14 +0200778 return rc;
779 }
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200780
781 rc = 0;
782 }
783
784 if (nsvc) {
Harald Welte2b7d4652010-05-11 18:40:45 +0200785 nsvc->ip.bts_addr = *saddr;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200786 rc = gprs_ns_process_msg(nsi, msg, nsvc);
787 }
788
789 return rc;
790}
791
792const char *gprs_ns_format_peer(struct gprs_nsvc *nsvc)
793{
794 static char buf[80];
795 snprintf(buf, sizeof(buf), "%s:%u",
796 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
797 ntohs(nsvc->ip.bts_addr.sin_port));
798
799 return buf;
800}
801
802/*! \brief Create/get NS-VC independently from underlying transport layer
803 * \param nsi NS instance to which the data belongs
804 * \param[in] msg message buffer containing newly-received data
805 * \param[in] fallback_nsvc is used to send error messages back to the peer
806 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
807 * been created or found
808 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
809 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
810 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
811 * has been created and registered, and GPRS_NS_CS_FOUND if an
812 * existing NS-VC object has been found with the same NSEI.
813 *
814 * This contains the initial NS automaton state (NS-VC not yet attached).
815 */
816int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
817 struct gprs_nsvc *fallback_nsvc,
818 struct gprs_nsvc **new_nsvc)
819{
820 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
821 struct gprs_nsvc *existing_nsvc;
822
823 struct tlv_parsed tp;
824 uint16_t nsei;
825
826 int rc;
827
828 if (nsh->pdu_type == NS_PDUT_STATUS) {
829 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
830 "for non-existing NS-VC\n",
831 gprs_ns_format_peer(fallback_nsvc));
832 return GPRS_NS_CS_SKIPPED;
833 }
834
835 /* Only the RESET procedure creates a new NSVC */
836 if (nsh->pdu_type != NS_PDUT_RESET) {
837 /* Since we have no NSVC, we have to use a fake */
838 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
839 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
840 "from %s for non-existing NS-VC\n",
841 nsh->pdu_type, gprs_ns_format_peer(fallback_nsvc));
842 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
843 fallback_nsvc->state = NSE_S_ALIVE;
844
845 rc = gprs_ns_tx_status(fallback_nsvc,
846 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
847 if (rc < 0) {
848 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
849 rc, gprs_ns_format_peer(fallback_nsvc));
850 return rc;
851 }
852 return GPRS_NS_CS_REJECTED;
853 }
854
855 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
856 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
857 if (rc < 0) {
858 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
859 "TLV Parse\n", rc);
860 return rc;
861 }
862 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
863 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
864 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
865 gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
866 msg);
867 return -EINVAL;
868 }
869 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
870 /* Check if we already know this NSEI, the remote end might
871 * simply have changed addresses, or it is a SGSN */
872 existing_nsvc = gprs_nsvc_by_nsei(nsi, nsei);
873 if (!existing_nsvc) {
874 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
875 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
876 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
877 gprs_ns_format_peer(fallback_nsvc));
878
879 return GPRS_NS_CS_CREATED;
880 }
881
882 *new_nsvc = existing_nsvc;
883 return GPRS_NS_CS_FOUND;
884}
885
886/*! \brief Process NS message independently from underlying transport layer
887 * \param nsi NS instance to which the data belongs
888 * \param[in] msg message buffer containing newly-received data
889 * \param[in] nsvc refers to the virtual connection
890 * \returns 0 in case of success, < 0 in case of error
891 *
892 * This contains the main NS automaton.
893 */
894int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
895 struct gprs_nsvc *nsvc)
896{
897 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
898 int rc = 0;
899
900 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800901
Harald Weltecca49632012-06-16 17:45:59 +0800902 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200903
Harald Welte144e0292010-05-13 11:45:07 +0200904 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200905 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200906 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200907
Harald Welte9ba50052010-03-14 15:45:01 +0800908 switch (nsh->pdu_type) {
909 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000910 /* If we're dead and blocked and suddenly receive a
911 * NS-ALIVE out of the blue, we might have been re-started
912 * and should send a NS-RESET to make sure everything recovers
913 * fine. */
914 if (nsvc->state == NSE_S_BLOCKED)
915 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
916 else
917 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800918 break;
919 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200920 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200921 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200922 if (nsvc->remote_end_is_sgsn) {
923 /* FIXME: this should be one level higher */
924 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200925 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200926 }
Harald Welte9ba50052010-03-14 15:45:01 +0800927 break;
928 case NS_PDUT_UNITDATA:
929 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200930 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800931 break;
932 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200933 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800934 break;
935 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200936 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800937 break;
938 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200939 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200940 /* mark NS-VC as blocked + active */
941 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200942 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200943 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000944 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200945 /* stop RESET timer */
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200946 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200947 }
Harald Welte90af1942010-05-15 23:02:24 +0200948 /* Initiate TEST proc.: Send ALIVE and start timer */
949 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltec9531202010-05-28 09:58:27 +0200950 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800951 break;
952 case NS_PDUT_UNBLOCK:
953 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200954 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800955 nsvc->state &= ~NSE_S_BLOCKED;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200956 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800957 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800958 break;
959 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200960 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200961 /* mark NS-VC as unblocked + active */
962 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200963 nsvc->remote_state = NSE_S_ALIVE;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200964 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +0800965 break;
966 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200967 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800968 break;
969 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200970 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200971 /* mark remote NS-VC as blocked + active */
972 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800973 break;
974 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200975 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200976 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800977 rc = -EINVAL;
978 break;
979 }
980 return rc;
981}
982
Harald Weltea9f23c82011-11-23 15:01:31 +0100983/*! \brief Create a new GPRS NS instance
984 * \param[in] cb Call-back function for incoming BSSGP data
985 * \returns dynamically allocated gprs_ns_inst
986 */
Harald Welte4fcdd762012-06-16 16:40:42 +0800987struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +0200988{
Harald Welte4fcdd762012-06-16 16:40:42 +0800989 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +0200990
991 nsi->cb = cb;
992 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +0000993 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
994 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
995 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
996 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
997 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
998 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
999 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001000
Harald Welte60cc6ac2010-05-13 14:20:56 +02001001 /* Create the dummy NSVC that we use for sending
1002 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001003 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001004 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001005
Harald Welte3771d092010-04-30 20:26:32 +02001006 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001007}
1008
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001009void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001010{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001011 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001012
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001013 /* delete all NSVCs and clear their timers */
1014 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1015 gprs_nsvc_delete(nsvc);
1016
1017 /* close socket and unregister */
1018 if (nsi->nsip.fd.data) {
1019 close(nsi->nsip.fd.fd);
1020 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001021 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001022 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001023}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001024
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001025/*! \brief Destroy an entire NS instance
1026 * \param nsi gprs_ns_inst that is to be destroyed
1027 *
1028 * This function releases all resources associated with the
1029 * NS-instance.
1030 */
1031void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1032{
1033 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001034 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001035 talloc_free(nsi);
1036}
1037
1038
1039/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1040 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1041
1042/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001043static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001044 struct sockaddr_in *saddr)
1045{
Harald Welteba4c6662010-05-19 15:38:10 +02001046 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001047 int ret = 0;
1048 socklen_t saddr_len = sizeof(*saddr);
1049
1050 if (!msg) {
1051 *error = -ENOMEM;
1052 return NULL;
1053 }
1054
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001055 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001056 (struct sockaddr *)saddr, &saddr_len);
1057 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001058 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001059 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001060 msgb_free(msg);
1061 *error = ret;
1062 return NULL;
1063 } else if (ret == 0) {
1064 msgb_free(msg);
1065 *error = ret;
1066 return NULL;
1067 }
1068
1069 msg->l2h = msg->data;
1070 msgb_put(msg, ret);
1071
1072 return msg;
1073}
1074
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001075static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001076{
1077 int error;
1078 struct sockaddr_in saddr;
1079 struct gprs_ns_inst *nsi = bfd->data;
1080 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1081
1082 if (!msg)
1083 return error;
1084
Harald Welteb3ee2652010-05-19 14:38:50 +02001085 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001086
1087 msgb_free(msg);
1088
1089 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001090}
1091
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001092static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001093{
1094 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1095 return -EIO;
1096}
1097
Harald Welteb3ee2652010-05-19 14:38:50 +02001098static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001099{
1100 int rc;
1101 struct gprs_ns_inst *nsi = nsvc->nsi;
1102 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1103
1104 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1105 (struct sockaddr *)daddr, sizeof(*daddr));
1106
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001107 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001108
1109 return rc;
1110}
1111
1112/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001113static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001114{
1115 int rc = 0;
1116
1117 if (what & BSC_FD_READ)
1118 rc = handle_nsip_read(bfd);
1119 if (what & BSC_FD_WRITE)
1120 rc = handle_nsip_write(bfd);
1121
1122 return rc;
1123}
1124
Harald Weltea9f23c82011-11-23 15:01:31 +01001125/*! \brief Create a listening socket for GPRS NS/UDP/IP
1126 * \param[in] nsi NS protocol instance to listen
1127 * \returns >=0 (fd) in case of success, negative in case of error
1128 *
1129 * A call to this function will create a UDP socket bound to the port
1130 * number and IP address specified in the NS protocol instance. The
1131 * file descriptor of the socket will be stored in nsi->nsip.fd.
1132 */
Harald Welte7fb05232010-05-19 15:09:09 +02001133int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001134{
Harald Welte73952e32012-06-16 14:59:56 +08001135 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001136 int ret;
1137
Harald Welte73952e32012-06-16 14:59:56 +08001138 in.s_addr = htonl(nsi->nsip.local_ip);
1139
1140 nsi->nsip.fd.cb = nsip_fd_cb;
1141 nsi->nsip.fd.data = nsi;
1142 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1143 IPPROTO_UDP, inet_ntoa(in),
1144 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001145 if (ret < 0)
1146 return ret;
1147
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001148 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1149 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1150 if (ret < 0)
1151 LOGP(DNS, LOGL_ERROR,
1152 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1153 nsi->nsip.dscp, ret, errno);
1154
Harald Weltef030b212010-04-26 19:18:54 +02001155
1156 return ret;
1157}
Harald Welte3771d092010-04-30 20:26:32 +02001158
Harald Weltea9f23c82011-11-23 15:01:31 +01001159/*! \brief Initiate a RESET procedure
1160 * \param[in] nsvc NS-VC in which to start the procedure
1161 * \param[in] cause Numeric NS cause value
1162 *
1163 * This is a high-level function initiating a NS-RESET procedure. It
1164 * will not only send a NS-RESET, but also set the state to BLOCKED and
1165 * start the Tns-reset timer.
1166 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001167void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001168{
Harald Welteb1020d52010-05-25 22:17:30 +02001169 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1170 nsvc->nsei);
1171
Harald Welte731d1fc2010-05-14 11:53:08 +00001172 /* Mark NS-VC locally as blocked and dead */
1173 nsvc->state = NSE_S_BLOCKED;
1174 /* Send NS-RESET PDU */
1175 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1176 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1177 nsvc->nsei);
1178 }
1179 /* Start Tns-reset */
1180 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001181}
1182
Harald Weltea9f23c82011-11-23 15:01:31 +01001183/*! \brief Establish a NS connection (from the BSS) to the SGSN
1184 * \param nsi NS-instance
1185 * \param[in] dest Destination IP/Port
1186 * \param[in] nsei NSEI of the to-be-established NS-VC
1187 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1188 * \returns struct gprs_nsvc representing the new NS-VC
1189 *
1190 * This function will establish a single NS/UDP/IP connection in uplink
1191 * (BSS to SGSN) direction.
1192 */
Harald Weltef5430362012-06-17 12:25:53 +08001193struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001194 struct sockaddr_in *dest, uint16_t nsei,
1195 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001196{
1197 struct gprs_nsvc *nsvc;
1198
1199 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001200 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001201 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001202 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001203 nsvc->nsei = nsei;
1204 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001205 nsvc->remote_end_is_sgsn = 1;
1206
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001207 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1208 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001209}
Harald Weltea9f23c82011-11-23 15:01:31 +01001210
Harald Weltecca49632012-06-16 17:45:59 +08001211void gprs_ns_set_log_ss(int ss)
1212{
1213 DNS = ss;
1214}
1215
Harald Weltea9f23c82011-11-23 15:01:31 +01001216/*! }@ */