blob: 23c0be88e93a49b970760d0b93051b868b466726 [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 Welte7fa89c22014-10-26 20:33:09 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
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 Welte7fa89c22014-10-26 20:33:09 +010016 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010019 * 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>
Harald Weltebfe62e52017-05-15 12:48:30 +020074#include <osmocom/core/byteswap.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010075#include <osmocom/gsm/tlv.h>
76#include <osmocom/core/talloc.h>
77#include <osmocom/core/select.h>
78#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +020079#include <osmocom/core/stat_item.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010080#include <osmocom/core/stats.h>
Harald Welte73952e32012-06-16 14:59:56 +080081#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080082#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080083#include <osmocom/gprs/gprs_ns.h>
84#include <osmocom/gprs/gprs_bssgp.h>
85#include <osmocom/gprs/gprs_ns_frgre.h>
86
Harald Weltecca49632012-06-16 17:45:59 +080087#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080088
Harald Welte9ba50052010-03-14 15:45:01 +080089static const struct tlv_definition ns_att_tlvdef = {
90 .def = {
91 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
93 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
94 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
95 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
96 },
97};
98
Harald Weltec51c23c2010-05-13 12:55:20 +020099enum ns_ctr {
100 NS_CTR_PKTS_IN,
101 NS_CTR_PKTS_OUT,
102 NS_CTR_BYTES_IN,
103 NS_CTR_BYTES_OUT,
104 NS_CTR_BLOCKED,
105 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200106 NS_CTR_REPLACED,
107 NS_CTR_NSEI_CHG,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200108 NS_CTR_INV_VCI,
109 NS_CTR_INV_NSEI,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200110 NS_CTR_LOST_ALIVE,
111 NS_CTR_LOST_RESET,
Harald Weltec51c23c2010-05-13 12:55:20 +0200112};
113
Harald Welte144e0292010-05-13 11:45:07 +0200114static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200115 { "packets.in", "Packets at NS Level ( In)" },
116 { "packets.out","Packets at NS Level (Out)" },
117 { "bytes.in", "Bytes at NS Level ( In)" },
118 { "bytes.out", "Bytes at NS Level (Out)" },
119 { "blocked", "NS-VC Block count " },
120 { "dead", "NS-VC gone dead count " },
121 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200122 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200123 { "inv-nsvci", "NS-VCI was invalid count " },
124 { "inv-nsei", "NSEI was invalid count " },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200125 { "lost.alive", "ALIVE ACK missing count " },
126 { "lost.reset", "RESET ACK missing count " },
Harald Welte144e0292010-05-13 11:45:07 +0200127};
128
129static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200130 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200131 .group_description = "NSVC Peer Statistics",
132 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
133 .ctr_desc = nsvc_ctr_description,
Jacob Erlbeckfed2a482015-11-03 16:18:56 +0100134 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte144e0292010-05-13 11:45:07 +0200135};
136
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200137enum ns_stat {
138 NS_STAT_ALIVE_DELAY,
139};
140
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100141static const struct osmo_stat_item_desc nsvc_stat_description[] = {
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100142 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200143};
144
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100145static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200146 .group_name_prefix = "ns.nsvc",
147 .group_description = "NSVC Peer Statistics",
148 .num_items = ARRAY_SIZE(nsvc_stat_description),
149 .item_desc = nsvc_stat_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100150 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200151};
152
Max54ca30c2017-05-22 14:48:27 +0200153const struct value_string gprs_ns_signal_ns_names[] = {
154 { S_NS_RESET, "NS-RESET" },
155 { S_NS_BLOCK, "NS-BLOCK" },
156 { S_NS_UNBLOCK, "NS-UNBLOCK" },
157 { S_NS_ALIVE_EXP, "NS-ALIVE expired" },
158 { S_NS_REPLACED, "NSVC replaced" },
159 { S_NS_MISMATCH, "Unexpected IE" },
160 { 0, NULL }
161};
162
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200163#define CHECK_TX_RC(rc, nsvc) \
164 if (rc < 0) \
165 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
166 rc, gprs_ns_ll_str(nsvc));
167
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200168struct msgb *gprs_ns_msgb_alloc(void)
169{
170 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
171 "GPRS/NS");
172 if (!msg) {
173 LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
174 NS_ALLOC_SIZE);
175 }
176 return msg;
177}
178
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200179
Harald Weltea9f23c82011-11-23 15:01:31 +0100180/*! \brief Lookup struct gprs_nsvc based on NSVCI
181 * \param[in] nsi NS instance in which to search
182 * \param[in] nsvci NSVCI to be searched
183 * \returns gprs_nsvc of respective NSVCI
184 */
Harald Weltef5430362012-06-17 12:25:53 +0800185struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200186{
187 struct gprs_nsvc *nsvc;
188 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
189 if (nsvc->nsvci == nsvci)
190 return nsvc;
191 }
192 return NULL;
193}
194
Harald Weltea9f23c82011-11-23 15:01:31 +0100195/*! \brief Lookup struct gprs_nsvc based on NSEI
196 * \param[in] nsi NS instance in which to search
197 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200198 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100199 */
Harald Weltef5430362012-06-17 12:25:53 +0800200struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200201{
202 struct gprs_nsvc *nsvc;
203 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
204 if (nsvc->nsei == nsei)
205 return nsvc;
206 }
207 return NULL;
208}
209
Jacob Erlbeck69017152013-10-14 12:03:54 +0200210static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
211 uint16_t nsei)
212{
213 struct gprs_nsvc *nsvc;
214 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
215 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200216 if (!(nsvc->state & NSE_S_BLOCKED) &&
217 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200218 return nsvc;
219 }
220 }
221 return NULL;
222}
223
Harald Weltef030b212010-04-26 19:18:54 +0200224/* Lookup struct gprs_nsvc based on remote peer socket addr */
225static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
226 struct sockaddr_in *sin)
227{
228 struct gprs_nsvc *nsvc;
229 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000230 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
231 sin->sin_addr.s_addr &&
232 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200233 return nsvc;
234 }
235 return NULL;
236}
237
Harald Welte69a4cf22010-05-03 20:55:10 +0200238static void gprs_ns_timer_cb(void *data);
239
Harald Weltef5430362012-06-17 12:25:53 +0800240struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200241{
242 struct gprs_nsvc *nsvc;
243
Harald Welteb1020d52010-05-25 22:17:30 +0200244 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
245
Harald Weltef030b212010-04-26 19:18:54 +0200246 nsvc = talloc_zero(nsi, struct gprs_nsvc);
247 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100248 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200249 /* before RESET procedure: BLOCKED and DEAD */
250 nsvc->state = NSE_S_BLOCKED;
251 nsvc->nsi = nsi;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200252 osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc);
Harald Welte144e0292010-05-13 11:45:07 +0200253 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100254 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200255
Harald Weltef030b212010-04-26 19:18:54 +0200256 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
257
258 return nsvc;
259}
Harald Welte9ba50052010-03-14 15:45:01 +0800260
Harald Weltea9f23c82011-11-23 15:01:31 +0100261/*! \brief Delete given NS-VC
262 * \param[in] nsvc gprs_nsvc to be deleted
263 */
Harald Weltef5430362012-06-17 12:25:53 +0800264void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000265{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200266 if (osmo_timer_pending(&nsvc->timer))
267 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000268 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200269 rate_ctr_group_free(nsvc->ctrg);
Jacob Erlbeck0975a7f2015-12-17 09:51:53 +0100270 osmo_stat_item_group_free(nsvc->statg);
Harald Welte2bffac52010-05-12 15:55:23 +0000271 talloc_free(nsvc);
272}
273
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200274static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200275 uint8_t cause)
276{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200277 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200278
279 nssd.nsvc = nsvc;
280 nssd.cause = cause;
281
Harald Welte4fcdd762012-06-16 16:40:42 +0800282 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200283}
284
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200285static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
286 struct msgb *msg,
287 uint8_t pdu_type, uint8_t ie_type)
288{
289 struct ns_signal_data nssd = {0};
290
291 nssd.nsvc = nsvc;
292 nssd.pdu_type = pdu_type;
293 nssd.ie_type = ie_type;
294 nssd.msg = msg;
295
296 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
297}
298
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200299static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
300{
301 struct ns_signal_data nssd = {0};
302
303 nssd.nsvc = nsvc;
304 nssd.old_nsvc = old_nsvc;
305
306 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
307}
308
Max584f4b62017-05-04 11:59:52 +0200309const struct value_string gprs_ns_pdu_strings[] = {
310 /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */
311 { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */
312 { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */
313 { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */
314 { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */
315 { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */
316 { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */
317 { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */
318 { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */
319 { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */
320 { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */
321 /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */
322 { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */
323 { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */
324 { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */
325 { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */
326 { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */
327 { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */
328 { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */
329 { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */
330 { 0, NULL }
331};
332
Harald Welte9ba50052010-03-14 15:45:01 +0800333/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200334static const struct value_string ns_cause_str[] = {
335 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
336 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
337 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
338 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
339 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
340 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
341 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
342 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200343 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200344 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
345 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
346 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800347};
348
Harald Weltea9f23c82011-11-23 15:01:31 +0100349/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200350const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800351{
Harald Welte2577d412010-05-02 09:37:45 +0200352 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800353}
354
Harald Welte24a655f2010-04-30 19:54:29 +0200355static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200356extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200357
Harald Welte24a655f2010-04-30 19:54:29 +0200358static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800359{
Harald Weltef030b212010-04-26 19:18:54 +0200360 int ret;
361
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100362 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200363
Harald Welte144e0292010-05-13 11:45:07 +0200364 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200365 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
366 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200367
Harald Welteb3ee2652010-05-19 14:38:50 +0200368 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200369 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200370 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200371 if (ret < 0)
372 LOGP(DNS, LOGL_INFO,
373 "failed to send NS message via UDP: %s\n",
374 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200375 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200376 case GPRS_NS_LL_FR_GRE:
377 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200378 if (ret < 0)
379 LOGP(DNS, LOGL_INFO,
380 "failed to send NS message via FR/GRE: %s\n",
381 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200382 break;
Harald Weltef030b212010-04-26 19:18:54 +0200383 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200384 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200385 msgb_free(msg);
386 ret = -EIO;
387 break;
388 }
389 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800390}
391
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200392static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800393{
Harald Welteba4c6662010-05-19 15:38:10 +0200394 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800395 struct gprs_ns_hdr *nsh;
396
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100397 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200398
Harald Welte9ba50052010-03-14 15:45:01 +0800399 if (!msg)
400 return -ENOMEM;
401
Harald Welte144e0292010-05-13 11:45:07 +0200402 msg->l2h = msgb_put(msg, sizeof(*nsh));
403 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800404
405 nsh->pdu_type = pdu_type;
406
Harald Welte24a655f2010-04-30 19:54:29 +0200407 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800408}
409
Harald Weltea9f23c82011-11-23 15:01:31 +0100410/*! \brief Transmit a NS-RESET on a given NSVC
411 * \param[in] nsvc NS-VC used for transmission
412 * \paam[in] cause Numeric NS cause value
413 */
Harald Welte834f26d2010-05-11 06:20:54 +0200414int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200415{
Harald Welteba4c6662010-05-19 15:38:10 +0200416 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200417 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200418 uint16_t nsvci = osmo_htons(nsvc->nsvci);
419 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200420
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100421 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200422
Harald Welte69a4cf22010-05-03 20:55:10 +0200423 if (!msg)
424 return -ENOMEM;
425
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000426 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
427 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
428
Harald Welte144e0292010-05-13 11:45:07 +0200429 msg->l2h = msgb_put(msg, sizeof(*nsh));
430 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200431 nsh->pdu_type = NS_PDUT_RESET;
432
433 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
434 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
435 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
436
437 return gprs_ns_tx(nsvc, msg);
438
439}
440
Harald Weltea9f23c82011-11-23 15:01:31 +0100441/*! \brief Transmit a NS-STATUS on a given NSVC
442 * \param[in] nsvc NS-VC to be used for transmission
443 * \param[in] cause Numeric NS cause value
444 * \param[in] bvci BVCI to be reset within NSVC
445 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200446int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
447 uint16_t bvci, struct msgb *orig_msg)
448{
Harald Welteba4c6662010-05-19 15:38:10 +0200449 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200450 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200451 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200452
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100453 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200454
Harald Weltebfe62e52017-05-15 12:48:30 +0200455 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200456
457 if (!msg)
458 return -ENOMEM;
459
Harald Welte90af1942010-05-15 23:02:24 +0200460 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000461 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
462
Harald Welte144e0292010-05-13 11:45:07 +0200463 msg->l2h = msgb_put(msg, sizeof(*nsh));
464 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200465 nsh->pdu_type = NS_PDUT_STATUS;
466
467 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
468
469 /* Section 9.2.7.1: Static conditions for NS-VCI */
470 if (cause == NS_CAUSE_NSVC_BLOCKED ||
471 cause == NS_CAUSE_NSVC_UNKNOWN)
472 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
473
474 /* Section 9.2.7.2: Static conditions for NS PDU */
475 switch (cause) {
476 case NS_CAUSE_SEM_INCORR_PDU:
477 case NS_CAUSE_PDU_INCOMP_PSTATE:
478 case NS_CAUSE_PROTO_ERR_UNSPEC:
479 case NS_CAUSE_INVAL_ESSENT_IE:
480 case NS_CAUSE_MISSING_ESSENT_IE:
481 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
482 orig_msg->l2h);
483 break;
484 default:
485 break;
486 }
487
488 /* Section 9.2.7.3: Static conditions for BVCI */
489 if (cause == NS_CAUSE_BVCI_UNKNOWN)
490 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
491
492 return gprs_ns_tx(nsvc, msg);
493}
494
Harald Weltea9f23c82011-11-23 15:01:31 +0100495/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
496 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
497 * \param[in] cause Numeric NS Cause value
498 * \returns 0 in case of success
499 */
Harald Welte834f26d2010-05-11 06:20:54 +0200500int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
501{
Harald Welteba4c6662010-05-19 15:38:10 +0200502 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200503 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200504 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200505
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100506 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200507
Harald Welte834f26d2010-05-11 06:20:54 +0200508 if (!msg)
509 return -ENOMEM;
510
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000511 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
512 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
513
Harald Welte834f26d2010-05-11 06:20:54 +0200514 /* be conservative and mark it as blocked even now! */
515 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200516 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200517
Harald Welte144e0292010-05-13 11:45:07 +0200518 msg->l2h = msgb_put(msg, sizeof(*nsh));
519 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200520 nsh->pdu_type = NS_PDUT_BLOCK;
521
522 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
523 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
524
525 return gprs_ns_tx(nsvc, msg);
526}
527
Harald Weltea9f23c82011-11-23 15:01:31 +0100528/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
529 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
530 * \returns 0 in case of success
531 */
Harald Welte834f26d2010-05-11 06:20:54 +0200532int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
533{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100534 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000535 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
536 nsvc->nsei, nsvc->nsvci);
537
Harald Welte834f26d2010-05-11 06:20:54 +0200538 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
539}
540
Harald Weltea9f23c82011-11-23 15:01:31 +0100541/*! \brief Transmit a NS-ALIVE on a given NS-VC
542 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
543 * \returns 0 in case of success
544 */
Harald Welteb983c312010-05-12 12:11:45 +0000545int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
546{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100547 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000548 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
549 nsvc->nsei, nsvc->nsvci);
550
551 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
552}
553
Harald Weltea9f23c82011-11-23 15:01:31 +0100554/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
555 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
556 * \returns 0 in case of success
557 */
Harald Welteb983c312010-05-12 12:11:45 +0000558int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
559{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100560 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000561 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
562 nsvc->nsei, nsvc->nsvci);
563
564 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
565}
566
Harald Weltefe4ab902010-05-12 17:19:53 +0000567static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
568 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
569 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
570 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200571};
572
Harald Welte7c24b9e2010-05-12 12:21:52 +0000573static const struct value_string timer_mode_strs[] = {
574 { NSVC_TIMER_TNS_RESET, "tns-reset" },
575 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
576 { NSVC_TIMER_TNS_TEST, "tns-test" },
577 { 0, NULL }
578};
579
Harald Welte69a4cf22010-05-03 20:55:10 +0200580static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
581{
Harald Weltefe4ab902010-05-12 17:19:53 +0000582 enum ns_timeout tout = timer_mode_tout[mode];
583 unsigned int seconds = nsvc->nsi->timeout[tout];
584
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100585 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000586 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
587 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000588 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000589
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200590 if (osmo_timer_pending(&nsvc->timer))
591 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200592
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200593 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200594 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200595 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200596}
597
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200598static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
599{
600 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200601 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200602 timersub(&now, &nsvc->timer_started, &elapsed);
603
604 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
605}
606
Harald Welte80405452010-05-03 20:16:13 +0200607static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800608{
609 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000610 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
611 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800612
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100613 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000614 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
615 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000616 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000617
Harald Welte80405452010-05-03 20:16:13 +0200618 switch (nsvc->timer_mode) {
619 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800620 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200621 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800622 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000623 if (nsvc->alive_retries >
624 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800625 /* mark as dead and blocked */
626 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200627 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
628 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200629 LOGP(DNS, LOGL_NOTICE,
630 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200631 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000632 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200633 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
634 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800635 return;
636 }
Harald Welteb983c312010-05-12 12:11:45 +0000637 /* Tns-test case: send NS-ALIVE PDU */
638 gprs_ns_tx_alive(nsvc);
639 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200640 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200641 break;
642 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800643 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000644 gprs_ns_tx_alive(nsvc);
645 /* start Tns-alive timer (transition into faster
646 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000647 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200648 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
649 break;
650 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200651 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100652 if (!(nsvc->state & NSE_S_RESET))
653 LOGP(DNS, LOGL_NOTICE,
654 "NSEI=%u Reset timed out but RESET flag is not set\n",
655 nsvc->nsei);
656 /* Mark NS-VC locally as blocked and dead */
657 nsvc->state = NSE_S_BLOCKED | NSE_S_RESET;
Harald Welte69a4cf22010-05-03 20:55:10 +0200658 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200659 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200660 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200661 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200662 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200663 case _NSVC_TIMER_NR:
664 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800665 }
Harald Welte9ba50052010-03-14 15:45:01 +0800666}
667
668/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800669static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800670{
Harald Welteba4c6662010-05-19 15:38:10 +0200671 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800672 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200673 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800674
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100675 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800676 if (!msg)
677 return -ENOMEM;
678
Harald Weltebfe62e52017-05-15 12:48:30 +0200679 nsvci = osmo_htons(nsvc->nsvci);
680 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800681
Harald Welte144e0292010-05-13 11:45:07 +0200682 msg->l2h = msgb_put(msg, sizeof(*nsh));
683 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800684
685 nsh->pdu_type = NS_PDUT_RESET_ACK;
686
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200687 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200688 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800689
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200690 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
691 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800692
Harald Welte24a655f2010-04-30 19:54:29 +0200693 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800694}
695
Harald Weltea9f23c82011-11-23 15:01:31 +0100696/*! \brief High-level function for transmitting a NS-UNITDATA messsage
697 * \param[in] nsi NS-instance on which we shall transmit
698 * \param[in] msg struct msgb to be trasnmitted
699 *
700 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
701 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
702 * header for the NS-UNITDATA message type and sends it off.
703 *
704 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
705 */
Harald Welte24a655f2010-04-30 19:54:29 +0200706int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800707{
Harald Welte24a655f2010-04-30 19:54:29 +0200708 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800709 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200710 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200711
Jacob Erlbeck69017152013-10-14 12:03:54 +0200712 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200713 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200714 int rc;
715 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
716 LOGP(DNS, LOGL_ERROR,
717 "All NS-VCs for NSEI %u are either dead or blocked!\n",
718 msgb_nsei(msg));
719 rc = -EBUSY;
720 } else {
721 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
722 "to NS-VC!\n", msgb_nsei(msg));
723 rc = -EINVAL;
724 }
725
Harald Weltef47df642010-08-09 21:15:40 +0800726 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200727 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200728 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100729 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800730
Harald Welteec20ba42010-05-13 12:18:49 +0200731 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
732 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800733 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200734 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800735 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800736 return -EIO;
737 }
738
739 nsh->pdu_type = NS_PDUT_UNITDATA;
740 /* spare octet in data[0] */
741 nsh->data[1] = bvci >> 8;
742 nsh->data[2] = bvci & 0xff;
743
Harald Welte24a655f2010-04-30 19:54:29 +0200744 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800745}
746
747/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200748static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800749{
750 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200751 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800752
Harald Weltec1402a62010-05-12 11:48:44 +0200753 if (nsvc->state & NSE_S_BLOCKED)
754 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
755 0, msg);
756
Harald Welte9ba50052010-03-14 15:45:01 +0800757 /* spare octet in data[0] */
758 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200759 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200760 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800761
762 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200763 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800764}
765
766/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200767static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800768{
Harald Weltef030b212010-04-26 19:18:54 +0200769 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800770 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200771 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800772 int rc;
773
Harald Welte41337832010-05-16 00:24:26 +0200774 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800775
Harald Weltebd33f3d2010-05-30 17:19:38 +0200776 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
777 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200778 if (rc < 0) {
779 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
780 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
781 "Error during TLV Parse\n", nsvc->nsei);
782 return rc;
783 }
Harald Welte9ba50052010-03-14 15:45:01 +0800784
785 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200786 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800787 return -EINVAL;
788 }
789
790 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200791 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800792
793 return 0;
794}
795
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200796/* Replace a nsvc object with another based on NSVCI.
797 * This function replaces looks for a NSVC with the given NSVCI and replaces it
798 * if possible and necessary. If replaced, the former value of *nsvc is
799 * returned in *old_nsvc.
800 * \return != 0 if *nsvc points to a matching NSVC.
801 */
802static int gprs_nsvc_replace_if_found(uint16_t nsvci,
803 struct gprs_nsvc **nsvc,
804 struct gprs_nsvc **old_nsvc)
805{
806 struct gprs_nsvc *matching_nsvc;
807
808 if ((*nsvc)->nsvci == nsvci) {
809 *old_nsvc = NULL;
810 return 1;
811 }
812
813 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
814
815 if (!matching_nsvc)
816 return 0;
817
818 /* The NS-VCI is already used by this NS-VC */
819
820 char *old_peer;
821
822 /* Exchange the NS-VC objects */
823 *old_nsvc = *nsvc;
824 *nsvc = matching_nsvc;
825
826 /* Do logging */
827 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
828 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
829 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
830
831 talloc_free(old_peer);
832
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200833 return 1;
834}
835
Harald Welte9ba50052010-03-14 15:45:01 +0800836/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200837static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800838{
839 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800840 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200841 uint8_t cause;
842 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200843 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800844 int rc;
845
Harald Weltebd33f3d2010-05-30 17:19:38 +0200846 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
847 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200848 if (rc < 0) {
849 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200850 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200851 return rc;
852 }
Harald Welte9ba50052010-03-14 15:45:01 +0800853
854 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
855 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
856 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200857 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200858 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800859 return -EINVAL;
860 }
861
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200862 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +0200863 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
864 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800865
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200866 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
867 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
868 nsei, nsvci, gprs_ns_cause_str(cause));
869
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200870 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
871 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200872 /* The incoming RESET doesn't match the NSVCI. Send an
873 * appropriate RESET_ACK and ignore the RESET.
874 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
875 */
876 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
877 NS_PDUT_RESET,
878 NS_IE_VCI);
879 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
880 gprs_ns_tx_reset_ack(*nsvc);
881 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200882 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200883
884 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200885 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
886 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
887 "at %s\n",
888 nsvci, (*nsvc)->nsvci,
889 gprs_ns_ll_str(*nsvc));
890 orig_nsvc = *nsvc;
891 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200892 (*nsvc)->nsei = nsei;
893 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200894 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200895
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200896 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
897 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
898 /* The incoming RESET doesn't match the NSEI. Send an
899 * appropriate RESET_ACK and ignore the RESET.
900 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
901 */
902 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
903 NS_PDUT_RESET,
904 NS_IE_NSEI);
905 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200906 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200907 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200908 return 0;
909 }
910
911 /* NSEI has changed */
912 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
913 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200914 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200915
Harald Weltec1402a62010-05-12 11:48:44 +0200916 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200917 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200918
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200919 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200920 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200921 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200922
923 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200924 gprs_ns_ll_copy(*nsvc, orig_nsvc);
925 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200926 } else {
927 (*nsvc)->nsei = nsei;
928 (*nsvc)->nsvci = nsvci;
929 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +0100930 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
931 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200932 }
Harald Welte9ba50052010-03-14 15:45:01 +0800933
Harald Welte834f26d2010-05-11 06:20:54 +0200934 /* inform interested parties about the fact that this NSVC
935 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200936 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200937
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200938 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200939
940 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200941 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
942 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200943
944 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800945}
946
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200947static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
948{
949 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
950 struct tlv_parsed tp;
951 uint16_t nsvci, nsei;
952 struct gprs_nsvc *orig_nsvc = NULL;
953 int rc;
954
955 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
956 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
957 if (rc < 0) {
958 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
959 "Error during TLV Parse\n", (*nsvc)->nsei);
960 return rc;
961 }
962
963 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
964 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
965 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200966 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200967 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200968 return -EINVAL;
969 }
970
Harald Weltebfe62e52017-05-15 12:48:30 +0200971 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
972 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200973
974 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
975 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
976 nsei, nsvci);
977
978 if (!((*nsvc)->state & NSE_S_RESET)) {
979 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
980 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
981 */
982 LOGP(DNS, LOGL_ERROR,
983 "NS RESET ACK Discarding unexpected message for "
984 "NS-VCI %d from SGSN NSEI=%d\n",
985 nsvci, nsei);
986 return 0;
987 }
988
989 if (!(*nsvc)->nsvci_is_valid) {
990 LOGP(DNS, LOGL_NOTICE,
991 "NS RESET ACK Uninitialised NS-VC (%u) for "
992 "NS-VCI %d, NSEI=%d from %s\n",
993 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
994 return -EINVAL;
995 }
996
997 if ((*nsvc)->nsvci != nsvci) {
998 /* NS-VCI has changed */
999
1000 /* if !0, use another NSVC object that matches the NSVCI */
1001 int use_other_nsvc;
1002
1003 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001004 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1005 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001006
1007 if (use_other_nsvc)
1008 /* Update *nsvc to point to the right NSVC object */
1009 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1010 &orig_nsvc);
1011
1012 if (!use_other_nsvc) {
1013 /* The incoming RESET_ACK doesn't match the NSVCI.
1014 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1015 */
1016 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1017 NS_PDUT_RESET_ACK,
1018 NS_IE_VCI);
1019 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1020 LOGP(DNS, LOGL_ERROR,
1021 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1022 "from %s\n",
1023 nsvci,
1024 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1025 nsei, gprs_ns_ll_str(*nsvc));
1026 return -EINVAL;
1027 }
1028
1029 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001030 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001031 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1032
1033 /* Update the ll info fields */
1034 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1035 gprs_ns_ll_clear(orig_nsvc);
1036 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001037 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1038 /* The incoming RESET_ACK doesn't match the NSEI.
1039 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1040 */
1041 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1042 NS_PDUT_RESET_ACK,
1043 NS_IE_NSEI);
1044 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1045 LOGP(DNS, LOGL_ERROR,
1046 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1047 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1048 return -EINVAL;
1049 }
1050
1051 /* NSEI has changed */
1052 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1053 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001054 }
1055
1056 /* Mark NS-VC as blocked and alive */
1057 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
1058 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
1059 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1060 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1061 /* stop RESET timer */
1062 osmo_timer_del(&(*nsvc)->timer);
1063 }
1064 /* Initiate TEST proc.: Send ALIVE and start timer */
1065 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1066 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1067
1068 return rc;
1069}
1070
Harald Welte834f26d2010-05-11 06:20:54 +02001071static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1072{
1073 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1074 struct tlv_parsed tp;
1075 uint8_t *cause;
1076 int rc;
1077
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001078 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001079
1080 nsvc->state |= NSE_S_BLOCKED;
1081
Harald Weltebd33f3d2010-05-30 17:19:38 +02001082 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1083 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001084 if (rc < 0) {
1085 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1086 "Error during TLV Parse\n", nsvc->nsei);
1087 return rc;
1088 }
Harald Welte834f26d2010-05-11 06:20:54 +02001089
1090 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1091 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001092 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001093 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001094 return -EINVAL;
1095 }
1096
1097 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1098 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1099
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001100 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001101 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001102
1103 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1104}
1105
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001106int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1107 struct gprs_nsvc *fallback_nsvc,
1108 struct gprs_nsvc **new_nsvc);
1109
1110int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001111 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001112
Harald Weltea9f23c82011-11-23 15:01:31 +01001113/*! \brief Receive incoming NS message from underlying transport layer
1114 * \param nsi NS instance to which the data belongs
1115 * \param[in] msg message buffer containing newly-received data
1116 * \param[in] saddr socketaddr from which data was received
1117 * \param[in] ll link-layer type in which data was received
1118 * \returns 0 in case of success, < 0 in case of error
1119 *
1120 * This is the main entry point int othe NS imlementation where frames
1121 * from the underlying transport (normally UDP) enter.
1122 */
Harald Weltef030b212010-04-26 19:18:54 +02001123int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001124 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001125{
Harald Weltef030b212010-04-26 19:18:54 +02001126 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001127 int rc = 0;
1128
Harald Weltef030b212010-04-26 19:18:54 +02001129 /* look up the NSVC based on source address */
1130 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001131
Harald Weltef030b212010-04-26 19:18:54 +02001132 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001133 struct gprs_nsvc *fallback_nsvc;
1134
1135 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001136 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001137 fallback_nsvc->ip.bts_addr = *saddr;
1138 fallback_nsvc->ll = ll;
1139
1140 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1141
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001142 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001143 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001144
1145 rc = 0;
1146 }
1147
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001148 if (nsvc)
1149 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001150
1151 return rc;
1152}
1153
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001154const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001155{
1156 static char buf[80];
1157 snprintf(buf, sizeof(buf), "%s:%u",
1158 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
Harald Weltebfe62e52017-05-15 12:48:30 +02001159 osmo_ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001160 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001161
1162 return buf;
1163}
1164
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001165void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1166{
1167 nsvc->ll = other->ll;
1168
1169 switch (nsvc->ll) {
1170 case GPRS_NS_LL_UDP:
1171 nsvc->ip = other->ip;
1172 break;
1173 case GPRS_NS_LL_FR_GRE:
1174 nsvc->frgre = other->frgre;
1175 break;
1176 default:
1177 break;
1178 }
1179}
1180
1181void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1182{
1183 switch (nsvc->ll) {
1184 case GPRS_NS_LL_UDP:
1185 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1186 nsvc->ip.bts_addr.sin_port = 0;
1187 break;
1188 case GPRS_NS_LL_FR_GRE:
1189 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1190 nsvc->frgre.bts_addr.sin_port = 0;
1191 break;
1192 default:
1193 break;
1194 }
1195}
1196
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001197/*! \brief Create/get NS-VC independently from underlying transport layer
1198 * \param nsi NS instance to which the data belongs
1199 * \param[in] msg message buffer containing newly-received data
1200 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001201 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001202 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1203 * been created or found
1204 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1205 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1206 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1207 * has been created and registered, and GPRS_NS_CS_FOUND if an
1208 * existing NS-VC object has been found with the same NSEI.
1209 *
1210 * This contains the initial NS automaton state (NS-VC not yet attached).
1211 */
1212int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1213 struct gprs_nsvc *fallback_nsvc,
1214 struct gprs_nsvc **new_nsvc)
1215{
1216 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1217 struct gprs_nsvc *existing_nsvc;
1218
1219 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001220 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001221 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001222
1223 int rc;
1224
1225 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001226 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001227 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1228 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001229 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001230 return GPRS_NS_CS_SKIPPED;
1231 }
1232
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001233 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1234 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1235 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1236 "for non-existing NS-VC\n",
1237 gprs_ns_ll_str(fallback_nsvc));
1238 return GPRS_NS_CS_SKIPPED;
1239 }
1240
1241 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1242 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1243 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1244 "for non-existing NS-VC\n",
1245 gprs_ns_ll_str(fallback_nsvc));
1246 return GPRS_NS_CS_SKIPPED;
1247 }
1248
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001249 /* Only the RESET procedure creates a new NSVC */
1250 if (nsh->pdu_type != NS_PDUT_RESET) {
1251 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001252 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001253 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001254 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001255 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001256 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001257 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001258 fallback_nsvc->state = NSE_S_ALIVE;
1259
1260 rc = gprs_ns_tx_status(fallback_nsvc,
1261 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1262 if (rc < 0) {
1263 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001264 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001265 return rc;
1266 }
1267 return GPRS_NS_CS_REJECTED;
1268 }
1269
1270 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1271 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1272 if (rc < 0) {
1273 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1274 "TLV Parse\n", rc);
1275 return rc;
1276 }
1277 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1278 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1279 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001280 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001281 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001282 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001283 return -EINVAL;
1284 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001285 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1286 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001287 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001288 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001289 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001290 if (!existing_nsvc) {
1291 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001292 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001293 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001294 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001295 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001296 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001297
1298 return GPRS_NS_CS_CREATED;
1299 }
1300
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001301 /* Check NSEI */
1302 if (existing_nsvc->nsei != nsei) {
1303 LOGP(DNS, LOGL_NOTICE,
1304 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1305 nsvci, existing_nsvc->nsei, nsei);
1306
1307 /* Override old NSEI */
1308 existing_nsvc->nsei = nsei;
1309
1310 /* Do statistics */
1311 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1312 }
1313
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001314 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001315 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001316 return GPRS_NS_CS_FOUND;
1317}
1318
1319/*! \brief Process NS message independently from underlying transport layer
1320 * \param nsi NS instance to which the data belongs
1321 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001322 * \param[inout] nsvc refers to the virtual connection, may be modified when
1323 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001324 * \returns 0 in case of success, < 0 in case of error
1325 *
1326 * This contains the main NS automaton.
1327 */
1328int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001329 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001330{
1331 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1332 int rc = 0;
1333
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001334 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001335
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001336 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001337
Harald Welte144e0292010-05-13 11:45:07 +02001338 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001339 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1340 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001341
Harald Welte9ba50052010-03-14 15:45:01 +08001342 switch (nsh->pdu_type) {
1343 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001344 /* If we're dead and blocked and suddenly receive a
1345 * NS-ALIVE out of the blue, we might have been re-started
1346 * and should send a NS-RESET to make sure everything recovers
1347 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001348 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001349 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1350 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001351 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001352 break;
1353 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001354 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001355 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001356 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001357 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001358 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1359 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001360 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001361 if ((*nsvc)->state & NSE_S_BLOCKED)
1362 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001363 }
Harald Welte9ba50052010-03-14 15:45:01 +08001364 break;
1365 case NS_PDUT_UNITDATA:
1366 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001367 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001368 break;
1369 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001370 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001371 break;
1372 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001373 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001374 break;
1375 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001376 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001377 break;
1378 case NS_PDUT_UNBLOCK:
1379 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001380 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1381 (*nsvc)->state &= ~NSE_S_BLOCKED;
1382 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1383 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001384 break;
1385 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001386 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001387 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001388 (*nsvc)->state = NSE_S_ALIVE;
1389 (*nsvc)->remote_state = NSE_S_ALIVE;
1390 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001391 break;
1392 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001393 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001394 break;
1395 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001396 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001397 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001398 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001399 break;
1400 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001401 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001402 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001403 rc = -EINVAL;
1404 break;
1405 }
1406 return rc;
1407}
1408
Harald Weltea9f23c82011-11-23 15:01:31 +01001409/*! \brief Create a new GPRS NS instance
1410 * \param[in] cb Call-back function for incoming BSSGP data
1411 * \returns dynamically allocated gprs_ns_inst
1412 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001413struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001414{
Harald Welte4fcdd762012-06-16 16:40:42 +08001415 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001416
1417 nsi->cb = cb;
1418 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001419 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1420 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1421 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1422 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1423 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1424 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1425 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001426
Harald Welte60cc6ac2010-05-13 14:20:56 +02001427 /* Create the dummy NSVC that we use for sending
1428 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001429 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001430 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001431 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001432 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001433
Harald Welte3771d092010-04-30 20:26:32 +02001434 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001435}
1436
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001437void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001438{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001439 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001440
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001441 gprs_nsvc_delete(nsi->unknown_nsvc);
1442
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001443 /* delete all NSVCs and clear their timers */
1444 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1445 gprs_nsvc_delete(nsvc);
1446
1447 /* close socket and unregister */
1448 if (nsi->nsip.fd.data) {
1449 close(nsi->nsip.fd.fd);
1450 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001451 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001452 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001453}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001454
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001455/*! \brief Destroy an entire NS instance
1456 * \param nsi gprs_ns_inst that is to be destroyed
1457 *
1458 * This function releases all resources associated with the
1459 * NS-instance.
1460 */
1461void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1462{
1463 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001464 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001465 talloc_free(nsi);
1466}
1467
1468
1469/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1470 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1471
1472/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001473static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001474 struct sockaddr_in *saddr)
1475{
Harald Welteba4c6662010-05-19 15:38:10 +02001476 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001477 int ret = 0;
1478 socklen_t saddr_len = sizeof(*saddr);
1479
1480 if (!msg) {
1481 *error = -ENOMEM;
1482 return NULL;
1483 }
1484
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001485 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001486 (struct sockaddr *)saddr, &saddr_len);
1487 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001488 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001489 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001490 msgb_free(msg);
1491 *error = ret;
1492 return NULL;
1493 } else if (ret == 0) {
1494 msgb_free(msg);
1495 *error = ret;
1496 return NULL;
1497 }
1498
1499 msg->l2h = msg->data;
1500 msgb_put(msg, ret);
1501
1502 return msg;
1503}
1504
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001505static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001506{
1507 int error;
1508 struct sockaddr_in saddr;
1509 struct gprs_ns_inst *nsi = bfd->data;
1510 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1511
1512 if (!msg)
1513 return error;
1514
Harald Welteb3ee2652010-05-19 14:38:50 +02001515 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001516
1517 msgb_free(msg);
1518
1519 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001520}
1521
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001522static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001523{
1524 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1525 return -EIO;
1526}
1527
Harald Welteb3ee2652010-05-19 14:38:50 +02001528static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001529{
1530 int rc;
1531 struct gprs_ns_inst *nsi = nsvc->nsi;
1532 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1533
1534 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1535 (struct sockaddr *)daddr, sizeof(*daddr));
1536
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001537 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001538
1539 return rc;
1540}
1541
1542/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001543static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001544{
1545 int rc = 0;
1546
1547 if (what & BSC_FD_READ)
1548 rc = handle_nsip_read(bfd);
1549 if (what & BSC_FD_WRITE)
1550 rc = handle_nsip_write(bfd);
1551
1552 return rc;
1553}
1554
Harald Weltea9f23c82011-11-23 15:01:31 +01001555/*! \brief Create a listening socket for GPRS NS/UDP/IP
1556 * \param[in] nsi NS protocol instance to listen
1557 * \returns >=0 (fd) in case of success, negative in case of error
1558 *
1559 * A call to this function will create a UDP socket bound to the port
1560 * number and IP address specified in the NS protocol instance. The
1561 * file descriptor of the socket will be stored in nsi->nsip.fd.
1562 */
Harald Welte7fb05232010-05-19 15:09:09 +02001563int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001564{
Harald Welte73952e32012-06-16 14:59:56 +08001565 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001566 int ret;
1567
Harald Weltebfe62e52017-05-15 12:48:30 +02001568 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Harald Welte73952e32012-06-16 14:59:56 +08001569
1570 nsi->nsip.fd.cb = nsip_fd_cb;
1571 nsi->nsip.fd.data = nsi;
1572 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1573 IPPROTO_UDP, inet_ntoa(in),
1574 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001575 if (ret < 0)
1576 return ret;
1577
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001578 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1579 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1580 if (ret < 0)
1581 LOGP(DNS, LOGL_ERROR,
1582 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1583 nsi->nsip.dscp, ret, errno);
1584
Harald Weltef030b212010-04-26 19:18:54 +02001585
1586 return ret;
1587}
Harald Welte3771d092010-04-30 20:26:32 +02001588
Harald Weltea9f23c82011-11-23 15:01:31 +01001589/*! \brief Initiate a RESET procedure
1590 * \param[in] nsvc NS-VC in which to start the procedure
1591 * \param[in] cause Numeric NS cause value
1592 *
1593 * This is a high-level function initiating a NS-RESET procedure. It
1594 * will not only send a NS-RESET, but also set the state to BLOCKED and
1595 * start the Tns-reset timer.
1596 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001597int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001598{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001599 int rc;
1600
Harald Welteb1020d52010-05-25 22:17:30 +02001601 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1602 nsvc->nsei);
1603
Harald Welte731d1fc2010-05-14 11:53:08 +00001604 /* Mark NS-VC locally as blocked and dead */
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001605 nsvc->state = NSE_S_BLOCKED | NSE_S_RESET;
1606
Harald Welte731d1fc2010-05-14 11:53:08 +00001607 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001608 rc = gprs_ns_tx_reset(nsvc, cause);
1609 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001610 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1611 nsvc->nsei);
1612 }
1613 /* Start Tns-reset */
1614 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001615
1616 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001617}
1618
Harald Weltea9f23c82011-11-23 15:01:31 +01001619/*! \brief Establish a NS connection (from the BSS) to the SGSN
1620 * \param nsi NS-instance
1621 * \param[in] dest Destination IP/Port
1622 * \param[in] nsei NSEI of the to-be-established NS-VC
1623 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1624 * \returns struct gprs_nsvc representing the new NS-VC
1625 *
1626 * This function will establish a single NS/UDP/IP connection in uplink
1627 * (BSS to SGSN) direction.
1628 */
Harald Weltef5430362012-06-17 12:25:53 +08001629struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001630 struct sockaddr_in *dest, uint16_t nsei,
1631 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001632{
1633 struct gprs_nsvc *nsvc;
1634
1635 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001636 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001637 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001638 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001639 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001640 nsvc->remote_end_is_sgsn = 1;
1641
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001642 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1643 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001644}
Harald Weltea9f23c82011-11-23 15:01:31 +01001645
Harald Weltecca49632012-06-16 17:45:59 +08001646void gprs_ns_set_log_ss(int ss)
1647{
1648 DNS = ss;
1649}
1650
Harald Weltea9f23c82011-11-23 15:01:31 +01001651/*! }@ */