blob: 9b7cc056f362ac377720b6da005a0afcba5ed653 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_ns.c
2 * GPRS Networks Service (NS) messages on the Gb interface.
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05). */
4/*
Harald Weltee08da972017-11-13 01:00:26 +09005 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2016-2017 sysmocom - s.f.m.c. GmbH
Harald Welte9ba50052010-03-14 15:45:01 +08007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte9ba50052010-03-14 15:45:01 +080012 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010013 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080015 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080021 *
Harald Welte7fa89c22014-10-26 20:33:09 +010022 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010023 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080024 *
25 */
26
Harald Weltea9f23c82011-11-23 15:01:31 +010027/*! \addtogroup libgb
28 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 *
Harald Weltea9f23c82011-11-23 15:01:31 +010030 * 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:
Harald Welte9ba50052010-03-14 15:45:01 +080039 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020040 * NS Network Service
41 * NSVC NS Virtual Connection
42 * NSEI NS Entity Identifier
43 * NSVL NS Virtual Link
44 * NSVLI NS Virtual Link Identifier
45 * BVC BSSGP Virtual Connection
46 * BVCI BSSGP Virtual Connection Identifier
47 * NSVCG NS Virtual Connection Goup
48 * Blocked NS-VC cannot be used for user traffic
49 * Alive Ability of a NS-VC to provide communication
50 *
51 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
Harald Welte9ba50052010-03-14 15:45:01 +080052 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
53 * NS then has to firgure out which NSVC's are responsible for this BVCI.
54 * Those mappings are administratively configured.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020055 *
56 * This implementation has the following limitations:
57 * - Only one NS-VC for each NSE: No load-sharing function
58 * - NSVCI 65535 and 65534 are reserved for internal use
59 * - Only UDP is supported as of now, no frame relay support
60 * - The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
61 * - There are no BLOCK and UNBLOCK timers (yet?)
62 *
63 * \file gprs_ns.c */
Harald Weltedd1c83c2010-05-13 13:58:08 +020064
Harald Welte9ba50052010-03-14 15:45:01 +080065#include <stdlib.h>
66#include <unistd.h>
67#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020068#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080069
Holger Hans Peter Freyther3c16de22012-07-12 14:13:41 +020070#include <sys/types.h>
71#include <sys/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080072#include <arpa/inet.h>
73
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010074#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020075#include <osmocom/core/byteswap.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010076#include <osmocom/gsm/tlv.h>
77#include <osmocom/core/talloc.h>
78#include <osmocom/core/select.h>
79#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +020080#include <osmocom/core/stat_item.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010081#include <osmocom/core/stats.h>
Harald Welte73952e32012-06-16 14:59:56 +080082#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080083#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080084#include <osmocom/gprs/gprs_ns.h>
85#include <osmocom/gprs/gprs_bssgp.h>
86#include <osmocom/gprs/gprs_ns_frgre.h>
87
Harald Weltecca49632012-06-16 17:45:59 +080088#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080089
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020090#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
91#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
Maxc513c0f2017-10-23 15:11:14 +020092#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
93#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
94
Harald Welte9ba50052010-03-14 15:45:01 +080095static const struct tlv_definition ns_att_tlvdef = {
96 .def = {
97 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
98 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
99 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
100 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
101 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
102 },
103};
104
Harald Weltec51c23c2010-05-13 12:55:20 +0200105enum ns_ctr {
106 NS_CTR_PKTS_IN,
107 NS_CTR_PKTS_OUT,
108 NS_CTR_BYTES_IN,
109 NS_CTR_BYTES_OUT,
110 NS_CTR_BLOCKED,
111 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200112 NS_CTR_REPLACED,
113 NS_CTR_NSEI_CHG,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200114 NS_CTR_INV_VCI,
115 NS_CTR_INV_NSEI,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200116 NS_CTR_LOST_ALIVE,
117 NS_CTR_LOST_RESET,
Harald Weltec51c23c2010-05-13 12:55:20 +0200118};
119
Harald Welte144e0292010-05-13 11:45:07 +0200120static const struct rate_ctr_desc nsvc_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +0800121 { "packets:in", "Packets at NS Level ( In)" },
122 { "packets:out","Packets at NS Level (Out)" },
123 { "bytes:in", "Bytes at NS Level ( In)" },
124 { "bytes:out", "Bytes at NS Level (Out)" },
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200125 { "blocked", "NS-VC Block count " },
126 { "dead", "NS-VC gone dead count " },
127 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200128 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200129 { "inv-nsvci", "NS-VCI was invalid count " },
130 { "inv-nsei", "NSEI was invalid count " },
Harald Weltea7a50652017-10-03 17:49:21 +0800131 { "lost:alive", "ALIVE ACK missing count " },
132 { "lost:reset", "RESET ACK missing count " },
Harald Welte144e0292010-05-13 11:45:07 +0200133};
134
135static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +0800136 .group_name_prefix = "ns:nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200137 .group_description = "NSVC Peer Statistics",
138 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
139 .ctr_desc = nsvc_ctr_description,
Jacob Erlbeckfed2a482015-11-03 16:18:56 +0100140 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte144e0292010-05-13 11:45:07 +0200141};
142
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200143enum ns_stat {
144 NS_STAT_ALIVE_DELAY,
145};
146
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100147static const struct osmo_stat_item_desc nsvc_stat_description[] = {
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100148 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200149};
150
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100151static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200152 .group_name_prefix = "ns.nsvc",
153 .group_description = "NSVC Peer Statistics",
154 .num_items = ARRAY_SIZE(nsvc_stat_description),
155 .item_desc = nsvc_stat_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100156 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200157};
158
Max54ca30c2017-05-22 14:48:27 +0200159const struct value_string gprs_ns_signal_ns_names[] = {
160 { S_NS_RESET, "NS-RESET" },
161 { S_NS_BLOCK, "NS-BLOCK" },
162 { S_NS_UNBLOCK, "NS-UNBLOCK" },
163 { S_NS_ALIVE_EXP, "NS-ALIVE expired" },
164 { S_NS_REPLACED, "NSVC replaced" },
165 { S_NS_MISMATCH, "Unexpected IE" },
166 { 0, NULL }
167};
168
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200169#define CHECK_TX_RC(rc, nsvc) \
170 if (rc < 0) \
171 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
172 rc, gprs_ns_ll_str(nsvc));
173
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200174struct msgb *gprs_ns_msgb_alloc(void)
175{
176 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
177 "GPRS/NS");
178 if (!msg) {
179 LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
180 NS_ALLOC_SIZE);
181 }
182 return msg;
183}
184
Maxc513c0f2017-10-23 15:11:14 +0200185/* FIXME: convert to osmo_fsm */
186static inline void ns_set_state_with_log(struct gprs_nsvc *nsvc, uint32_t state, bool is_remote,
187 const char *file, unsigned line)
188{
189 uint32_t old_state = is_remote ? nsvc->remote_state : nsvc->state;
190
191 LOGPSRC(DNS, LOGL_DEBUG, file, line, "NSEI %d (NS-VCI=%u) setting %sstate [%s,%s,%s] -> [%s,%s,%s]\n",
192 nsvc->nsei, nsvc->nsvci, is_remote ? "remote " : "",
193 NS_DESC_A(old_state), NS_DESC_B(old_state), NS_DESC_R(old_state),
194 NS_DESC_A(state), NS_DESC_B(state), NS_DESC_R(state));
195
196 if (is_remote)
197 nsvc->remote_state = state;
198 else
199 nsvc->state = state;
200}
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! Lookup struct gprs_nsvc based on NSVCI
Harald Weltea9f23c82011-11-23 15:01:31 +0100203 * \param[in] nsi NS instance in which to search
204 * \param[in] nsvci NSVCI to be searched
205 * \returns gprs_nsvc of respective NSVCI
206 */
Harald Weltef5430362012-06-17 12:25:53 +0800207struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200208{
209 struct gprs_nsvc *nsvc;
210 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
211 if (nsvc->nsvci == nsvci)
212 return nsvc;
213 }
214 return NULL;
215}
216
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200217/*! Lookup struct gprs_nsvc based on NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100218 * \param[in] nsi NS instance in which to search
219 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200220 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100221 */
Harald Weltef5430362012-06-17 12:25:53 +0800222struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200223{
224 struct gprs_nsvc *nsvc;
225 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
226 if (nsvc->nsei == nsei)
227 return nsvc;
228 }
229 return NULL;
230}
231
Jacob Erlbeck69017152013-10-14 12:03:54 +0200232static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
233 uint16_t nsei)
234{
235 struct gprs_nsvc *nsvc;
236 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
237 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200238 if (!(nsvc->state & NSE_S_BLOCKED) &&
239 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200240 return nsvc;
241 }
242 }
243 return NULL;
244}
245
Harald Weltef030b212010-04-26 19:18:54 +0200246/* Lookup struct gprs_nsvc based on remote peer socket addr */
247static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
248 struct sockaddr_in *sin)
249{
250 struct gprs_nsvc *nsvc;
251 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000252 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
253 sin->sin_addr.s_addr &&
254 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200255 return nsvc;
256 }
257 return NULL;
258}
259
Harald Welte69a4cf22010-05-03 20:55:10 +0200260static void gprs_ns_timer_cb(void *data);
261
Harald Weltef5430362012-06-17 12:25:53 +0800262struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200263{
264 struct gprs_nsvc *nsvc;
265
Harald Welteb1020d52010-05-25 22:17:30 +0200266 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
267
Harald Weltef030b212010-04-26 19:18:54 +0200268 nsvc = talloc_zero(nsi, struct gprs_nsvc);
269 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100270 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200271 /* before RESET procedure: BLOCKED and DEAD */
Maxc513c0f2017-10-23 15:11:14 +0200272 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltef030b212010-04-26 19:18:54 +0200273 nsvc->nsi = nsi;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200274 osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc);
Harald Welte144e0292010-05-13 11:45:07 +0200275 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100276 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200277
Harald Weltef030b212010-04-26 19:18:54 +0200278 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
279
280 return nsvc;
281}
Harald Welte9ba50052010-03-14 15:45:01 +0800282
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283/*! Delete given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100284 * \param[in] nsvc gprs_nsvc to be deleted
285 */
Harald Weltef5430362012-06-17 12:25:53 +0800286void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000287{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200288 if (osmo_timer_pending(&nsvc->timer))
289 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000290 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200291 rate_ctr_group_free(nsvc->ctrg);
Jacob Erlbeck0975a7f2015-12-17 09:51:53 +0100292 osmo_stat_item_group_free(nsvc->statg);
Harald Welte2bffac52010-05-12 15:55:23 +0000293 talloc_free(nsvc);
294}
295
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200296static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200297 uint8_t cause)
298{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200299 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200300
301 nssd.nsvc = nsvc;
302 nssd.cause = cause;
303
Harald Welte4fcdd762012-06-16 16:40:42 +0800304 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200305}
306
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200307static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
308 struct msgb *msg,
309 uint8_t pdu_type, uint8_t ie_type)
310{
311 struct ns_signal_data nssd = {0};
312
313 nssd.nsvc = nsvc;
314 nssd.pdu_type = pdu_type;
315 nssd.ie_type = ie_type;
316 nssd.msg = msg;
317
318 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
319}
320
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200321static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
322{
323 struct ns_signal_data nssd = {0};
324
325 nssd.nsvc = nsvc;
326 nssd.old_nsvc = old_nsvc;
327
328 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
329}
330
Max584f4b62017-05-04 11:59:52 +0200331const struct value_string gprs_ns_pdu_strings[] = {
332 /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */
333 { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */
334 { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */
335 { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */
336 { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */
337 { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */
338 { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */
339 { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */
340 { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */
341 { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */
342 { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */
343 /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */
344 { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */
345 { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */
346 { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */
347 { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */
348 { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */
349 { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */
350 { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */
351 { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */
352 { 0, NULL }
353};
354
Harald Welte9ba50052010-03-14 15:45:01 +0800355/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200356static const struct value_string ns_cause_str[] = {
357 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
358 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
359 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
360 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
361 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
362 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
363 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
364 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200365 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200366 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
367 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
368 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800369};
370
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200371/*! Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200372const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800373{
Harald Welte2577d412010-05-02 09:37:45 +0200374 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800375}
376
Harald Welte24a655f2010-04-30 19:54:29 +0200377static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200378extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200379
Harald Welte24a655f2010-04-30 19:54:29 +0200380static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800381{
Harald Weltef030b212010-04-26 19:18:54 +0200382 int ret;
383
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100384 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200385
Harald Welte144e0292010-05-13 11:45:07 +0200386 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200387 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
388 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200389
Harald Welteb3ee2652010-05-19 14:38:50 +0200390 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200391 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200392 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200393 if (ret < 0)
394 LOGP(DNS, LOGL_INFO,
395 "failed to send NS message via UDP: %s\n",
396 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200397 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200398 case GPRS_NS_LL_FR_GRE:
399 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200400 if (ret < 0)
401 LOGP(DNS, LOGL_INFO,
402 "failed to send NS message via FR/GRE: %s\n",
403 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200404 break;
Harald Weltef030b212010-04-26 19:18:54 +0200405 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200406 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200407 msgb_free(msg);
408 ret = -EIO;
409 break;
410 }
411 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800412}
413
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200414static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800415{
Harald Welteba4c6662010-05-19 15:38:10 +0200416 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800417 struct gprs_ns_hdr *nsh;
418
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100419 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200420
Harald Welte9ba50052010-03-14 15:45:01 +0800421 if (!msg)
422 return -ENOMEM;
423
Harald Welte144e0292010-05-13 11:45:07 +0200424 msg->l2h = msgb_put(msg, sizeof(*nsh));
425 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800426
427 nsh->pdu_type = pdu_type;
428
Harald Welte24a655f2010-04-30 19:54:29 +0200429 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800430}
431
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200432/*! Transmit a NS-RESET on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100433 * \param[in] nsvc NS-VC used for transmission
434 * \paam[in] cause Numeric NS cause value
435 */
Harald Welte834f26d2010-05-11 06:20:54 +0200436int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200437{
Harald Welteba4c6662010-05-19 15:38:10 +0200438 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200439 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200440 uint16_t nsvci = osmo_htons(nsvc->nsvci);
441 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200442
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100443 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200444
Harald Welte69a4cf22010-05-03 20:55:10 +0200445 if (!msg)
446 return -ENOMEM;
447
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000448 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
449 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
450
Harald Welte144e0292010-05-13 11:45:07 +0200451 msg->l2h = msgb_put(msg, sizeof(*nsh));
452 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200453 nsh->pdu_type = NS_PDUT_RESET;
454
455 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
456 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
457 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
458
459 return gprs_ns_tx(nsvc, msg);
460
461}
462
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200463/*! Transmit a NS-STATUS on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100464 * \param[in] nsvc NS-VC to be used for transmission
465 * \param[in] cause Numeric NS cause value
466 * \param[in] bvci BVCI to be reset within NSVC
467 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200468int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
469 uint16_t bvci, struct msgb *orig_msg)
470{
Harald Welteba4c6662010-05-19 15:38:10 +0200471 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200472 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200473 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200474
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100475 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200476
Harald Weltebfe62e52017-05-15 12:48:30 +0200477 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200478
479 if (!msg)
480 return -ENOMEM;
481
Harald Welte90af1942010-05-15 23:02:24 +0200482 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000483 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
484
Harald Welte144e0292010-05-13 11:45:07 +0200485 msg->l2h = msgb_put(msg, sizeof(*nsh));
486 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200487 nsh->pdu_type = NS_PDUT_STATUS;
488
489 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
490
491 /* Section 9.2.7.1: Static conditions for NS-VCI */
492 if (cause == NS_CAUSE_NSVC_BLOCKED ||
493 cause == NS_CAUSE_NSVC_UNKNOWN)
494 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
495
496 /* Section 9.2.7.2: Static conditions for NS PDU */
497 switch (cause) {
498 case NS_CAUSE_SEM_INCORR_PDU:
499 case NS_CAUSE_PDU_INCOMP_PSTATE:
500 case NS_CAUSE_PROTO_ERR_UNSPEC:
501 case NS_CAUSE_INVAL_ESSENT_IE:
502 case NS_CAUSE_MISSING_ESSENT_IE:
503 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
504 orig_msg->l2h);
505 break;
506 default:
507 break;
508 }
509
510 /* Section 9.2.7.3: Static conditions for BVCI */
511 if (cause == NS_CAUSE_BVCI_UNKNOWN)
512 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
513
514 return gprs_ns_tx(nsvc, msg);
515}
516
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200517/*! Transmit a NS-BLOCK on a tiven NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100518 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
519 * \param[in] cause Numeric NS Cause value
520 * \returns 0 in case of success
521 */
Harald Welte834f26d2010-05-11 06:20:54 +0200522int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
523{
Harald Welteba4c6662010-05-19 15:38:10 +0200524 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200525 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200526 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200527
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100528 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200529
Harald Welte834f26d2010-05-11 06:20:54 +0200530 if (!msg)
531 return -ENOMEM;
532
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000533 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
534 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
535
Harald Welte834f26d2010-05-11 06:20:54 +0200536 /* be conservative and mark it as blocked even now! */
Maxc513c0f2017-10-23 15:11:14 +0200537 ns_mark_blocked(nsvc);
Harald Weltec51c23c2010-05-13 12:55:20 +0200538 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200539
Harald Welte144e0292010-05-13 11:45:07 +0200540 msg->l2h = msgb_put(msg, sizeof(*nsh));
541 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200542 nsh->pdu_type = NS_PDUT_BLOCK;
543
544 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
545 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
546
547 return gprs_ns_tx(nsvc, msg);
548}
549
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200550/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100551 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
552 * \returns 0 in case of success
553 */
Harald Welte834f26d2010-05-11 06:20:54 +0200554int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
555{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100556 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000557 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
558 nsvc->nsei, nsvc->nsvci);
559
Harald Welte834f26d2010-05-11 06:20:54 +0200560 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
561}
562
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200563/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100564 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
565 * \returns 0 in case of success
566 */
Harald Welteb983c312010-05-12 12:11:45 +0000567int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
568{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100569 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000570 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
571 nsvc->nsei, nsvc->nsvci);
572
573 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
574}
575
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200576/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100577 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
578 * \returns 0 in case of success
579 */
Harald Welteb983c312010-05-12 12:11:45 +0000580int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
581{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100582 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000583 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
584 nsvc->nsei, nsvc->nsvci);
585
586 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
587}
588
Harald Weltefe4ab902010-05-12 17:19:53 +0000589static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
590 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
591 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
592 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200593};
594
Harald Welte7c24b9e2010-05-12 12:21:52 +0000595static const struct value_string timer_mode_strs[] = {
596 { NSVC_TIMER_TNS_RESET, "tns-reset" },
597 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
598 { NSVC_TIMER_TNS_TEST, "tns-test" },
599 { 0, NULL }
600};
601
Harald Welte69a4cf22010-05-03 20:55:10 +0200602static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
603{
Harald Weltefe4ab902010-05-12 17:19:53 +0000604 enum ns_timeout tout = timer_mode_tout[mode];
605 unsigned int seconds = nsvc->nsi->timeout[tout];
606
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100607 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000608 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
609 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000610 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000611
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200612 if (osmo_timer_pending(&nsvc->timer))
613 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200614
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200615 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200616 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200617 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200618}
619
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200620static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
621{
622 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200623 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200624 timersub(&now, &nsvc->timer_started, &elapsed);
625
626 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
627}
628
Harald Welte80405452010-05-03 20:16:13 +0200629static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800630{
631 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000632 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
633 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800634
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100635 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000636 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
637 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000638 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000639
Harald Welte80405452010-05-03 20:16:13 +0200640 switch (nsvc->timer_mode) {
641 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800642 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200643 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800644 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000645 if (nsvc->alive_retries >
646 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800647 /* mark as dead and blocked */
Maxc513c0f2017-10-23 15:11:14 +0200648 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltec51c23c2010-05-13 12:55:20 +0200649 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
650 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200651 LOGP(DNS, LOGL_NOTICE,
652 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200653 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000654 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200655 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
656 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800657 return;
658 }
Harald Welteb983c312010-05-12 12:11:45 +0000659 /* Tns-test case: send NS-ALIVE PDU */
660 gprs_ns_tx_alive(nsvc);
661 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200662 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200663 break;
664 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800665 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000666 gprs_ns_tx_alive(nsvc);
667 /* start Tns-alive timer (transition into faster
668 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000669 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200670 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
671 break;
672 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200673 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100674 if (!(nsvc->state & NSE_S_RESET))
675 LOGP(DNS, LOGL_NOTICE,
676 "NSEI=%u Reset timed out but RESET flag is not set\n",
677 nsvc->nsei);
678 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200679 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200680 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200681 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200682 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200683 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200684 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200685 case _NSVC_TIMER_NR:
686 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800687 }
Harald Welte9ba50052010-03-14 15:45:01 +0800688}
689
690/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800691static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800692{
Harald Welteba4c6662010-05-19 15:38:10 +0200693 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800694 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200695 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800696
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100697 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800698 if (!msg)
699 return -ENOMEM;
700
Harald Weltebfe62e52017-05-15 12:48:30 +0200701 nsvci = osmo_htons(nsvc->nsvci);
702 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800703
Harald Welte144e0292010-05-13 11:45:07 +0200704 msg->l2h = msgb_put(msg, sizeof(*nsh));
705 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800706
707 nsh->pdu_type = NS_PDUT_RESET_ACK;
708
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200709 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200710 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800711
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200712 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
713 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800714
Harald Welte24a655f2010-04-30 19:54:29 +0200715 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800716}
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +0100719 * \param[in] nsi NS-instance on which we shall transmit
720 * \param[in] msg struct msgb to be trasnmitted
721 *
722 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
723 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
724 * header for the NS-UNITDATA message type and sends it off.
725 *
726 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
727 */
Harald Welte24a655f2010-04-30 19:54:29 +0200728int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800729{
Harald Welte24a655f2010-04-30 19:54:29 +0200730 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800731 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200732 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200733
Jacob Erlbeck69017152013-10-14 12:03:54 +0200734 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200735 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200736 int rc;
737 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
738 LOGP(DNS, LOGL_ERROR,
739 "All NS-VCs for NSEI %u are either dead or blocked!\n",
740 msgb_nsei(msg));
741 rc = -EBUSY;
742 } else {
743 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
744 "to NS-VC!\n", msgb_nsei(msg));
745 rc = -EINVAL;
746 }
747
Harald Weltef47df642010-08-09 21:15:40 +0800748 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200749 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200750 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100751 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800752
Harald Welteec20ba42010-05-13 12:18:49 +0200753 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
754 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800755 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200756 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800757 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800758 return -EIO;
759 }
760
761 nsh->pdu_type = NS_PDUT_UNITDATA;
762 /* spare octet in data[0] */
763 nsh->data[1] = bvci >> 8;
764 nsh->data[2] = bvci & 0xff;
765
Harald Welte24a655f2010-04-30 19:54:29 +0200766 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800767}
768
769/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200770static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800771{
772 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200773 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800774
Harald Weltec1402a62010-05-12 11:48:44 +0200775 if (nsvc->state & NSE_S_BLOCKED)
776 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
777 0, msg);
778
Harald Welte9ba50052010-03-14 15:45:01 +0800779 /* spare octet in data[0] */
780 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200781 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200782 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800783
784 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200785 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800786}
787
788/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200789static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800790{
Harald Weltef030b212010-04-26 19:18:54 +0200791 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800792 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200793 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800794 int rc;
795
Harald Welte41337832010-05-16 00:24:26 +0200796 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800797
Harald Weltebd33f3d2010-05-30 17:19:38 +0200798 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
799 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200800 if (rc < 0) {
801 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
802 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
803 "Error during TLV Parse\n", nsvc->nsei);
804 return rc;
805 }
Harald Welte9ba50052010-03-14 15:45:01 +0800806
807 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200808 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800809 return -EINVAL;
810 }
811
812 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200813 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800814
815 return 0;
816}
817
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200818/* Replace a nsvc object with another based on NSVCI.
819 * This function replaces looks for a NSVC with the given NSVCI and replaces it
820 * if possible and necessary. If replaced, the former value of *nsvc is
821 * returned in *old_nsvc.
822 * \return != 0 if *nsvc points to a matching NSVC.
823 */
824static int gprs_nsvc_replace_if_found(uint16_t nsvci,
825 struct gprs_nsvc **nsvc,
826 struct gprs_nsvc **old_nsvc)
827{
828 struct gprs_nsvc *matching_nsvc;
829
830 if ((*nsvc)->nsvci == nsvci) {
831 *old_nsvc = NULL;
832 return 1;
833 }
834
835 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
836
837 if (!matching_nsvc)
838 return 0;
839
840 /* The NS-VCI is already used by this NS-VC */
841
842 char *old_peer;
843
844 /* Exchange the NS-VC objects */
845 *old_nsvc = *nsvc;
846 *nsvc = matching_nsvc;
847
848 /* Do logging */
849 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
850 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
851 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
852
853 talloc_free(old_peer);
854
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200855 return 1;
856}
857
Harald Welte9ba50052010-03-14 15:45:01 +0800858/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200859static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800860{
861 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800862 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200863 uint8_t cause;
864 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200865 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800866 int rc;
867
Harald Weltebd33f3d2010-05-30 17:19:38 +0200868 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
869 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200870 if (rc < 0) {
871 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200872 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200873 return rc;
874 }
Harald Welte9ba50052010-03-14 15:45:01 +0800875
876 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
877 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
878 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200879 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200880 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800881 return -EINVAL;
882 }
883
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200884 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +0200885 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
886 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800887
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200888 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
889 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
890 nsei, nsvci, gprs_ns_cause_str(cause));
891
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200892 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
893 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200894 /* The incoming RESET doesn't match the NSVCI. Send an
895 * appropriate RESET_ACK and ignore the RESET.
896 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
897 */
898 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
899 NS_PDUT_RESET,
900 NS_IE_VCI);
901 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
902 gprs_ns_tx_reset_ack(*nsvc);
903 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200904 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200905
906 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200907 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
908 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
909 "at %s\n",
910 nsvci, (*nsvc)->nsvci,
911 gprs_ns_ll_str(*nsvc));
912 orig_nsvc = *nsvc;
913 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200914 (*nsvc)->nsei = nsei;
915 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200916 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200917
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200918 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
919 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
920 /* The incoming RESET doesn't match the NSEI. Send an
921 * appropriate RESET_ACK and ignore the RESET.
922 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
923 */
924 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
925 NS_PDUT_RESET,
926 NS_IE_NSEI);
927 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200928 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200929 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200930 return 0;
931 }
932
933 /* NSEI has changed */
934 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
935 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200936 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200937
Harald Weltec1402a62010-05-12 11:48:44 +0200938 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +0200939 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +0200940
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200941 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200942 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200943 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200944
945 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200946 gprs_ns_ll_copy(*nsvc, orig_nsvc);
947 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200948 } else {
949 (*nsvc)->nsei = nsei;
950 (*nsvc)->nsvci = nsvci;
951 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +0100952 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
953 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200954 }
Harald Welte9ba50052010-03-14 15:45:01 +0800955
Harald Welte834f26d2010-05-11 06:20:54 +0200956 /* inform interested parties about the fact that this NSVC
957 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200958 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200959
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200960 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200961
962 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200963 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
964 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200965
966 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800967}
968
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200969static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
970{
971 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
972 struct tlv_parsed tp;
973 uint16_t nsvci, nsei;
974 struct gprs_nsvc *orig_nsvc = NULL;
975 int rc;
976
977 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
978 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
979 if (rc < 0) {
980 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
981 "Error during TLV Parse\n", (*nsvc)->nsei);
982 return rc;
983 }
984
985 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
986 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
987 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200988 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200989 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200990 return -EINVAL;
991 }
992
Harald Weltebfe62e52017-05-15 12:48:30 +0200993 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
994 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200995
996 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
997 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
998 nsei, nsvci);
999
1000 if (!((*nsvc)->state & NSE_S_RESET)) {
1001 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1002 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1003 */
1004 LOGP(DNS, LOGL_ERROR,
1005 "NS RESET ACK Discarding unexpected message for "
1006 "NS-VCI %d from SGSN NSEI=%d\n",
1007 nsvci, nsei);
1008 return 0;
1009 }
1010
1011 if (!(*nsvc)->nsvci_is_valid) {
1012 LOGP(DNS, LOGL_NOTICE,
1013 "NS RESET ACK Uninitialised NS-VC (%u) for "
1014 "NS-VCI %d, NSEI=%d from %s\n",
1015 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1016 return -EINVAL;
1017 }
1018
1019 if ((*nsvc)->nsvci != nsvci) {
1020 /* NS-VCI has changed */
1021
1022 /* if !0, use another NSVC object that matches the NSVCI */
1023 int use_other_nsvc;
1024
1025 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001026 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1027 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001028
1029 if (use_other_nsvc)
1030 /* Update *nsvc to point to the right NSVC object */
1031 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1032 &orig_nsvc);
1033
1034 if (!use_other_nsvc) {
1035 /* The incoming RESET_ACK doesn't match the NSVCI.
1036 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1037 */
1038 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1039 NS_PDUT_RESET_ACK,
1040 NS_IE_VCI);
1041 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1042 LOGP(DNS, LOGL_ERROR,
1043 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1044 "from %s\n",
1045 nsvci,
1046 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1047 nsei, gprs_ns_ll_str(*nsvc));
1048 return -EINVAL;
1049 }
1050
1051 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001052 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001053 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1054
1055 /* Update the ll info fields */
1056 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1057 gprs_ns_ll_clear(orig_nsvc);
1058 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001059 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1060 /* The incoming RESET_ACK doesn't match the NSEI.
1061 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1062 */
1063 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1064 NS_PDUT_RESET_ACK,
1065 NS_IE_NSEI);
1066 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1067 LOGP(DNS, LOGL_ERROR,
1068 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1069 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1070 return -EINVAL;
1071 }
1072
1073 /* NSEI has changed */
1074 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1075 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001076 }
1077
1078 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001079 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1080 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001081 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1082 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1083 /* stop RESET timer */
1084 osmo_timer_del(&(*nsvc)->timer);
1085 }
1086 /* Initiate TEST proc.: Send ALIVE and start timer */
1087 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1088 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1089
1090 return rc;
1091}
1092
Harald Welte834f26d2010-05-11 06:20:54 +02001093static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1094{
1095 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1096 struct tlv_parsed tp;
1097 uint8_t *cause;
1098 int rc;
1099
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001100 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001101
Maxc513c0f2017-10-23 15:11:14 +02001102 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001103
Harald Weltebd33f3d2010-05-30 17:19:38 +02001104 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1105 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001106 if (rc < 0) {
1107 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1108 "Error during TLV Parse\n", nsvc->nsei);
1109 return rc;
1110 }
Harald Welte834f26d2010-05-11 06:20:54 +02001111
1112 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1113 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001114 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001115 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001116 return -EINVAL;
1117 }
1118
1119 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1120 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1121
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001122 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001123 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001124
1125 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1126}
1127
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001128int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1129 struct gprs_nsvc *fallback_nsvc,
1130 struct gprs_nsvc **new_nsvc);
1131
1132int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001133 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001134
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001135/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001136 * \param nsi NS instance to which the data belongs
1137 * \param[in] msg message buffer containing newly-received data
1138 * \param[in] saddr socketaddr from which data was received
1139 * \param[in] ll link-layer type in which data was received
1140 * \returns 0 in case of success, < 0 in case of error
1141 *
1142 * This is the main entry point int othe NS imlementation where frames
1143 * from the underlying transport (normally UDP) enter.
1144 */
Harald Weltef030b212010-04-26 19:18:54 +02001145int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001146 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001147{
Harald Weltef030b212010-04-26 19:18:54 +02001148 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001149 int rc = 0;
1150
Harald Weltef030b212010-04-26 19:18:54 +02001151 /* look up the NSVC based on source address */
1152 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001153
Harald Weltef030b212010-04-26 19:18:54 +02001154 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001155 struct gprs_nsvc *fallback_nsvc;
1156
1157 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001158 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001159 fallback_nsvc->ip.bts_addr = *saddr;
1160 fallback_nsvc->ll = ll;
1161
1162 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1163
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001164 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001165 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001166
1167 rc = 0;
1168 }
1169
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001170 if (nsvc)
1171 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001172
1173 return rc;
1174}
1175
Maxcbf5cdf2017-10-24 18:18:55 +02001176const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001177{
1178 static char buf[80];
Max95308592017-10-24 15:54:28 +02001179
1180 switch(nsvc->ll) {
1181 case GPRS_NS_LL_UDP:
1182 snprintf(buf, sizeof(buf), "%s:%u",
1183 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1184 break;
1185 case GPRS_NS_LL_FR_GRE:
1186 snprintf(buf, sizeof(buf), "%s:%u",
1187 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1188 break;
1189 default:
1190 buf[0] = '\0';
1191 break;
1192 }
1193
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001194 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001195
1196 return buf;
1197}
1198
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001199void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1200{
1201 nsvc->ll = other->ll;
1202
1203 switch (nsvc->ll) {
1204 case GPRS_NS_LL_UDP:
1205 nsvc->ip = other->ip;
1206 break;
1207 case GPRS_NS_LL_FR_GRE:
1208 nsvc->frgre = other->frgre;
1209 break;
1210 default:
1211 break;
1212 }
1213}
1214
1215void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1216{
1217 switch (nsvc->ll) {
1218 case GPRS_NS_LL_UDP:
1219 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1220 nsvc->ip.bts_addr.sin_port = 0;
1221 break;
1222 case GPRS_NS_LL_FR_GRE:
1223 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1224 nsvc->frgre.bts_addr.sin_port = 0;
1225 break;
1226 default:
1227 break;
1228 }
1229}
1230
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001231/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001232 * \param nsi NS instance to which the data belongs
1233 * \param[in] msg message buffer containing newly-received data
1234 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001235 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001236 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1237 * been created or found
1238 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1239 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1240 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1241 * has been created and registered, and GPRS_NS_CS_FOUND if an
1242 * existing NS-VC object has been found with the same NSEI.
1243 *
1244 * This contains the initial NS automaton state (NS-VC not yet attached).
1245 */
1246int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1247 struct gprs_nsvc *fallback_nsvc,
1248 struct gprs_nsvc **new_nsvc)
1249{
1250 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1251 struct gprs_nsvc *existing_nsvc;
1252
1253 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001254 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001255 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001256
1257 int rc;
1258
1259 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001260 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001261 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1262 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001263 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001264 return GPRS_NS_CS_SKIPPED;
1265 }
1266
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001267 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1268 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1269 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1270 "for non-existing NS-VC\n",
1271 gprs_ns_ll_str(fallback_nsvc));
1272 return GPRS_NS_CS_SKIPPED;
1273 }
1274
1275 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1276 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1277 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1278 "for non-existing NS-VC\n",
1279 gprs_ns_ll_str(fallback_nsvc));
1280 return GPRS_NS_CS_SKIPPED;
1281 }
1282
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001283 /* Only the RESET procedure creates a new NSVC */
1284 if (nsh->pdu_type != NS_PDUT_RESET) {
1285 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001286 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001287 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001288 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001289 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001290 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001291 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001292 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001293
1294 rc = gprs_ns_tx_status(fallback_nsvc,
1295 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1296 if (rc < 0) {
1297 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001298 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001299 return rc;
1300 }
1301 return GPRS_NS_CS_REJECTED;
1302 }
1303
1304 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1305 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1306 if (rc < 0) {
1307 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1308 "TLV Parse\n", rc);
1309 return rc;
1310 }
1311 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1312 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1313 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001314 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001315 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001316 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001317 return -EINVAL;
1318 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001319 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1320 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001321 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001322 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001323 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001324 if (!existing_nsvc) {
1325 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001326 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001327 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001328 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001329 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001330 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001331
1332 return GPRS_NS_CS_CREATED;
1333 }
1334
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001335 /* Check NSEI */
1336 if (existing_nsvc->nsei != nsei) {
1337 LOGP(DNS, LOGL_NOTICE,
1338 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1339 nsvci, existing_nsvc->nsei, nsei);
1340
1341 /* Override old NSEI */
1342 existing_nsvc->nsei = nsei;
1343
1344 /* Do statistics */
1345 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1346 }
1347
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001348 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001349 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001350 return GPRS_NS_CS_FOUND;
1351}
1352
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001353/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001354 * \param nsi NS instance to which the data belongs
1355 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001356 * \param[inout] nsvc refers to the virtual connection, may be modified when
1357 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001358 * \returns 0 in case of success, < 0 in case of error
1359 *
1360 * This contains the main NS automaton.
1361 */
1362int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001363 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001364{
1365 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1366 int rc = 0;
1367
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001368 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001369
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001370 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001371
Harald Welte144e0292010-05-13 11:45:07 +02001372 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001373 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1374 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001375
Harald Welte9ba50052010-03-14 15:45:01 +08001376 switch (nsh->pdu_type) {
1377 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001378 /* If we're dead and blocked and suddenly receive a
1379 * NS-ALIVE out of the blue, we might have been re-started
1380 * and should send a NS-RESET to make sure everything recovers
1381 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001382 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001383 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1384 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001385 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001386 break;
1387 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001388 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001389 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001390 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001391 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001392 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1393 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001394 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001395 if ((*nsvc)->state & NSE_S_BLOCKED)
1396 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001397 }
Harald Welte9ba50052010-03-14 15:45:01 +08001398 break;
1399 case NS_PDUT_UNITDATA:
1400 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001401 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001402 break;
1403 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001404 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001405 break;
1406 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001407 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001408 break;
1409 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001410 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001411 break;
1412 case NS_PDUT_UNBLOCK:
1413 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001414 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001415 ns_mark_unblocked(*nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001416 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1417 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001418 break;
1419 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001420 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001421 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001422 ns_set_state(*nsvc, NSE_S_ALIVE);
1423 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001424 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001425 break;
1426 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001427 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001428 break;
1429 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001430 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001431 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001432 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001433 break;
1434 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001435 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001436 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001437 rc = -EINVAL;
1438 break;
1439 }
1440 return rc;
1441}
1442
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001443/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001444 * \param[in] cb Call-back function for incoming BSSGP data
1445 * \returns dynamically allocated gprs_ns_inst
1446 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001447struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001448{
Harald Welte4fcdd762012-06-16 16:40:42 +08001449 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001450
1451 nsi->cb = cb;
1452 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001453 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1454 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1455 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1456 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1457 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1458 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1459 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001460
Harald Welte60cc6ac2010-05-13 14:20:56 +02001461 /* Create the dummy NSVC that we use for sending
1462 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001463 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001464 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001465 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001466 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001467
Harald Welte3771d092010-04-30 20:26:32 +02001468 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001469}
1470
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001471void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001472{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001473 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001474
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001475 gprs_nsvc_delete(nsi->unknown_nsvc);
1476
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001477 /* delete all NSVCs and clear their timers */
1478 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1479 gprs_nsvc_delete(nsvc);
1480
1481 /* close socket and unregister */
1482 if (nsi->nsip.fd.data) {
1483 close(nsi->nsip.fd.fd);
1484 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001485 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001486 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001487}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001488
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001489/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001490 * \param nsi gprs_ns_inst that is to be destroyed
1491 *
1492 * This function releases all resources associated with the
1493 * NS-instance.
1494 */
1495void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1496{
1497 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001498 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001499 talloc_free(nsi);
1500}
1501
1502
1503/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1504 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1505
1506/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001507static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001508 struct sockaddr_in *saddr)
1509{
Harald Welteba4c6662010-05-19 15:38:10 +02001510 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001511 int ret = 0;
1512 socklen_t saddr_len = sizeof(*saddr);
1513
1514 if (!msg) {
1515 *error = -ENOMEM;
1516 return NULL;
1517 }
1518
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001519 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001520 (struct sockaddr *)saddr, &saddr_len);
1521 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001522 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001523 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001524 msgb_free(msg);
1525 *error = ret;
1526 return NULL;
1527 } else if (ret == 0) {
1528 msgb_free(msg);
1529 *error = ret;
1530 return NULL;
1531 }
1532
1533 msg->l2h = msg->data;
1534 msgb_put(msg, ret);
1535
1536 return msg;
1537}
1538
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001539static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001540{
1541 int error;
1542 struct sockaddr_in saddr;
1543 struct gprs_ns_inst *nsi = bfd->data;
1544 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1545
1546 if (!msg)
1547 return error;
1548
Harald Welteb3ee2652010-05-19 14:38:50 +02001549 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001550
1551 msgb_free(msg);
1552
1553 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001554}
1555
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001556static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001557{
1558 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1559 return -EIO;
1560}
1561
Harald Welteb3ee2652010-05-19 14:38:50 +02001562static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001563{
1564 int rc;
1565 struct gprs_ns_inst *nsi = nsvc->nsi;
1566 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1567
1568 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1569 (struct sockaddr *)daddr, sizeof(*daddr));
1570
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001571 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001572
1573 return rc;
1574}
1575
1576/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001577static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001578{
1579 int rc = 0;
1580
1581 if (what & BSC_FD_READ)
1582 rc = handle_nsip_read(bfd);
1583 if (what & BSC_FD_WRITE)
1584 rc = handle_nsip_write(bfd);
1585
1586 return rc;
1587}
1588
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001589/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01001590 * \param[in] nsi NS protocol instance to listen
1591 * \returns >=0 (fd) in case of success, negative in case of error
1592 *
1593 * A call to this function will create a UDP socket bound to the port
1594 * number and IP address specified in the NS protocol instance. The
1595 * file descriptor of the socket will be stored in nsi->nsip.fd.
1596 */
Harald Welte7fb05232010-05-19 15:09:09 +02001597int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001598{
Harald Welte73952e32012-06-16 14:59:56 +08001599 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001600 struct in_addr remote;
1601 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02001602 int ret;
1603
Harald Weltebfe62e52017-05-15 12:48:30 +02001604 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02001605 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08001606
1607 nsi->nsip.fd.cb = nsip_fd_cb;
1608 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001609
1610 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
1611 /* connect to ensure only we only accept packets from the
1612 * configured remote end/peer */
1613 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
1614 ret =
1615 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1616 IPPROTO_UDP, inet_ntoa(in),
1617 nsi->nsip.local_port, remote_str,
1618 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
1619
1620 LOGP(DNS, LOGL_NOTICE,
1621 "Listening for nsip packets from %s:%u on %s:%u\n",
1622 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
1623 } else {
1624 /* Accept UDP packets from any source IP/Port */
1625 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1626 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND);
1627
1628 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
1629 }
1630
Harald Weltef030b212010-04-26 19:18:54 +02001631 if (ret < 0)
1632 return ret;
1633
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001634 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1635 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1636 if (ret < 0)
1637 LOGP(DNS, LOGL_ERROR,
1638 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1639 nsi->nsip.dscp, ret, errno);
1640
Maxa19c1262017-10-20 15:39:45 +02001641 LOGP(DNS, LOGL_NOTICE, "NS UDP socket at %s:%d\n", inet_ntoa(in), nsi->nsip.local_port);
Harald Weltef030b212010-04-26 19:18:54 +02001642
1643 return ret;
1644}
Harald Welte3771d092010-04-30 20:26:32 +02001645
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001646/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01001647 * \param[in] nsvc NS-VC in which to start the procedure
1648 * \param[in] cause Numeric NS cause value
1649 *
1650 * This is a high-level function initiating a NS-RESET procedure. It
1651 * will not only send a NS-RESET, but also set the state to BLOCKED and
1652 * start the Tns-reset timer.
1653 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001654int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001655{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001656 int rc;
1657
Harald Welteb1020d52010-05-25 22:17:30 +02001658 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1659 nsvc->nsei);
1660
Harald Welte731d1fc2010-05-14 11:53:08 +00001661 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02001662 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001663
Harald Welte731d1fc2010-05-14 11:53:08 +00001664 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001665 rc = gprs_ns_tx_reset(nsvc, cause);
1666 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001667 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1668 nsvc->nsei);
1669 }
1670 /* Start Tns-reset */
1671 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001672
1673 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001674}
1675
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001676/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01001677 * \param nsi NS-instance
1678 * \param[in] dest Destination IP/Port
1679 * \param[in] nsei NSEI of the to-be-established NS-VC
1680 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1681 * \returns struct gprs_nsvc representing the new NS-VC
1682 *
1683 * This function will establish a single NS/UDP/IP connection in uplink
1684 * (BSS to SGSN) direction.
1685 */
Harald Weltef5430362012-06-17 12:25:53 +08001686struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001687 struct sockaddr_in *dest, uint16_t nsei,
1688 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001689{
1690 struct gprs_nsvc *nsvc;
1691
1692 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001693 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001694 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001695 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001696 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001697 nsvc->remote_end_is_sgsn = 1;
1698
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001699 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1700 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001701}
Harald Weltea9f23c82011-11-23 15:01:31 +01001702
Harald Weltecca49632012-06-16 17:45:59 +08001703void gprs_ns_set_log_ss(int ss)
1704{
1705 DNS = ss;
1706}
1707
Daniel Willmannf1318fe2018-05-29 20:55:18 +02001708/*! Append the nsvc state to a talloc string
1709 * \param s The string to append to (allocated with talloc)
1710 * \param[in] nsvc The NS-VC to print the state of
1711 * \returns The new string with state information appended to it
1712 *
1713 * This function will append a comma-separated state of the NS-VC to the
1714 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
1715 */
1716char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
1717{
1718 s = talloc_asprintf_append(s,
1719 "%u,%u,%s,%s,%s,%s,%s\n",
1720 nsvc->nsei, nsvc->nsvci,
1721 NS_DESC_A(nsvc->state),
1722 NS_DESC_B(nsvc->state),
1723 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
1724 NS_DESC_A(nsvc->remote_state),
1725 NS_DESC_B(nsvc->remote_state));
1726
1727 return s;
1728}
1729
Harald Welte96e2a002017-06-12 21:44:18 +02001730/*! @} */