blob: 976a101856bbe975c364c4865f38d86ae7f16c39 [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" },
Harald Welte47cf21f2019-02-23 13:42:06 +0100368 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
369 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
370 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
371 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
372 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
373 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
374 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
Harald Welte2577d412010-05-02 09:37:45 +0200375 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800376};
377
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200378/*! Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200379const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800380{
Harald Welte2577d412010-05-02 09:37:45 +0200381 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800382}
383
Harald Welte24a655f2010-04-30 19:54:29 +0200384static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200385extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200386
Harald Welte24a655f2010-04-30 19:54:29 +0200387static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800388{
Harald Weltef030b212010-04-26 19:18:54 +0200389 int ret;
390
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100391 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200392
Harald Welte144e0292010-05-13 11:45:07 +0200393 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200394 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
395 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200396
Harald Welteb3ee2652010-05-19 14:38:50 +0200397 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200398 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200399 ret = nsip_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 UDP: %s\n",
403 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200404 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200405 case GPRS_NS_LL_FR_GRE:
406 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200407 if (ret < 0)
408 LOGP(DNS, LOGL_INFO,
409 "failed to send NS message via FR/GRE: %s\n",
410 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200411 break;
Harald Weltef030b212010-04-26 19:18:54 +0200412 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200413 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200414 msgb_free(msg);
415 ret = -EIO;
416 break;
417 }
418 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800419}
420
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200421static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800422{
Harald Welteba4c6662010-05-19 15:38:10 +0200423 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800424 struct gprs_ns_hdr *nsh;
425
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100426 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200427
Harald Welte9ba50052010-03-14 15:45:01 +0800428 if (!msg)
429 return -ENOMEM;
430
Harald Welte144e0292010-05-13 11:45:07 +0200431 msg->l2h = msgb_put(msg, sizeof(*nsh));
432 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800433
434 nsh->pdu_type = pdu_type;
435
Harald Welte24a655f2010-04-30 19:54:29 +0200436 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800437}
438
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200439/*! Transmit a NS-RESET on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100440 * \param[in] nsvc NS-VC used for transmission
441 * \paam[in] cause Numeric NS cause value
442 */
Harald Welte834f26d2010-05-11 06:20:54 +0200443int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200444{
Harald Welteba4c6662010-05-19 15:38:10 +0200445 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200446 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200447 uint16_t nsvci = osmo_htons(nsvc->nsvci);
448 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200449
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100450 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200451
Harald Welte69a4cf22010-05-03 20:55:10 +0200452 if (!msg)
453 return -ENOMEM;
454
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000455 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
456 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
457
Harald Welte144e0292010-05-13 11:45:07 +0200458 msg->l2h = msgb_put(msg, sizeof(*nsh));
459 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200460 nsh->pdu_type = NS_PDUT_RESET;
461
462 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
463 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
464 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
465
466 return gprs_ns_tx(nsvc, msg);
467
468}
469
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200470/*! Transmit a NS-STATUS on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100471 * \param[in] nsvc NS-VC to be used for transmission
472 * \param[in] cause Numeric NS cause value
473 * \param[in] bvci BVCI to be reset within NSVC
474 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200475int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
476 uint16_t bvci, struct msgb *orig_msg)
477{
Harald Welteba4c6662010-05-19 15:38:10 +0200478 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200479 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200480 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200481
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100482 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200483
Harald Weltebfe62e52017-05-15 12:48:30 +0200484 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200485
486 if (!msg)
487 return -ENOMEM;
488
Harald Welte90af1942010-05-15 23:02:24 +0200489 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000490 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
491
Harald Welte144e0292010-05-13 11:45:07 +0200492 msg->l2h = msgb_put(msg, sizeof(*nsh));
493 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200494 nsh->pdu_type = NS_PDUT_STATUS;
495
496 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
497
498 /* Section 9.2.7.1: Static conditions for NS-VCI */
499 if (cause == NS_CAUSE_NSVC_BLOCKED ||
500 cause == NS_CAUSE_NSVC_UNKNOWN)
501 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
502
503 /* Section 9.2.7.2: Static conditions for NS PDU */
504 switch (cause) {
505 case NS_CAUSE_SEM_INCORR_PDU:
506 case NS_CAUSE_PDU_INCOMP_PSTATE:
507 case NS_CAUSE_PROTO_ERR_UNSPEC:
508 case NS_CAUSE_INVAL_ESSENT_IE:
509 case NS_CAUSE_MISSING_ESSENT_IE:
510 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
511 orig_msg->l2h);
512 break;
513 default:
514 break;
515 }
516
517 /* Section 9.2.7.3: Static conditions for BVCI */
518 if (cause == NS_CAUSE_BVCI_UNKNOWN)
519 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
520
521 return gprs_ns_tx(nsvc, msg);
522}
523
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200524/*! Transmit a NS-BLOCK on a tiven NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100525 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
526 * \param[in] cause Numeric NS Cause value
527 * \returns 0 in case of success
528 */
Harald Welte834f26d2010-05-11 06:20:54 +0200529int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
530{
Harald Welteba4c6662010-05-19 15:38:10 +0200531 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200532 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200533 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200534
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100535 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200536
Harald Welte834f26d2010-05-11 06:20:54 +0200537 if (!msg)
538 return -ENOMEM;
539
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000540 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
541 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
542
Harald Welte834f26d2010-05-11 06:20:54 +0200543 /* be conservative and mark it as blocked even now! */
Maxc513c0f2017-10-23 15:11:14 +0200544 ns_mark_blocked(nsvc);
Harald Weltec51c23c2010-05-13 12:55:20 +0200545 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200546
Harald Welte144e0292010-05-13 11:45:07 +0200547 msg->l2h = msgb_put(msg, sizeof(*nsh));
548 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200549 nsh->pdu_type = NS_PDUT_BLOCK;
550
551 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
552 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
553
554 return gprs_ns_tx(nsvc, msg);
555}
556
Harald Welte9ef2e152019-02-19 23:42:04 +0100557/*! Transmit a NS-BLOCK-ACK on a given NS-VC
558 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
559 * \returns 0 in case of success
560 */
561static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc)
562{
563 struct msgb *msg;
564 struct gprs_ns_hdr *nsh;
565 uint16_t nsvci = osmo_htons(nsvc->nsvci);
566
567 log_set_context(LOG_CTX_GB_NSVC, nsvc);
568
569 msg = gprs_ns_msgb_alloc();
570 if (!msg)
571 return -ENOMEM;
572
573 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci);
574
575 /* be conservative and mark it as blocked even now! */
576 msg->l2h = msgb_put(msg, sizeof(*nsh));
577 nsh = (struct gprs_ns_hdr *) msg->l2h;
578 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
579
580 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
581
582 return gprs_ns_tx(nsvc, msg);
583}
584
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200585/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100586 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
587 * \returns 0 in case of success
588 */
Harald Welte834f26d2010-05-11 06:20:54 +0200589int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
590{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100591 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000592 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
593 nsvc->nsei, nsvc->nsvci);
594
Harald Welte834f26d2010-05-11 06:20:54 +0200595 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
596}
597
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200598/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100599 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
600 * \returns 0 in case of success
601 */
Harald Welteb983c312010-05-12 12:11:45 +0000602int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
603{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100604 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000605 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
606 nsvc->nsei, nsvc->nsvci);
607
608 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
609}
610
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200611/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100612 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
613 * \returns 0 in case of success
614 */
Harald Welteb983c312010-05-12 12:11:45 +0000615int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
616{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100617 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000618 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
619 nsvc->nsei, nsvc->nsvci);
620
621 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
622}
623
Harald Weltefe4ab902010-05-12 17:19:53 +0000624static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
625 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
626 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
627 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200628};
629
Harald Welte7c24b9e2010-05-12 12:21:52 +0000630static const struct value_string timer_mode_strs[] = {
631 { NSVC_TIMER_TNS_RESET, "tns-reset" },
632 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
633 { NSVC_TIMER_TNS_TEST, "tns-test" },
634 { 0, NULL }
635};
636
Harald Welte69a4cf22010-05-03 20:55:10 +0200637static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
638{
Harald Weltefe4ab902010-05-12 17:19:53 +0000639 enum ns_timeout tout = timer_mode_tout[mode];
640 unsigned int seconds = nsvc->nsi->timeout[tout];
641
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100642 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000643 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
644 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000645 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100646
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200647 if (osmo_timer_pending(&nsvc->timer))
648 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200649
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200650 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200651 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200652 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200653}
654
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200655static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
656{
657 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200658 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200659 timersub(&now, &nsvc->timer_started, &elapsed);
660
661 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
662}
663
Harald Welte80405452010-05-03 20:16:13 +0200664static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800665{
666 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000667 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
668 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800669
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100670 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000671 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
672 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000673 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100674
Harald Welte80405452010-05-03 20:16:13 +0200675 switch (nsvc->timer_mode) {
676 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800677 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200678 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800679 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000680 if (nsvc->alive_retries >
681 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800682 /* mark as dead and blocked */
Maxc513c0f2017-10-23 15:11:14 +0200683 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltec51c23c2010-05-13 12:55:20 +0200684 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
685 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200686 LOGP(DNS, LOGL_NOTICE,
687 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200688 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000689 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200690 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
691 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800692 return;
693 }
Harald Welteb983c312010-05-12 12:11:45 +0000694 /* Tns-test case: send NS-ALIVE PDU */
695 gprs_ns_tx_alive(nsvc);
696 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200697 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200698 break;
699 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800700 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000701 gprs_ns_tx_alive(nsvc);
702 /* start Tns-alive timer (transition into faster
703 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000704 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200705 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
706 break;
707 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200708 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100709 if (!(nsvc->state & NSE_S_RESET))
710 LOGP(DNS, LOGL_NOTICE,
711 "NSEI=%u Reset timed out but RESET flag is not set\n",
712 nsvc->nsei);
713 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200714 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200715 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200716 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200717 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200718 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200719 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200720 case _NSVC_TIMER_NR:
721 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800722 }
Harald Welte9ba50052010-03-14 15:45:01 +0800723}
724
725/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800726static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800727{
Harald Welteba4c6662010-05-19 15:38:10 +0200728 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800729 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200730 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800731
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100732 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800733 if (!msg)
734 return -ENOMEM;
735
Harald Weltebfe62e52017-05-15 12:48:30 +0200736 nsvci = osmo_htons(nsvc->nsvci);
737 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800738
Harald Welte144e0292010-05-13 11:45:07 +0200739 msg->l2h = msgb_put(msg, sizeof(*nsh));
740 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800741
742 nsh->pdu_type = NS_PDUT_RESET_ACK;
743
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200744 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200745 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800746
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200747 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
748 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800749
Harald Welte24a655f2010-04-30 19:54:29 +0200750 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800751}
752
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200753/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +0100754 * \param[in] nsi NS-instance on which we shall transmit
755 * \param[in] msg struct msgb to be trasnmitted
756 *
757 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
758 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
759 * header for the NS-UNITDATA message type and sends it off.
760 *
761 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
762 */
Harald Welte24a655f2010-04-30 19:54:29 +0200763int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800764{
Harald Welte24a655f2010-04-30 19:54:29 +0200765 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800766 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200767 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200768
Jacob Erlbeck69017152013-10-14 12:03:54 +0200769 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200770 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200771 int rc;
772 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
773 LOGP(DNS, LOGL_ERROR,
774 "All NS-VCs for NSEI %u are either dead or blocked!\n",
775 msgb_nsei(msg));
776 rc = -EBUSY;
777 } else {
778 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
779 "to NS-VC!\n", msgb_nsei(msg));
780 rc = -EINVAL;
781 }
782
Harald Weltef47df642010-08-09 21:15:40 +0800783 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200784 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200785 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100786 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800787
Harald Welteec20ba42010-05-13 12:18:49 +0200788 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
789 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800790 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200791 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800792 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800793 return -EIO;
794 }
795
796 nsh->pdu_type = NS_PDUT_UNITDATA;
797 /* spare octet in data[0] */
798 nsh->data[1] = bvci >> 8;
799 nsh->data[2] = bvci & 0xff;
800
Harald Welte24a655f2010-04-30 19:54:29 +0200801 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800802}
803
804/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200805static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800806{
807 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200808 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800809
Harald Weltec1402a62010-05-12 11:48:44 +0200810 if (nsvc->state & NSE_S_BLOCKED)
811 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
812 0, msg);
813
Harald Welte9ba50052010-03-14 15:45:01 +0800814 /* spare octet in data[0] */
815 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200816 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200817 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800818
819 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200820 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800821}
822
823/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200824static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800825{
Harald Weltef030b212010-04-26 19:18:54 +0200826 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800827 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200828 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800829 int rc;
830
Harald Welte41337832010-05-16 00:24:26 +0200831 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800832
Harald Weltebd33f3d2010-05-30 17:19:38 +0200833 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
834 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200835 if (rc < 0) {
836 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
837 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
838 "Error during TLV Parse\n", nsvc->nsei);
839 return rc;
840 }
Harald Welte9ba50052010-03-14 15:45:01 +0800841
842 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200843 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800844 return -EINVAL;
845 }
846
847 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200848 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800849
850 return 0;
851}
852
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200853/* Replace a nsvc object with another based on NSVCI.
854 * This function replaces looks for a NSVC with the given NSVCI and replaces it
855 * if possible and necessary. If replaced, the former value of *nsvc is
856 * returned in *old_nsvc.
857 * \return != 0 if *nsvc points to a matching NSVC.
858 */
859static int gprs_nsvc_replace_if_found(uint16_t nsvci,
860 struct gprs_nsvc **nsvc,
861 struct gprs_nsvc **old_nsvc)
862{
863 struct gprs_nsvc *matching_nsvc;
864
865 if ((*nsvc)->nsvci == nsvci) {
866 *old_nsvc = NULL;
867 return 1;
868 }
869
870 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
871
872 if (!matching_nsvc)
873 return 0;
874
875 /* The NS-VCI is already used by this NS-VC */
876
877 char *old_peer;
878
879 /* Exchange the NS-VC objects */
880 *old_nsvc = *nsvc;
881 *nsvc = matching_nsvc;
882
883 /* Do logging */
884 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
885 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
886 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
887
888 talloc_free(old_peer);
889
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200890 return 1;
891}
892
Harald Welte9ba50052010-03-14 15:45:01 +0800893/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200894static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800895{
896 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800897 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200898 uint8_t cause;
899 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200900 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800901 int rc;
902
Harald Weltebd33f3d2010-05-30 17:19:38 +0200903 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
904 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200905 if (rc < 0) {
906 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200907 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200908 return rc;
909 }
Harald Welte9ba50052010-03-14 15:45:01 +0800910
911 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
912 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
913 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200914 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200915 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800916 return -EINVAL;
917 }
918
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200919 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +0200920 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
921 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800922
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200923 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
924 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
925 nsei, nsvci, gprs_ns_cause_str(cause));
926
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200927 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
928 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200929 /* The incoming RESET doesn't match the NSVCI. Send an
930 * appropriate RESET_ACK and ignore the RESET.
931 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
932 */
933 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
934 NS_PDUT_RESET,
935 NS_IE_VCI);
936 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
937 gprs_ns_tx_reset_ack(*nsvc);
938 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200939 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200940
941 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200942 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
943 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
944 "at %s\n",
945 nsvci, (*nsvc)->nsvci,
946 gprs_ns_ll_str(*nsvc));
947 orig_nsvc = *nsvc;
948 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200949 (*nsvc)->nsei = nsei;
950 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200951 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200952
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200953 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
954 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
955 /* The incoming RESET doesn't match the NSEI. Send an
956 * appropriate RESET_ACK and ignore the RESET.
957 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
958 */
959 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
960 NS_PDUT_RESET,
961 NS_IE_NSEI);
962 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200963 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200964 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200965 return 0;
966 }
967
968 /* NSEI has changed */
969 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
970 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200971 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200972
Harald Weltec1402a62010-05-12 11:48:44 +0200973 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +0200974 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +0200975
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200976 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200977 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200978 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200979
980 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200981 gprs_ns_ll_copy(*nsvc, orig_nsvc);
982 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200983 } else {
984 (*nsvc)->nsei = nsei;
985 (*nsvc)->nsvci = nsvci;
986 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +0100987 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
988 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200989 }
Harald Welte9ba50052010-03-14 15:45:01 +0800990
Harald Welte834f26d2010-05-11 06:20:54 +0200991 /* inform interested parties about the fact that this NSVC
992 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200993 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200994
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200995 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200996
997 /* start the test procedure */
Harald Welte4091f922019-02-19 11:36:52 +0100998 gprs_ns_tx_alive(*nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200999 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001000
1001 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +08001002}
1003
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001004static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
1005{
1006 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1007 struct tlv_parsed tp;
1008 uint16_t nsvci, nsei;
1009 struct gprs_nsvc *orig_nsvc = NULL;
1010 int rc;
1011
1012 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1013 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1014 if (rc < 0) {
1015 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
1016 "Error during TLV Parse\n", (*nsvc)->nsei);
1017 return rc;
1018 }
1019
1020 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
1021 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1022 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +02001023 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001024 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001025 return -EINVAL;
1026 }
1027
Harald Weltebfe62e52017-05-15 12:48:30 +02001028 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1029 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001030
1031 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
1032 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1033 nsei, nsvci);
1034
1035 if (!((*nsvc)->state & NSE_S_RESET)) {
1036 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1037 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1038 */
1039 LOGP(DNS, LOGL_ERROR,
1040 "NS RESET ACK Discarding unexpected message for "
1041 "NS-VCI %d from SGSN NSEI=%d\n",
1042 nsvci, nsei);
1043 return 0;
1044 }
1045
1046 if (!(*nsvc)->nsvci_is_valid) {
1047 LOGP(DNS, LOGL_NOTICE,
1048 "NS RESET ACK Uninitialised NS-VC (%u) for "
1049 "NS-VCI %d, NSEI=%d from %s\n",
1050 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1051 return -EINVAL;
1052 }
1053
1054 if ((*nsvc)->nsvci != nsvci) {
1055 /* NS-VCI has changed */
1056
1057 /* if !0, use another NSVC object that matches the NSVCI */
1058 int use_other_nsvc;
1059
1060 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001061 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1062 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001063
1064 if (use_other_nsvc)
1065 /* Update *nsvc to point to the right NSVC object */
1066 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1067 &orig_nsvc);
1068
1069 if (!use_other_nsvc) {
1070 /* The incoming RESET_ACK doesn't match the NSVCI.
1071 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1072 */
1073 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1074 NS_PDUT_RESET_ACK,
1075 NS_IE_VCI);
1076 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1077 LOGP(DNS, LOGL_ERROR,
1078 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1079 "from %s\n",
1080 nsvci,
1081 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1082 nsei, gprs_ns_ll_str(*nsvc));
1083 return -EINVAL;
1084 }
1085
1086 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001087 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001088 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1089
1090 /* Update the ll info fields */
1091 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1092 gprs_ns_ll_clear(orig_nsvc);
1093 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001094 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1095 /* The incoming RESET_ACK doesn't match the NSEI.
1096 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1097 */
1098 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1099 NS_PDUT_RESET_ACK,
1100 NS_IE_NSEI);
1101 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1102 LOGP(DNS, LOGL_ERROR,
1103 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1104 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1105 return -EINVAL;
1106 }
1107
1108 /* NSEI has changed */
1109 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1110 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001111 }
1112
1113 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001114 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1115 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001116 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1117 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1118 /* stop RESET timer */
1119 osmo_timer_del(&(*nsvc)->timer);
1120 }
1121 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte4091f922019-02-19 11:36:52 +01001122 rc = gprs_ns_tx_alive(*nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001123 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1124
1125 return rc;
1126}
1127
Harald Welte834f26d2010-05-11 06:20:54 +02001128static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1129{
1130 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1131 struct tlv_parsed tp;
1132 uint8_t *cause;
1133 int rc;
1134
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001135 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001136
Maxc513c0f2017-10-23 15:11:14 +02001137 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001138
Harald Weltebd33f3d2010-05-30 17:19:38 +02001139 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1140 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001141 if (rc < 0) {
1142 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1143 "Error during TLV Parse\n", nsvc->nsei);
1144 return rc;
1145 }
Harald Welte834f26d2010-05-11 06:20:54 +02001146
1147 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1148 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001149 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001150 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001151 return -EINVAL;
1152 }
1153
1154 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1155 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1156
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001157 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001158 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001159
Harald Welte9ef2e152019-02-19 23:42:04 +01001160 return gprs_ns_tx_block_ack(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001161}
1162
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001163int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1164 struct gprs_nsvc *fallback_nsvc,
1165 struct gprs_nsvc **new_nsvc);
1166
1167int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001168 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001169
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001170/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001171 * \param nsi NS instance to which the data belongs
1172 * \param[in] msg message buffer containing newly-received data
1173 * \param[in] saddr socketaddr from which data was received
1174 * \param[in] ll link-layer type in which data was received
1175 * \returns 0 in case of success, < 0 in case of error
1176 *
1177 * This is the main entry point int othe NS imlementation where frames
1178 * from the underlying transport (normally UDP) enter.
1179 */
Harald Weltef030b212010-04-26 19:18:54 +02001180int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001181 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001182{
Harald Weltef030b212010-04-26 19:18:54 +02001183 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001184 int rc = 0;
1185
Harald Weltef030b212010-04-26 19:18:54 +02001186 /* look up the NSVC based on source address */
1187 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001188
Harald Weltef030b212010-04-26 19:18:54 +02001189 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001190 struct gprs_nsvc *fallback_nsvc;
1191
1192 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001193 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001194 fallback_nsvc->ip.bts_addr = *saddr;
1195 fallback_nsvc->ll = ll;
1196
1197 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1198
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001199 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001200 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001201
1202 rc = 0;
1203 }
1204
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001205 if (nsvc)
1206 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001207
1208 return rc;
1209}
1210
Maxcbf5cdf2017-10-24 18:18:55 +02001211const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001212{
1213 static char buf[80];
Max95308592017-10-24 15:54:28 +02001214
1215 switch(nsvc->ll) {
1216 case GPRS_NS_LL_UDP:
1217 snprintf(buf, sizeof(buf), "%s:%u",
1218 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1219 break;
1220 case GPRS_NS_LL_FR_GRE:
1221 snprintf(buf, sizeof(buf), "%s:%u",
1222 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1223 break;
1224 default:
1225 buf[0] = '\0';
1226 break;
1227 }
1228
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001229 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001230
1231 return buf;
1232}
1233
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001234void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1235{
1236 nsvc->ll = other->ll;
1237
1238 switch (nsvc->ll) {
1239 case GPRS_NS_LL_UDP:
1240 nsvc->ip = other->ip;
1241 break;
1242 case GPRS_NS_LL_FR_GRE:
1243 nsvc->frgre = other->frgre;
1244 break;
1245 default:
1246 break;
1247 }
1248}
1249
1250void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1251{
1252 switch (nsvc->ll) {
1253 case GPRS_NS_LL_UDP:
1254 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1255 nsvc->ip.bts_addr.sin_port = 0;
1256 break;
1257 case GPRS_NS_LL_FR_GRE:
1258 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1259 nsvc->frgre.bts_addr.sin_port = 0;
1260 break;
1261 default:
1262 break;
1263 }
1264}
1265
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001266/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001267 * \param nsi NS instance to which the data belongs
1268 * \param[in] msg message buffer containing newly-received data
1269 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001270 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001271 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1272 * been created or found
1273 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1274 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1275 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1276 * has been created and registered, and GPRS_NS_CS_FOUND if an
1277 * existing NS-VC object has been found with the same NSEI.
1278 *
1279 * This contains the initial NS automaton state (NS-VC not yet attached).
1280 */
1281int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1282 struct gprs_nsvc *fallback_nsvc,
1283 struct gprs_nsvc **new_nsvc)
1284{
1285 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1286 struct gprs_nsvc *existing_nsvc;
1287
1288 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001289 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001290 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001291
1292 int rc;
1293
1294 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001295 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001296 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1297 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001298 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001299 return GPRS_NS_CS_SKIPPED;
1300 }
1301
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001302 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1303 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1304 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1305 "for non-existing NS-VC\n",
1306 gprs_ns_ll_str(fallback_nsvc));
1307 return GPRS_NS_CS_SKIPPED;
1308 }
1309
1310 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1311 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1312 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1313 "for non-existing NS-VC\n",
1314 gprs_ns_ll_str(fallback_nsvc));
1315 return GPRS_NS_CS_SKIPPED;
1316 }
1317
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001318 /* Only the RESET procedure creates a new NSVC */
1319 if (nsh->pdu_type != NS_PDUT_RESET) {
1320 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001321 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001322 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001323 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001324 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001325 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001326 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001327 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001328
1329 rc = gprs_ns_tx_status(fallback_nsvc,
1330 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1331 if (rc < 0) {
1332 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001333 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001334 return rc;
1335 }
1336 return GPRS_NS_CS_REJECTED;
1337 }
1338
1339 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1340 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1341 if (rc < 0) {
1342 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1343 "TLV Parse\n", rc);
1344 return rc;
1345 }
1346 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1347 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1348 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001349 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001350 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001351 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001352 return -EINVAL;
1353 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001354 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1355 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001356 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001357 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001358 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001359 if (!existing_nsvc) {
1360 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001361 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001362 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001363 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001364 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001365 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001366
1367 return GPRS_NS_CS_CREATED;
1368 }
1369
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001370 /* Check NSEI */
1371 if (existing_nsvc->nsei != nsei) {
1372 LOGP(DNS, LOGL_NOTICE,
1373 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1374 nsvci, existing_nsvc->nsei, nsei);
1375
1376 /* Override old NSEI */
1377 existing_nsvc->nsei = nsei;
1378
1379 /* Do statistics */
1380 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1381 }
1382
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001383 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001384 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001385 return GPRS_NS_CS_FOUND;
1386}
1387
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001388/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001389 * \param nsi NS instance to which the data belongs
1390 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001391 * \param[inout] nsvc refers to the virtual connection, may be modified when
1392 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001393 * \returns 0 in case of success, < 0 in case of error
1394 *
1395 * This contains the main NS automaton.
1396 */
1397int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001398 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001399{
1400 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1401 int rc = 0;
1402
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001403 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001404
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001405 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001406
Harald Welte144e0292010-05-13 11:45:07 +02001407 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001408 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1409 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001410
Harald Welte9ba50052010-03-14 15:45:01 +08001411 switch (nsh->pdu_type) {
1412 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001413 /* If we're dead and blocked and suddenly receive a
1414 * NS-ALIVE out of the blue, we might have been re-started
1415 * and should send a NS-RESET to make sure everything recovers
1416 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001417 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001418 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1419 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001420 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001421 break;
1422 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001423 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001424 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001425 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001426 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001427 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1428 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001429 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001430 if ((*nsvc)->state & NSE_S_BLOCKED)
1431 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001432 }
Harald Welte9ba50052010-03-14 15:45:01 +08001433 break;
1434 case NS_PDUT_UNITDATA:
1435 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001436 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001437 break;
1438 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001439 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001440 break;
1441 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001442 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001443 break;
1444 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001445 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001446 break;
1447 case NS_PDUT_UNBLOCK:
1448 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001449 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001450 ns_mark_unblocked(*nsvc);
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001451 /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001452 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Stefan Sperling797558e2018-11-19 17:18:41 +01001453 if (rc < 0)
1454 break;
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001455 /*
1456 * UNBLOCK_ACK has been transmitted.
1457 * Signal handlers may send additional messages following UNBLOCK_ACK under
1458 * the assumption that NS is now in UNBLOCKED state at our peer's end.
1459 */
Stefan Sperling797558e2018-11-19 17:18:41 +01001460 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001461 break;
1462 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001463 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001464 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001465 ns_set_state(*nsvc, NSE_S_ALIVE);
1466 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001467 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001468 break;
1469 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001470 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001471 break;
1472 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001473 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001474 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001475 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001476 break;
1477 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001478 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001479 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001480 rc = -EINVAL;
1481 break;
1482 }
1483 return rc;
1484}
1485
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001486/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001487 * \param[in] cb Call-back function for incoming BSSGP data
1488 * \returns dynamically allocated gprs_ns_inst
1489 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001490struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001491{
Harald Welte4fcdd762012-06-16 16:40:42 +08001492 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001493
1494 nsi->cb = cb;
1495 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001496 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1497 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1498 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1499 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1500 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1501 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1502 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001503
Harald Welte60cc6ac2010-05-13 14:20:56 +02001504 /* Create the dummy NSVC that we use for sending
1505 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001506 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001507 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001508 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001509 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001510
Harald Welte3771d092010-04-30 20:26:32 +02001511 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001512}
1513
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001514void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001515{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001516 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001517
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001518 gprs_nsvc_delete(nsi->unknown_nsvc);
1519
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001520 /* delete all NSVCs and clear their timers */
1521 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1522 gprs_nsvc_delete(nsvc);
1523
1524 /* close socket and unregister */
1525 if (nsi->nsip.fd.data) {
1526 close(nsi->nsip.fd.fd);
1527 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001528 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001529 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001530}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001531
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001532/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001533 * \param nsi gprs_ns_inst that is to be destroyed
1534 *
1535 * This function releases all resources associated with the
1536 * NS-instance.
1537 */
1538void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1539{
1540 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001541 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001542 talloc_free(nsi);
1543}
1544
1545
1546/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1547 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1548
1549/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001550static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001551 struct sockaddr_in *saddr)
1552{
Harald Welteba4c6662010-05-19 15:38:10 +02001553 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001554 int ret = 0;
1555 socklen_t saddr_len = sizeof(*saddr);
1556
1557 if (!msg) {
1558 *error = -ENOMEM;
1559 return NULL;
1560 }
1561
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001562 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001563 (struct sockaddr *)saddr, &saddr_len);
1564 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001565 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001566 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001567 msgb_free(msg);
1568 *error = ret;
1569 return NULL;
1570 } else if (ret == 0) {
1571 msgb_free(msg);
1572 *error = ret;
1573 return NULL;
1574 }
1575
1576 msg->l2h = msg->data;
1577 msgb_put(msg, ret);
1578
1579 return msg;
1580}
1581
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001582static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001583{
1584 int error;
1585 struct sockaddr_in saddr;
1586 struct gprs_ns_inst *nsi = bfd->data;
1587 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1588
1589 if (!msg)
1590 return error;
1591
Harald Welteb3ee2652010-05-19 14:38:50 +02001592 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001593
1594 msgb_free(msg);
1595
1596 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001597}
1598
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001599static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001600{
1601 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1602 return -EIO;
1603}
1604
Harald Welteb3ee2652010-05-19 14:38:50 +02001605static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001606{
1607 int rc;
1608 struct gprs_ns_inst *nsi = nsvc->nsi;
1609 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1610
1611 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1612 (struct sockaddr *)daddr, sizeof(*daddr));
1613
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001614 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001615
1616 return rc;
1617}
1618
1619/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001620static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001621{
1622 int rc = 0;
1623
1624 if (what & BSC_FD_READ)
1625 rc = handle_nsip_read(bfd);
1626 if (what & BSC_FD_WRITE)
1627 rc = handle_nsip_write(bfd);
1628
1629 return rc;
1630}
1631
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001632/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01001633 * \param[in] nsi NS protocol instance to listen
1634 * \returns >=0 (fd) in case of success, negative in case of error
1635 *
1636 * A call to this function will create a UDP socket bound to the port
1637 * number and IP address specified in the NS protocol instance. The
1638 * file descriptor of the socket will be stored in nsi->nsip.fd.
1639 */
Harald Welte7fb05232010-05-19 15:09:09 +02001640int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001641{
Harald Welte73952e32012-06-16 14:59:56 +08001642 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001643 struct in_addr remote;
1644 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02001645 int ret;
1646
Harald Weltebfe62e52017-05-15 12:48:30 +02001647 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02001648 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08001649
1650 nsi->nsip.fd.cb = nsip_fd_cb;
1651 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02001652
1653 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
1654 /* connect to ensure only we only accept packets from the
1655 * configured remote end/peer */
1656 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
1657 ret =
1658 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1659 IPPROTO_UDP, inet_ntoa(in),
1660 nsi->nsip.local_port, remote_str,
1661 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
1662
1663 LOGP(DNS, LOGL_NOTICE,
1664 "Listening for nsip packets from %s:%u on %s:%u\n",
1665 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
1666 } else {
1667 /* Accept UDP packets from any source IP/Port */
1668 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1669 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND);
1670
1671 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
1672 }
1673
Stefan Sperlingf6538212018-10-10 16:09:32 +02001674 if (ret < 0) {
1675 nsi->nsip.fd.cb = NULL;
1676 nsi->nsip.fd.data = NULL;
Harald Weltef030b212010-04-26 19:18:54 +02001677 return ret;
Stefan Sperlingf6538212018-10-10 16:09:32 +02001678 }
Harald Weltef030b212010-04-26 19:18:54 +02001679
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001680 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1681 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1682 if (ret < 0)
1683 LOGP(DNS, LOGL_ERROR,
1684 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1685 nsi->nsip.dscp, ret, errno);
1686
Maxa19c1262017-10-20 15:39:45 +02001687 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 +02001688
1689 return ret;
1690}
Harald Welte3771d092010-04-30 20:26:32 +02001691
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001692/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01001693 * \param[in] nsvc NS-VC in which to start the procedure
1694 * \param[in] cause Numeric NS cause value
1695 *
1696 * This is a high-level function initiating a NS-RESET procedure. It
1697 * will not only send a NS-RESET, but also set the state to BLOCKED and
1698 * start the Tns-reset timer.
1699 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001700int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001701{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001702 int rc;
1703
Harald Welteb1020d52010-05-25 22:17:30 +02001704 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1705 nsvc->nsei);
1706
Harald Welte731d1fc2010-05-14 11:53:08 +00001707 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02001708 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001709
Harald Welte731d1fc2010-05-14 11:53:08 +00001710 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001711 rc = gprs_ns_tx_reset(nsvc, cause);
1712 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001713 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1714 nsvc->nsei);
1715 }
1716 /* Start Tns-reset */
1717 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001718
1719 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001720}
1721
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001722/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01001723 * \param nsi NS-instance
1724 * \param[in] dest Destination IP/Port
1725 * \param[in] nsei NSEI of the to-be-established NS-VC
1726 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1727 * \returns struct gprs_nsvc representing the new NS-VC
1728 *
1729 * This function will establish a single NS/UDP/IP connection in uplink
1730 * (BSS to SGSN) direction.
1731 */
Harald Weltef5430362012-06-17 12:25:53 +08001732struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001733 struct sockaddr_in *dest, uint16_t nsei,
1734 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001735{
1736 struct gprs_nsvc *nsvc;
1737
1738 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001739 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001740 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001741 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001742 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001743 nsvc->remote_end_is_sgsn = 1;
1744
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001745 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1746 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001747}
Harald Weltea9f23c82011-11-23 15:01:31 +01001748
Harald Weltecca49632012-06-16 17:45:59 +08001749void gprs_ns_set_log_ss(int ss)
1750{
1751 DNS = ss;
1752}
1753
Daniel Willmannf1318fe2018-05-29 20:55:18 +02001754/*! Append the nsvc state to a talloc string
1755 * \param s The string to append to (allocated with talloc)
1756 * \param[in] nsvc The NS-VC to print the state of
1757 * \returns The new string with state information appended to it
1758 *
1759 * This function will append a comma-separated state of the NS-VC to the
1760 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
1761 */
1762char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
1763{
1764 s = talloc_asprintf_append(s,
1765 "%u,%u,%s,%s,%s,%s,%s\n",
1766 nsvc->nsei, nsvc->nsvci,
1767 NS_DESC_A(nsvc->state),
1768 NS_DESC_B(nsvc->state),
1769 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
1770 NS_DESC_A(nsvc->remote_state),
1771 NS_DESC_B(nsvc->remote_state));
1772
1773 return s;
1774}
1775
Harald Welte96e2a002017-06-12 21:44:18 +02001776/*! @} */