blob: 4e584adc196dd7adc00a11099f308a033a2b280f [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.
Harald Welte17a642d2018-07-01 19:09:52 +02003 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005/*
Harald Welte17a642d2018-07-01 19:09:52 +02006 * (C) 2009-2018 by Harald Welte <laforge@gnumonks.org>
Harald Weltee08da972017-11-13 01:00:26 +09007 * (C) 2016-2017 sysmocom - s.f.m.c. GmbH
Harald Welte9ba50052010-03-14 15:45:01 +08008 *
9 * All Rights Reserved
10 *
Harald Weltee08da972017-11-13 01:00:26 +090011 * SPDX-License-Identifier: GPL-2.0+
12 *
Harald Welte9ba50052010-03-14 15:45:01 +080013 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010014 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080016 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010021 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080022 *
Harald Welte7fa89c22014-10-26 20:33:09 +010023 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010024 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080025 *
26 */
27
Harald Weltea9f23c82011-11-23 15:01:31 +010028/*! \addtogroup libgb
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 *
Harald Weltea9f23c82011-11-23 15:01:31 +010031 * GPRS Networks Service (NS) messages on the Gb interface
32 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
33 *
34 * Some introduction into NS: NS is used typically on top of frame relay,
Harald Welte9ba50052010-03-14 15:45:01 +080035 * but in the ip.access world it is encapsulated in UDP packets. It serves
36 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
37 * do much, apart from providing congestion notification and status indication.
38 *
39 * Terms:
Harald Welte9ba50052010-03-14 15:45:01 +080040 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020041 * NS Network Service
42 * NSVC NS Virtual Connection
43 * NSEI NS Entity Identifier
44 * NSVL NS Virtual Link
45 * NSVLI NS Virtual Link Identifier
46 * BVC BSSGP Virtual Connection
47 * BVCI BSSGP Virtual Connection Identifier
48 * NSVCG NS Virtual Connection Goup
49 * Blocked NS-VC cannot be used for user traffic
50 * Alive Ability of a NS-VC to provide communication
51 *
52 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
Harald Welte9ba50052010-03-14 15:45:01 +080053 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
Alexander Chemeris8abe45d2020-03-02 17:05:27 +030054 * NS then has to figure out which NSVC's are responsible for this BVCI.
Harald Welte9ba50052010-03-14 15:45:01 +080055 * Those mappings are administratively configured.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020056 *
57 * This implementation has the following limitations:
58 * - Only one NS-VC for each NSE: No load-sharing function
59 * - NSVCI 65535 and 65534 are reserved for internal use
60 * - Only UDP is supported as of now, no frame relay support
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020061 * - 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
Harald Welte047f3872018-07-01 21:04:45 +020074#include <osmocom/core/fsm.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010075#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020076#include <osmocom/core/byteswap.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010077#include <osmocom/gsm/tlv.h>
78#include <osmocom/core/talloc.h>
79#include <osmocom/core/select.h>
80#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +020081#include <osmocom/core/stat_item.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010082#include <osmocom/core/stats.h>
Harald Welte73952e32012-06-16 14:59:56 +080083#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080084#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080085#include <osmocom/gprs/gprs_ns.h>
86#include <osmocom/gprs/gprs_bssgp.h>
87#include <osmocom/gprs/gprs_ns_frgre.h>
88
Harald Weltecca49632012-06-16 17:45:59 +080089#include "common_vty.h"
Harald Welte17a642d2018-07-01 19:09:52 +020090#include "gb_internal.h"
Harald Welte9ba50052010-03-14 15:45:01 +080091
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020092#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
93#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
Maxc513c0f2017-10-23 15:11:14 +020094#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
95#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
Harald Welte047f3872018-07-01 21:04:45 +020096#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
97#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
98
99#define ERR_IF_NSVC_USES_SNS(nsvc, reason) \
100 do { \
101 if ((nsvc)->nsi->bss_sns_fi) { \
102 LOGP(DNS, LOGL_ERROR, "NSEI=%u Asked to %s. Rejected on IP-SNS\n", \
103 nsvc->nsei, reason); \
104 osmo_log_backtrace(DNS, LOGL_ERROR); \
105 return -EIO; \
106 } \
107 } while (0)
Maxc513c0f2017-10-23 15:11:14 +0200108
Harald Welte9ba50052010-03-14 15:45:01 +0800109static const struct tlv_definition ns_att_tlvdef = {
110 .def = {
111 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
112 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
113 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
114 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
115 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
Harald Welte17a642d2018-07-01 19:09:52 +0200116 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
117 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
118 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
119 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
120 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
121 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
Harald Welte047f3872018-07-01 21:04:45 +0200122 /* TODO: IP_ADDR can be 5 or 17 bytes long, depending on first byte. This
123 * cannot be expressed in our TLV parser. However, we don't do IPv6 anyway */
124 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
Harald Welte9ba50052010-03-14 15:45:01 +0800125 },
126};
127
Harald Weltec51c23c2010-05-13 12:55:20 +0200128enum ns_ctr {
129 NS_CTR_PKTS_IN,
130 NS_CTR_PKTS_OUT,
131 NS_CTR_BYTES_IN,
132 NS_CTR_BYTES_OUT,
133 NS_CTR_BLOCKED,
134 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200135 NS_CTR_REPLACED,
136 NS_CTR_NSEI_CHG,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200137 NS_CTR_INV_VCI,
138 NS_CTR_INV_NSEI,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200139 NS_CTR_LOST_ALIVE,
140 NS_CTR_LOST_RESET,
Harald Weltec51c23c2010-05-13 12:55:20 +0200141};
142
Harald Welte144e0292010-05-13 11:45:07 +0200143static const struct rate_ctr_desc nsvc_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +0800144 { "packets:in", "Packets at NS Level ( In)" },
145 { "packets:out","Packets at NS Level (Out)" },
146 { "bytes:in", "Bytes at NS Level ( In)" },
147 { "bytes:out", "Bytes at NS Level (Out)" },
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200148 { "blocked", "NS-VC Block count " },
149 { "dead", "NS-VC gone dead count " },
150 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200151 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200152 { "inv-nsvci", "NS-VCI was invalid count " },
153 { "inv-nsei", "NSEI was invalid count " },
Harald Weltea7a50652017-10-03 17:49:21 +0800154 { "lost:alive", "ALIVE ACK missing count " },
155 { "lost:reset", "RESET ACK missing count " },
Harald Welte144e0292010-05-13 11:45:07 +0200156};
157
158static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +0800159 .group_name_prefix = "ns:nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200160 .group_description = "NSVC Peer Statistics",
161 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
162 .ctr_desc = nsvc_ctr_description,
Jacob Erlbeckfed2a482015-11-03 16:18:56 +0100163 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte144e0292010-05-13 11:45:07 +0200164};
165
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200166enum ns_stat {
167 NS_STAT_ALIVE_DELAY,
168};
169
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100170static const struct osmo_stat_item_desc nsvc_stat_description[] = {
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100171 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200172};
173
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100174static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200175 .group_name_prefix = "ns.nsvc",
176 .group_description = "NSVC Peer Statistics",
177 .num_items = ARRAY_SIZE(nsvc_stat_description),
178 .item_desc = nsvc_stat_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100179 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200180};
181
Max54ca30c2017-05-22 14:48:27 +0200182const struct value_string gprs_ns_signal_ns_names[] = {
183 { S_NS_RESET, "NS-RESET" },
184 { S_NS_BLOCK, "NS-BLOCK" },
185 { S_NS_UNBLOCK, "NS-UNBLOCK" },
186 { S_NS_ALIVE_EXP, "NS-ALIVE expired" },
187 { S_NS_REPLACED, "NSVC replaced" },
188 { S_NS_MISMATCH, "Unexpected IE" },
Harald Welte047f3872018-07-01 21:04:45 +0200189 { S_SNS_CONFIGURED, "SNS Configured" },
Max54ca30c2017-05-22 14:48:27 +0200190 { 0, NULL }
191};
192
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200193#define CHECK_TX_RC(rc, nsvc) \
194 if (rc < 0) \
195 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
196 rc, gprs_ns_ll_str(nsvc));
197
Harald Welte94c9b442019-03-16 14:38:19 +0100198static bool nsvc_is_not_used(const struct gprs_nsvc *nsvc)
199{
200 if (nsvc->data_weight == 0 && nsvc->sig_weight == 0)
201 return true;
202 else
203 return false;
204}
205
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200206struct msgb *gprs_ns_msgb_alloc(void)
207{
208 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
209 "GPRS/NS");
210 if (!msg) {
211 LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
212 NS_ALLOC_SIZE);
213 }
214 return msg;
215}
216
Maxc513c0f2017-10-23 15:11:14 +0200217/* FIXME: convert to osmo_fsm */
218static inline void ns_set_state_with_log(struct gprs_nsvc *nsvc, uint32_t state, bool is_remote,
219 const char *file, unsigned line)
220{
221 uint32_t old_state = is_remote ? nsvc->remote_state : nsvc->state;
222
223 LOGPSRC(DNS, LOGL_DEBUG, file, line, "NSEI %d (NS-VCI=%u) setting %sstate [%s,%s,%s] -> [%s,%s,%s]\n",
224 nsvc->nsei, nsvc->nsvci, is_remote ? "remote " : "",
225 NS_DESC_A(old_state), NS_DESC_B(old_state), NS_DESC_R(old_state),
226 NS_DESC_A(state), NS_DESC_B(state), NS_DESC_R(state));
227
228 if (is_remote)
229 nsvc->remote_state = state;
230 else
231 nsvc->state = state;
232}
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200233
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200234/*! Lookup struct gprs_nsvc based on NSVCI
Harald Weltea9f23c82011-11-23 15:01:31 +0100235 * \param[in] nsi NS instance in which to search
236 * \param[in] nsvci NSVCI to be searched
237 * \returns gprs_nsvc of respective NSVCI
238 */
Harald Weltef5430362012-06-17 12:25:53 +0800239struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200240{
241 struct gprs_nsvc *nsvc;
242 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
243 if (nsvc->nsvci == nsvci)
244 return nsvc;
245 }
246 return NULL;
247}
248
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200249/*! Lookup struct gprs_nsvc based on NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100250 * \param[in] nsi NS instance in which to search
251 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200252 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100253 */
Harald Weltef5430362012-06-17 12:25:53 +0800254struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200255{
256 struct gprs_nsvc *nsvc;
257 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
258 if (nsvc->nsei == nsei)
259 return nsvc;
260 }
261 return NULL;
262}
263
Harald Welte047f3872018-07-01 21:04:45 +0200264/*! Determine active NS-VC for given NSEI + BVCI.
265 * Use this function to determine which of the NS-VCs inside the NS Instance
266 * shall be used to transmit data for given NSEI + BVCI */
Jacob Erlbeck69017152013-10-14 12:03:54 +0200267static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
Harald Welte047f3872018-07-01 21:04:45 +0200268 uint16_t nsei, uint16_t bvci)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200269{
270 struct gprs_nsvc *nsvc;
271 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte047f3872018-07-01 21:04:45 +0200272 /* if signalling BVCI, skip any NSVC with signalling weight == 0 */
273 if (bvci == 0 && nsvc->sig_weight == 0)
274 continue;
275 /* if point-to-point BVCI, skip any NSVC with data weight == 0 */
276 if (bvci != 0 && nsvc->data_weight == 0)
277 continue;
Jacob Erlbeck69017152013-10-14 12:03:54 +0200278 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200279 if (!(nsvc->state & NSE_S_BLOCKED) &&
280 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200281 return nsvc;
282 }
283 }
284 return NULL;
285}
286
Harald Welte047f3872018-07-01 21:04:45 +0200287/*! Lookup NS-VC based on specified remote peer socket addr.
288 * \param[in] nsi NS Instance within which we shall look up the NS-VC
289 * \param[in] sin Remote peer Socket Address (IP + UDP Port)
290 * \returns NS-VC matching the given peer; NULL in case of none */
291struct gprs_nsvc *gprs_nsvc_by_rem_addr(struct gprs_ns_inst *nsi, const struct sockaddr_in *sin)
Harald Weltef030b212010-04-26 19:18:54 +0200292{
293 struct gprs_nsvc *nsvc;
294 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000295 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
296 sin->sin_addr.s_addr &&
297 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200298 return nsvc;
299 }
300 return NULL;
301}
302
Harald Welte69a4cf22010-05-03 20:55:10 +0200303static void gprs_ns_timer_cb(void *data);
304
Harald Welte047f3872018-07-01 21:04:45 +0200305/*! Create a new NS-VC (Virtual Circuit) within given instance
306 * \param[in] nsi NS Instance in which to create the NSVC
307 * \param[in] nsvci] NS Virtual Connection Identifier for this NSVC
308 * \param[in] sig_weight Signalling Weight of this NS-VC. Use "0" for no signalling
309 * \param[in] data_weight Data WEight of this NS-VC. Use "0" for no data
310 * \returns newly-created gprs_nsvc within nsi. NULL on error. */
311struct gprs_nsvc *gprs_nsvc_create2(struct gprs_ns_inst *nsi, uint16_t nsvci,
312 uint8_t sig_weight, uint8_t data_weight)
Harald Weltef030b212010-04-26 19:18:54 +0200313{
314 struct gprs_nsvc *nsvc;
315
Harald Welte047f3872018-07-01 21:04:45 +0200316 if (gprs_nsvc_by_nsvci(nsi, nsvci)) {
317 LOGP(DNS, LOGL_ERROR, "Cannot create NS-VC for already-existing NSVCI=%u\n", nsvci);
318 return NULL;
319 }
320
Alexander Chemeris07cdd762020-03-03 19:20:10 +0300321 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC with Signal weight %u, Data weight %u\n",
322 nsvci, sig_weight, data_weight);
Harald Welteb1020d52010-05-25 22:17:30 +0200323
Harald Weltef030b212010-04-26 19:18:54 +0200324 nsvc = talloc_zero(nsi, struct gprs_nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200325 if (!nsvc)
326 return NULL;
Harald Weltef030b212010-04-26 19:18:54 +0200327 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100328 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200329 /* before RESET procedure: BLOCKED and DEAD */
Harald Welte047f3872018-07-01 21:04:45 +0200330 if (nsi->bss_sns_fi)
331 ns_set_state(nsvc, 0);
332 else
333 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltef030b212010-04-26 19:18:54 +0200334 nsvc->nsi = nsi;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200335 osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc);
Harald Welte144e0292010-05-13 11:45:07 +0200336 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte6d3135c2019-05-08 14:00:37 +0200337 if (!nsvc->ctrg) {
338 talloc_free(nsvc);
339 return NULL;
340 }
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100341 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte047f3872018-07-01 21:04:45 +0200342 nsvc->sig_weight = sig_weight;
343 nsvc->data_weight = data_weight;
Harald Welte69a4cf22010-05-03 20:55:10 +0200344
Harald Weltef030b212010-04-26 19:18:54 +0200345 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
346
347 return nsvc;
348}
Harald Welte9ba50052010-03-14 15:45:01 +0800349
Harald Welte047f3872018-07-01 21:04:45 +0200350/*! Old API for creating a NS-VC. Uses gprs_nsvc_create2 with fixed weights. */
351struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
352{
353 return gprs_nsvc_create2(nsi, nsvci, 1, 1);
354}
355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! Delete given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100357 * \param[in] nsvc gprs_nsvc to be deleted
358 */
Harald Weltef5430362012-06-17 12:25:53 +0800359void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000360{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200361 if (osmo_timer_pending(&nsvc->timer))
362 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000363 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200364 rate_ctr_group_free(nsvc->ctrg);
Jacob Erlbeck0975a7f2015-12-17 09:51:53 +0100365 osmo_stat_item_group_free(nsvc->statg);
Harald Welte2bffac52010-05-12 15:55:23 +0000366 talloc_free(nsvc);
367}
368
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200369static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200370 uint8_t cause)
371{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200372 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200373
374 nssd.nsvc = nsvc;
375 nssd.cause = cause;
376
Harald Welte4fcdd762012-06-16 16:40:42 +0800377 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200378}
379
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200380static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
381 struct msgb *msg,
382 uint8_t pdu_type, uint8_t ie_type)
383{
384 struct ns_signal_data nssd = {0};
385
386 nssd.nsvc = nsvc;
387 nssd.pdu_type = pdu_type;
388 nssd.ie_type = ie_type;
389 nssd.msg = msg;
390
391 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
392}
393
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200394static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
395{
396 struct ns_signal_data nssd = {0};
397
398 nssd.nsvc = nsvc;
399 nssd.old_nsvc = old_nsvc;
400
401 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
402}
403
Max584f4b62017-05-04 11:59:52 +0200404const struct value_string gprs_ns_pdu_strings[] = {
405 /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */
406 { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */
407 { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */
408 { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */
409 { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */
410 { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */
411 { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */
412 { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */
413 { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */
414 { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */
415 { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */
416 /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */
417 { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */
418 { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */
419 { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */
420 { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */
421 { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */
422 { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */
423 { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */
424 { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */
425 { 0, NULL }
426};
427
Harald Welte9ba50052010-03-14 15:45:01 +0800428/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200429static const struct value_string ns_cause_str[] = {
430 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
431 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
432 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
433 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
434 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
435 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
436 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
437 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200438 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200439 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
440 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
Harald Welte47cf21f2019-02-23 13:42:06 +0100441 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
442 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
443 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
444 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
445 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
446 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
447 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
Harald Welte2577d412010-05-02 09:37:45 +0200448 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800449};
450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200452const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800453{
Harald Welte2577d412010-05-02 09:37:45 +0200454 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800455}
456
Harald Welte24a655f2010-04-30 19:54:29 +0200457static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200458extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200459
Harald Welte94c9b442019-03-16 14:38:19 +0100460static bool ns_is_sns(uint8_t pdu_type)
461{
462 switch (pdu_type) {
463 case SNS_PDUT_CONFIG:
464 case SNS_PDUT_ACK:
465 case SNS_PDUT_ADD:
466 case SNS_PDUT_CHANGE_WEIGHT:
467 case SNS_PDUT_DELETE:
468 case SNS_PDUT_CONFIG_ACK:
469 case SNS_PDUT_SIZE:
470 case SNS_PDUT_SIZE_ACK:
471 return true;
472 default:
473 return false;
474 }
475}
476
Harald Welte24a655f2010-04-30 19:54:29 +0200477static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800478{
Harald Welte94c9b442019-03-16 14:38:19 +0100479 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200480 int ret;
481
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 Welte94c9b442019-03-16 14:38:19 +0100484 /* A pre-configured endpoint shall not be used for NSE data or signalling
485 * traffic (with the exception of Size and Configuration procedures) unless it
486 * is configured by the SGSN using the auto-configuration procedures. */
487 if (nsvc_is_not_used(nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
488 LOGP(DNS, LOGL_NOTICE, "Not transmitting %s on unused/pre-configured endpoint\n",
489 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
490 msgb_free(msg);
491 return -EINVAL;
492 }
493
Harald Welte144e0292010-05-13 11:45:07 +0200494 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200495 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
496 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200497
Harald Welteb3ee2652010-05-19 14:38:50 +0200498 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200499 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200500 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200501 if (ret < 0)
502 LOGP(DNS, LOGL_INFO,
503 "failed to send NS message via UDP: %s\n",
504 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200505 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200506 case GPRS_NS_LL_FR_GRE:
507 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200508 if (ret < 0)
509 LOGP(DNS, LOGL_INFO,
510 "failed to send NS message via FR/GRE: %s\n",
511 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200512 break;
Harald Weltef030b212010-04-26 19:18:54 +0200513 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200514 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200515 msgb_free(msg);
516 ret = -EIO;
517 break;
518 }
519 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800520}
521
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200522static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800523{
Harald Welteba4c6662010-05-19 15:38:10 +0200524 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800525 struct gprs_ns_hdr *nsh;
526
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100527 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200528
Harald Welte9ba50052010-03-14 15:45:01 +0800529 if (!msg)
530 return -ENOMEM;
531
Harald Welte144e0292010-05-13 11:45:07 +0200532 msg->l2h = msgb_put(msg, sizeof(*nsh));
533 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800534
535 nsh->pdu_type = pdu_type;
536
Harald Welte24a655f2010-04-30 19:54:29 +0200537 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800538}
539
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200540/*! Transmit a NS-RESET on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100541 * \param[in] nsvc NS-VC used for transmission
542 * \paam[in] cause Numeric NS cause value
543 */
Harald Welte834f26d2010-05-11 06:20:54 +0200544int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200545{
Harald Welte047f3872018-07-01 21:04:45 +0200546 struct msgb *msg;
Harald Welte69a4cf22010-05-03 20:55:10 +0200547 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200548 uint16_t nsvci = osmo_htons(nsvc->nsvci);
549 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200550
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100551 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200552
Harald Welte047f3872018-07-01 21:04:45 +0200553 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET");
554
555 msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200556 if (!msg)
557 return -ENOMEM;
558
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000559 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
560 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
561
Harald Welte144e0292010-05-13 11:45:07 +0200562 msg->l2h = msgb_put(msg, sizeof(*nsh));
563 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200564 nsh->pdu_type = NS_PDUT_RESET;
565
566 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
567 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
568 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
569
570 return gprs_ns_tx(nsvc, msg);
571
572}
573
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200574/*! Transmit a NS-STATUS on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100575 * \param[in] nsvc NS-VC to be used for transmission
576 * \param[in] cause Numeric NS cause value
577 * \param[in] bvci BVCI to be reset within NSVC
578 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200579int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
580 uint16_t bvci, struct msgb *orig_msg)
581{
Harald Welteba4c6662010-05-19 15:38:10 +0200582 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200583 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200584 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200585
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100586 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200587
Harald Weltebfe62e52017-05-15 12:48:30 +0200588 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200589
590 if (!msg)
591 return -ENOMEM;
592
Harald Welte90af1942010-05-15 23:02:24 +0200593 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000594 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
595
Harald Welte144e0292010-05-13 11:45:07 +0200596 msg->l2h = msgb_put(msg, sizeof(*nsh));
597 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200598 nsh->pdu_type = NS_PDUT_STATUS;
599
600 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
601
602 /* Section 9.2.7.1: Static conditions for NS-VCI */
603 if (cause == NS_CAUSE_NSVC_BLOCKED ||
604 cause == NS_CAUSE_NSVC_UNKNOWN)
605 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
606
607 /* Section 9.2.7.2: Static conditions for NS PDU */
608 switch (cause) {
609 case NS_CAUSE_SEM_INCORR_PDU:
610 case NS_CAUSE_PDU_INCOMP_PSTATE:
611 case NS_CAUSE_PROTO_ERR_UNSPEC:
612 case NS_CAUSE_INVAL_ESSENT_IE:
613 case NS_CAUSE_MISSING_ESSENT_IE:
614 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
615 orig_msg->l2h);
616 break;
617 default:
618 break;
619 }
620
621 /* Section 9.2.7.3: Static conditions for BVCI */
622 if (cause == NS_CAUSE_BVCI_UNKNOWN)
623 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
624
625 return gprs_ns_tx(nsvc, msg);
626}
627
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200628/*! Transmit a NS-BLOCK on a tiven NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100629 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
630 * \param[in] cause Numeric NS Cause value
631 * \returns 0 in case of success
632 */
Harald Welte834f26d2010-05-11 06:20:54 +0200633int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
634{
Harald Welte047f3872018-07-01 21:04:45 +0200635 struct msgb *msg;
Harald Welte834f26d2010-05-11 06:20:54 +0200636 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200637 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200638
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100639 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200640
Harald Welte047f3872018-07-01 21:04:45 +0200641 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK");
642
643 msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200644 if (!msg)
645 return -ENOMEM;
646
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000647 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
648 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
649
Harald Welte834f26d2010-05-11 06:20:54 +0200650 /* be conservative and mark it as blocked even now! */
Maxc513c0f2017-10-23 15:11:14 +0200651 ns_mark_blocked(nsvc);
Harald Weltec51c23c2010-05-13 12:55:20 +0200652 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200653
Harald Welte144e0292010-05-13 11:45:07 +0200654 msg->l2h = msgb_put(msg, sizeof(*nsh));
655 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200656 nsh->pdu_type = NS_PDUT_BLOCK;
657
658 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
659 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
660
661 return gprs_ns_tx(nsvc, msg);
662}
663
Harald Welte9ef2e152019-02-19 23:42:04 +0100664/*! Transmit a NS-BLOCK-ACK on a given NS-VC
665 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
666 * \returns 0 in case of success
667 */
668static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc)
669{
670 struct msgb *msg;
671 struct gprs_ns_hdr *nsh;
672 uint16_t nsvci = osmo_htons(nsvc->nsvci);
673
674 log_set_context(LOG_CTX_GB_NSVC, nsvc);
675
Harald Welte047f3872018-07-01 21:04:45 +0200676 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK ACK");
677
Harald Welte9ef2e152019-02-19 23:42:04 +0100678 msg = gprs_ns_msgb_alloc();
679 if (!msg)
680 return -ENOMEM;
681
682 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci);
683
684 /* be conservative and mark it as blocked even now! */
685 msg->l2h = msgb_put(msg, sizeof(*nsh));
686 nsh = (struct gprs_ns_hdr *) msg->l2h;
687 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
688
689 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
690
691 return gprs_ns_tx(nsvc, msg);
692}
693
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200694/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100695 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
696 * \returns 0 in case of success
697 */
Harald Welte834f26d2010-05-11 06:20:54 +0200698int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
699{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100700 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200701
702 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK");
703
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000704 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
705 nsvc->nsei, nsvc->nsvci);
706
Harald Welte834f26d2010-05-11 06:20:54 +0200707 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
708}
709
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200710/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100711 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
712 * \returns 0 in case of success
713 */
Harald Welteb983c312010-05-12 12:11:45 +0000714int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
715{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100716 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000717 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
718 nsvc->nsei, nsvc->nsvci);
719
720 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
721}
722
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200723/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100724 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
725 * \returns 0 in case of success
726 */
Harald Welteb983c312010-05-12 12:11:45 +0000727int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
728{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100729 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000730 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
731 nsvc->nsei, nsvc->nsvci);
732
733 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
734}
735
Harald Weltefe4ab902010-05-12 17:19:53 +0000736static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
737 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
738 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
739 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200740};
741
Harald Welte7c24b9e2010-05-12 12:21:52 +0000742static const struct value_string timer_mode_strs[] = {
743 { NSVC_TIMER_TNS_RESET, "tns-reset" },
744 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
745 { NSVC_TIMER_TNS_TEST, "tns-test" },
746 { 0, NULL }
747};
748
Harald Welte69a4cf22010-05-03 20:55:10 +0200749static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
750{
Harald Weltefe4ab902010-05-12 17:19:53 +0000751 enum ns_timeout tout = timer_mode_tout[mode];
752 unsigned int seconds = nsvc->nsi->timeout[tout];
753
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100754 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000755 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
756 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000757 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100758
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200759 if (osmo_timer_pending(&nsvc->timer))
760 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200761
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200762 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200763 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200764 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200765}
766
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200767static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
768{
769 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200770 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200771 timersub(&now, &nsvc->timer_started, &elapsed);
772
773 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
774}
775
Harald Welte80405452010-05-03 20:16:13 +0200776static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800777{
778 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000779 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
780 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800781
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100782 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000783 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
784 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000785 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100786
Harald Welte80405452010-05-03 20:16:13 +0200787 switch (nsvc->timer_mode) {
788 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800789 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200790 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800791 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000792 if (nsvc->alive_retries >
793 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte047f3872018-07-01 21:04:45 +0200794 /* mark as dead (and blocked unless IP-SNS) */
Harald Weltec51c23c2010-05-13 12:55:20 +0200795 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Welte047f3872018-07-01 21:04:45 +0200796 if (!nsvc->nsi->bss_sns_fi) {
797 ns_set_state(nsvc, NSE_S_BLOCKED);
798 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
799 } else
800 ns_set_state(nsvc, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200801 LOGP(DNS, LOGL_NOTICE,
802 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200803 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000804 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200805 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
Harald Welte047f3872018-07-01 21:04:45 +0200806 /* FIXME: should we send this signal in case of SNS? */
807 if (!nsvc->nsi->bss_sns_fi)
808 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800809 return;
810 }
Harald Welteb983c312010-05-12 12:11:45 +0000811 /* Tns-test case: send NS-ALIVE PDU */
812 gprs_ns_tx_alive(nsvc);
813 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200814 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200815 break;
816 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800817 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000818 gprs_ns_tx_alive(nsvc);
819 /* start Tns-alive timer (transition into faster
820 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000821 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200822 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
823 break;
824 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200825 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100826 if (!(nsvc->state & NSE_S_RESET))
827 LOGP(DNS, LOGL_NOTICE,
828 "NSEI=%u Reset timed out but RESET flag is not set\n",
829 nsvc->nsei);
830 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200831 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200832 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200833 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200834 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200835 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200836 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200837 case _NSVC_TIMER_NR:
838 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800839 }
Harald Welte9ba50052010-03-14 15:45:01 +0800840}
841
842/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800843static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800844{
Harald Welte047f3872018-07-01 21:04:45 +0200845 struct msgb *msg;
Harald Welte9ba50052010-03-14 15:45:01 +0800846 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200847 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800848
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100849 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200850
851 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET ACK");
852
853 msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800854 if (!msg)
855 return -ENOMEM;
856
Harald Weltebfe62e52017-05-15 12:48:30 +0200857 nsvci = osmo_htons(nsvc->nsvci);
858 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800859
Harald Welte144e0292010-05-13 11:45:07 +0200860 msg->l2h = msgb_put(msg, sizeof(*nsh));
861 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800862
863 nsh->pdu_type = NS_PDUT_RESET_ACK;
864
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200865 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200866 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800867
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200868 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
869 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800870
Harald Welte24a655f2010-04-30 19:54:29 +0200871 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800872}
873
Harald Welte17a642d2018-07-01 19:09:52 +0200874/*! Encode + Transmit a SNS-ACK as per Section 9.3.1.
875 * \param[in] nsvc NS-VC through which to transmit the ACK
876 * \param[in] trans_id Transaction ID which to acknowledge
877 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
878 * \param[in] ip4_elems Array of IPv4 Elements
879 * \param[in] num_ip4_elems number of ip4_elems
880 * \returns 0 on success; negative in case of error */
881int gprs_ns_tx_sns_ack(struct gprs_nsvc *nsvc, uint8_t trans_id, uint8_t *cause,
882 const struct gprs_ns_ie_ip4_elem *ip4_elems,
883 unsigned int num_ip4_elems)
884{
885 struct msgb *msg = gprs_ns_msgb_alloc();
886 struct gprs_ns_hdr *nsh;
887 uint16_t nsei;
888
889 log_set_context(LOG_CTX_GB_NSVC, nsvc);
890 if (!msg)
891 return -ENOMEM;
892
Harald Welte047f3872018-07-01 21:04:45 +0200893 if (!nsvc->nsi->bss_sns_fi) {
894 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
895 nsvc->nsei);
896 msgb_free(msg);
897 return -EIO;
898 }
899
Harald Welte17a642d2018-07-01 19:09:52 +0200900 nsei = osmo_htons(nsvc->nsei);
901
902 msg->l2h = msgb_put(msg, sizeof(*nsh));
903 nsh = (struct gprs_ns_hdr *) msg->l2h;
904
905 nsh->pdu_type = SNS_PDUT_ACK;
906 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
907 msgb_v_put(msg, trans_id);
908 if (cause)
909 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
910 if (ip4_elems) {
911 /* List of IP4 Elements 10.3.2c */
912 msgb_tvlv_put(msg, NS_IE_IPv4_LIST,
913 num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
914 (const uint8_t *)ip4_elems);
915 }
916 /* FIXME: List of IP6 elements 10.3.2d */
917 return gprs_ns_tx(nsvc, msg);
918}
919
920/*! Encode + Transmit a SNS-CONFIG as per Section 9.3.4.
921 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
922 * \param[in] end_flag Whether or not this is the last SNS-CONFIG
923 * \param[in] ip4_elems Array of IPv4 Elements
924 * \param[in] num_ip4_elems number of ip4_elems
925 * \returns 0 on success; negative in case of error */
926int gprs_ns_tx_sns_config(struct gprs_nsvc *nsvc, bool end_flag,
927 const struct gprs_ns_ie_ip4_elem *ip4_elems,
928 unsigned int num_ip4_elems)
929{
930 struct msgb *msg = gprs_ns_msgb_alloc();
931 struct gprs_ns_hdr *nsh;
932 uint16_t nsei;
933
934 log_set_context(LOG_CTX_GB_NSVC, nsvc);
935 if (!msg)
936 return -ENOMEM;
937
Harald Welte047f3872018-07-01 21:04:45 +0200938 if (!nsvc->nsi->bss_sns_fi) {
939 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
940 nsvc->nsei);
941 msgb_free(msg);
942 return -EIO;
943 }
944
Harald Welte17a642d2018-07-01 19:09:52 +0200945 nsei = osmo_htons(nsvc->nsei);
946
947 msg->l2h = msgb_put(msg, sizeof(*nsh));
948 nsh = (struct gprs_ns_hdr *) msg->l2h;
949
950 nsh->pdu_type = SNS_PDUT_CONFIG;
951
952 msgb_v_put(msg, end_flag ? 0x01 : 0x00);
953 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
954
955 /* List of IP4 Elements 10.3.2c */
956 msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
957 (const uint8_t *)ip4_elems);
958 /* FIXME: List of IP6 elements 10.3.2d */
959
960 return gprs_ns_tx(nsvc, msg);
961}
962
963/*! Encode + Transmit a SNS-CONFIG-ACK as per Section 9.3.5.
964 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG-ACK
965 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
966 * \returns 0 on success; negative in case of error */
967int gprs_ns_tx_sns_config_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
968{
969 struct msgb *msg = gprs_ns_msgb_alloc();
970 struct gprs_ns_hdr *nsh;
971 uint16_t nsei;
972
973 log_set_context(LOG_CTX_GB_NSVC, nsvc);
974 if (!msg)
975 return -ENOMEM;
976
Harald Welte047f3872018-07-01 21:04:45 +0200977 if (!nsvc->nsi->bss_sns_fi) {
978 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
979 nsvc->nsei);
980 msgb_free(msg);
981 return -EIO;
982 }
983
Harald Welte17a642d2018-07-01 19:09:52 +0200984 nsei = osmo_htons(nsvc->nsei);
985
986 msg->l2h = msgb_put(msg, sizeof(*nsh));
987 nsh = (struct gprs_ns_hdr *) msg->l2h;
988
989 nsh->pdu_type = SNS_PDUT_CONFIG_ACK;
990
991 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
992 if (cause)
993 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
994
995 return gprs_ns_tx(nsvc, msg);
996}
997
998
999/*! Encode + transmit a SNS-SIZE as per Section 9.3.7.
1000 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE
1001 * \param[in] reset_flag Whether or not to add a RESET flag
1002 * \param[in] max_nr_nsvc Maximum number of NS-VCs
1003 * \param[in] ip4_ep_nr Number of IPv4 endpoints (NULL if none)
1004 * \param[in] ip6_ep_nr Number of IPv6 endpoints (NULL if none)
1005 * \returns 0 on success; negative in case of error */
1006int gprs_ns_tx_sns_size(struct gprs_nsvc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
1007 uint16_t *ip4_ep_nr, uint16_t *ip6_ep_nr)
1008{
1009 struct msgb *msg = gprs_ns_msgb_alloc();
1010 struct gprs_ns_hdr *nsh;
1011 uint16_t nsei;
1012
1013 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1014 if (!msg)
1015 return -ENOMEM;
1016
Harald Welte047f3872018-07-01 21:04:45 +02001017 if (!nsvc->nsi->bss_sns_fi) {
1018 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1019 nsvc->nsei);
1020 msgb_free(msg);
1021 return -EIO;
1022 }
1023
Harald Welte17a642d2018-07-01 19:09:52 +02001024 nsei = osmo_htons(nsvc->nsei);
1025
1026 msg->l2h = msgb_put(msg, sizeof(*nsh));
1027 nsh = (struct gprs_ns_hdr *) msg->l2h;
1028
1029 nsh->pdu_type = SNS_PDUT_SIZE;
1030
1031 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1032 msgb_tv_put(msg, NS_IE_RESET_FLAG, reset_flag ? 0x01 : 0x00);
1033 msgb_tv16_put(msg, NS_IE_MAX_NR_NSVC, max_nr_nsvc);
1034 if (ip4_ep_nr)
1035 msgb_tv16_put(msg, NS_IE_IPv4_EP_NR, *ip4_ep_nr);
1036 if (ip6_ep_nr)
1037 msgb_tv16_put(msg, NS_IE_IPv6_EP_NR, *ip6_ep_nr);
1038
1039 return gprs_ns_tx(nsvc, msg);
1040}
1041
1042/*! Encode + Transmit a SNS-SIZE-ACK as per Section 9.3.8.
1043 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE-ACK
1044 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
1045 * \returns 0 on success; negative in case of error */
1046int gprs_ns_tx_sns_size_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
1047{
1048 struct msgb *msg = gprs_ns_msgb_alloc();
1049 struct gprs_ns_hdr *nsh;
1050 uint16_t nsei;
1051
1052 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1053 if (!msg)
1054 return -ENOMEM;
1055
Harald Welte047f3872018-07-01 21:04:45 +02001056 if (!nsvc->nsi->bss_sns_fi) {
1057 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1058 nsvc->nsei);
1059 msgb_free(msg);
1060 return -EIO;
1061 }
1062
Harald Welte17a642d2018-07-01 19:09:52 +02001063 nsei = osmo_htons(nsvc->nsei);
1064
1065 msg->l2h = msgb_put(msg, sizeof(*nsh));
1066 nsh = (struct gprs_ns_hdr *) msg->l2h;
1067
1068 nsh->pdu_type = SNS_PDUT_SIZE_ACK;
1069
1070 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1071 if (cause)
1072 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
1073
1074 return gprs_ns_tx(nsvc, msg);
1075}
1076
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001077/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +01001078 * \param[in] nsi NS-instance on which we shall transmit
1079 * \param[in] msg struct msgb to be trasnmitted
1080 *
1081 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
Alexander Chemeris8abe45d2020-03-02 17:05:27 +03001082 * if the NS-VC is ALIVE and not BLOCKED. After that, it adds a NS
Harald Weltea9f23c82011-11-23 15:01:31 +01001083 * header for the NS-UNITDATA message type and sends it off.
1084 *
1085 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
1086 */
Harald Welte24a655f2010-04-30 19:54:29 +02001087int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001088{
Harald Welte24a655f2010-04-30 19:54:29 +02001089 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001090 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001091 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +02001092
Harald Welte047f3872018-07-01 21:04:45 +02001093 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg), msgb_bvci(msg));
Harald Welte24a655f2010-04-30 19:54:29 +02001094 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +02001095 int rc;
1096 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
1097 LOGP(DNS, LOGL_ERROR,
1098 "All NS-VCs for NSEI %u are either dead or blocked!\n",
1099 msgb_nsei(msg));
1100 rc = -EBUSY;
1101 } else {
1102 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
1103 "to NS-VC!\n", msgb_nsei(msg));
1104 rc = -EINVAL;
1105 }
1106
Harald Weltef47df642010-08-09 21:15:40 +08001107 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +02001108 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +02001109 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001110 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001111
Harald Welteec20ba42010-05-13 12:18:49 +02001112 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
1113 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001114 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001115 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +08001116 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001117 return -EIO;
1118 }
1119
1120 nsh->pdu_type = NS_PDUT_UNITDATA;
1121 /* spare octet in data[0] */
1122 nsh->data[1] = bvci >> 8;
1123 nsh->data[2] = bvci & 0xff;
1124
Harald Welte24a655f2010-04-30 19:54:29 +02001125 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001126}
1127
1128/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +02001129static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001130{
1131 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001132 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001133
Harald Weltec1402a62010-05-12 11:48:44 +02001134 if (nsvc->state & NSE_S_BLOCKED)
1135 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
1136 0, msg);
1137
Harald Welte9ba50052010-03-14 15:45:01 +08001138 /* spare octet in data[0] */
1139 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +02001140 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +02001141 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001142
1143 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +02001144 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001145}
1146
1147/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +02001148static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001149{
Harald Weltef030b212010-04-26 19:18:54 +02001150 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001151 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001152 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +08001153 int rc;
1154
Harald Welte41337832010-05-16 00:24:26 +02001155 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +08001156
Harald Weltebd33f3d2010-05-30 17:19:38 +02001157 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1158 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001159 if (rc < 0) {
1160 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
1161 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
1162 "Error during TLV Parse\n", nsvc->nsei);
1163 return rc;
1164 }
Harald Welte9ba50052010-03-14 15:45:01 +08001165
1166 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001167 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +08001168 return -EINVAL;
1169 }
1170
1171 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +02001172 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +08001173
1174 return 0;
1175}
1176
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001177/* Replace a nsvc object with another based on NSVCI.
1178 * This function replaces looks for a NSVC with the given NSVCI and replaces it
1179 * if possible and necessary. If replaced, the former value of *nsvc is
1180 * returned in *old_nsvc.
1181 * \return != 0 if *nsvc points to a matching NSVC.
1182 */
1183static int gprs_nsvc_replace_if_found(uint16_t nsvci,
1184 struct gprs_nsvc **nsvc,
1185 struct gprs_nsvc **old_nsvc)
1186{
1187 struct gprs_nsvc *matching_nsvc;
1188
1189 if ((*nsvc)->nsvci == nsvci) {
1190 *old_nsvc = NULL;
1191 return 1;
1192 }
1193
1194 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
1195
1196 if (!matching_nsvc)
1197 return 0;
1198
1199 /* The NS-VCI is already used by this NS-VC */
1200
1201 char *old_peer;
1202
1203 /* Exchange the NS-VC objects */
1204 *old_nsvc = *nsvc;
1205 *nsvc = matching_nsvc;
1206
1207 /* Do logging */
1208 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
1209 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
1210 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
1211
1212 talloc_free(old_peer);
1213
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001214 return 1;
1215}
1216
Harald Welte9ba50052010-03-14 15:45:01 +08001217/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001218static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001219{
1220 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001221 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001222 uint8_t cause;
1223 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001224 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +08001225 int rc;
1226
Harald Weltebd33f3d2010-05-30 17:19:38 +02001227 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1228 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001229 if (rc < 0) {
1230 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001231 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +02001232 return rc;
1233 }
Harald Welte9ba50052010-03-14 15:45:01 +08001234
1235 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1236 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
1237 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001238 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001239 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001240 return -EINVAL;
1241 }
1242
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001243 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +02001244 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1245 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +08001246
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001247 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
1248 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1249 nsei, nsvci, gprs_ns_cause_str(cause));
1250
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001251 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
1252 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001253 /* The incoming RESET doesn't match the NSVCI. Send an
1254 * appropriate RESET_ACK and ignore the RESET.
1255 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
1256 */
1257 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1258 NS_PDUT_RESET,
1259 NS_IE_VCI);
1260 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1261 gprs_ns_tx_reset_ack(*nsvc);
1262 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001263 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001264
1265 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001266 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
1267 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
1268 "at %s\n",
1269 nsvci, (*nsvc)->nsvci,
1270 gprs_ns_ll_str(*nsvc));
1271 orig_nsvc = *nsvc;
1272 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001273 (*nsvc)->nsei = nsei;
1274 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001275 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001276
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001277 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
1278 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1279 /* The incoming RESET doesn't match the NSEI. Send an
1280 * appropriate RESET_ACK and ignore the RESET.
1281 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
1282 */
1283 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1284 NS_PDUT_RESET,
1285 NS_IE_NSEI);
1286 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +02001287 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001288 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001289 return 0;
1290 }
1291
1292 /* NSEI has changed */
1293 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1294 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001295 }
Harald Weltebbc9fac2010-05-03 18:54:12 +02001296
Harald Weltec1402a62010-05-12 11:48:44 +02001297 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001298 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +02001299
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001300 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001301 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001302 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001303
1304 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001305 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1306 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001307 } else {
1308 (*nsvc)->nsei = nsei;
1309 (*nsvc)->nsvci = nsvci;
1310 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +01001311 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
1312 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001313 }
Harald Welte9ba50052010-03-14 15:45:01 +08001314
Harald Welte834f26d2010-05-11 06:20:54 +02001315 /* inform interested parties about the fact that this NSVC
1316 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001317 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +02001318
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001319 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001320
1321 /* start the test procedure */
Harald Welte35042a22019-02-23 14:07:00 +01001322 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001323
1324 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +08001325}
1326
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001327static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
1328{
1329 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1330 struct tlv_parsed tp;
1331 uint16_t nsvci, nsei;
1332 struct gprs_nsvc *orig_nsvc = NULL;
1333 int rc;
1334
1335 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1336 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1337 if (rc < 0) {
1338 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
1339 "Error during TLV Parse\n", (*nsvc)->nsei);
1340 return rc;
1341 }
1342
1343 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
1344 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1345 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +02001346 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001347 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001348 return -EINVAL;
1349 }
1350
Harald Weltebfe62e52017-05-15 12:48:30 +02001351 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1352 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001353
1354 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
1355 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1356 nsei, nsvci);
1357
1358 if (!((*nsvc)->state & NSE_S_RESET)) {
1359 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1360 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1361 */
1362 LOGP(DNS, LOGL_ERROR,
1363 "NS RESET ACK Discarding unexpected message for "
1364 "NS-VCI %d from SGSN NSEI=%d\n",
1365 nsvci, nsei);
1366 return 0;
1367 }
1368
1369 if (!(*nsvc)->nsvci_is_valid) {
1370 LOGP(DNS, LOGL_NOTICE,
1371 "NS RESET ACK Uninitialised NS-VC (%u) for "
1372 "NS-VCI %d, NSEI=%d from %s\n",
1373 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1374 return -EINVAL;
1375 }
1376
1377 if ((*nsvc)->nsvci != nsvci) {
1378 /* NS-VCI has changed */
1379
1380 /* if !0, use another NSVC object that matches the NSVCI */
1381 int use_other_nsvc;
1382
1383 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001384 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1385 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001386
1387 if (use_other_nsvc)
1388 /* Update *nsvc to point to the right NSVC object */
1389 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1390 &orig_nsvc);
1391
1392 if (!use_other_nsvc) {
1393 /* The incoming RESET_ACK doesn't match the NSVCI.
1394 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1395 */
1396 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1397 NS_PDUT_RESET_ACK,
1398 NS_IE_VCI);
1399 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1400 LOGP(DNS, LOGL_ERROR,
1401 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1402 "from %s\n",
1403 nsvci,
1404 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1405 nsei, gprs_ns_ll_str(*nsvc));
1406 return -EINVAL;
1407 }
1408
1409 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001410 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001411 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1412
1413 /* Update the ll info fields */
1414 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1415 gprs_ns_ll_clear(orig_nsvc);
1416 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001417 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1418 /* The incoming RESET_ACK doesn't match the NSEI.
1419 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1420 */
1421 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1422 NS_PDUT_RESET_ACK,
1423 NS_IE_NSEI);
1424 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1425 LOGP(DNS, LOGL_ERROR,
1426 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1427 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1428 return -EINVAL;
1429 }
1430
1431 /* NSEI has changed */
1432 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1433 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001434 }
1435
1436 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001437 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1438 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001439 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1440 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1441 /* stop RESET timer */
1442 osmo_timer_del(&(*nsvc)->timer);
1443 }
1444 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte35042a22019-02-23 14:07:00 +01001445 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001446
Harald Welte35042a22019-02-23 14:07:00 +01001447 return 0;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001448}
1449
Harald Welte834f26d2010-05-11 06:20:54 +02001450static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1451{
1452 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1453 struct tlv_parsed tp;
1454 uint8_t *cause;
1455 int rc;
1456
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001457 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001458
Maxc513c0f2017-10-23 15:11:14 +02001459 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001460
Harald Weltebd33f3d2010-05-30 17:19:38 +02001461 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1462 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001463 if (rc < 0) {
1464 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1465 "Error during TLV Parse\n", nsvc->nsei);
1466 return rc;
1467 }
Harald Welte834f26d2010-05-11 06:20:54 +02001468
1469 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1470 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001471 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001472 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001473 return -EINVAL;
1474 }
1475
1476 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1477 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1478
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001479 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001480 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001481
Harald Welte9ef2e152019-02-19 23:42:04 +01001482 return gprs_ns_tx_block_ack(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001483}
1484
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001485int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1486 struct gprs_nsvc *fallback_nsvc,
1487 struct gprs_nsvc **new_nsvc);
1488
1489int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001490 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001491
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001492/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001493 * \param nsi NS instance to which the data belongs
1494 * \param[in] msg message buffer containing newly-received data
1495 * \param[in] saddr socketaddr from which data was received
1496 * \param[in] ll link-layer type in which data was received
1497 * \returns 0 in case of success, < 0 in case of error
1498 *
1499 * This is the main entry point int othe NS imlementation where frames
1500 * from the underlying transport (normally UDP) enter.
1501 */
Harald Weltef030b212010-04-26 19:18:54 +02001502int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001503 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001504{
Harald Weltef030b212010-04-26 19:18:54 +02001505 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001506 int rc = 0;
1507
Harald Weltef030b212010-04-26 19:18:54 +02001508 /* look up the NSVC based on source address */
Harald Welte047f3872018-07-01 21:04:45 +02001509 nsvc = gprs_nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001510
Harald Weltef030b212010-04-26 19:18:54 +02001511 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001512 struct gprs_nsvc *fallback_nsvc;
1513
1514 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001515 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001516 fallback_nsvc->ip.bts_addr = *saddr;
1517 fallback_nsvc->ll = ll;
1518
1519 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1520
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001521 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001522 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001523
1524 rc = 0;
1525 }
1526
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001527 if (nsvc)
1528 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001529
1530 return rc;
1531}
1532
Harald Welte4a62eda2019-03-18 18:27:00 +01001533char *gprs_ns_ll_str_buf(char *buf, size_t buf_len, const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001534{
Max95308592017-10-24 15:54:28 +02001535 switch(nsvc->ll) {
1536 case GPRS_NS_LL_UDP:
Harald Welte4a62eda2019-03-18 18:27:00 +01001537 snprintf(buf, buf_len, "%s:%u",
Max95308592017-10-24 15:54:28 +02001538 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1539 break;
1540 case GPRS_NS_LL_FR_GRE:
Harald Welte4a62eda2019-03-18 18:27:00 +01001541 snprintf(buf, buf_len, "%s:%u",
Max95308592017-10-24 15:54:28 +02001542 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1543 break;
1544 default:
1545 buf[0] = '\0';
1546 break;
1547 }
1548
Harald Welte4a62eda2019-03-18 18:27:00 +01001549 buf[buf_len - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001550
1551 return buf;
1552}
1553
Harald Welte4a62eda2019-03-18 18:27:00 +01001554const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
1555{
Harald Welte171ef822019-03-28 10:49:05 +01001556 static __thread char buf[80];
Harald Welte4a62eda2019-03-18 18:27:00 +01001557 return gprs_ns_ll_str_buf(buf, sizeof(buf), nsvc);
1558}
1559
Harald Welte179f3572019-03-18 18:38:47 +01001560char *gprs_ns_ll_str_c(const void *ctx, const struct gprs_nsvc *nsvc)
1561{
1562 char *buf = talloc_size(ctx, INET6_ADDRSTRLEN+10);
1563 if (!buf)
1564 return buf;
1565 return gprs_ns_ll_str_buf(buf, INET6_ADDRSTRLEN+10, nsvc);
1566}
1567
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001568void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1569{
1570 nsvc->ll = other->ll;
1571
1572 switch (nsvc->ll) {
1573 case GPRS_NS_LL_UDP:
1574 nsvc->ip = other->ip;
1575 break;
1576 case GPRS_NS_LL_FR_GRE:
1577 nsvc->frgre = other->frgre;
1578 break;
1579 default:
1580 break;
1581 }
1582}
1583
1584void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1585{
1586 switch (nsvc->ll) {
1587 case GPRS_NS_LL_UDP:
1588 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1589 nsvc->ip.bts_addr.sin_port = 0;
1590 break;
1591 case GPRS_NS_LL_FR_GRE:
1592 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1593 nsvc->frgre.bts_addr.sin_port = 0;
1594 break;
1595 default:
1596 break;
1597 }
1598}
1599
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001600/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001601 * \param nsi NS instance to which the data belongs
1602 * \param[in] msg message buffer containing newly-received data
1603 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001604 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001605 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1606 * been created or found
1607 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1608 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1609 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1610 * has been created and registered, and GPRS_NS_CS_FOUND if an
1611 * existing NS-VC object has been found with the same NSEI.
1612 *
1613 * This contains the initial NS automaton state (NS-VC not yet attached).
1614 */
1615int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1616 struct gprs_nsvc *fallback_nsvc,
1617 struct gprs_nsvc **new_nsvc)
1618{
1619 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1620 struct gprs_nsvc *existing_nsvc;
1621
1622 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001623 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001624 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001625
1626 int rc;
1627
1628 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001629 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001630 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1631 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001632 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001633 return GPRS_NS_CS_SKIPPED;
1634 }
1635
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001636 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1637 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1638 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1639 "for non-existing NS-VC\n",
1640 gprs_ns_ll_str(fallback_nsvc));
1641 return GPRS_NS_CS_SKIPPED;
1642 }
1643
1644 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1645 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1646 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1647 "for non-existing NS-VC\n",
1648 gprs_ns_ll_str(fallback_nsvc));
1649 return GPRS_NS_CS_SKIPPED;
1650 }
1651
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001652 /* Only the RESET procedure creates a new NSVC */
1653 if (nsh->pdu_type != NS_PDUT_RESET) {
1654 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001655 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001656 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001657 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001658 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001659 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001660 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001661 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001662
1663 rc = gprs_ns_tx_status(fallback_nsvc,
1664 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1665 if (rc < 0) {
1666 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001667 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001668 return rc;
1669 }
1670 return GPRS_NS_CS_REJECTED;
1671 }
1672
1673 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1674 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1675 if (rc < 0) {
1676 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1677 "TLV Parse\n", rc);
1678 return rc;
1679 }
1680 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1681 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1682 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001683 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001684 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001685 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001686 return -EINVAL;
1687 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001688 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1689 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001690 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001691 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001692 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001693 if (!existing_nsvc) {
1694 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001695 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001696 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001697 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001698 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001699 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001700
1701 return GPRS_NS_CS_CREATED;
1702 }
1703
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001704 /* Check NSEI */
1705 if (existing_nsvc->nsei != nsei) {
1706 LOGP(DNS, LOGL_NOTICE,
1707 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1708 nsvci, existing_nsvc->nsei, nsei);
1709
1710 /* Override old NSEI */
1711 existing_nsvc->nsei = nsei;
1712
1713 /* Do statistics */
1714 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1715 }
1716
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001717 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001718 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001719 return GPRS_NS_CS_FOUND;
1720}
1721
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001722/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001723 * \param nsi NS instance to which the data belongs
1724 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001725 * \param[inout] nsvc refers to the virtual connection, may be modified when
1726 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001727 * \returns 0 in case of success, < 0 in case of error
1728 *
1729 * This contains the main NS automaton.
1730 */
1731int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001732 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001733{
1734 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte047f3872018-07-01 21:04:45 +02001735 struct tlv_parsed tp;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001736 int rc = 0;
1737
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001738 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001739
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001740 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001741
Harald Welte144e0292010-05-13 11:45:07 +02001742 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001743 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1744 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001745
Harald Welte94c9b442019-03-16 14:38:19 +01001746 if (nsvc_is_not_used(*nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
1747 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s on unused/pre-configured endpoint, discarding\n",
1748 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1749 gprs_ns_tx_status(*nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1750 return 0;
1751 }
1752
Harald Welte9ba50052010-03-14 15:45:01 +08001753 switch (nsh->pdu_type) {
1754 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001755 /* If we're dead and blocked and suddenly receive a
1756 * NS-ALIVE out of the blue, we might have been re-started
1757 * and should send a NS-RESET to make sure everything recovers
1758 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001759 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001760 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1761 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001762 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001763 break;
1764 case NS_PDUT_ALIVE_ACK:
Harald Welte047f3872018-07-01 21:04:45 +02001765 ns_mark_alive(*nsvc);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001766 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001767 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001768 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001769 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001770 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1771 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001772 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001773 if ((*nsvc)->state & NSE_S_BLOCKED)
1774 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001775 }
Harald Welte9ba50052010-03-14 15:45:01 +08001776 break;
1777 case NS_PDUT_UNITDATA:
1778 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001779 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001780 break;
1781 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001782 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001783 break;
1784 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001785 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001786 break;
1787 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001788 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001789 break;
1790 case NS_PDUT_UNBLOCK:
1791 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001792 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001793 ns_mark_unblocked(*nsvc);
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001794 /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001795 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Stefan Sperling797558e2018-11-19 17:18:41 +01001796 if (rc < 0)
1797 break;
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001798 /*
1799 * UNBLOCK_ACK has been transmitted.
1800 * Signal handlers may send additional messages following UNBLOCK_ACK under
1801 * the assumption that NS is now in UNBLOCKED state at our peer's end.
1802 */
Stefan Sperling797558e2018-11-19 17:18:41 +01001803 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001804 break;
1805 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001806 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001807 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001808 ns_set_state(*nsvc, NSE_S_ALIVE);
1809 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001810 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001811 break;
1812 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001813 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001814 break;
1815 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001816 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001817 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001818 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001819 break;
Harald Welte047f3872018-07-01 21:04:45 +02001820 case SNS_PDUT_CONFIG:
1821 if (!nsi->bss_sns_fi)
1822 goto unexpected_sns;
1823 /* one additional byte ('end flag') before the TLV part starts */
1824 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+1,
1825 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1826 if (rc < 0) {
1827 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1828 return rc;
1829 }
1830 /* All sub-network service related message types */
1831 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1832 break;
1833 case SNS_PDUT_ACK:
1834 case SNS_PDUT_ADD:
1835 case SNS_PDUT_CHANGE_WEIGHT:
1836 case SNS_PDUT_DELETE:
1837 if (!nsi->bss_sns_fi)
1838 goto unexpected_sns;
1839 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
1840 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+5,
1841 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
1842 if (rc < 0) {
1843 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1844 return rc;
1845 }
1846 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1847 tp.lv[NS_IE_NSEI].len = 2;
1848 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1849 tp.lv[NS_IE_TRANS_ID].len = 1;
1850 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1851 break;
1852 case SNS_PDUT_CONFIG_ACK:
1853 case SNS_PDUT_SIZE:
1854 case SNS_PDUT_SIZE_ACK:
1855 if (!nsi->bss_sns_fi)
1856 goto unexpected_sns;
1857 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1858 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1859 if (rc < 0) {
1860 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1861 return rc;
1862 }
1863 /* All sub-network service related message types */
1864 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1865 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001866 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001867 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001868 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001869 rc = -EINVAL;
1870 break;
Harald Welte047f3872018-07-01 21:04:45 +02001871unexpected_sns:
1872 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s for NS Instance that has no SNS!\n",
1873 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1874 rc = -EINVAL;
1875 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001876 }
1877 return rc;
1878}
1879
Harald Welte047f3872018-07-01 21:04:45 +02001880static bool gprs_sns_fsm_registered = false;
1881
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001882/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001883 * \param[in] cb Call-back function for incoming BSSGP data
1884 * \returns dynamically allocated gprs_ns_inst
1885 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001886struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001887{
Harald Welte797d5332019-12-01 13:38:35 +01001888 struct gprs_ns_inst *nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001889
Harald Welte047f3872018-07-01 21:04:45 +02001890 if (!gprs_sns_fsm_registered) {
Harald Welte797d5332019-12-01 13:38:35 +01001891 int rc = gprs_sns_init();
1892 if (rc < 0)
1893 return NULL;
Harald Welte047f3872018-07-01 21:04:45 +02001894 gprs_sns_fsm_registered = true;
1895 }
1896
Harald Welte797d5332019-12-01 13:38:35 +01001897 nsi = talloc_zero(ctx, struct gprs_ns_inst);
1898 if (!nsi)
1899 return NULL;
Harald Weltef030b212010-04-26 19:18:54 +02001900 nsi->cb = cb;
1901 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001902 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1903 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1904 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1905 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1906 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1907 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1908 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Welte047f3872018-07-01 21:04:45 +02001909 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Harald Weltef030b212010-04-26 19:18:54 +02001910
Harald Welte60cc6ac2010-05-13 14:20:56 +02001911 /* Create the dummy NSVC that we use for sending
1912 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001913 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001914 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001915 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001916 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001917
Harald Welte3771d092010-04-30 20:26:32 +02001918 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001919}
1920
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001921void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001922{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001923 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001924
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001925 gprs_nsvc_delete(nsi->unknown_nsvc);
1926
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001927 /* delete all NSVCs and clear their timers */
1928 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1929 gprs_nsvc_delete(nsvc);
1930
1931 /* close socket and unregister */
1932 if (nsi->nsip.fd.data) {
1933 close(nsi->nsip.fd.fd);
1934 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001935 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001936 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001937}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001938
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001939/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001940 * \param nsi gprs_ns_inst that is to be destroyed
1941 *
1942 * This function releases all resources associated with the
1943 * NS-instance.
1944 */
1945void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1946{
1947 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001948 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001949 talloc_free(nsi);
1950}
1951
1952
1953/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1954 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1955
1956/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001957static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001958 struct sockaddr_in *saddr)
1959{
Harald Welteba4c6662010-05-19 15:38:10 +02001960 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001961 int ret = 0;
1962 socklen_t saddr_len = sizeof(*saddr);
1963
1964 if (!msg) {
1965 *error = -ENOMEM;
1966 return NULL;
1967 }
1968
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001969 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001970 (struct sockaddr *)saddr, &saddr_len);
1971 if (ret < 0) {
Max38176d22018-11-06 17:04:21 +01001972 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n",
1973 strerror(errno), osmo_sock_get_name2(bfd->fd));
Harald Weltef030b212010-04-26 19:18:54 +02001974 msgb_free(msg);
1975 *error = ret;
1976 return NULL;
1977 } else if (ret == 0) {
1978 msgb_free(msg);
1979 *error = ret;
1980 return NULL;
1981 }
1982
1983 msg->l2h = msg->data;
1984 msgb_put(msg, ret);
1985
1986 return msg;
1987}
1988
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001989static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001990{
1991 int error;
1992 struct sockaddr_in saddr;
1993 struct gprs_ns_inst *nsi = bfd->data;
1994 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1995
1996 if (!msg)
1997 return error;
1998
Harald Welteb3ee2652010-05-19 14:38:50 +02001999 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00002000
2001 msgb_free(msg);
2002
2003 return error;
Harald Weltef030b212010-04-26 19:18:54 +02002004}
2005
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02002006static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02002007{
2008 /* FIXME: actually send the data here instead of nsip_sendmsg() */
2009 return -EIO;
2010}
2011
Harald Welteb3ee2652010-05-19 14:38:50 +02002012static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02002013{
2014 int rc;
2015 struct gprs_ns_inst *nsi = nsvc->nsi;
2016 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
2017
2018 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
2019 (struct sockaddr *)daddr, sizeof(*daddr));
2020
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01002021 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02002022
2023 return rc;
2024}
2025
2026/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02002027static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02002028{
2029 int rc = 0;
2030
Harald Welte16886992019-03-20 10:26:39 +01002031 if (what & OSMO_FD_READ)
Harald Weltef030b212010-04-26 19:18:54 +02002032 rc = handle_nsip_read(bfd);
Harald Welte16886992019-03-20 10:26:39 +01002033 if (what & OSMO_FD_WRITE)
Harald Weltef030b212010-04-26 19:18:54 +02002034 rc = handle_nsip_write(bfd);
2035
2036 return rc;
2037}
2038
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002039/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01002040 * \param[in] nsi NS protocol instance to listen
2041 * \returns >=0 (fd) in case of success, negative in case of error
2042 *
2043 * A call to this function will create a UDP socket bound to the port
2044 * number and IP address specified in the NS protocol instance. The
2045 * file descriptor of the socket will be stored in nsi->nsip.fd.
2046 */
Harald Welte7fb05232010-05-19 15:09:09 +02002047int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02002048{
Harald Welte73952e32012-06-16 14:59:56 +08002049 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002050 struct in_addr remote;
2051 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02002052 int ret;
2053
Harald Weltebfe62e52017-05-15 12:48:30 +02002054 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02002055 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08002056
2057 nsi->nsip.fd.cb = nsip_fd_cb;
2058 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002059
2060 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
2061 /* connect to ensure only we only accept packets from the
2062 * configured remote end/peer */
2063 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
2064 ret =
2065 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
2066 IPPROTO_UDP, inet_ntoa(in),
2067 nsi->nsip.local_port, remote_str,
2068 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
2069
2070 LOGP(DNS, LOGL_NOTICE,
2071 "Listening for nsip packets from %s:%u on %s:%u\n",
2072 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
2073 } else {
2074 /* Accept UDP packets from any source IP/Port */
2075 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
2076 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND);
2077
2078 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
2079 }
2080
Stefan Sperlingf6538212018-10-10 16:09:32 +02002081 if (ret < 0) {
2082 nsi->nsip.fd.cb = NULL;
2083 nsi->nsip.fd.data = NULL;
Harald Weltef030b212010-04-26 19:18:54 +02002084 return ret;
Stefan Sperlingf6538212018-10-10 16:09:32 +02002085 }
Harald Weltef030b212010-04-26 19:18:54 +02002086
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01002087 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
2088 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
2089 if (ret < 0)
2090 LOGP(DNS, LOGL_ERROR,
2091 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
2092 nsi->nsip.dscp, ret, errno);
2093
Maxa19c1262017-10-20 15:39:45 +02002094 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 +02002095
2096 return ret;
2097}
Harald Welte3771d092010-04-30 20:26:32 +02002098
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002099/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01002100 * \param[in] nsvc NS-VC in which to start the procedure
2101 * \param[in] cause Numeric NS cause value
2102 *
2103 * This is a high-level function initiating a NS-RESET procedure. It
2104 * will not only send a NS-RESET, but also set the state to BLOCKED and
2105 * start the Tns-reset timer.
2106 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002107int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00002108{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002109 int rc;
2110
Harald Welte047f3872018-07-01 21:04:45 +02002111 ERR_IF_NSVC_USES_SNS(nsvc, "RESET procedure based on API request");
2112
Harald Welteb1020d52010-05-25 22:17:30 +02002113 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
2114 nsvc->nsei);
2115
Harald Welte731d1fc2010-05-14 11:53:08 +00002116 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02002117 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02002118
Harald Welte731d1fc2010-05-14 11:53:08 +00002119 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002120 rc = gprs_ns_tx_reset(nsvc, cause);
2121 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00002122 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
2123 nsvc->nsei);
2124 }
2125 /* Start Tns-reset */
2126 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002127
2128 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00002129}
2130
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002131/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01002132 * \param nsi NS-instance
2133 * \param[in] dest Destination IP/Port
2134 * \param[in] nsei NSEI of the to-be-established NS-VC
2135 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2136 * \returns struct gprs_nsvc representing the new NS-VC
2137 *
2138 * This function will establish a single NS/UDP/IP connection in uplink
2139 * (BSS to SGSN) direction.
2140 */
Harald Weltef5430362012-06-17 12:25:53 +08002141struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02002142 struct sockaddr_in *dest, uint16_t nsei,
2143 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02002144{
2145 struct gprs_nsvc *nsvc;
2146
Harald Welte047f3872018-07-01 21:04:45 +02002147 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00002148 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08002149 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00002150 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02002151 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02002152 nsvc->remote_end_is_sgsn = 1;
2153
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08002154 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
2155 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02002156}
Harald Weltea9f23c82011-11-23 15:01:31 +01002157
Harald Welte047f3872018-07-01 21:04:45 +02002158/*! Establish a NS connection (from the BSS) to the SGSN using SNS auto-configuration
2159 * \param nsi NS-instance
2160 * \param[in] dest Destination IP/Port
2161 * \param[in] nsei NSEI of the to-be-established NS-VC
2162 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2163 * \returns struct gprs_nsvc representing the new NS-VC
2164 *
2165 * This function will establish a single NS/UDP/IP connection in uplink
2166 * (BSS to SGSN) direction. It will start with the SNS-SIZE procedure,
2167 * followed by BSS-originated SNS-CONFIG, then SGSN-originated SNS-CONFIG.
2168 *
2169 * Once configuration completes, the user will be notified by the S_SNS_CONFIGURED signal,
2170 * at which point he typically would want to initiate NS-RESET by means of gprs_nsvc_reset().
2171 */
2172struct gprs_nsvc *gprs_ns_nsip_connect_sns(struct gprs_ns_inst *nsi,
2173 struct sockaddr_in *dest, uint16_t nsei,
2174 uint16_t nsvci)
2175{
2176 struct gprs_nsvc *nsvc;
2177
2178 /* FIXME: We are getting the order wrong here. Normally, one would want
2179 * to start the SNS FSM *before* creating any NS-VC and then create the NS-VC
2180 * after the SNS layer has established the IP/port/etc. However, this would
2181 * require some massive code and API changes compared to existing libosmogb,
2182 * so let's keep the old logic. */
2183 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte94c9b442019-03-16 14:38:19 +01002184 if (!nsvc) {
2185 /* create NSVC with 0 data + signalling weight. This is illegal in SNS
2186 * and can hence only be created locally and serves as indication that
2187 * this NS-VC shall not be used for anything except SNS _unless_ it is
2188 * modified via SNS-{CONFIG,CHANGEWEIGHT,ADD} to become part of the
2189 * active NS-VCs */
2190 nsvc = gprs_nsvc_create2(nsi, nsvci, 0, 0);
2191 }
Harald Welte047f3872018-07-01 21:04:45 +02002192 nsvc->ip.bts_addr = *dest;
2193 nsvc->nsei = nsei;
2194 nsvc->remote_end_is_sgsn = 1;
2195 /* NSVCs are always UNBLOCKED in IP-SNS */
2196 ns_set_state(nsvc, 0);
2197
2198 if (nsi->bss_sns_fi)
2199 osmo_fsm_inst_term(nsi->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
2200 nsi->bss_sns_fi = gprs_sns_bss_fsm_alloc(nsi, nsvc, "NSIP");
2201 gprs_sns_bss_fsm_start(nsi);
2202 return nsvc;
2203}
2204
Harald Weltecca49632012-06-16 17:45:59 +08002205void gprs_ns_set_log_ss(int ss)
2206{
2207 DNS = ss;
2208}
2209
Daniel Willmannf1318fe2018-05-29 20:55:18 +02002210/*! Append the nsvc state to a talloc string
2211 * \param s The string to append to (allocated with talloc)
2212 * \param[in] nsvc The NS-VC to print the state of
2213 * \returns The new string with state information appended to it
2214 *
2215 * This function will append a comma-separated state of the NS-VC to the
2216 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
2217 */
2218char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
2219{
2220 s = talloc_asprintf_append(s,
2221 "%u,%u,%s,%s,%s,%s,%s\n",
2222 nsvc->nsei, nsvc->nsvci,
2223 NS_DESC_A(nsvc->state),
2224 NS_DESC_B(nsvc->state),
2225 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
2226 NS_DESC_A(nsvc->remote_state),
2227 NS_DESC_B(nsvc->remote_state));
2228
2229 return s;
2230}
2231
Harald Welte35042a22019-02-23 14:07:00 +01002232/*! Start the ALIVE timer procedure in all NS-VCs part of this NS Instance */
2233void gprs_nsvc_start_test(struct gprs_nsvc *nsvc)
2234{
Harald Welte94c9b442019-03-16 14:38:19 +01002235 /* skip the initial NS-VC unless it has explicitly been configured
2236 * via SNS-CONFIG from the SGSN */
2237 if (nsvc_is_not_used(nsvc))
2238 return;
Harald Welte35042a22019-02-23 14:07:00 +01002239 gprs_ns_tx_alive(nsvc);
2240 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
2241}
2242
Harald Welte047f3872018-07-01 21:04:45 +02002243void gprs_start_alive_all_nsvcs(struct gprs_ns_inst *nsi)
2244{
2245 struct gprs_nsvc *nsvc;
2246 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
2247 /* start the test procedure */
2248 gprs_nsvc_start_test(nsvc);
2249 }
2250}
2251
Harald Welte96e2a002017-06-12 21:44:18 +02002252/*! @} */