blob: 6046deadade6eee13b2a76f78754f2835ccd80a6 [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 Welte057122e2020-06-06 22:04:20 +0200330 if (nsi->bss_sns_fi || !nsi->nsip.use_reset_block_unblock)
Harald Welte047f3872018-07-01 21:04:45 +0200331 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200350/*! Delete given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100351 * \param[in] nsvc gprs_nsvc to be deleted
352 */
Harald Weltef5430362012-06-17 12:25:53 +0800353void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000354{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200355 if (osmo_timer_pending(&nsvc->timer))
356 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000357 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200358 rate_ctr_group_free(nsvc->ctrg);
Jacob Erlbeck0975a7f2015-12-17 09:51:53 +0100359 osmo_stat_item_group_free(nsvc->statg);
Harald Welte2bffac52010-05-12 15:55:23 +0000360 talloc_free(nsvc);
361}
362
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200363static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200364 uint8_t cause)
365{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200366 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200367
368 nssd.nsvc = nsvc;
369 nssd.cause = cause;
370
Harald Welte4fcdd762012-06-16 16:40:42 +0800371 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200372}
373
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200374static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
375 struct msgb *msg,
376 uint8_t pdu_type, uint8_t ie_type)
377{
378 struct ns_signal_data nssd = {0};
379
380 nssd.nsvc = nsvc;
381 nssd.pdu_type = pdu_type;
382 nssd.ie_type = ie_type;
383 nssd.msg = msg;
384
385 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
386}
387
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200388static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
389{
390 struct ns_signal_data nssd = {0};
391
392 nssd.nsvc = nsvc;
393 nssd.old_nsvc = old_nsvc;
394
395 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
396}
397
Max584f4b62017-05-04 11:59:52 +0200398const struct value_string gprs_ns_pdu_strings[] = {
399 /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */
400 { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */
401 { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */
402 { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */
403 { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */
404 { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */
405 { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */
406 { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */
407 { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */
408 { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */
409 { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */
410 /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */
411 { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */
412 { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */
413 { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */
414 { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */
415 { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */
416 { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */
417 { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */
418 { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */
419 { 0, NULL }
420};
421
Harald Welte9ba50052010-03-14 15:45:01 +0800422/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200423static const struct value_string ns_cause_str[] = {
424 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
425 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
426 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
427 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
428 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
429 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
430 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
431 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200432 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200433 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
434 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
Harald Welte47cf21f2019-02-23 13:42:06 +0100435 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
436 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
437 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
438 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
439 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
440 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
441 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
Harald Welte2577d412010-05-02 09:37:45 +0200442 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800443};
444
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200445/*! Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200446const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800447{
Harald Welte2577d412010-05-02 09:37:45 +0200448 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800449}
450
Harald Welte24a655f2010-04-30 19:54:29 +0200451static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200452extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200453
Harald Welte94c9b442019-03-16 14:38:19 +0100454static bool ns_is_sns(uint8_t pdu_type)
455{
456 switch (pdu_type) {
457 case SNS_PDUT_CONFIG:
458 case SNS_PDUT_ACK:
459 case SNS_PDUT_ADD:
460 case SNS_PDUT_CHANGE_WEIGHT:
461 case SNS_PDUT_DELETE:
462 case SNS_PDUT_CONFIG_ACK:
463 case SNS_PDUT_SIZE:
464 case SNS_PDUT_SIZE_ACK:
465 return true;
466 default:
467 return false;
468 }
469}
470
Harald Welte24a655f2010-04-30 19:54:29 +0200471static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800472{
Harald Welte94c9b442019-03-16 14:38:19 +0100473 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200474 int ret;
475
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100476 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200477
Harald Welte94c9b442019-03-16 14:38:19 +0100478 /* A pre-configured endpoint shall not be used for NSE data or signalling
479 * traffic (with the exception of Size and Configuration procedures) unless it
480 * is configured by the SGSN using the auto-configuration procedures. */
481 if (nsvc_is_not_used(nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
482 LOGP(DNS, LOGL_NOTICE, "Not transmitting %s on unused/pre-configured endpoint\n",
483 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
484 msgb_free(msg);
485 return -EINVAL;
486 }
487
Harald Welte144e0292010-05-13 11:45:07 +0200488 /* Increment number of Uplink bytes */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200489 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_PKTS_OUT));
490 rate_ctr_add(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_BYTES_OUT), msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200491
Harald Welteb3ee2652010-05-19 14:38:50 +0200492 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200493 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200494 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200495 if (ret < 0)
496 LOGP(DNS, LOGL_INFO,
497 "failed to send NS message via UDP: %s\n",
498 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200499 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200500 case GPRS_NS_LL_FR_GRE:
501 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200502 if (ret < 0)
503 LOGP(DNS, LOGL_INFO,
504 "failed to send NS message via FR/GRE: %s\n",
505 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200506 break;
Harald Weltef030b212010-04-26 19:18:54 +0200507 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200508 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200509 msgb_free(msg);
510 ret = -EIO;
511 break;
512 }
513 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800514}
515
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200516static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800517{
Harald Welteba4c6662010-05-19 15:38:10 +0200518 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800519 struct gprs_ns_hdr *nsh;
520
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100521 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200522
Harald Welte9ba50052010-03-14 15:45:01 +0800523 if (!msg)
524 return -ENOMEM;
525
Harald Welte144e0292010-05-13 11:45:07 +0200526 msg->l2h = msgb_put(msg, sizeof(*nsh));
527 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800528
529 nsh->pdu_type = pdu_type;
530
Harald Welte24a655f2010-04-30 19:54:29 +0200531 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800532}
533
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200534/*! Transmit a NS-RESET on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100535 * \param[in] nsvc NS-VC used for transmission
536 * \paam[in] cause Numeric NS cause value
537 */
Harald Welte834f26d2010-05-11 06:20:54 +0200538int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200539{
Harald Welte047f3872018-07-01 21:04:45 +0200540 struct msgb *msg;
Harald Welte69a4cf22010-05-03 20:55:10 +0200541 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200542 uint16_t nsvci = osmo_htons(nsvc->nsvci);
543 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200544
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100545 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200546
Harald Welte047f3872018-07-01 21:04:45 +0200547 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET");
548
549 msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200550 if (!msg)
551 return -ENOMEM;
552
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000553 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
554 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
555
Harald Welte144e0292010-05-13 11:45:07 +0200556 msg->l2h = msgb_put(msg, sizeof(*nsh));
557 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200558 nsh->pdu_type = NS_PDUT_RESET;
559
560 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
561 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
562 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
563
564 return gprs_ns_tx(nsvc, msg);
565
566}
567
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200568/*! Transmit a NS-STATUS on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100569 * \param[in] nsvc NS-VC to be used for transmission
570 * \param[in] cause Numeric NS cause value
571 * \param[in] bvci BVCI to be reset within NSVC
572 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200573int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
574 uint16_t bvci, struct msgb *orig_msg)
575{
Harald Welteba4c6662010-05-19 15:38:10 +0200576 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200577 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200578 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200579
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100580 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200581
Harald Weltebfe62e52017-05-15 12:48:30 +0200582 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200583
584 if (!msg)
585 return -ENOMEM;
586
Harald Welte90af1942010-05-15 23:02:24 +0200587 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000588 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
589
Harald Welte144e0292010-05-13 11:45:07 +0200590 msg->l2h = msgb_put(msg, sizeof(*nsh));
591 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200592 nsh->pdu_type = NS_PDUT_STATUS;
593
594 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
595
596 /* Section 9.2.7.1: Static conditions for NS-VCI */
597 if (cause == NS_CAUSE_NSVC_BLOCKED ||
598 cause == NS_CAUSE_NSVC_UNKNOWN)
599 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
600
601 /* Section 9.2.7.2: Static conditions for NS PDU */
602 switch (cause) {
603 case NS_CAUSE_SEM_INCORR_PDU:
604 case NS_CAUSE_PDU_INCOMP_PSTATE:
605 case NS_CAUSE_PROTO_ERR_UNSPEC:
606 case NS_CAUSE_INVAL_ESSENT_IE:
607 case NS_CAUSE_MISSING_ESSENT_IE:
608 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
609 orig_msg->l2h);
610 break;
611 default:
612 break;
613 }
614
615 /* Section 9.2.7.3: Static conditions for BVCI */
616 if (cause == NS_CAUSE_BVCI_UNKNOWN)
617 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
618
619 return gprs_ns_tx(nsvc, msg);
620}
621
Alexander Couzens9c86a2c2020-07-12 20:04:07 +0200622/*! Transmit a NS-BLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100623 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
624 * \param[in] cause Numeric NS Cause value
625 * \returns 0 in case of success
626 */
Harald Welte834f26d2010-05-11 06:20:54 +0200627int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
628{
Harald Welte047f3872018-07-01 21:04:45 +0200629 struct msgb *msg;
Harald Welte834f26d2010-05-11 06:20:54 +0200630 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200631 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200632
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100633 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200634
Harald Welte047f3872018-07-01 21:04:45 +0200635 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK");
636
637 msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200638 if (!msg)
639 return -ENOMEM;
640
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000641 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
642 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
643
Harald Welte834f26d2010-05-11 06:20:54 +0200644 /* be conservative and mark it as blocked even now! */
Maxc513c0f2017-10-23 15:11:14 +0200645 ns_mark_blocked(nsvc);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200646 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_BLOCKED));
Harald Welte834f26d2010-05-11 06:20:54 +0200647
Harald Welte144e0292010-05-13 11:45:07 +0200648 msg->l2h = msgb_put(msg, sizeof(*nsh));
649 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200650 nsh->pdu_type = NS_PDUT_BLOCK;
651
652 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
653 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
654
655 return gprs_ns_tx(nsvc, msg);
656}
657
Harald Welte9ef2e152019-02-19 23:42:04 +0100658/*! Transmit a NS-BLOCK-ACK on a given NS-VC
659 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
660 * \returns 0 in case of success
661 */
662static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc)
663{
664 struct msgb *msg;
665 struct gprs_ns_hdr *nsh;
666 uint16_t nsvci = osmo_htons(nsvc->nsvci);
667
668 log_set_context(LOG_CTX_GB_NSVC, nsvc);
669
Harald Welte047f3872018-07-01 21:04:45 +0200670 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK ACK");
671
Harald Welte9ef2e152019-02-19 23:42:04 +0100672 msg = gprs_ns_msgb_alloc();
673 if (!msg)
674 return -ENOMEM;
675
676 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci);
677
678 /* be conservative and mark it as blocked even now! */
679 msg->l2h = msgb_put(msg, sizeof(*nsh));
680 nsh = (struct gprs_ns_hdr *) msg->l2h;
681 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
682
683 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
684
685 return gprs_ns_tx(nsvc, msg);
686}
687
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200688/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100689 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
690 * \returns 0 in case of success
691 */
Harald Welte834f26d2010-05-11 06:20:54 +0200692int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
693{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100694 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200695
696 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK");
697
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000698 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
699 nsvc->nsei, nsvc->nsvci);
700
Harald Welte834f26d2010-05-11 06:20:54 +0200701 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
702}
703
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200704/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100705 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
706 * \returns 0 in case of success
707 */
Harald Welteb983c312010-05-12 12:11:45 +0000708int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
709{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100710 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000711 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
712 nsvc->nsei, nsvc->nsvci);
713
714 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
715}
716
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200717/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100718 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
719 * \returns 0 in case of success
720 */
Harald Welteb983c312010-05-12 12:11:45 +0000721int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
722{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100723 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000724 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
725 nsvc->nsei, nsvc->nsvci);
726
727 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
728}
729
Harald Weltefe4ab902010-05-12 17:19:53 +0000730static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
731 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
732 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
733 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200734};
735
Harald Welte7c24b9e2010-05-12 12:21:52 +0000736static const struct value_string timer_mode_strs[] = {
737 { NSVC_TIMER_TNS_RESET, "tns-reset" },
738 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
739 { NSVC_TIMER_TNS_TEST, "tns-test" },
740 { 0, NULL }
741};
742
Harald Welte69a4cf22010-05-03 20:55:10 +0200743static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
744{
Harald Weltefe4ab902010-05-12 17:19:53 +0000745 enum ns_timeout tout = timer_mode_tout[mode];
746 unsigned int seconds = nsvc->nsi->timeout[tout];
747
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100748 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000749 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
750 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000751 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100752
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200753 if (osmo_timer_pending(&nsvc->timer))
754 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200755
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200756 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200757 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200758 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200759}
760
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200761static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
762{
763 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200764 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200765 timersub(&now, &nsvc->timer_started, &elapsed);
766
767 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
768}
769
Harald Welte80405452010-05-03 20:16:13 +0200770static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800771{
772 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000773 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
774 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800775
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100776 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000777 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
778 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000779 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100780
Harald Welte80405452010-05-03 20:16:13 +0200781 switch (nsvc->timer_mode) {
782 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800783 /* Tns-alive case: we expired without response ! */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200784 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_LOST_ALIVE));
Harald Welte9ba50052010-03-14 15:45:01 +0800785 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000786 if (nsvc->alive_retries >
787 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte047f3872018-07-01 21:04:45 +0200788 /* mark as dead (and blocked unless IP-SNS) */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200789 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_DEAD));
Harald Welte057122e2020-06-06 22:04:20 +0200790 if (!nsvc->nsi->bss_sns_fi && nsvc->nsi->nsip.use_reset_block_unblock) {
Harald Welte047f3872018-07-01 21:04:45 +0200791 ns_set_state(nsvc, NSE_S_BLOCKED);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200792 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_BLOCKED));
Harald Welte047f3872018-07-01 21:04:45 +0200793 } else
794 ns_set_state(nsvc, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200795 LOGP(DNS, LOGL_NOTICE,
796 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200797 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000798 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200799 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
Harald Welte047f3872018-07-01 21:04:45 +0200800 /* FIXME: should we send this signal in case of SNS? */
Harald Welte057122e2020-06-06 22:04:20 +0200801 if (!nsvc->nsi->bss_sns_fi && nsvc->nsi->nsip.use_reset_block_unblock)
Harald Welte047f3872018-07-01 21:04:45 +0200802 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800803 return;
804 }
Harald Welteb983c312010-05-12 12:11:45 +0000805 /* Tns-test case: send NS-ALIVE PDU */
806 gprs_ns_tx_alive(nsvc);
807 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200808 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200809 break;
810 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800811 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000812 gprs_ns_tx_alive(nsvc);
813 /* start Tns-alive timer (transition into faster
814 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000815 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200816 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
817 break;
818 case NSVC_TIMER_TNS_RESET:
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200819 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_LOST_RESET));
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100820 if (!(nsvc->state & NSE_S_RESET))
821 LOGP(DNS, LOGL_NOTICE,
822 "NSEI=%u Reset timed out but RESET flag is not set\n",
823 nsvc->nsei);
824 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200825 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200826 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200827 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200828 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200829 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200830 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200831 case _NSVC_TIMER_NR:
832 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800833 }
Harald Welte9ba50052010-03-14 15:45:01 +0800834}
835
836/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800837static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800838{
Harald Welte047f3872018-07-01 21:04:45 +0200839 struct msgb *msg;
Harald Welte9ba50052010-03-14 15:45:01 +0800840 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200841 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800842
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100843 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200844
845 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET ACK");
846
847 msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800848 if (!msg)
849 return -ENOMEM;
850
Harald Weltebfe62e52017-05-15 12:48:30 +0200851 nsvci = osmo_htons(nsvc->nsvci);
852 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800853
Harald Welte144e0292010-05-13 11:45:07 +0200854 msg->l2h = msgb_put(msg, sizeof(*nsh));
855 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800856
857 nsh->pdu_type = NS_PDUT_RESET_ACK;
858
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200859 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200860 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800861
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200862 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
863 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800864
Harald Welte24a655f2010-04-30 19:54:29 +0200865 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800866}
867
Harald Welte17a642d2018-07-01 19:09:52 +0200868/*! Encode + Transmit a SNS-ACK as per Section 9.3.1.
869 * \param[in] nsvc NS-VC through which to transmit the ACK
870 * \param[in] trans_id Transaction ID which to acknowledge
871 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
872 * \param[in] ip4_elems Array of IPv4 Elements
873 * \param[in] num_ip4_elems number of ip4_elems
874 * \returns 0 on success; negative in case of error */
875int gprs_ns_tx_sns_ack(struct gprs_nsvc *nsvc, uint8_t trans_id, uint8_t *cause,
876 const struct gprs_ns_ie_ip4_elem *ip4_elems,
877 unsigned int num_ip4_elems)
878{
879 struct msgb *msg = gprs_ns_msgb_alloc();
880 struct gprs_ns_hdr *nsh;
881 uint16_t nsei;
882
883 log_set_context(LOG_CTX_GB_NSVC, nsvc);
884 if (!msg)
885 return -ENOMEM;
886
Harald Welte047f3872018-07-01 21:04:45 +0200887 if (!nsvc->nsi->bss_sns_fi) {
888 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
889 nsvc->nsei);
890 msgb_free(msg);
891 return -EIO;
892 }
893
Harald Welte17a642d2018-07-01 19:09:52 +0200894 nsei = osmo_htons(nsvc->nsei);
895
896 msg->l2h = msgb_put(msg, sizeof(*nsh));
897 nsh = (struct gprs_ns_hdr *) msg->l2h;
898
899 nsh->pdu_type = SNS_PDUT_ACK;
900 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
901 msgb_v_put(msg, trans_id);
902 if (cause)
903 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
904 if (ip4_elems) {
905 /* List of IP4 Elements 10.3.2c */
906 msgb_tvlv_put(msg, NS_IE_IPv4_LIST,
907 num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
908 (const uint8_t *)ip4_elems);
909 }
910 /* FIXME: List of IP6 elements 10.3.2d */
911 return gprs_ns_tx(nsvc, msg);
912}
913
914/*! Encode + Transmit a SNS-CONFIG as per Section 9.3.4.
915 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
916 * \param[in] end_flag Whether or not this is the last SNS-CONFIG
917 * \param[in] ip4_elems Array of IPv4 Elements
918 * \param[in] num_ip4_elems number of ip4_elems
919 * \returns 0 on success; negative in case of error */
920int gprs_ns_tx_sns_config(struct gprs_nsvc *nsvc, bool end_flag,
921 const struct gprs_ns_ie_ip4_elem *ip4_elems,
922 unsigned int num_ip4_elems)
923{
924 struct msgb *msg = gprs_ns_msgb_alloc();
925 struct gprs_ns_hdr *nsh;
926 uint16_t nsei;
927
928 log_set_context(LOG_CTX_GB_NSVC, nsvc);
929 if (!msg)
930 return -ENOMEM;
931
Harald Welte047f3872018-07-01 21:04:45 +0200932 if (!nsvc->nsi->bss_sns_fi) {
933 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
934 nsvc->nsei);
935 msgb_free(msg);
936 return -EIO;
937 }
938
Harald Welte17a642d2018-07-01 19:09:52 +0200939 nsei = osmo_htons(nsvc->nsei);
940
941 msg->l2h = msgb_put(msg, sizeof(*nsh));
942 nsh = (struct gprs_ns_hdr *) msg->l2h;
943
944 nsh->pdu_type = SNS_PDUT_CONFIG;
945
946 msgb_v_put(msg, end_flag ? 0x01 : 0x00);
947 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
948
949 /* List of IP4 Elements 10.3.2c */
950 msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
951 (const uint8_t *)ip4_elems);
952 /* FIXME: List of IP6 elements 10.3.2d */
953
954 return gprs_ns_tx(nsvc, msg);
955}
956
957/*! Encode + Transmit a SNS-CONFIG-ACK as per Section 9.3.5.
958 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG-ACK
959 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
960 * \returns 0 on success; negative in case of error */
961int gprs_ns_tx_sns_config_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
962{
963 struct msgb *msg = gprs_ns_msgb_alloc();
964 struct gprs_ns_hdr *nsh;
965 uint16_t nsei;
966
967 log_set_context(LOG_CTX_GB_NSVC, nsvc);
968 if (!msg)
969 return -ENOMEM;
970
Harald Welte047f3872018-07-01 21:04:45 +0200971 if (!nsvc->nsi->bss_sns_fi) {
972 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
973 nsvc->nsei);
974 msgb_free(msg);
975 return -EIO;
976 }
977
Harald Welte17a642d2018-07-01 19:09:52 +0200978 nsei = osmo_htons(nsvc->nsei);
979
980 msg->l2h = msgb_put(msg, sizeof(*nsh));
981 nsh = (struct gprs_ns_hdr *) msg->l2h;
982
983 nsh->pdu_type = SNS_PDUT_CONFIG_ACK;
984
985 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
986 if (cause)
987 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
988
989 return gprs_ns_tx(nsvc, msg);
990}
991
992
993/*! Encode + transmit a SNS-SIZE as per Section 9.3.7.
994 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE
995 * \param[in] reset_flag Whether or not to add a RESET flag
996 * \param[in] max_nr_nsvc Maximum number of NS-VCs
997 * \param[in] ip4_ep_nr Number of IPv4 endpoints (NULL if none)
998 * \param[in] ip6_ep_nr Number of IPv6 endpoints (NULL if none)
999 * \returns 0 on success; negative in case of error */
1000int gprs_ns_tx_sns_size(struct gprs_nsvc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
1001 uint16_t *ip4_ep_nr, uint16_t *ip6_ep_nr)
1002{
1003 struct msgb *msg = gprs_ns_msgb_alloc();
1004 struct gprs_ns_hdr *nsh;
1005 uint16_t nsei;
1006
1007 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1008 if (!msg)
1009 return -ENOMEM;
1010
Harald Welte047f3872018-07-01 21:04:45 +02001011 if (!nsvc->nsi->bss_sns_fi) {
1012 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1013 nsvc->nsei);
1014 msgb_free(msg);
1015 return -EIO;
1016 }
1017
Harald Welte17a642d2018-07-01 19:09:52 +02001018 nsei = osmo_htons(nsvc->nsei);
1019
1020 msg->l2h = msgb_put(msg, sizeof(*nsh));
1021 nsh = (struct gprs_ns_hdr *) msg->l2h;
1022
1023 nsh->pdu_type = SNS_PDUT_SIZE;
1024
1025 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1026 msgb_tv_put(msg, NS_IE_RESET_FLAG, reset_flag ? 0x01 : 0x00);
1027 msgb_tv16_put(msg, NS_IE_MAX_NR_NSVC, max_nr_nsvc);
1028 if (ip4_ep_nr)
1029 msgb_tv16_put(msg, NS_IE_IPv4_EP_NR, *ip4_ep_nr);
1030 if (ip6_ep_nr)
1031 msgb_tv16_put(msg, NS_IE_IPv6_EP_NR, *ip6_ep_nr);
1032
1033 return gprs_ns_tx(nsvc, msg);
1034}
1035
1036/*! Encode + Transmit a SNS-SIZE-ACK as per Section 9.3.8.
1037 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE-ACK
1038 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
1039 * \returns 0 on success; negative in case of error */
1040int gprs_ns_tx_sns_size_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
1041{
1042 struct msgb *msg = gprs_ns_msgb_alloc();
1043 struct gprs_ns_hdr *nsh;
1044 uint16_t nsei;
1045
1046 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1047 if (!msg)
1048 return -ENOMEM;
1049
Harald Welte047f3872018-07-01 21:04:45 +02001050 if (!nsvc->nsi->bss_sns_fi) {
1051 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1052 nsvc->nsei);
1053 msgb_free(msg);
1054 return -EIO;
1055 }
1056
Harald Welte17a642d2018-07-01 19:09:52 +02001057 nsei = osmo_htons(nsvc->nsei);
1058
1059 msg->l2h = msgb_put(msg, sizeof(*nsh));
1060 nsh = (struct gprs_ns_hdr *) msg->l2h;
1061
1062 nsh->pdu_type = SNS_PDUT_SIZE_ACK;
1063
1064 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1065 if (cause)
1066 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
1067
1068 return gprs_ns_tx(nsvc, msg);
1069}
1070
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001071/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +01001072 * \param[in] nsi NS-instance on which we shall transmit
1073 * \param[in] msg struct msgb to be trasnmitted
1074 *
1075 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
Alexander Chemeris8abe45d2020-03-02 17:05:27 +03001076 * if the NS-VC is ALIVE and not BLOCKED. After that, it adds a NS
Harald Weltea9f23c82011-11-23 15:01:31 +01001077 * header for the NS-UNITDATA message type and sends it off.
1078 *
1079 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
1080 */
Harald Welte24a655f2010-04-30 19:54:29 +02001081int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001082{
Harald Welte24a655f2010-04-30 19:54:29 +02001083 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001084 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001085 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +02001086
Harald Welte047f3872018-07-01 21:04:45 +02001087 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg), msgb_bvci(msg));
Harald Welte24a655f2010-04-30 19:54:29 +02001088 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +02001089 int rc;
1090 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
1091 LOGP(DNS, LOGL_ERROR,
1092 "All NS-VCs for NSEI %u are either dead or blocked!\n",
1093 msgb_nsei(msg));
1094 rc = -EBUSY;
1095 } else {
1096 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
1097 "to NS-VC!\n", msgb_nsei(msg));
1098 rc = -EINVAL;
1099 }
1100
Harald Weltef47df642010-08-09 21:15:40 +08001101 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +02001102 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +02001103 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001104 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001105
Harald Welteec20ba42010-05-13 12:18:49 +02001106 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
1107 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001108 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001109 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +08001110 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001111 return -EIO;
1112 }
1113
1114 nsh->pdu_type = NS_PDUT_UNITDATA;
1115 /* spare octet in data[0] */
1116 nsh->data[1] = bvci >> 8;
1117 nsh->data[2] = bvci & 0xff;
1118
Harald Welte24a655f2010-04-30 19:54:29 +02001119 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001120}
1121
1122/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +02001123static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001124{
1125 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001126 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001127
Harald Weltec1402a62010-05-12 11:48:44 +02001128 if (nsvc->state & NSE_S_BLOCKED)
1129 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
1130 0, msg);
1131
Harald Welte9ba50052010-03-14 15:45:01 +08001132 /* spare octet in data[0] */
1133 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +02001134 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +02001135 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001136
1137 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +02001138 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001139}
1140
1141/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +02001142static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001143{
Harald Weltef030b212010-04-26 19:18:54 +02001144 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001145 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001146 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +08001147 int rc;
1148
Harald Welte41337832010-05-16 00:24:26 +02001149 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +08001150
Harald Weltebd33f3d2010-05-30 17:19:38 +02001151 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1152 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001153 if (rc < 0) {
1154 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
1155 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
1156 "Error during TLV Parse\n", nsvc->nsei);
1157 return rc;
1158 }
Harald Welte9ba50052010-03-14 15:45:01 +08001159
1160 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001161 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +08001162 return -EINVAL;
1163 }
1164
1165 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +02001166 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +08001167
1168 return 0;
1169}
1170
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001171/* Replace a nsvc object with another based on NSVCI.
1172 * This function replaces looks for a NSVC with the given NSVCI and replaces it
1173 * if possible and necessary. If replaced, the former value of *nsvc is
1174 * returned in *old_nsvc.
1175 * \return != 0 if *nsvc points to a matching NSVC.
1176 */
1177static int gprs_nsvc_replace_if_found(uint16_t nsvci,
1178 struct gprs_nsvc **nsvc,
1179 struct gprs_nsvc **old_nsvc)
1180{
1181 struct gprs_nsvc *matching_nsvc;
1182
1183 if ((*nsvc)->nsvci == nsvci) {
1184 *old_nsvc = NULL;
1185 return 1;
1186 }
1187
1188 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
1189
1190 if (!matching_nsvc)
1191 return 0;
1192
1193 /* The NS-VCI is already used by this NS-VC */
1194
1195 char *old_peer;
1196
1197 /* Exchange the NS-VC objects */
1198 *old_nsvc = *nsvc;
1199 *nsvc = matching_nsvc;
1200
1201 /* Do logging */
1202 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
1203 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
1204 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
1205
1206 talloc_free(old_peer);
1207
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001208 return 1;
1209}
1210
Harald Welte9ba50052010-03-14 15:45:01 +08001211/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001212static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001213{
1214 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001215 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001216 uint8_t cause;
1217 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001218 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +08001219 int rc;
1220
Harald Weltebd33f3d2010-05-30 17:19:38 +02001221 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1222 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001223 if (rc < 0) {
1224 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001225 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +02001226 return rc;
1227 }
Harald Welte9ba50052010-03-14 15:45:01 +08001228
1229 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1230 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
1231 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001232 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001233 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001234 return -EINVAL;
1235 }
1236
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001237 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +02001238 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1239 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +08001240
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001241 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
1242 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1243 nsei, nsvci, gprs_ns_cause_str(cause));
1244
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001245 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
1246 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001247 /* The incoming RESET doesn't match the NSVCI. Send an
1248 * appropriate RESET_ACK and ignore the RESET.
1249 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
1250 */
1251 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1252 NS_PDUT_RESET,
1253 NS_IE_VCI);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001254 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_INV_VCI));
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001255 gprs_ns_tx_reset_ack(*nsvc);
1256 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001257 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001258
1259 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001260 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
1261 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
1262 "at %s\n",
1263 nsvci, (*nsvc)->nsvci,
1264 gprs_ns_ll_str(*nsvc));
1265 orig_nsvc = *nsvc;
Harald Welte68944962020-06-07 10:31:48 +02001266 *nsvc = gprs_nsvc_create2((*nsvc)->nsi, nsvci, 1, 1);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001267 (*nsvc)->nsei = nsei;
1268 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001269 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001270
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001271 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
1272 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1273 /* The incoming RESET doesn't match the NSEI. Send an
1274 * appropriate RESET_ACK and ignore the RESET.
1275 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
1276 */
1277 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1278 NS_PDUT_RESET,
1279 NS_IE_NSEI);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001280 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_INV_NSEI));
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +02001281 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001282 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001283 return 0;
1284 }
1285
1286 /* NSEI has changed */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001287 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_NSEI_CHG));
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001288 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001289 }
Harald Weltebbc9fac2010-05-03 18:54:12 +02001290
Harald Weltec1402a62010-05-12 11:48:44 +02001291 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001292 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +02001293
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001294 if (orig_nsvc) {
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001295 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_REPLACED));
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001296 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001297
1298 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001299 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1300 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001301 } else {
1302 (*nsvc)->nsei = nsei;
1303 (*nsvc)->nsvci = nsvci;
1304 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +01001305 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
1306 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001307 }
Harald Welte9ba50052010-03-14 15:45:01 +08001308
Harald Welte834f26d2010-05-11 06:20:54 +02001309 /* inform interested parties about the fact that this NSVC
1310 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001311 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +02001312
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001313 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001314
1315 /* start the test procedure */
Harald Welte35042a22019-02-23 14:07:00 +01001316 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001317
1318 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +08001319}
1320
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001321static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
1322{
1323 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1324 struct tlv_parsed tp;
1325 uint16_t nsvci, nsei;
1326 struct gprs_nsvc *orig_nsvc = NULL;
1327 int rc;
1328
1329 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1330 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1331 if (rc < 0) {
1332 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
1333 "Error during TLV Parse\n", (*nsvc)->nsei);
1334 return rc;
1335 }
1336
1337 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
1338 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1339 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +02001340 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001341 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001342 return -EINVAL;
1343 }
1344
Harald Weltebfe62e52017-05-15 12:48:30 +02001345 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1346 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001347
1348 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
1349 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1350 nsei, nsvci);
1351
1352 if (!((*nsvc)->state & NSE_S_RESET)) {
1353 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1354 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1355 */
1356 LOGP(DNS, LOGL_ERROR,
1357 "NS RESET ACK Discarding unexpected message for "
1358 "NS-VCI %d from SGSN NSEI=%d\n",
1359 nsvci, nsei);
1360 return 0;
1361 }
1362
1363 if (!(*nsvc)->nsvci_is_valid) {
1364 LOGP(DNS, LOGL_NOTICE,
1365 "NS RESET ACK Uninitialised NS-VC (%u) for "
1366 "NS-VCI %d, NSEI=%d from %s\n",
1367 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1368 return -EINVAL;
1369 }
1370
1371 if ((*nsvc)->nsvci != nsvci) {
1372 /* NS-VCI has changed */
1373
1374 /* if !0, use another NSVC object that matches the NSVCI */
1375 int use_other_nsvc;
1376
1377 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001378 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1379 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001380
1381 if (use_other_nsvc)
1382 /* Update *nsvc to point to the right NSVC object */
1383 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1384 &orig_nsvc);
1385
1386 if (!use_other_nsvc) {
1387 /* The incoming RESET_ACK doesn't match the NSVCI.
1388 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1389 */
1390 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1391 NS_PDUT_RESET_ACK,
1392 NS_IE_VCI);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001393 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_INV_VCI));
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001394 LOGP(DNS, LOGL_ERROR,
1395 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1396 "from %s\n",
1397 nsvci,
1398 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1399 nsei, gprs_ns_ll_str(*nsvc));
1400 return -EINVAL;
1401 }
1402
1403 /* Notify others */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001404 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_REPLACED));
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001405 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1406
1407 /* Update the ll info fields */
1408 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1409 gprs_ns_ll_clear(orig_nsvc);
1410 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001411 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1412 /* The incoming RESET_ACK doesn't match the NSEI.
1413 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1414 */
1415 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1416 NS_PDUT_RESET_ACK,
1417 NS_IE_NSEI);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001418 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_INV_NSEI));
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001419 LOGP(DNS, LOGL_ERROR,
1420 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1421 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1422 return -EINVAL;
1423 }
1424
1425 /* NSEI has changed */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001426 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_NSEI_CHG));
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001427 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001428 }
1429
1430 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001431 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1432 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001433 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_BLOCKED));
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001434 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1435 /* stop RESET timer */
1436 osmo_timer_del(&(*nsvc)->timer);
1437 }
1438 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte35042a22019-02-23 14:07:00 +01001439 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001440
Harald Welte35042a22019-02-23 14:07:00 +01001441 return 0;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001442}
1443
Harald Welte834f26d2010-05-11 06:20:54 +02001444static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1445{
1446 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1447 struct tlv_parsed tp;
1448 uint8_t *cause;
1449 int rc;
1450
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001451 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001452
Maxc513c0f2017-10-23 15:11:14 +02001453 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001454
Harald Weltebd33f3d2010-05-30 17:19:38 +02001455 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1456 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001457 if (rc < 0) {
1458 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1459 "Error during TLV Parse\n", nsvc->nsei);
1460 return rc;
1461 }
Harald Welte834f26d2010-05-11 06:20:54 +02001462
1463 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1464 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001465 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001466 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001467 return -EINVAL;
1468 }
1469
1470 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1471 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1472
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001473 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001474 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_BLOCKED));
Harald Welte834f26d2010-05-11 06:20:54 +02001475
Harald Welte9ef2e152019-02-19 23:42:04 +01001476 return gprs_ns_tx_block_ack(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001477}
1478
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001479int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1480 struct gprs_nsvc *fallback_nsvc,
1481 struct gprs_nsvc **new_nsvc);
1482
1483int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001484 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001485
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001486/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001487 * \param nsi NS instance to which the data belongs
1488 * \param[in] msg message buffer containing newly-received data
1489 * \param[in] saddr socketaddr from which data was received
1490 * \param[in] ll link-layer type in which data was received
1491 * \returns 0 in case of success, < 0 in case of error
1492 *
1493 * This is the main entry point int othe NS imlementation where frames
1494 * from the underlying transport (normally UDP) enter.
1495 */
Harald Weltef030b212010-04-26 19:18:54 +02001496int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001497 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001498{
Harald Weltef030b212010-04-26 19:18:54 +02001499 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001500 int rc = 0;
1501
Harald Weltef030b212010-04-26 19:18:54 +02001502 /* look up the NSVC based on source address */
Harald Welte047f3872018-07-01 21:04:45 +02001503 nsvc = gprs_nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001504
Harald Weltef030b212010-04-26 19:18:54 +02001505 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001506 struct gprs_nsvc *fallback_nsvc;
1507
1508 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001509 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001510 fallback_nsvc->ip.bts_addr = *saddr;
1511 fallback_nsvc->ll = ll;
1512
1513 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1514
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001515 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001516 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001517
1518 rc = 0;
1519 }
1520
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001521 if (nsvc)
1522 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001523
1524 return rc;
1525}
1526
Harald Welte4a62eda2019-03-18 18:27:00 +01001527char *gprs_ns_ll_str_buf(char *buf, size_t buf_len, const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001528{
Max95308592017-10-24 15:54:28 +02001529 switch(nsvc->ll) {
1530 case GPRS_NS_LL_UDP:
Harald Welte4a62eda2019-03-18 18:27:00 +01001531 snprintf(buf, buf_len, "%s:%u",
Max95308592017-10-24 15:54:28 +02001532 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1533 break;
1534 case GPRS_NS_LL_FR_GRE:
Harald Welte4a62eda2019-03-18 18:27:00 +01001535 snprintf(buf, buf_len, "%s:%u",
Max95308592017-10-24 15:54:28 +02001536 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1537 break;
1538 default:
1539 buf[0] = '\0';
1540 break;
1541 }
1542
Harald Welte4a62eda2019-03-18 18:27:00 +01001543 buf[buf_len - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001544
1545 return buf;
1546}
1547
Harald Welte4a62eda2019-03-18 18:27:00 +01001548const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
1549{
Harald Welte171ef822019-03-28 10:49:05 +01001550 static __thread char buf[80];
Harald Welte4a62eda2019-03-18 18:27:00 +01001551 return gprs_ns_ll_str_buf(buf, sizeof(buf), nsvc);
1552}
1553
Harald Welte179f3572019-03-18 18:38:47 +01001554char *gprs_ns_ll_str_c(const void *ctx, const struct gprs_nsvc *nsvc)
1555{
1556 char *buf = talloc_size(ctx, INET6_ADDRSTRLEN+10);
1557 if (!buf)
1558 return buf;
1559 return gprs_ns_ll_str_buf(buf, INET6_ADDRSTRLEN+10, nsvc);
1560}
1561
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001562void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1563{
1564 nsvc->ll = other->ll;
1565
1566 switch (nsvc->ll) {
1567 case GPRS_NS_LL_UDP:
1568 nsvc->ip = other->ip;
1569 break;
1570 case GPRS_NS_LL_FR_GRE:
1571 nsvc->frgre = other->frgre;
1572 break;
1573 default:
1574 break;
1575 }
1576}
1577
1578void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1579{
1580 switch (nsvc->ll) {
1581 case GPRS_NS_LL_UDP:
1582 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1583 nsvc->ip.bts_addr.sin_port = 0;
1584 break;
1585 case GPRS_NS_LL_FR_GRE:
1586 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1587 nsvc->frgre.bts_addr.sin_port = 0;
1588 break;
1589 default:
1590 break;
1591 }
1592}
1593
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001594/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001595 * \param nsi NS instance to which the data belongs
1596 * \param[in] msg message buffer containing newly-received data
1597 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001598 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001599 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1600 * been created or found
1601 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1602 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1603 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1604 * has been created and registered, and GPRS_NS_CS_FOUND if an
1605 * existing NS-VC object has been found with the same NSEI.
1606 *
1607 * This contains the initial NS automaton state (NS-VC not yet attached).
1608 */
1609int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1610 struct gprs_nsvc *fallback_nsvc,
1611 struct gprs_nsvc **new_nsvc)
1612{
1613 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1614 struct gprs_nsvc *existing_nsvc;
1615
1616 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001617 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001618 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001619
1620 int rc;
1621
1622 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001623 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001624 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1625 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001626 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001627 return GPRS_NS_CS_SKIPPED;
1628 }
1629
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001630 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1631 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1632 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1633 "for non-existing NS-VC\n",
1634 gprs_ns_ll_str(fallback_nsvc));
1635 return GPRS_NS_CS_SKIPPED;
1636 }
1637
1638 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1639 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1640 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1641 "for non-existing NS-VC\n",
1642 gprs_ns_ll_str(fallback_nsvc));
1643 return GPRS_NS_CS_SKIPPED;
1644 }
1645
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001646 /* Only the RESET procedure creates a new NSVC */
1647 if (nsh->pdu_type != NS_PDUT_RESET) {
1648 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001649 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001650 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001651 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001652 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001653 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001654 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001655 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001656
1657 rc = gprs_ns_tx_status(fallback_nsvc,
1658 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1659 if (rc < 0) {
1660 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001661 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001662 return rc;
1663 }
1664 return GPRS_NS_CS_REJECTED;
1665 }
1666
1667 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1668 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1669 if (rc < 0) {
1670 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1671 "TLV Parse\n", rc);
1672 return rc;
1673 }
1674 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1675 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1676 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001677 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001678 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001679 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001680 return -EINVAL;
1681 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001682 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1683 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001684 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001685 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001686 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001687 if (!existing_nsvc) {
Harald Welte68944962020-06-07 10:31:48 +02001688 *new_nsvc = gprs_nsvc_create2(nsi, 0xffff, 1, 1);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001689 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001690 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001691 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001692 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001693 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001694
1695 return GPRS_NS_CS_CREATED;
1696 }
1697
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001698 /* Check NSEI */
1699 if (existing_nsvc->nsei != nsei) {
1700 LOGP(DNS, LOGL_NOTICE,
1701 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1702 nsvci, existing_nsvc->nsei, nsei);
1703
1704 /* Override old NSEI */
1705 existing_nsvc->nsei = nsei;
1706
1707 /* Do statistics */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001708 rate_ctr_inc(rate_ctr_group_get_ctr(existing_nsvc->ctrg, NS_CTR_NSEI_CHG));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001709 }
1710
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001711 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001712 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001713 return GPRS_NS_CS_FOUND;
1714}
1715
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001716/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001717 * \param nsi NS instance to which the data belongs
1718 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001719 * \param[inout] nsvc refers to the virtual connection, may be modified when
1720 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001721 * \returns 0 in case of success, < 0 in case of error
1722 *
1723 * This contains the main NS automaton.
1724 */
1725int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001726 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001727{
1728 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte047f3872018-07-01 21:04:45 +02001729 struct tlv_parsed tp;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001730 int rc = 0;
1731
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001732 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001733
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001734 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001735
Harald Welte144e0292010-05-13 11:45:07 +02001736 /* Increment number of Incoming bytes */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001737 rate_ctr_inc(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_PKTS_IN));
1738 rate_ctr_add(rate_ctr_group_get_ctr((*nsvc)->ctrg, NS_CTR_BYTES_IN), msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001739
Harald Welte94c9b442019-03-16 14:38:19 +01001740 if (nsvc_is_not_used(*nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
1741 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s on unused/pre-configured endpoint, discarding\n",
1742 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1743 gprs_ns_tx_status(*nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1744 return 0;
1745 }
1746
Harald Welte9ba50052010-03-14 15:45:01 +08001747 switch (nsh->pdu_type) {
1748 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001749 /* If we're dead and blocked and suddenly receive a
1750 * NS-ALIVE out of the blue, we might have been re-started
1751 * and should send a NS-RESET to make sure everything recovers
1752 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001753 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001754 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
Harald Welte057122e2020-06-06 22:04:20 +02001755 else if (!((*nsvc)->state & NSE_S_RESET)) {
1756 /* if we're not alive, we cannot transmit the ACK; set ALIVE */
1757 if (!((*nsvc)->state & NSE_S_ALIVE))
1758 ns_mark_alive(*nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001759 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte057122e2020-06-06 22:04:20 +02001760 }
Harald Welte9ba50052010-03-14 15:45:01 +08001761 break;
1762 case NS_PDUT_ALIVE_ACK:
Harald Welte047f3872018-07-01 21:04:45 +02001763 ns_mark_alive(*nsvc);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001764 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001765 osmo_stat_item_set(osmo_stat_item_group_get_item((*nsvc)->statg, NS_STAT_ALIVE_DELAY),
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001766 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001767 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001768 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1769 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001770 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001771 if ((*nsvc)->state & NSE_S_BLOCKED)
1772 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001773 }
Harald Welte9ba50052010-03-14 15:45:01 +08001774 break;
1775 case NS_PDUT_UNITDATA:
1776 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001777 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001778 break;
1779 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001780 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001781 break;
1782 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001783 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001784 break;
1785 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001786 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001787 break;
1788 case NS_PDUT_UNBLOCK:
1789 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001790 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001791 ns_mark_unblocked(*nsvc);
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001792 /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001793 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Stefan Sperling797558e2018-11-19 17:18:41 +01001794 if (rc < 0)
1795 break;
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001796 /*
1797 * UNBLOCK_ACK has been transmitted.
1798 * Signal handlers may send additional messages following UNBLOCK_ACK under
1799 * the assumption that NS is now in UNBLOCKED state at our peer's end.
1800 */
Stefan Sperling797558e2018-11-19 17:18:41 +01001801 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001802 break;
1803 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001804 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001805 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001806 ns_set_state(*nsvc, NSE_S_ALIVE);
1807 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001808 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001809 break;
1810 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001811 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001812 break;
1813 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001814 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001815 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001816 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001817 break;
Harald Welte047f3872018-07-01 21:04:45 +02001818 case SNS_PDUT_CONFIG:
1819 if (!nsi->bss_sns_fi)
1820 goto unexpected_sns;
1821 /* one additional byte ('end flag') before the TLV part starts */
1822 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+1,
1823 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1824 if (rc < 0) {
1825 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1826 return rc;
1827 }
1828 /* All sub-network service related message types */
1829 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1830 break;
1831 case SNS_PDUT_ACK:
1832 case SNS_PDUT_ADD:
1833 case SNS_PDUT_CHANGE_WEIGHT:
1834 case SNS_PDUT_DELETE:
1835 if (!nsi->bss_sns_fi)
1836 goto unexpected_sns;
1837 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
1838 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+5,
1839 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
1840 if (rc < 0) {
1841 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1842 return rc;
1843 }
1844 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1845 tp.lv[NS_IE_NSEI].len = 2;
1846 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1847 tp.lv[NS_IE_TRANS_ID].len = 1;
1848 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1849 break;
1850 case SNS_PDUT_CONFIG_ACK:
1851 case SNS_PDUT_SIZE:
1852 case SNS_PDUT_SIZE_ACK:
1853 if (!nsi->bss_sns_fi)
1854 goto unexpected_sns;
1855 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1856 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1857 if (rc < 0) {
1858 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1859 return rc;
1860 }
1861 /* All sub-network service related message types */
1862 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1863 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001864 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001865 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001866 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001867 rc = -EINVAL;
1868 break;
Harald Welte047f3872018-07-01 21:04:45 +02001869unexpected_sns:
1870 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s for NS Instance that has no SNS!\n",
1871 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1872 rc = -EINVAL;
1873 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001874 }
1875 return rc;
1876}
1877
Harald Welte047f3872018-07-01 21:04:45 +02001878static bool gprs_sns_fsm_registered = false;
1879
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001880/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001881 * \param[in] cb Call-back function for incoming BSSGP data
1882 * \returns dynamically allocated gprs_ns_inst
1883 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001884struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001885{
Harald Welte797d5332019-12-01 13:38:35 +01001886 struct gprs_ns_inst *nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001887
Harald Welte047f3872018-07-01 21:04:45 +02001888 if (!gprs_sns_fsm_registered) {
Harald Welte797d5332019-12-01 13:38:35 +01001889 int rc = gprs_sns_init();
1890 if (rc < 0)
1891 return NULL;
Harald Welte047f3872018-07-01 21:04:45 +02001892 gprs_sns_fsm_registered = true;
1893 }
1894
Harald Welte797d5332019-12-01 13:38:35 +01001895 nsi = talloc_zero(ctx, struct gprs_ns_inst);
1896 if (!nsi)
1897 return NULL;
Harald Weltef030b212010-04-26 19:18:54 +02001898 nsi->cb = cb;
1899 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001900 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1901 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1902 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1903 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1904 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1905 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1906 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Welte047f3872018-07-01 21:04:45 +02001907 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Harald Weltef030b212010-04-26 19:18:54 +02001908
Harald Welte60cc6ac2010-05-13 14:20:56 +02001909 /* Create the dummy NSVC that we use for sending
1910 * messages to non-existant/unknown NS-VC's */
Harald Welte68944962020-06-07 10:31:48 +02001911 nsi->unknown_nsvc = gprs_nsvc_create2(nsi, 0xfffe, 1, 1);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001912 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001913 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001914 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001915
Harald Welte057122e2020-06-06 22:04:20 +02001916 /* By default we are in IPA compatible mode, that is we use NS-RESET, NS-BLOCK
1917 * and NS-UNBLOCK procedures even for an IP/UDP based Gb interface, in violation
1918 * of 3GPP TS 48.016. */
1919 nsi->nsip.use_reset_block_unblock = true;
1920
Harald Welte3771d092010-04-30 20:26:32 +02001921 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001922}
1923
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001924void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001925{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001926 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001927
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001928 gprs_nsvc_delete(nsi->unknown_nsvc);
1929
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001930 /* delete all NSVCs and clear their timers */
1931 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1932 gprs_nsvc_delete(nsvc);
1933
1934 /* close socket and unregister */
1935 if (nsi->nsip.fd.data) {
1936 close(nsi->nsip.fd.fd);
1937 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001938 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001939 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001940}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001941
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001942/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001943 * \param nsi gprs_ns_inst that is to be destroyed
1944 *
1945 * This function releases all resources associated with the
1946 * NS-instance.
1947 */
1948void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1949{
1950 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001951 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001952 talloc_free(nsi);
1953}
1954
1955
1956/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1957 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1958
1959/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001960static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001961 struct sockaddr_in *saddr)
1962{
Harald Welteba4c6662010-05-19 15:38:10 +02001963 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001964 int ret = 0;
1965 socklen_t saddr_len = sizeof(*saddr);
1966
1967 if (!msg) {
1968 *error = -ENOMEM;
1969 return NULL;
1970 }
1971
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001972 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001973 (struct sockaddr *)saddr, &saddr_len);
1974 if (ret < 0) {
Max38176d22018-11-06 17:04:21 +01001975 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n",
1976 strerror(errno), osmo_sock_get_name2(bfd->fd));
Harald Weltef030b212010-04-26 19:18:54 +02001977 msgb_free(msg);
1978 *error = ret;
1979 return NULL;
1980 } else if (ret == 0) {
1981 msgb_free(msg);
1982 *error = ret;
1983 return NULL;
1984 }
1985
1986 msg->l2h = msg->data;
1987 msgb_put(msg, ret);
1988
1989 return msg;
1990}
1991
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001992static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001993{
1994 int error;
1995 struct sockaddr_in saddr;
1996 struct gprs_ns_inst *nsi = bfd->data;
1997 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1998
1999 if (!msg)
2000 return error;
2001
Harald Welteb3ee2652010-05-19 14:38:50 +02002002 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00002003
2004 msgb_free(msg);
2005
2006 return error;
Harald Weltef030b212010-04-26 19:18:54 +02002007}
2008
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02002009static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02002010{
2011 /* FIXME: actually send the data here instead of nsip_sendmsg() */
2012 return -EIO;
2013}
2014
Harald Welteb3ee2652010-05-19 14:38:50 +02002015static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02002016{
2017 int rc;
2018 struct gprs_ns_inst *nsi = nsvc->nsi;
2019 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
2020
2021 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
2022 (struct sockaddr *)daddr, sizeof(*daddr));
2023
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01002024 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02002025
2026 return rc;
2027}
2028
2029/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02002030static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02002031{
2032 int rc = 0;
2033
Harald Welte16886992019-03-20 10:26:39 +01002034 if (what & OSMO_FD_READ)
Harald Weltef030b212010-04-26 19:18:54 +02002035 rc = handle_nsip_read(bfd);
Harald Welte16886992019-03-20 10:26:39 +01002036 if (what & OSMO_FD_WRITE)
Harald Weltef030b212010-04-26 19:18:54 +02002037 rc = handle_nsip_write(bfd);
2038
2039 return rc;
2040}
2041
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002042/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01002043 * \param[in] nsi NS protocol instance to listen
2044 * \returns >=0 (fd) in case of success, negative in case of error
2045 *
2046 * A call to this function will create a UDP socket bound to the port
2047 * number and IP address specified in the NS protocol instance. The
2048 * file descriptor of the socket will be stored in nsi->nsip.fd.
2049 */
Harald Welte7fb05232010-05-19 15:09:09 +02002050int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02002051{
Harald Welte73952e32012-06-16 14:59:56 +08002052 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002053 struct in_addr remote;
2054 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02002055 int ret;
2056
Harald Weltebfe62e52017-05-15 12:48:30 +02002057 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02002058 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08002059
2060 nsi->nsip.fd.cb = nsip_fd_cb;
2061 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002062
2063 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
2064 /* connect to ensure only we only accept packets from the
2065 * configured remote end/peer */
2066 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
2067 ret =
2068 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
2069 IPPROTO_UDP, inet_ntoa(in),
2070 nsi->nsip.local_port, remote_str,
Harald Welte915caf72021-04-28 19:47:27 +02002071 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT |
2072 OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(nsi->nsip.dscp));
Philipp Maier224a6dd2017-10-17 15:23:53 +02002073
2074 LOGP(DNS, LOGL_NOTICE,
2075 "Listening for nsip packets from %s:%u on %s:%u\n",
2076 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
2077 } else {
2078 /* Accept UDP packets from any source IP/Port */
2079 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
Harald Welte915caf72021-04-28 19:47:27 +02002080 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port,
2081 OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(nsi->nsip.dscp));
Philipp Maier224a6dd2017-10-17 15:23:53 +02002082
2083 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
2084 }
2085
Stefan Sperlingf6538212018-10-10 16:09:32 +02002086 if (ret < 0) {
2087 nsi->nsip.fd.cb = NULL;
2088 nsi->nsip.fd.data = NULL;
Harald Weltef030b212010-04-26 19:18:54 +02002089 return ret;
Stefan Sperlingf6538212018-10-10 16:09:32 +02002090 }
Harald Weltef030b212010-04-26 19:18:54 +02002091
Maxa19c1262017-10-20 15:39:45 +02002092 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 +02002093
2094 return ret;
2095}
Harald Welte3771d092010-04-30 20:26:32 +02002096
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002097/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01002098 * \param[in] nsvc NS-VC in which to start the procedure
2099 * \param[in] cause Numeric NS cause value
2100 *
2101 * This is a high-level function initiating a NS-RESET procedure. It
2102 * will not only send a NS-RESET, but also set the state to BLOCKED and
2103 * start the Tns-reset timer.
2104 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002105int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00002106{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002107 int rc;
2108
Harald Welte047f3872018-07-01 21:04:45 +02002109 ERR_IF_NSVC_USES_SNS(nsvc, "RESET procedure based on API request");
2110
Harald Welteb1020d52010-05-25 22:17:30 +02002111 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
2112 nsvc->nsei);
2113
Harald Welte731d1fc2010-05-14 11:53:08 +00002114 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02002115 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02002116
Harald Welte731d1fc2010-05-14 11:53:08 +00002117 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002118 rc = gprs_ns_tx_reset(nsvc, cause);
2119 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00002120 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
2121 nsvc->nsei);
2122 }
2123 /* Start Tns-reset */
2124 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002125
2126 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00002127}
2128
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002129/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01002130 * \param nsi NS-instance
2131 * \param[in] dest Destination IP/Port
2132 * \param[in] nsei NSEI of the to-be-established NS-VC
2133 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2134 * \returns struct gprs_nsvc representing the new NS-VC
2135 *
2136 * This function will establish a single NS/UDP/IP connection in uplink
2137 * (BSS to SGSN) direction.
2138 */
Harald Weltef5430362012-06-17 12:25:53 +08002139struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02002140 struct sockaddr_in *dest, uint16_t nsei,
2141 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02002142{
2143 struct gprs_nsvc *nsvc;
2144
Harald Welte047f3872018-07-01 21:04:45 +02002145 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00002146 if (!nsvc)
Harald Welte68944962020-06-07 10:31:48 +02002147 nsvc = gprs_nsvc_create2(nsi, nsvci, 1, 1);
Harald Welte38407ef2010-05-12 11:56:39 +00002148 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02002149 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02002150 nsvc->remote_end_is_sgsn = 1;
2151
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08002152 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
2153 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02002154}
Harald Weltea9f23c82011-11-23 15:01:31 +01002155
Harald Welte047f3872018-07-01 21:04:45 +02002156/*! Establish a NS connection (from the BSS) to the SGSN using SNS auto-configuration
2157 * \param nsi NS-instance
2158 * \param[in] dest Destination IP/Port
2159 * \param[in] nsei NSEI of the to-be-established NS-VC
2160 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2161 * \returns struct gprs_nsvc representing the new NS-VC
2162 *
2163 * This function will establish a single NS/UDP/IP connection in uplink
2164 * (BSS to SGSN) direction. It will start with the SNS-SIZE procedure,
2165 * followed by BSS-originated SNS-CONFIG, then SGSN-originated SNS-CONFIG.
2166 *
2167 * Once configuration completes, the user will be notified by the S_SNS_CONFIGURED signal,
2168 * at which point he typically would want to initiate NS-RESET by means of gprs_nsvc_reset().
2169 */
2170struct gprs_nsvc *gprs_ns_nsip_connect_sns(struct gprs_ns_inst *nsi,
2171 struct sockaddr_in *dest, uint16_t nsei,
2172 uint16_t nsvci)
2173{
2174 struct gprs_nsvc *nsvc;
2175
2176 /* FIXME: We are getting the order wrong here. Normally, one would want
2177 * to start the SNS FSM *before* creating any NS-VC and then create the NS-VC
2178 * after the SNS layer has established the IP/port/etc. However, this would
2179 * require some massive code and API changes compared to existing libosmogb,
2180 * so let's keep the old logic. */
2181 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte94c9b442019-03-16 14:38:19 +01002182 if (!nsvc) {
2183 /* create NSVC with 0 data + signalling weight. This is illegal in SNS
2184 * and can hence only be created locally and serves as indication that
2185 * this NS-VC shall not be used for anything except SNS _unless_ it is
2186 * modified via SNS-{CONFIG,CHANGEWEIGHT,ADD} to become part of the
2187 * active NS-VCs */
2188 nsvc = gprs_nsvc_create2(nsi, nsvci, 0, 0);
2189 }
Harald Welte047f3872018-07-01 21:04:45 +02002190 nsvc->ip.bts_addr = *dest;
2191 nsvc->nsei = nsei;
2192 nsvc->remote_end_is_sgsn = 1;
2193 /* NSVCs are always UNBLOCKED in IP-SNS */
2194 ns_set_state(nsvc, 0);
2195
2196 if (nsi->bss_sns_fi)
2197 osmo_fsm_inst_term(nsi->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
2198 nsi->bss_sns_fi = gprs_sns_bss_fsm_alloc(nsi, nsvc, "NSIP");
2199 gprs_sns_bss_fsm_start(nsi);
2200 return nsvc;
2201}
2202
Harald Weltecca49632012-06-16 17:45:59 +08002203void gprs_ns_set_log_ss(int ss)
2204{
2205 DNS = ss;
2206}
2207
Daniel Willmannf1318fe2018-05-29 20:55:18 +02002208/*! Append the nsvc state to a talloc string
2209 * \param s The string to append to (allocated with talloc)
2210 * \param[in] nsvc The NS-VC to print the state of
2211 * \returns The new string with state information appended to it
2212 *
2213 * This function will append a comma-separated state of the NS-VC to the
2214 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
2215 */
2216char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
2217{
2218 s = talloc_asprintf_append(s,
2219 "%u,%u,%s,%s,%s,%s,%s\n",
2220 nsvc->nsei, nsvc->nsvci,
2221 NS_DESC_A(nsvc->state),
2222 NS_DESC_B(nsvc->state),
2223 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
2224 NS_DESC_A(nsvc->remote_state),
2225 NS_DESC_B(nsvc->remote_state));
2226
2227 return s;
2228}
2229
Harald Welte35042a22019-02-23 14:07:00 +01002230/*! Start the ALIVE timer procedure in all NS-VCs part of this NS Instance */
2231void gprs_nsvc_start_test(struct gprs_nsvc *nsvc)
2232{
Harald Welte94c9b442019-03-16 14:38:19 +01002233 /* skip the initial NS-VC unless it has explicitly been configured
2234 * via SNS-CONFIG from the SGSN */
2235 if (nsvc_is_not_used(nsvc))
2236 return;
Harald Welte35042a22019-02-23 14:07:00 +01002237 gprs_ns_tx_alive(nsvc);
2238 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
2239}
2240
Harald Welte047f3872018-07-01 21:04:45 +02002241void gprs_start_alive_all_nsvcs(struct gprs_ns_inst *nsi)
2242{
2243 struct gprs_nsvc *nsvc;
2244 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
2245 /* start the test procedure */
2246 gprs_nsvc_start_test(nsvc);
2247 }
2248}
2249
Harald Welte96e2a002017-06-12 21:44:18 +02002250/*! @} */