blob: 54964d382785c2007d7cbb3738751198a3ba15bd [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
Harald Welte9ef2e152019-02-19 23:42:04 +0100550/*! Transmit a NS-BLOCK-ACK on a given NS-VC
551 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
552 * \returns 0 in case of success
553 */
554static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc)
555{
556 struct msgb *msg;
557 struct gprs_ns_hdr *nsh;
558 uint16_t nsvci = osmo_htons(nsvc->nsvci);
559
560 log_set_context(LOG_CTX_GB_NSVC, nsvc);
561
562 msg = gprs_ns_msgb_alloc();
563 if (!msg)
564 return -ENOMEM;
565
566 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci);
567
568 /* be conservative and mark it as blocked even now! */
569 msg->l2h = msgb_put(msg, sizeof(*nsh));
570 nsh = (struct gprs_ns_hdr *) msg->l2h;
571 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
572
573 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
574
575 return gprs_ns_tx(nsvc, msg);
576}
577
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200578/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100579 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
580 * \returns 0 in case of success
581 */
Harald Welte834f26d2010-05-11 06:20:54 +0200582int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
583{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100584 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000585 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
586 nsvc->nsei, nsvc->nsvci);
587
Harald Welte834f26d2010-05-11 06:20:54 +0200588 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
589}
590
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200591/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100592 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
593 * \returns 0 in case of success
594 */
Harald Welteb983c312010-05-12 12:11:45 +0000595int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
596{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100597 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000598 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
599 nsvc->nsei, nsvc->nsvci);
600
601 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
602}
603
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200604/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100605 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
606 * \returns 0 in case of success
607 */
Harald Welteb983c312010-05-12 12:11:45 +0000608int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
609{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100610 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000611 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
612 nsvc->nsei, nsvc->nsvci);
613
614 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
615}
616
Harald Weltefe4ab902010-05-12 17:19:53 +0000617static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
618 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
619 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
620 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200621};
622
Harald Welte7c24b9e2010-05-12 12:21:52 +0000623static const struct value_string timer_mode_strs[] = {
624 { NSVC_TIMER_TNS_RESET, "tns-reset" },
625 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
626 { NSVC_TIMER_TNS_TEST, "tns-test" },
627 { 0, NULL }
628};
629
Harald Welte69a4cf22010-05-03 20:55:10 +0200630static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
631{
Harald Weltefe4ab902010-05-12 17:19:53 +0000632 enum ns_timeout tout = timer_mode_tout[mode];
633 unsigned int seconds = nsvc->nsi->timeout[tout];
634
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 Starting timer in mode %s (%u seconds)\n",
637 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000638 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100639
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200640 if (osmo_timer_pending(&nsvc->timer))
641 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200642
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200643 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200644 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200645 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200646}
647
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200648static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
649{
650 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200651 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200652 timersub(&now, &nsvc->timer_started, &elapsed);
653
654 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
655}
656
Harald Welte80405452010-05-03 20:16:13 +0200657static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800658{
659 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000660 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
661 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800662
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100663 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000664 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
665 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000666 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100667
Harald Welte80405452010-05-03 20:16:13 +0200668 switch (nsvc->timer_mode) {
669 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800670 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200671 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800672 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000673 if (nsvc->alive_retries >
674 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800675 /* mark as dead and blocked */
Maxc513c0f2017-10-23 15:11:14 +0200676 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltec51c23c2010-05-13 12:55:20 +0200677 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
678 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200679 LOGP(DNS, LOGL_NOTICE,
680 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200681 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000682 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200683 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
684 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800685 return;
686 }
Harald Welteb983c312010-05-12 12:11:45 +0000687 /* Tns-test case: send NS-ALIVE PDU */
688 gprs_ns_tx_alive(nsvc);
689 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200690 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200691 break;
692 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800693 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000694 gprs_ns_tx_alive(nsvc);
695 /* start Tns-alive timer (transition into faster
696 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000697 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200698 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
699 break;
700 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200701 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100702 if (!(nsvc->state & NSE_S_RESET))
703 LOGP(DNS, LOGL_NOTICE,
704 "NSEI=%u Reset timed out but RESET flag is not set\n",
705 nsvc->nsei);
706 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200707 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200708 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200709 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200710 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200711 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200712 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200713 case _NSVC_TIMER_NR:
714 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800715 }
Harald Welte9ba50052010-03-14 15:45:01 +0800716}
717
718/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800719static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800720{
Harald Welteba4c6662010-05-19 15:38:10 +0200721 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800722 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200723 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800724
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100725 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800726 if (!msg)
727 return -ENOMEM;
728
Harald Weltebfe62e52017-05-15 12:48:30 +0200729 nsvci = osmo_htons(nsvc->nsvci);
730 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800731
Harald Welte144e0292010-05-13 11:45:07 +0200732 msg->l2h = msgb_put(msg, sizeof(*nsh));
733 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800734
735 nsh->pdu_type = NS_PDUT_RESET_ACK;
736
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200737 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200738 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800739
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200740 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
741 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800742
Harald Welte24a655f2010-04-30 19:54:29 +0200743 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800744}
745
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200746/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +0100747 * \param[in] nsi NS-instance on which we shall transmit
748 * \param[in] msg struct msgb to be trasnmitted
749 *
750 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
751 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
752 * header for the NS-UNITDATA message type and sends it off.
753 *
754 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
755 */
Harald Welte24a655f2010-04-30 19:54:29 +0200756int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800757{
Harald Welte24a655f2010-04-30 19:54:29 +0200758 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800759 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200760 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200761
Jacob Erlbeck69017152013-10-14 12:03:54 +0200762 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200763 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200764 int rc;
765 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
766 LOGP(DNS, LOGL_ERROR,
767 "All NS-VCs for NSEI %u are either dead or blocked!\n",
768 msgb_nsei(msg));
769 rc = -EBUSY;
770 } else {
771 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
772 "to NS-VC!\n", msgb_nsei(msg));
773 rc = -EINVAL;
774 }
775
Harald Weltef47df642010-08-09 21:15:40 +0800776 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200777 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200778 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100779 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800780
Harald Welteec20ba42010-05-13 12:18:49 +0200781 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
782 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800783 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200784 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800785 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800786 return -EIO;
787 }
788
789 nsh->pdu_type = NS_PDUT_UNITDATA;
790 /* spare octet in data[0] */
791 nsh->data[1] = bvci >> 8;
792 nsh->data[2] = bvci & 0xff;
793
Harald Welte24a655f2010-04-30 19:54:29 +0200794 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800795}
796
797/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200798static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800799{
800 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200801 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800802
Harald Weltec1402a62010-05-12 11:48:44 +0200803 if (nsvc->state & NSE_S_BLOCKED)
804 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
805 0, msg);
806
Harald Welte9ba50052010-03-14 15:45:01 +0800807 /* spare octet in data[0] */
808 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200809 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200810 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800811
812 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200813 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800814}
815
816/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200817static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800818{
Harald Weltef030b212010-04-26 19:18:54 +0200819 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800820 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200821 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800822 int rc;
823
Harald Welte41337832010-05-16 00:24:26 +0200824 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800825
Harald Weltebd33f3d2010-05-30 17:19:38 +0200826 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
827 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200828 if (rc < 0) {
829 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
830 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
831 "Error during TLV Parse\n", nsvc->nsei);
832 return rc;
833 }
Harald Welte9ba50052010-03-14 15:45:01 +0800834
835 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200836 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800837 return -EINVAL;
838 }
839
840 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200841 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800842
843 return 0;
844}
845
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200846/* Replace a nsvc object with another based on NSVCI.
847 * This function replaces looks for a NSVC with the given NSVCI and replaces it
848 * if possible and necessary. If replaced, the former value of *nsvc is
849 * returned in *old_nsvc.
850 * \return != 0 if *nsvc points to a matching NSVC.
851 */
852static int gprs_nsvc_replace_if_found(uint16_t nsvci,
853 struct gprs_nsvc **nsvc,
854 struct gprs_nsvc **old_nsvc)
855{
856 struct gprs_nsvc *matching_nsvc;
857
858 if ((*nsvc)->nsvci == nsvci) {
859 *old_nsvc = NULL;
860 return 1;
861 }
862
863 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
864
865 if (!matching_nsvc)
866 return 0;
867
868 /* The NS-VCI is already used by this NS-VC */
869
870 char *old_peer;
871
872 /* Exchange the NS-VC objects */
873 *old_nsvc = *nsvc;
874 *nsvc = matching_nsvc;
875
876 /* Do logging */
877 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
878 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
879 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
880
881 talloc_free(old_peer);
882
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200883 return 1;
884}
885
Harald Welte9ba50052010-03-14 15:45:01 +0800886/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200887static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800888{
889 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800890 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200891 uint8_t cause;
892 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200893 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800894 int rc;
895
Harald Weltebd33f3d2010-05-30 17:19:38 +0200896 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
897 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200898 if (rc < 0) {
899 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200900 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200901 return rc;
902 }
Harald Welte9ba50052010-03-14 15:45:01 +0800903
904 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
905 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
906 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200907 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200908 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800909 return -EINVAL;
910 }
911
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200912 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +0200913 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
914 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800915
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200916 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
917 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
918 nsei, nsvci, gprs_ns_cause_str(cause));
919
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200920 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
921 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200922 /* The incoming RESET doesn't match the NSVCI. Send an
923 * appropriate RESET_ACK and ignore the RESET.
924 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
925 */
926 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
927 NS_PDUT_RESET,
928 NS_IE_VCI);
929 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
930 gprs_ns_tx_reset_ack(*nsvc);
931 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200932 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200933
934 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200935 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
936 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
937 "at %s\n",
938 nsvci, (*nsvc)->nsvci,
939 gprs_ns_ll_str(*nsvc));
940 orig_nsvc = *nsvc;
941 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200942 (*nsvc)->nsei = nsei;
943 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200944 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200945
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200946 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
947 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
948 /* The incoming RESET doesn't match the NSEI. Send an
949 * appropriate RESET_ACK and ignore the RESET.
950 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
951 */
952 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
953 NS_PDUT_RESET,
954 NS_IE_NSEI);
955 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200956 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200957 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200958 return 0;
959 }
960
961 /* NSEI has changed */
962 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
963 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200964 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200965
Harald Weltec1402a62010-05-12 11:48:44 +0200966 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +0200967 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +0200968
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200969 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200970 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200971 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200972
973 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200974 gprs_ns_ll_copy(*nsvc, orig_nsvc);
975 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200976 } else {
977 (*nsvc)->nsei = nsei;
978 (*nsvc)->nsvci = nsvci;
979 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +0100980 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
981 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200982 }
Harald Welte9ba50052010-03-14 15:45:01 +0800983
Harald Welte834f26d2010-05-11 06:20:54 +0200984 /* inform interested parties about the fact that this NSVC
985 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200986 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200987
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200988 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200989
990 /* start the test procedure */
Harald Welte4091f922019-02-19 11:36:52 +0100991 gprs_ns_tx_alive(*nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200992 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200993
994 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800995}
996
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200997static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
998{
999 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1000 struct tlv_parsed tp;
1001 uint16_t nsvci, nsei;
1002 struct gprs_nsvc *orig_nsvc = NULL;
1003 int rc;
1004
1005 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1006 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1007 if (rc < 0) {
1008 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
1009 "Error during TLV Parse\n", (*nsvc)->nsei);
1010 return rc;
1011 }
1012
1013 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
1014 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1015 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +02001016 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001017 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001018 return -EINVAL;
1019 }
1020
Harald Weltebfe62e52017-05-15 12:48:30 +02001021 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1022 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001023
1024 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
1025 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1026 nsei, nsvci);
1027
1028 if (!((*nsvc)->state & NSE_S_RESET)) {
1029 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1030 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1031 */
1032 LOGP(DNS, LOGL_ERROR,
1033 "NS RESET ACK Discarding unexpected message for "
1034 "NS-VCI %d from SGSN NSEI=%d\n",
1035 nsvci, nsei);
1036 return 0;
1037 }
1038
1039 if (!(*nsvc)->nsvci_is_valid) {
1040 LOGP(DNS, LOGL_NOTICE,
1041 "NS RESET ACK Uninitialised NS-VC (%u) for "
1042 "NS-VCI %d, NSEI=%d from %s\n",
1043 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1044 return -EINVAL;
1045 }
1046
1047 if ((*nsvc)->nsvci != nsvci) {
1048 /* NS-VCI has changed */
1049
1050 /* if !0, use another NSVC object that matches the NSVCI */
1051 int use_other_nsvc;
1052
1053 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001054 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1055 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001056
1057 if (use_other_nsvc)
1058 /* Update *nsvc to point to the right NSVC object */
1059 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1060 &orig_nsvc);
1061
1062 if (!use_other_nsvc) {
1063 /* The incoming RESET_ACK doesn't match the NSVCI.
1064 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1065 */
1066 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1067 NS_PDUT_RESET_ACK,
1068 NS_IE_VCI);
1069 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1070 LOGP(DNS, LOGL_ERROR,
1071 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1072 "from %s\n",
1073 nsvci,
1074 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1075 nsei, gprs_ns_ll_str(*nsvc));
1076 return -EINVAL;
1077 }
1078
1079 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001080 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001081 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1082
1083 /* Update the ll info fields */
1084 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1085 gprs_ns_ll_clear(orig_nsvc);
1086 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001087 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1088 /* The incoming RESET_ACK doesn't match the NSEI.
1089 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1090 */
1091 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1092 NS_PDUT_RESET_ACK,
1093 NS_IE_NSEI);
1094 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1095 LOGP(DNS, LOGL_ERROR,
1096 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1097 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1098 return -EINVAL;
1099 }
1100
1101 /* NSEI has changed */
1102 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1103 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001104 }
1105
1106 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001107 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1108 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001109 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1110 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1111 /* stop RESET timer */
1112 osmo_timer_del(&(*nsvc)->timer);
1113 }
1114 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte4091f922019-02-19 11:36:52 +01001115 rc = gprs_ns_tx_alive(*nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001116 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1117
1118 return rc;
1119}
1120
Harald Welte834f26d2010-05-11 06:20:54 +02001121static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1122{
1123 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1124 struct tlv_parsed tp;
1125 uint8_t *cause;
1126 int rc;
1127
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001128 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001129
Maxc513c0f2017-10-23 15:11:14 +02001130 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001131
Harald Weltebd33f3d2010-05-30 17:19:38 +02001132 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1133 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001134 if (rc < 0) {
1135 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1136 "Error during TLV Parse\n", nsvc->nsei);
1137 return rc;
1138 }
Harald Welte834f26d2010-05-11 06:20:54 +02001139
1140 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1141 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001142 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001143 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001144 return -EINVAL;
1145 }
1146
1147 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1148 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1149
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001150 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001151 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001152
Harald Welte9ef2e152019-02-19 23:42:04 +01001153 return gprs_ns_tx_block_ack(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001154}
1155
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001156int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1157 struct gprs_nsvc *fallback_nsvc,
1158 struct gprs_nsvc **new_nsvc);
1159
1160int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001161 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001162
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001163/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001164 * \param nsi NS instance to which the data belongs
1165 * \param[in] msg message buffer containing newly-received data
1166 * \param[in] saddr socketaddr from which data was received
1167 * \param[in] ll link-layer type in which data was received
1168 * \returns 0 in case of success, < 0 in case of error
1169 *
1170 * This is the main entry point int othe NS imlementation where frames
1171 * from the underlying transport (normally UDP) enter.
1172 */
Harald Weltef030b212010-04-26 19:18:54 +02001173int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001174 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001175{
Harald Weltef030b212010-04-26 19:18:54 +02001176 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001177 int rc = 0;
1178
Harald Weltef030b212010-04-26 19:18:54 +02001179 /* look up the NSVC based on source address */
1180 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001181
Harald Weltef030b212010-04-26 19:18:54 +02001182 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001183 struct gprs_nsvc *fallback_nsvc;
1184
1185 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001186 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001187 fallback_nsvc->ip.bts_addr = *saddr;
1188 fallback_nsvc->ll = ll;
1189
1190 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1191
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001192 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001193 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001194
1195 rc = 0;
1196 }
1197
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001198 if (nsvc)
1199 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001200
1201 return rc;
1202}
1203
Maxcbf5cdf2017-10-24 18:18:55 +02001204const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001205{
1206 static char buf[80];
Max95308592017-10-24 15:54:28 +02001207
1208 switch(nsvc->ll) {
1209 case GPRS_NS_LL_UDP:
1210 snprintf(buf, sizeof(buf), "%s:%u",
1211 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1212 break;
1213 case GPRS_NS_LL_FR_GRE:
1214 snprintf(buf, sizeof(buf), "%s:%u",
1215 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1216 break;
1217 default:
1218 buf[0] = '\0';
1219 break;
1220 }
1221
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001222 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001223
1224 return buf;
1225}
1226
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001227void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1228{
1229 nsvc->ll = other->ll;
1230
1231 switch (nsvc->ll) {
1232 case GPRS_NS_LL_UDP:
1233 nsvc->ip = other->ip;
1234 break;
1235 case GPRS_NS_LL_FR_GRE:
1236 nsvc->frgre = other->frgre;
1237 break;
1238 default:
1239 break;
1240 }
1241}
1242
1243void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1244{
1245 switch (nsvc->ll) {
1246 case GPRS_NS_LL_UDP:
1247 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1248 nsvc->ip.bts_addr.sin_port = 0;
1249 break;
1250 case GPRS_NS_LL_FR_GRE:
1251 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1252 nsvc->frgre.bts_addr.sin_port = 0;
1253 break;
1254 default:
1255 break;
1256 }
1257}
1258
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001259/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001260 * \param nsi NS instance to which the data belongs
1261 * \param[in] msg message buffer containing newly-received data
1262 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001263 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001264 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1265 * been created or found
1266 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1267 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1268 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1269 * has been created and registered, and GPRS_NS_CS_FOUND if an
1270 * existing NS-VC object has been found with the same NSEI.
1271 *
1272 * This contains the initial NS automaton state (NS-VC not yet attached).
1273 */
1274int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1275 struct gprs_nsvc *fallback_nsvc,
1276 struct gprs_nsvc **new_nsvc)
1277{
1278 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1279 struct gprs_nsvc *existing_nsvc;
1280
1281 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001282 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001283 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001284
1285 int rc;
1286
1287 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001288 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001289 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1290 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001291 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001292 return GPRS_NS_CS_SKIPPED;
1293 }
1294
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001295 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1296 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1297 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1298 "for non-existing NS-VC\n",
1299 gprs_ns_ll_str(fallback_nsvc));
1300 return GPRS_NS_CS_SKIPPED;
1301 }
1302
1303 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1304 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1305 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1306 "for non-existing NS-VC\n",
1307 gprs_ns_ll_str(fallback_nsvc));
1308 return GPRS_NS_CS_SKIPPED;
1309 }
1310
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001311 /* Only the RESET procedure creates a new NSVC */
1312 if (nsh->pdu_type != NS_PDUT_RESET) {
1313 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001314 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001315 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001316 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001317 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001318 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001319 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001320 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001321
1322 rc = gprs_ns_tx_status(fallback_nsvc,
1323 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1324 if (rc < 0) {
1325 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001326 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001327 return rc;
1328 }
1329 return GPRS_NS_CS_REJECTED;
1330 }
1331
1332 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1333 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1334 if (rc < 0) {
1335 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1336 "TLV Parse\n", rc);
1337 return rc;
1338 }
1339 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1340 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1341 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001342 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001343 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001344 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001345 return -EINVAL;
1346 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001347 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1348 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001349 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001350 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001351 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001352 if (!existing_nsvc) {
1353 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001354 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001355 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001356 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001357 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001358 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001359
1360 return GPRS_NS_CS_CREATED;
1361 }
1362
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001363 /* Check NSEI */
1364 if (existing_nsvc->nsei != nsei) {
1365 LOGP(DNS, LOGL_NOTICE,
1366 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1367 nsvci, existing_nsvc->nsei, nsei);
1368
1369 /* Override old NSEI */
1370 existing_nsvc->nsei = nsei;
1371
1372 /* Do statistics */
1373 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1374 }
1375
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001376 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001377 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001378 return GPRS_NS_CS_FOUND;
1379}
1380
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001381/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001382 * \param nsi NS instance to which the data belongs
1383 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001384 * \param[inout] nsvc refers to the virtual connection, may be modified when
1385 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001386 * \returns 0 in case of success, < 0 in case of error
1387 *
1388 * This contains the main NS automaton.
1389 */
1390int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001391 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001392{
1393 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1394 int rc = 0;
1395
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001396 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001397
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001398 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001399
Harald Welte144e0292010-05-13 11:45:07 +02001400 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001401 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1402 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001403
Harald Welte9ba50052010-03-14 15:45:01 +08001404 switch (nsh->pdu_type) {
1405 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001406 /* If we're dead and blocked and suddenly receive a
1407 * NS-ALIVE out of the blue, we might have been re-started
1408 * and should send a NS-RESET to make sure everything recovers
1409 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001410 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001411 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1412 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001413 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001414 break;
1415 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001416 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001417 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001418 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001419 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001420 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1421 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001422 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001423 if ((*nsvc)->state & NSE_S_BLOCKED)
1424 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001425 }
Harald Welte9ba50052010-03-14 15:45:01 +08001426 break;
1427 case NS_PDUT_UNITDATA:
1428 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001429 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001430 break;
1431 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001432 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001433 break;
1434 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001435 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001436 break;
1437 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001438 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001439 break;
1440 case NS_PDUT_UNBLOCK:
1441 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001442 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001443 ns_mark_unblocked(*nsvc);
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001444 /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001445 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Stefan Sperling797558e2018-11-19 17:18:41 +01001446 if (rc < 0)
1447 break;
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001448 /*
1449 * UNBLOCK_ACK has been transmitted.
1450 * Signal handlers may send additional messages following UNBLOCK_ACK under
1451 * the assumption that NS is now in UNBLOCKED state at our peer's end.
1452 */
Stefan Sperling797558e2018-11-19 17:18:41 +01001453 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001454 break;
1455 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001456 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001457 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001458 ns_set_state(*nsvc, NSE_S_ALIVE);
1459 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001460 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001461 break;
1462 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001463 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001464 break;
1465 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001466 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001467 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001468 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001469 break;
1470 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001471 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001472 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001473 rc = -EINVAL;
1474 break;
1475 }
1476 return rc;
1477}
1478
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001479/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001480 * \param[in] cb Call-back function for incoming BSSGP data
1481 * \returns dynamically allocated gprs_ns_inst
1482 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001483struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001484{
Harald Welte4fcdd762012-06-16 16:40:42 +08001485 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001486
1487 nsi->cb = cb;
1488 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001489 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1490 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1491 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1492 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1493 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1494 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1495 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001496
Harald Welte60cc6ac2010-05-13 14:20:56 +02001497 /* Create the dummy NSVC that we use for sending
1498 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001499 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001500 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001501 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001502 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001503
Harald Welte3771d092010-04-30 20:26:32 +02001504 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001505}
1506
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001507void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001508{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001509 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001510
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001511 gprs_nsvc_delete(nsi->unknown_nsvc);
1512
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001513 /* delete all NSVCs and clear their timers */
1514 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1515 gprs_nsvc_delete(nsvc);
1516
1517 /* close socket and unregister */
1518 if (nsi->nsip.fd.data) {
1519 close(nsi->nsip.fd.fd);
1520 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001521 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001522 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001523}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001524
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001525/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001526 * \param nsi gprs_ns_inst that is to be destroyed
1527 *
1528 * This function releases all resources associated with the
1529 * NS-instance.
1530 */
1531void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1532{
1533 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001534 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001535 talloc_free(nsi);
1536}
1537
1538
1539/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1540 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1541
1542/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001543static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001544 struct sockaddr_in *saddr)
1545{
Harald Welteba4c6662010-05-19 15:38:10 +02001546 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001547 int ret = 0;
1548 socklen_t saddr_len = sizeof(*saddr);
1549
1550 if (!msg) {
1551 *error = -ENOMEM;
1552 return NULL;
1553 }
1554
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001555 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001556 (struct sockaddr *)saddr, &saddr_len);
1557 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001558 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001559 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001560 msgb_free(msg);
1561 *error = ret;
1562 return NULL;
1563 } else if (ret == 0) {
1564 msgb_free(msg);
1565 *error = ret;
1566 return NULL;
1567 }
1568
1569 msg->l2h = msg->data;
1570 msgb_put(msg, ret);
1571
1572 return msg;
1573}
1574
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001575static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001576{
1577 int error;
1578 struct sockaddr_in saddr;
1579 struct gprs_ns_inst *nsi = bfd->data;
1580 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1581
1582 if (!msg)
1583 return error;
1584
Harald Welteb3ee2652010-05-19 14:38:50 +02001585 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001586
1587 msgb_free(msg);
1588
1589 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001590}
1591
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001592static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001593{
1594 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1595 return -EIO;
1596}
1597
Harald Welteb3ee2652010-05-19 14:38:50 +02001598static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001599{
1600 int rc;
1601 struct gprs_ns_inst *nsi = nsvc->nsi;
1602 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1603
1604 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1605 (struct sockaddr *)daddr, sizeof(*daddr));
1606
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001607 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001608
1609 return rc;
1610}
1611
1612/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001613static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001614{
1615 int rc = 0;
1616
1617 if (what & BSC_FD_READ)
1618 rc = handle_nsip_read(bfd);
1619 if (what & BSC_FD_WRITE)
1620 rc = handle_nsip_write(bfd);
1621
1622 return rc;
1623}
1624
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001625/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01001626 * \param[in] nsi NS protocol instance to listen
1627 * \returns >=0 (fd) in case of success, negative in case of error
1628 *
1629 * A call to this function will create a UDP socket bound to the port
1630 * number and IP address specified in the NS protocol instance. The
1631 * file descriptor of the socket will be stored in nsi->nsip.fd.
1632 */
Harald Welte7fb05232010-05-19 15:09:09 +02001633int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001634{
Harald Welte73952e32012-06-16 14:59:56 +08001635 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001636 struct in_addr remote;
1637 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02001638 int ret;
1639
Harald Weltebfe62e52017-05-15 12:48:30 +02001640 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02001641 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08001642
1643 nsi->nsip.fd.cb = nsip_fd_cb;
1644 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001645
1646 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
1647 /* connect to ensure only we only accept packets from the
1648 * configured remote end/peer */
1649 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
1650 ret =
1651 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1652 IPPROTO_UDP, inet_ntoa(in),
1653 nsi->nsip.local_port, remote_str,
1654 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
1655
1656 LOGP(DNS, LOGL_NOTICE,
1657 "Listening for nsip packets from %s:%u on %s:%u\n",
1658 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
1659 } else {
1660 /* Accept UDP packets from any source IP/Port */
1661 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1662 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND);
1663
1664 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
1665 }
1666
Stefan Sperlingf6538212018-10-10 16:09:32 +02001667 if (ret < 0) {
1668 nsi->nsip.fd.cb = NULL;
1669 nsi->nsip.fd.data = NULL;
Harald Weltef030b212010-04-26 19:18:54 +02001670 return ret;
Stefan Sperlingf6538212018-10-10 16:09:32 +02001671 }
Harald Weltef030b212010-04-26 19:18:54 +02001672
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001673 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1674 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1675 if (ret < 0)
1676 LOGP(DNS, LOGL_ERROR,
1677 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1678 nsi->nsip.dscp, ret, errno);
1679
Maxa19c1262017-10-20 15:39:45 +02001680 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 +02001681
1682 return ret;
1683}
Harald Welte3771d092010-04-30 20:26:32 +02001684
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001685/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01001686 * \param[in] nsvc NS-VC in which to start the procedure
1687 * \param[in] cause Numeric NS cause value
1688 *
1689 * This is a high-level function initiating a NS-RESET procedure. It
1690 * will not only send a NS-RESET, but also set the state to BLOCKED and
1691 * start the Tns-reset timer.
1692 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001693int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001694{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001695 int rc;
1696
Harald Welteb1020d52010-05-25 22:17:30 +02001697 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1698 nsvc->nsei);
1699
Harald Welte731d1fc2010-05-14 11:53:08 +00001700 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02001701 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001702
Harald Welte731d1fc2010-05-14 11:53:08 +00001703 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001704 rc = gprs_ns_tx_reset(nsvc, cause);
1705 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001706 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1707 nsvc->nsei);
1708 }
1709 /* Start Tns-reset */
1710 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001711
1712 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001713}
1714
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001715/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01001716 * \param nsi NS-instance
1717 * \param[in] dest Destination IP/Port
1718 * \param[in] nsei NSEI of the to-be-established NS-VC
1719 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1720 * \returns struct gprs_nsvc representing the new NS-VC
1721 *
1722 * This function will establish a single NS/UDP/IP connection in uplink
1723 * (BSS to SGSN) direction.
1724 */
Harald Weltef5430362012-06-17 12:25:53 +08001725struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001726 struct sockaddr_in *dest, uint16_t nsei,
1727 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001728{
1729 struct gprs_nsvc *nsvc;
1730
1731 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001732 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001733 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001734 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001735 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001736 nsvc->remote_end_is_sgsn = 1;
1737
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001738 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1739 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001740}
Harald Weltea9f23c82011-11-23 15:01:31 +01001741
Harald Weltecca49632012-06-16 17:45:59 +08001742void gprs_ns_set_log_ss(int ss)
1743{
1744 DNS = ss;
1745}
1746
Daniel Willmannf1318fe2018-05-29 20:55:18 +02001747/*! Append the nsvc state to a talloc string
1748 * \param s The string to append to (allocated with talloc)
1749 * \param[in] nsvc The NS-VC to print the state of
1750 * \returns The new string with state information appended to it
1751 *
1752 * This function will append a comma-separated state of the NS-VC to the
1753 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
1754 */
1755char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
1756{
1757 s = talloc_asprintf_append(s,
1758 "%u,%u,%s,%s,%s,%s,%s\n",
1759 nsvc->nsei, nsvc->nsvci,
1760 NS_DESC_A(nsvc->state),
1761 NS_DESC_B(nsvc->state),
1762 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
1763 NS_DESC_A(nsvc->remote_state),
1764 NS_DESC_B(nsvc->remote_state));
1765
1766 return s;
1767}
1768
Harald Welte96e2a002017-06-12 21:44:18 +02001769/*! @} */