blob: 8c3b0fa2c5074f70673e22ed3c3f7598de75ece5 [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.
54 * NS then has to firgure out which NSVC's are responsible for this BVCI.
55 * 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
Harald Welteb1020d52010-05-25 22:17:30 +0200321 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
322
Harald Weltef030b212010-04-26 19:18:54 +0200323 nsvc = talloc_zero(nsi, struct gprs_nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200324 if (!nsvc)
325 return NULL;
Harald Weltef030b212010-04-26 19:18:54 +0200326 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100327 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200328 /* before RESET procedure: BLOCKED and DEAD */
Harald Welte047f3872018-07-01 21:04:45 +0200329 if (nsi->bss_sns_fi)
330 ns_set_state(nsvc, 0);
331 else
332 ns_set_state(nsvc, NSE_S_BLOCKED);
Harald Weltef030b212010-04-26 19:18:54 +0200333 nsvc->nsi = nsi;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200334 osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc);
Harald Welte144e0292010-05-13 11:45:07 +0200335 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100336 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte047f3872018-07-01 21:04:45 +0200337 nsvc->sig_weight = sig_weight;
338 nsvc->data_weight = data_weight;
Harald Welte69a4cf22010-05-03 20:55:10 +0200339
Harald Weltef030b212010-04-26 19:18:54 +0200340 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
341
342 return nsvc;
343}
Harald Welte9ba50052010-03-14 15:45:01 +0800344
Harald Welte047f3872018-07-01 21:04:45 +0200345/*! Old API for creating a NS-VC. Uses gprs_nsvc_create2 with fixed weights. */
346struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
347{
348 return gprs_nsvc_create2(nsi, nsvci, 1, 1);
349}
350
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200351/*! Delete given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100352 * \param[in] nsvc gprs_nsvc to be deleted
353 */
Harald Weltef5430362012-06-17 12:25:53 +0800354void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000355{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200356 if (osmo_timer_pending(&nsvc->timer))
357 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000358 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200359 rate_ctr_group_free(nsvc->ctrg);
Jacob Erlbeck0975a7f2015-12-17 09:51:53 +0100360 osmo_stat_item_group_free(nsvc->statg);
Harald Welte2bffac52010-05-12 15:55:23 +0000361 talloc_free(nsvc);
362}
363
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200364static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200365 uint8_t cause)
366{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200367 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200368
369 nssd.nsvc = nsvc;
370 nssd.cause = cause;
371
Harald Welte4fcdd762012-06-16 16:40:42 +0800372 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200373}
374
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200375static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
376 struct msgb *msg,
377 uint8_t pdu_type, uint8_t ie_type)
378{
379 struct ns_signal_data nssd = {0};
380
381 nssd.nsvc = nsvc;
382 nssd.pdu_type = pdu_type;
383 nssd.ie_type = ie_type;
384 nssd.msg = msg;
385
386 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
387}
388
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200389static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
390{
391 struct ns_signal_data nssd = {0};
392
393 nssd.nsvc = nsvc;
394 nssd.old_nsvc = old_nsvc;
395
396 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
397}
398
Max584f4b62017-05-04 11:59:52 +0200399const struct value_string gprs_ns_pdu_strings[] = {
400 /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */
401 { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */
402 { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */
403 { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */
404 { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */
405 { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */
406 { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */
407 { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */
408 { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */
409 { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */
410 { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */
411 /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */
412 { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */
413 { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */
414 { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */
415 { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */
416 { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */
417 { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */
418 { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */
419 { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */
420 { 0, NULL }
421};
422
Harald Welte9ba50052010-03-14 15:45:01 +0800423/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200424static const struct value_string ns_cause_str[] = {
425 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
426 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
427 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
428 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
429 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
430 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
431 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
432 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200433 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200434 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
435 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
Harald Welte47cf21f2019-02-23 13:42:06 +0100436 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
437 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
438 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
439 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
440 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
441 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
442 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
Harald Welte2577d412010-05-02 09:37:45 +0200443 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800444};
445
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200446/*! Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200447const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800448{
Harald Welte2577d412010-05-02 09:37:45 +0200449 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800450}
451
Harald Welte24a655f2010-04-30 19:54:29 +0200452static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200453extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200454
Harald Welte94c9b442019-03-16 14:38:19 +0100455static bool ns_is_sns(uint8_t pdu_type)
456{
457 switch (pdu_type) {
458 case SNS_PDUT_CONFIG:
459 case SNS_PDUT_ACK:
460 case SNS_PDUT_ADD:
461 case SNS_PDUT_CHANGE_WEIGHT:
462 case SNS_PDUT_DELETE:
463 case SNS_PDUT_CONFIG_ACK:
464 case SNS_PDUT_SIZE:
465 case SNS_PDUT_SIZE_ACK:
466 return true;
467 default:
468 return false;
469 }
470}
471
Harald Welte24a655f2010-04-30 19:54:29 +0200472static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800473{
Harald Welte94c9b442019-03-16 14:38:19 +0100474 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200475 int ret;
476
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100477 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200478
Harald Welte94c9b442019-03-16 14:38:19 +0100479 /* A pre-configured endpoint shall not be used for NSE data or signalling
480 * traffic (with the exception of Size and Configuration procedures) unless it
481 * is configured by the SGSN using the auto-configuration procedures. */
482 if (nsvc_is_not_used(nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
483 LOGP(DNS, LOGL_NOTICE, "Not transmitting %s on unused/pre-configured endpoint\n",
484 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
485 msgb_free(msg);
486 return -EINVAL;
487 }
488
Harald Welte144e0292010-05-13 11:45:07 +0200489 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200490 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
491 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200492
Harald Welteb3ee2652010-05-19 14:38:50 +0200493 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200494 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200495 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200496 if (ret < 0)
497 LOGP(DNS, LOGL_INFO,
498 "failed to send NS message via UDP: %s\n",
499 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200500 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200501 case GPRS_NS_LL_FR_GRE:
502 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200503 if (ret < 0)
504 LOGP(DNS, LOGL_INFO,
505 "failed to send NS message via FR/GRE: %s\n",
506 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200507 break;
Harald Weltef030b212010-04-26 19:18:54 +0200508 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200509 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200510 msgb_free(msg);
511 ret = -EIO;
512 break;
513 }
514 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800515}
516
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200517static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800518{
Harald Welteba4c6662010-05-19 15:38:10 +0200519 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800520 struct gprs_ns_hdr *nsh;
521
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100522 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200523
Harald Welte9ba50052010-03-14 15:45:01 +0800524 if (!msg)
525 return -ENOMEM;
526
Harald Welte144e0292010-05-13 11:45:07 +0200527 msg->l2h = msgb_put(msg, sizeof(*nsh));
528 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800529
530 nsh->pdu_type = pdu_type;
531
Harald Welte24a655f2010-04-30 19:54:29 +0200532 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800533}
534
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200535/*! Transmit a NS-RESET on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100536 * \param[in] nsvc NS-VC used for transmission
537 * \paam[in] cause Numeric NS cause value
538 */
Harald Welte834f26d2010-05-11 06:20:54 +0200539int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200540{
Harald Welte047f3872018-07-01 21:04:45 +0200541 struct msgb *msg;
Harald Welte69a4cf22010-05-03 20:55:10 +0200542 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200543 uint16_t nsvci = osmo_htons(nsvc->nsvci);
544 uint16_t nsei = osmo_htons(nsvc->nsei);
Harald Welte69a4cf22010-05-03 20:55:10 +0200545
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100546 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200547
Harald Welte047f3872018-07-01 21:04:45 +0200548 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET");
549
550 msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200551 if (!msg)
552 return -ENOMEM;
553
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000554 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
555 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
556
Harald Welte144e0292010-05-13 11:45:07 +0200557 msg->l2h = msgb_put(msg, sizeof(*nsh));
558 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200559 nsh->pdu_type = NS_PDUT_RESET;
560
561 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
562 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
563 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
564
565 return gprs_ns_tx(nsvc, msg);
566
567}
568
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200569/*! Transmit a NS-STATUS on a given NSVC
Harald Weltea9f23c82011-11-23 15:01:31 +0100570 * \param[in] nsvc NS-VC to be used for transmission
571 * \param[in] cause Numeric NS cause value
572 * \param[in] bvci BVCI to be reset within NSVC
573 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200574int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
575 uint16_t bvci, struct msgb *orig_msg)
576{
Harald Welteba4c6662010-05-19 15:38:10 +0200577 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200578 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200579 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200580
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100581 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200582
Harald Weltebfe62e52017-05-15 12:48:30 +0200583 bvci = osmo_htons(bvci);
Harald Weltec1402a62010-05-12 11:48:44 +0200584
585 if (!msg)
586 return -ENOMEM;
587
Harald Welte90af1942010-05-15 23:02:24 +0200588 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000589 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
590
Harald Welte144e0292010-05-13 11:45:07 +0200591 msg->l2h = msgb_put(msg, sizeof(*nsh));
592 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200593 nsh->pdu_type = NS_PDUT_STATUS;
594
595 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
596
597 /* Section 9.2.7.1: Static conditions for NS-VCI */
598 if (cause == NS_CAUSE_NSVC_BLOCKED ||
599 cause == NS_CAUSE_NSVC_UNKNOWN)
600 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
601
602 /* Section 9.2.7.2: Static conditions for NS PDU */
603 switch (cause) {
604 case NS_CAUSE_SEM_INCORR_PDU:
605 case NS_CAUSE_PDU_INCOMP_PSTATE:
606 case NS_CAUSE_PROTO_ERR_UNSPEC:
607 case NS_CAUSE_INVAL_ESSENT_IE:
608 case NS_CAUSE_MISSING_ESSENT_IE:
609 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
610 orig_msg->l2h);
611 break;
612 default:
613 break;
614 }
615
616 /* Section 9.2.7.3: Static conditions for BVCI */
617 if (cause == NS_CAUSE_BVCI_UNKNOWN)
618 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
619
620 return gprs_ns_tx(nsvc, msg);
621}
622
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200623/*! Transmit a NS-BLOCK on a tiven NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100624 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
625 * \param[in] cause Numeric NS Cause value
626 * \returns 0 in case of success
627 */
Harald Welte834f26d2010-05-11 06:20:54 +0200628int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
629{
Harald Welte047f3872018-07-01 21:04:45 +0200630 struct msgb *msg;
Harald Welte834f26d2010-05-11 06:20:54 +0200631 struct gprs_ns_hdr *nsh;
Harald Weltebfe62e52017-05-15 12:48:30 +0200632 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Harald Welte834f26d2010-05-11 06:20:54 +0200633
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100634 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200635
Harald Welte047f3872018-07-01 21:04:45 +0200636 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK");
637
638 msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200639 if (!msg)
640 return -ENOMEM;
641
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000642 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
643 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
644
Harald Welte834f26d2010-05-11 06:20:54 +0200645 /* be conservative and mark it as blocked even now! */
Maxc513c0f2017-10-23 15:11:14 +0200646 ns_mark_blocked(nsvc);
Harald Weltec51c23c2010-05-13 12:55:20 +0200647 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200648
Harald Welte144e0292010-05-13 11:45:07 +0200649 msg->l2h = msgb_put(msg, sizeof(*nsh));
650 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200651 nsh->pdu_type = NS_PDUT_BLOCK;
652
653 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
654 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
655
656 return gprs_ns_tx(nsvc, msg);
657}
658
Harald Welte9ef2e152019-02-19 23:42:04 +0100659/*! Transmit a NS-BLOCK-ACK on a given NS-VC
660 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
661 * \returns 0 in case of success
662 */
663static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc)
664{
665 struct msgb *msg;
666 struct gprs_ns_hdr *nsh;
667 uint16_t nsvci = osmo_htons(nsvc->nsvci);
668
669 log_set_context(LOG_CTX_GB_NSVC, nsvc);
670
Harald Welte047f3872018-07-01 21:04:45 +0200671 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK ACK");
672
Harald Welte9ef2e152019-02-19 23:42:04 +0100673 msg = gprs_ns_msgb_alloc();
674 if (!msg)
675 return -ENOMEM;
676
677 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci);
678
679 /* be conservative and mark it as blocked even now! */
680 msg->l2h = msgb_put(msg, sizeof(*nsh));
681 nsh = (struct gprs_ns_hdr *) msg->l2h;
682 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
683
684 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
685
686 return gprs_ns_tx(nsvc, msg);
687}
688
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200689/*! Transmit a NS-UNBLOCK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100690 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
691 * \returns 0 in case of success
692 */
Harald Welte834f26d2010-05-11 06:20:54 +0200693int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
694{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100695 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200696
697 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK");
698
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000699 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
700 nsvc->nsei, nsvc->nsvci);
701
Harald Welte834f26d2010-05-11 06:20:54 +0200702 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! Transmit a NS-ALIVE on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100706 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
707 * \returns 0 in case of success
708 */
Harald Welteb983c312010-05-12 12:11:45 +0000709int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
710{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100711 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000712 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
713 nsvc->nsei, nsvc->nsvci);
714
715 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
716}
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! Transmit a NS-ALIVE-ACK on a given NS-VC
Harald Weltea9f23c82011-11-23 15:01:31 +0100719 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
720 * \returns 0 in case of success
721 */
Harald Welteb983c312010-05-12 12:11:45 +0000722int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
723{
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100724 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000725 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
726 nsvc->nsei, nsvc->nsvci);
727
728 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
729}
730
Harald Weltefe4ab902010-05-12 17:19:53 +0000731static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
732 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
733 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
734 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200735};
736
Harald Welte7c24b9e2010-05-12 12:21:52 +0000737static const struct value_string timer_mode_strs[] = {
738 { NSVC_TIMER_TNS_RESET, "tns-reset" },
739 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
740 { NSVC_TIMER_TNS_TEST, "tns-test" },
741 { 0, NULL }
742};
743
Harald Welte69a4cf22010-05-03 20:55:10 +0200744static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
745{
Harald Weltefe4ab902010-05-12 17:19:53 +0000746 enum ns_timeout tout = timer_mode_tout[mode];
747 unsigned int seconds = nsvc->nsi->timeout[tout];
748
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100749 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000750 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
751 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000752 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100753
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200754 if (osmo_timer_pending(&nsvc->timer))
755 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200756
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200757 osmo_gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200758 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200759 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200760}
761
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200762static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
763{
764 struct timeval now, elapsed;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200765 osmo_gettimeofday(&now, NULL);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200766 timersub(&now, &nsvc->timer_started, &elapsed);
767
768 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
769}
770
Harald Welte80405452010-05-03 20:16:13 +0200771static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800772{
773 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000774 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
775 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800776
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100777 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000778 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
779 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000780 seconds);
Harald Welte9612fcb2019-02-19 11:43:35 +0100781
Harald Welte80405452010-05-03 20:16:13 +0200782 switch (nsvc->timer_mode) {
783 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800784 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200785 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800786 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000787 if (nsvc->alive_retries >
788 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte047f3872018-07-01 21:04:45 +0200789 /* mark as dead (and blocked unless IP-SNS) */
Harald Weltec51c23c2010-05-13 12:55:20 +0200790 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Welte047f3872018-07-01 21:04:45 +0200791 if (!nsvc->nsi->bss_sns_fi) {
792 ns_set_state(nsvc, NSE_S_BLOCKED);
793 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
794 } else
795 ns_set_state(nsvc, 0);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200796 LOGP(DNS, LOGL_NOTICE,
797 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200798 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000799 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200800 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
Harald Welte047f3872018-07-01 21:04:45 +0200801 /* FIXME: should we send this signal in case of SNS? */
802 if (!nsvc->nsi->bss_sns_fi)
803 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800804 return;
805 }
Harald Welteb983c312010-05-12 12:11:45 +0000806 /* Tns-test case: send NS-ALIVE PDU */
807 gprs_ns_tx_alive(nsvc);
808 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200809 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200810 break;
811 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800812 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000813 gprs_ns_tx_alive(nsvc);
814 /* start Tns-alive timer (transition into faster
815 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000816 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200817 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
818 break;
819 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200820 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Jacob Erlbeckcc6ac4c2015-12-17 13:22:36 +0100821 if (!(nsvc->state & NSE_S_RESET))
822 LOGP(DNS, LOGL_NOTICE,
823 "NSEI=%u Reset timed out but RESET flag is not set\n",
824 nsvc->nsei);
825 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +0200826 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Harald Welte69a4cf22010-05-03 20:55:10 +0200827 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200828 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200829 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200830 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200831 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200832 case _NSVC_TIMER_NR:
833 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800834 }
Harald Welte9ba50052010-03-14 15:45:01 +0800835}
836
837/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800838static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800839{
Harald Welte047f3872018-07-01 21:04:45 +0200840 struct msgb *msg;
Harald Welte9ba50052010-03-14 15:45:01 +0800841 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200842 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800843
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100844 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte047f3872018-07-01 21:04:45 +0200845
846 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET ACK");
847
848 msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800849 if (!msg)
850 return -ENOMEM;
851
Harald Weltebfe62e52017-05-15 12:48:30 +0200852 nsvci = osmo_htons(nsvc->nsvci);
853 nsei = osmo_htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800854
Harald Welte144e0292010-05-13 11:45:07 +0200855 msg->l2h = msgb_put(msg, sizeof(*nsh));
856 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800857
858 nsh->pdu_type = NS_PDUT_RESET_ACK;
859
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200860 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200861 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800862
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200863 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
864 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800865
Harald Welte24a655f2010-04-30 19:54:29 +0200866 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800867}
868
Harald Welte17a642d2018-07-01 19:09:52 +0200869/*! Encode + Transmit a SNS-ACK as per Section 9.3.1.
870 * \param[in] nsvc NS-VC through which to transmit the ACK
871 * \param[in] trans_id Transaction ID which to acknowledge
872 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
873 * \param[in] ip4_elems Array of IPv4 Elements
874 * \param[in] num_ip4_elems number of ip4_elems
875 * \returns 0 on success; negative in case of error */
876int gprs_ns_tx_sns_ack(struct gprs_nsvc *nsvc, uint8_t trans_id, uint8_t *cause,
877 const struct gprs_ns_ie_ip4_elem *ip4_elems,
878 unsigned int num_ip4_elems)
879{
880 struct msgb *msg = gprs_ns_msgb_alloc();
881 struct gprs_ns_hdr *nsh;
882 uint16_t nsei;
883
884 log_set_context(LOG_CTX_GB_NSVC, nsvc);
885 if (!msg)
886 return -ENOMEM;
887
Harald Welte047f3872018-07-01 21:04:45 +0200888 if (!nsvc->nsi->bss_sns_fi) {
889 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
890 nsvc->nsei);
891 msgb_free(msg);
892 return -EIO;
893 }
894
Harald Welte17a642d2018-07-01 19:09:52 +0200895 nsei = osmo_htons(nsvc->nsei);
896
897 msg->l2h = msgb_put(msg, sizeof(*nsh));
898 nsh = (struct gprs_ns_hdr *) msg->l2h;
899
900 nsh->pdu_type = SNS_PDUT_ACK;
901 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
902 msgb_v_put(msg, trans_id);
903 if (cause)
904 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
905 if (ip4_elems) {
906 /* List of IP4 Elements 10.3.2c */
907 msgb_tvlv_put(msg, NS_IE_IPv4_LIST,
908 num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
909 (const uint8_t *)ip4_elems);
910 }
911 /* FIXME: List of IP6 elements 10.3.2d */
912 return gprs_ns_tx(nsvc, msg);
913}
914
915/*! Encode + Transmit a SNS-CONFIG as per Section 9.3.4.
916 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
917 * \param[in] end_flag Whether or not this is the last SNS-CONFIG
918 * \param[in] ip4_elems Array of IPv4 Elements
919 * \param[in] num_ip4_elems number of ip4_elems
920 * \returns 0 on success; negative in case of error */
921int gprs_ns_tx_sns_config(struct gprs_nsvc *nsvc, bool end_flag,
922 const struct gprs_ns_ie_ip4_elem *ip4_elems,
923 unsigned int num_ip4_elems)
924{
925 struct msgb *msg = gprs_ns_msgb_alloc();
926 struct gprs_ns_hdr *nsh;
927 uint16_t nsei;
928
929 log_set_context(LOG_CTX_GB_NSVC, nsvc);
930 if (!msg)
931 return -ENOMEM;
932
Harald Welte047f3872018-07-01 21:04:45 +0200933 if (!nsvc->nsi->bss_sns_fi) {
934 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
935 nsvc->nsei);
936 msgb_free(msg);
937 return -EIO;
938 }
939
Harald Welte17a642d2018-07-01 19:09:52 +0200940 nsei = osmo_htons(nsvc->nsei);
941
942 msg->l2h = msgb_put(msg, sizeof(*nsh));
943 nsh = (struct gprs_ns_hdr *) msg->l2h;
944
945 nsh->pdu_type = SNS_PDUT_CONFIG;
946
947 msgb_v_put(msg, end_flag ? 0x01 : 0x00);
948 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
949
950 /* List of IP4 Elements 10.3.2c */
951 msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
952 (const uint8_t *)ip4_elems);
953 /* FIXME: List of IP6 elements 10.3.2d */
954
955 return gprs_ns_tx(nsvc, msg);
956}
957
958/*! Encode + Transmit a SNS-CONFIG-ACK as per Section 9.3.5.
959 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG-ACK
960 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
961 * \returns 0 on success; negative in case of error */
962int gprs_ns_tx_sns_config_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
963{
964 struct msgb *msg = gprs_ns_msgb_alloc();
965 struct gprs_ns_hdr *nsh;
966 uint16_t nsei;
967
968 log_set_context(LOG_CTX_GB_NSVC, nsvc);
969 if (!msg)
970 return -ENOMEM;
971
Harald Welte047f3872018-07-01 21:04:45 +0200972 if (!nsvc->nsi->bss_sns_fi) {
973 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
974 nsvc->nsei);
975 msgb_free(msg);
976 return -EIO;
977 }
978
Harald Welte17a642d2018-07-01 19:09:52 +0200979 nsei = osmo_htons(nsvc->nsei);
980
981 msg->l2h = msgb_put(msg, sizeof(*nsh));
982 nsh = (struct gprs_ns_hdr *) msg->l2h;
983
984 nsh->pdu_type = SNS_PDUT_CONFIG_ACK;
985
986 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
987 if (cause)
988 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
989
990 return gprs_ns_tx(nsvc, msg);
991}
992
993
994/*! Encode + transmit a SNS-SIZE as per Section 9.3.7.
995 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE
996 * \param[in] reset_flag Whether or not to add a RESET flag
997 * \param[in] max_nr_nsvc Maximum number of NS-VCs
998 * \param[in] ip4_ep_nr Number of IPv4 endpoints (NULL if none)
999 * \param[in] ip6_ep_nr Number of IPv6 endpoints (NULL if none)
1000 * \returns 0 on success; negative in case of error */
1001int gprs_ns_tx_sns_size(struct gprs_nsvc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
1002 uint16_t *ip4_ep_nr, uint16_t *ip6_ep_nr)
1003{
1004 struct msgb *msg = gprs_ns_msgb_alloc();
1005 struct gprs_ns_hdr *nsh;
1006 uint16_t nsei;
1007
1008 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1009 if (!msg)
1010 return -ENOMEM;
1011
Harald Welte047f3872018-07-01 21:04:45 +02001012 if (!nsvc->nsi->bss_sns_fi) {
1013 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1014 nsvc->nsei);
1015 msgb_free(msg);
1016 return -EIO;
1017 }
1018
Harald Welte17a642d2018-07-01 19:09:52 +02001019 nsei = osmo_htons(nsvc->nsei);
1020
1021 msg->l2h = msgb_put(msg, sizeof(*nsh));
1022 nsh = (struct gprs_ns_hdr *) msg->l2h;
1023
1024 nsh->pdu_type = SNS_PDUT_SIZE;
1025
1026 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1027 msgb_tv_put(msg, NS_IE_RESET_FLAG, reset_flag ? 0x01 : 0x00);
1028 msgb_tv16_put(msg, NS_IE_MAX_NR_NSVC, max_nr_nsvc);
1029 if (ip4_ep_nr)
1030 msgb_tv16_put(msg, NS_IE_IPv4_EP_NR, *ip4_ep_nr);
1031 if (ip6_ep_nr)
1032 msgb_tv16_put(msg, NS_IE_IPv6_EP_NR, *ip6_ep_nr);
1033
1034 return gprs_ns_tx(nsvc, msg);
1035}
1036
1037/*! Encode + Transmit a SNS-SIZE-ACK as per Section 9.3.8.
1038 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE-ACK
1039 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
1040 * \returns 0 on success; negative in case of error */
1041int gprs_ns_tx_sns_size_ack(struct gprs_nsvc *nsvc, uint8_t *cause)
1042{
1043 struct msgb *msg = gprs_ns_msgb_alloc();
1044 struct gprs_ns_hdr *nsh;
1045 uint16_t nsei;
1046
1047 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1048 if (!msg)
1049 return -ENOMEM;
1050
Harald Welte047f3872018-07-01 21:04:45 +02001051 if (!nsvc->nsi->bss_sns_fi) {
1052 LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n",
1053 nsvc->nsei);
1054 msgb_free(msg);
1055 return -EIO;
1056 }
1057
Harald Welte17a642d2018-07-01 19:09:52 +02001058 nsei = osmo_htons(nsvc->nsei);
1059
1060 msg->l2h = msgb_put(msg, sizeof(*nsh));
1061 nsh = (struct gprs_ns_hdr *) msg->l2h;
1062
1063 nsh->pdu_type = SNS_PDUT_SIZE_ACK;
1064
1065 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
1066 if (cause)
1067 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
1068
1069 return gprs_ns_tx(nsvc, msg);
1070}
1071
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001072/*! High-level function for transmitting a NS-UNITDATA messsage
Harald Weltea9f23c82011-11-23 15:01:31 +01001073 * \param[in] nsi NS-instance on which we shall transmit
1074 * \param[in] msg struct msgb to be trasnmitted
1075 *
1076 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
1077 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
1078 * header for the NS-UNITDATA message type and sends it off.
1079 *
1080 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
1081 */
Harald Welte24a655f2010-04-30 19:54:29 +02001082int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001083{
Harald Welte24a655f2010-04-30 19:54:29 +02001084 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001085 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001086 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +02001087
Harald Welte047f3872018-07-01 21:04:45 +02001088 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg), msgb_bvci(msg));
Harald Welte24a655f2010-04-30 19:54:29 +02001089 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +02001090 int rc;
1091 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
1092 LOGP(DNS, LOGL_ERROR,
1093 "All NS-VCs for NSEI %u are either dead or blocked!\n",
1094 msgb_nsei(msg));
1095 rc = -EBUSY;
1096 } else {
1097 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
1098 "to NS-VC!\n", msgb_nsei(msg));
1099 rc = -EINVAL;
1100 }
1101
Harald Weltef47df642010-08-09 21:15:40 +08001102 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +02001103 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +02001104 }
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001105 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001106
Harald Welteec20ba42010-05-13 12:18:49 +02001107 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
1108 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001109 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001110 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +08001111 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001112 return -EIO;
1113 }
1114
1115 nsh->pdu_type = NS_PDUT_UNITDATA;
1116 /* spare octet in data[0] */
1117 nsh->data[1] = bvci >> 8;
1118 nsh->data[2] = bvci & 0xff;
1119
Harald Welte24a655f2010-04-30 19:54:29 +02001120 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001121}
1122
1123/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +02001124static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001125{
1126 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001127 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001128
Harald Weltec1402a62010-05-12 11:48:44 +02001129 if (nsvc->state & NSE_S_BLOCKED)
1130 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
1131 0, msg);
1132
Harald Welte9ba50052010-03-14 15:45:01 +08001133 /* spare octet in data[0] */
1134 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +02001135 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +02001136 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +08001137
1138 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +02001139 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001140}
1141
1142/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +02001143static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001144{
Harald Weltef030b212010-04-26 19:18:54 +02001145 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001146 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001147 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +08001148 int rc;
1149
Harald Welte41337832010-05-16 00:24:26 +02001150 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +08001151
Harald Weltebd33f3d2010-05-30 17:19:38 +02001152 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1153 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001154 if (rc < 0) {
1155 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
1156 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
1157 "Error during TLV Parse\n", nsvc->nsei);
1158 return rc;
1159 }
Harald Welte9ba50052010-03-14 15:45:01 +08001160
1161 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001162 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +08001163 return -EINVAL;
1164 }
1165
1166 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +02001167 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +08001168
1169 return 0;
1170}
1171
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001172/* Replace a nsvc object with another based on NSVCI.
1173 * This function replaces looks for a NSVC with the given NSVCI and replaces it
1174 * if possible and necessary. If replaced, the former value of *nsvc is
1175 * returned in *old_nsvc.
1176 * \return != 0 if *nsvc points to a matching NSVC.
1177 */
1178static int gprs_nsvc_replace_if_found(uint16_t nsvci,
1179 struct gprs_nsvc **nsvc,
1180 struct gprs_nsvc **old_nsvc)
1181{
1182 struct gprs_nsvc *matching_nsvc;
1183
1184 if ((*nsvc)->nsvci == nsvci) {
1185 *old_nsvc = NULL;
1186 return 1;
1187 }
1188
1189 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
1190
1191 if (!matching_nsvc)
1192 return 0;
1193
1194 /* The NS-VCI is already used by this NS-VC */
1195
1196 char *old_peer;
1197
1198 /* Exchange the NS-VC objects */
1199 *old_nsvc = *nsvc;
1200 *nsvc = matching_nsvc;
1201
1202 /* Do logging */
1203 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
1204 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
1205 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
1206
1207 talloc_free(old_peer);
1208
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001209 return 1;
1210}
1211
Harald Welte9ba50052010-03-14 15:45:01 +08001212/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001213static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +08001214{
1215 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +08001216 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001217 uint8_t cause;
1218 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001219 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +08001220 int rc;
1221
Harald Weltebd33f3d2010-05-30 17:19:38 +02001222 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1223 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001224 if (rc < 0) {
1225 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001226 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +02001227 return rc;
1228 }
Harald Welte9ba50052010-03-14 15:45:01 +08001229
1230 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1231 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
1232 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001233 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001234 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001235 return -EINVAL;
1236 }
1237
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001238 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltebfe62e52017-05-15 12:48:30 +02001239 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1240 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +08001241
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001242 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
1243 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1244 nsei, nsvci, gprs_ns_cause_str(cause));
1245
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001246 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
1247 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001248 /* The incoming RESET doesn't match the NSVCI. Send an
1249 * appropriate RESET_ACK and ignore the RESET.
1250 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
1251 */
1252 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1253 NS_PDUT_RESET,
1254 NS_IE_VCI);
1255 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1256 gprs_ns_tx_reset_ack(*nsvc);
1257 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001258 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001259
1260 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001261 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
1262 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
1263 "at %s\n",
1264 nsvci, (*nsvc)->nsvci,
1265 gprs_ns_ll_str(*nsvc));
1266 orig_nsvc = *nsvc;
1267 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001268 (*nsvc)->nsei = nsei;
1269 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001270 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001271
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001272 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
1273 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1274 /* The incoming RESET doesn't match the NSEI. Send an
1275 * appropriate RESET_ACK and ignore the RESET.
1276 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
1277 */
1278 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1279 NS_PDUT_RESET,
1280 NS_IE_NSEI);
1281 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +02001282 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001283 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001284 return 0;
1285 }
1286
1287 /* NSEI has changed */
1288 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1289 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001290 }
Harald Weltebbc9fac2010-05-03 18:54:12 +02001291
Harald Weltec1402a62010-05-12 11:48:44 +02001292 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001293 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Weltee8b9ca22010-05-11 18:18:31 +02001294
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001295 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001296 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001297 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001298
1299 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001300 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1301 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001302 } else {
1303 (*nsvc)->nsei = nsei;
1304 (*nsvc)->nsvci = nsvci;
1305 (*nsvc)->nsvci_is_valid = 1;
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +01001306 rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci);
1307 osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001308 }
Harald Welte9ba50052010-03-14 15:45:01 +08001309
Harald Welte834f26d2010-05-11 06:20:54 +02001310 /* inform interested parties about the fact that this NSVC
1311 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001312 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +02001313
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001314 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001315
1316 /* start the test procedure */
Harald Welte35042a22019-02-23 14:07:00 +01001317 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +02001318
1319 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +08001320}
1321
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001322static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
1323{
1324 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1325 struct tlv_parsed tp;
1326 uint16_t nsvci, nsei;
1327 struct gprs_nsvc *orig_nsvc = NULL;
1328 int rc;
1329
1330 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1331 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1332 if (rc < 0) {
1333 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
1334 "Error during TLV Parse\n", (*nsvc)->nsei);
1335 return rc;
1336 }
1337
1338 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
1339 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1340 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +02001341 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001342 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001343 return -EINVAL;
1344 }
1345
Harald Weltebfe62e52017-05-15 12:48:30 +02001346 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1347 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001348
1349 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
1350 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
1351 nsei, nsvci);
1352
1353 if (!((*nsvc)->state & NSE_S_RESET)) {
1354 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
1355 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
1356 */
1357 LOGP(DNS, LOGL_ERROR,
1358 "NS RESET ACK Discarding unexpected message for "
1359 "NS-VCI %d from SGSN NSEI=%d\n",
1360 nsvci, nsei);
1361 return 0;
1362 }
1363
1364 if (!(*nsvc)->nsvci_is_valid) {
1365 LOGP(DNS, LOGL_NOTICE,
1366 "NS RESET ACK Uninitialised NS-VC (%u) for "
1367 "NS-VCI %d, NSEI=%d from %s\n",
1368 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
1369 return -EINVAL;
1370 }
1371
1372 if ((*nsvc)->nsvci != nsvci) {
1373 /* NS-VCI has changed */
1374
1375 /* if !0, use another NSVC object that matches the NSVCI */
1376 int use_other_nsvc;
1377
1378 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001379 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
1380 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001381
1382 if (use_other_nsvc)
1383 /* Update *nsvc to point to the right NSVC object */
1384 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
1385 &orig_nsvc);
1386
1387 if (!use_other_nsvc) {
1388 /* The incoming RESET_ACK doesn't match the NSVCI.
1389 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1390 */
1391 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1392 NS_PDUT_RESET_ACK,
1393 NS_IE_VCI);
1394 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
1395 LOGP(DNS, LOGL_ERROR,
1396 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
1397 "from %s\n",
1398 nsvci,
1399 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
1400 nsei, gprs_ns_ll_str(*nsvc));
1401 return -EINVAL;
1402 }
1403
1404 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +02001405 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001406 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
1407
1408 /* Update the ll info fields */
1409 gprs_ns_ll_copy(*nsvc, orig_nsvc);
1410 gprs_ns_ll_clear(orig_nsvc);
1411 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +02001412 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1413 /* The incoming RESET_ACK doesn't match the NSEI.
1414 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
1415 */
1416 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
1417 NS_PDUT_RESET_ACK,
1418 NS_IE_NSEI);
1419 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1420 LOGP(DNS, LOGL_ERROR,
1421 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1422 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1423 return -EINVAL;
1424 }
1425
1426 /* NSEI has changed */
1427 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1428 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001429 }
1430
1431 /* Mark NS-VC as blocked and alive */
Maxc513c0f2017-10-23 15:11:14 +02001432 ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
1433 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001434 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1435 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1436 /* stop RESET timer */
1437 osmo_timer_del(&(*nsvc)->timer);
1438 }
1439 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte35042a22019-02-23 14:07:00 +01001440 gprs_nsvc_start_test(*nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001441
Harald Welte35042a22019-02-23 14:07:00 +01001442 return 0;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001443}
1444
Harald Welte834f26d2010-05-11 06:20:54 +02001445static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1446{
1447 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1448 struct tlv_parsed tp;
1449 uint8_t *cause;
1450 int rc;
1451
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001452 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001453
Maxc513c0f2017-10-23 15:11:14 +02001454 ns_mark_blocked(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001455
Harald Weltebd33f3d2010-05-30 17:19:38 +02001456 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1457 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001458 if (rc < 0) {
1459 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1460 "Error during TLV Parse\n", nsvc->nsei);
1461 return rc;
1462 }
Harald Welte834f26d2010-05-11 06:20:54 +02001463
1464 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1465 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001466 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001467 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001468 return -EINVAL;
1469 }
1470
1471 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1472 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1473
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001474 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001475 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001476
Harald Welte9ef2e152019-02-19 23:42:04 +01001477 return gprs_ns_tx_block_ack(nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +02001478}
1479
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001480int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1481 struct gprs_nsvc *fallback_nsvc,
1482 struct gprs_nsvc **new_nsvc);
1483
1484int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001485 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001486
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001487/*! Receive incoming NS message from underlying transport layer
Harald Weltea9f23c82011-11-23 15:01:31 +01001488 * \param nsi NS instance to which the data belongs
1489 * \param[in] msg message buffer containing newly-received data
1490 * \param[in] saddr socketaddr from which data was received
1491 * \param[in] ll link-layer type in which data was received
1492 * \returns 0 in case of success, < 0 in case of error
1493 *
1494 * This is the main entry point int othe NS imlementation where frames
1495 * from the underlying transport (normally UDP) enter.
1496 */
Harald Weltef030b212010-04-26 19:18:54 +02001497int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001498 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001499{
Harald Weltef030b212010-04-26 19:18:54 +02001500 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001501 int rc = 0;
1502
Harald Weltef030b212010-04-26 19:18:54 +02001503 /* look up the NSVC based on source address */
Harald Welte047f3872018-07-01 21:04:45 +02001504 nsvc = gprs_nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001505
Harald Weltef030b212010-04-26 19:18:54 +02001506 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001507 struct gprs_nsvc *fallback_nsvc;
1508
1509 fallback_nsvc = nsi->unknown_nsvc;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001510 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001511 fallback_nsvc->ip.bts_addr = *saddr;
1512 fallback_nsvc->ll = ll;
1513
1514 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1515
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001516 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001517 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001518
1519 rc = 0;
1520 }
1521
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001522 if (nsvc)
1523 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001524
1525 return rc;
1526}
1527
Maxcbf5cdf2017-10-24 18:18:55 +02001528const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001529{
1530 static char buf[80];
Max95308592017-10-24 15:54:28 +02001531
1532 switch(nsvc->ll) {
1533 case GPRS_NS_LL_UDP:
1534 snprintf(buf, sizeof(buf), "%s:%u",
1535 inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port));
1536 break;
1537 case GPRS_NS_LL_FR_GRE:
1538 snprintf(buf, sizeof(buf), "%s:%u",
1539 inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port));
1540 break;
1541 default:
1542 buf[0] = '\0';
1543 break;
1544 }
1545
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001546 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001547
1548 return buf;
1549}
1550
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001551void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1552{
1553 nsvc->ll = other->ll;
1554
1555 switch (nsvc->ll) {
1556 case GPRS_NS_LL_UDP:
1557 nsvc->ip = other->ip;
1558 break;
1559 case GPRS_NS_LL_FR_GRE:
1560 nsvc->frgre = other->frgre;
1561 break;
1562 default:
1563 break;
1564 }
1565}
1566
1567void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1568{
1569 switch (nsvc->ll) {
1570 case GPRS_NS_LL_UDP:
1571 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1572 nsvc->ip.bts_addr.sin_port = 0;
1573 break;
1574 case GPRS_NS_LL_FR_GRE:
1575 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1576 nsvc->frgre.bts_addr.sin_port = 0;
1577 break;
1578 default:
1579 break;
1580 }
1581}
1582
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001583/*! Create/get NS-VC independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001584 * \param nsi NS instance to which the data belongs
1585 * \param[in] msg message buffer containing newly-received data
1586 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001587 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001588 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1589 * been created or found
1590 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1591 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1592 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1593 * has been created and registered, and GPRS_NS_CS_FOUND if an
1594 * existing NS-VC object has been found with the same NSEI.
1595 *
1596 * This contains the initial NS automaton state (NS-VC not yet attached).
1597 */
1598int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1599 struct gprs_nsvc *fallback_nsvc,
1600 struct gprs_nsvc **new_nsvc)
1601{
1602 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1603 struct gprs_nsvc *existing_nsvc;
1604
1605 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001606 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001607 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001608
1609 int rc;
1610
1611 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001612 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001613 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1614 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001615 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001616 return GPRS_NS_CS_SKIPPED;
1617 }
1618
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001619 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1620 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1621 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1622 "for non-existing NS-VC\n",
1623 gprs_ns_ll_str(fallback_nsvc));
1624 return GPRS_NS_CS_SKIPPED;
1625 }
1626
1627 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1628 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1629 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1630 "for non-existing NS-VC\n",
1631 gprs_ns_ll_str(fallback_nsvc));
1632 return GPRS_NS_CS_SKIPPED;
1633 }
1634
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001635 /* Only the RESET procedure creates a new NSVC */
1636 if (nsh->pdu_type != NS_PDUT_RESET) {
1637 /* Since we have no NSVC, we have to use a fake */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001638 log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc);
Max584f4b62017-05-04 11:59:52 +02001639 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s "
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001640 "from %s for non-existing NS-VC\n",
Max584f4b62017-05-04 11:59:52 +02001641 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001642 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001643 fallback_nsvc->nsvci_is_valid = 0;
Maxc513c0f2017-10-23 15:11:14 +02001644 ns_set_state(fallback_nsvc, NSE_S_ALIVE);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001645
1646 rc = gprs_ns_tx_status(fallback_nsvc,
1647 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1648 if (rc < 0) {
1649 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001650 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001651 return rc;
1652 }
1653 return GPRS_NS_CS_REJECTED;
1654 }
1655
1656 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1657 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1658 if (rc < 0) {
1659 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1660 "TLV Parse\n", rc);
1661 return rc;
1662 }
1663 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1664 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1665 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001666 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001667 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001668 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001669 return -EINVAL;
1670 }
Harald Weltebfe62e52017-05-15 12:48:30 +02001671 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
1672 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001673 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001674 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001675 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001676 if (!existing_nsvc) {
1677 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001678 (*new_nsvc)->nsvci_is_valid = 0;
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001679 log_set_context(LOG_CTX_GB_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001680 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001681 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001682 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001683
1684 return GPRS_NS_CS_CREATED;
1685 }
1686
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001687 /* Check NSEI */
1688 if (existing_nsvc->nsei != nsei) {
1689 LOGP(DNS, LOGL_NOTICE,
1690 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1691 nsvci, existing_nsvc->nsei, nsei);
1692
1693 /* Override old NSEI */
1694 existing_nsvc->nsei = nsei;
1695
1696 /* Do statistics */
1697 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1698 }
1699
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001700 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001701 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001702 return GPRS_NS_CS_FOUND;
1703}
1704
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001705/*! Process NS message independently from underlying transport layer
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001706 * \param nsi NS instance to which the data belongs
1707 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001708 * \param[inout] nsvc refers to the virtual connection, may be modified when
1709 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001710 * \returns 0 in case of success, < 0 in case of error
1711 *
1712 * This contains the main NS automaton.
1713 */
1714int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001715 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001716{
1717 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte047f3872018-07-01 21:04:45 +02001718 struct tlv_parsed tp;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001719 int rc = 0;
1720
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001721 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001722
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001723 log_set_context(LOG_CTX_GB_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001724
Harald Welte144e0292010-05-13 11:45:07 +02001725 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001726 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1727 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001728
Harald Welte94c9b442019-03-16 14:38:19 +01001729 if (nsvc_is_not_used(*nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) {
1730 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s on unused/pre-configured endpoint, discarding\n",
1731 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1732 gprs_ns_tx_status(*nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1733 return 0;
1734 }
1735
Harald Welte9ba50052010-03-14 15:45:01 +08001736 switch (nsh->pdu_type) {
1737 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001738 /* If we're dead and blocked and suddenly receive a
1739 * NS-ALIVE out of the blue, we might have been re-started
1740 * and should send a NS-RESET to make sure everything recovers
1741 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001742 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001743 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1744 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001745 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001746 break;
1747 case NS_PDUT_ALIVE_ACK:
Harald Welte047f3872018-07-01 21:04:45 +02001748 ns_mark_alive(*nsvc);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001749 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001750 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001751 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001752 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001753 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1754 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001755 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001756 if ((*nsvc)->state & NSE_S_BLOCKED)
1757 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001758 }
Harald Welte9ba50052010-03-14 15:45:01 +08001759 break;
1760 case NS_PDUT_UNITDATA:
1761 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001762 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001763 break;
1764 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001765 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001766 break;
1767 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001768 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001769 break;
1770 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001771 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001772 break;
1773 case NS_PDUT_UNBLOCK:
1774 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001775 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
Maxc513c0f2017-10-23 15:11:14 +02001776 ns_mark_unblocked(*nsvc);
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001777 /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001778 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Stefan Sperling797558e2018-11-19 17:18:41 +01001779 if (rc < 0)
1780 break;
Stefan Sperlingc6bfc632018-11-20 11:01:36 +01001781 /*
1782 * UNBLOCK_ACK has been transmitted.
1783 * Signal handlers may send additional messages following UNBLOCK_ACK under
1784 * the assumption that NS is now in UNBLOCKED state at our peer's end.
1785 */
Stefan Sperling797558e2018-11-19 17:18:41 +01001786 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001787 break;
1788 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001789 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001790 /* mark NS-VC as unblocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001791 ns_set_state(*nsvc, NSE_S_ALIVE);
1792 ns_set_remote_state(*nsvc, NSE_S_ALIVE);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001793 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001794 break;
1795 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001796 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001797 break;
1798 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001799 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001800 /* mark remote NS-VC as blocked + active */
Maxc513c0f2017-10-23 15:11:14 +02001801 ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +08001802 break;
Harald Welte047f3872018-07-01 21:04:45 +02001803 case SNS_PDUT_CONFIG:
1804 if (!nsi->bss_sns_fi)
1805 goto unexpected_sns;
1806 /* one additional byte ('end flag') before the TLV part starts */
1807 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+1,
1808 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1809 if (rc < 0) {
1810 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1811 return rc;
1812 }
1813 /* All sub-network service related message types */
1814 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1815 break;
1816 case SNS_PDUT_ACK:
1817 case SNS_PDUT_ADD:
1818 case SNS_PDUT_CHANGE_WEIGHT:
1819 case SNS_PDUT_DELETE:
1820 if (!nsi->bss_sns_fi)
1821 goto unexpected_sns;
1822 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
1823 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+5,
1824 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
1825 if (rc < 0) {
1826 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1827 return rc;
1828 }
1829 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1830 tp.lv[NS_IE_NSEI].len = 2;
1831 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1832 tp.lv[NS_IE_TRANS_ID].len = 1;
1833 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1834 break;
1835 case SNS_PDUT_CONFIG_ACK:
1836 case SNS_PDUT_SIZE:
1837 case SNS_PDUT_SIZE_ACK:
1838 if (!nsi->bss_sns_fi)
1839 goto unexpected_sns;
1840 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1841 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1842 if (rc < 0) {
1843 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
1844 return rc;
1845 }
1846 /* All sub-network service related message types */
1847 rc = gprs_ns_rx_sns(nsi, msg, &tp);
1848 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001849 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001850 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001851 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001852 rc = -EINVAL;
1853 break;
Harald Welte047f3872018-07-01 21:04:45 +02001854unexpected_sns:
1855 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s for NS Instance that has no SNS!\n",
1856 (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1857 rc = -EINVAL;
1858 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001859 }
1860 return rc;
1861}
1862
Harald Welte047f3872018-07-01 21:04:45 +02001863static bool gprs_sns_fsm_registered = false;
1864
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001865/*! Create a new GPRS NS instance
Harald Weltea9f23c82011-11-23 15:01:31 +01001866 * \param[in] cb Call-back function for incoming BSSGP data
1867 * \returns dynamically allocated gprs_ns_inst
1868 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001869struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001870{
Harald Welte4fcdd762012-06-16 16:40:42 +08001871 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001872
Harald Welte047f3872018-07-01 21:04:45 +02001873 if (!gprs_sns_fsm_registered) {
1874 gprs_sns_init();
1875 gprs_sns_fsm_registered = true;
1876 }
1877
Harald Weltef030b212010-04-26 19:18:54 +02001878 nsi->cb = cb;
1879 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001880 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1881 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1882 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1883 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1884 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1885 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1886 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Welte047f3872018-07-01 21:04:45 +02001887 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Harald Weltef030b212010-04-26 19:18:54 +02001888
Harald Welte60cc6ac2010-05-13 14:20:56 +02001889 /* Create the dummy NSVC that we use for sending
1890 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001891 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001892 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001893 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001894 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001895
Harald Welte3771d092010-04-30 20:26:32 +02001896 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001897}
1898
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001899void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001900{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001901 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001902
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001903 gprs_nsvc_delete(nsi->unknown_nsvc);
1904
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001905 /* delete all NSVCs and clear their timers */
1906 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1907 gprs_nsvc_delete(nsvc);
1908
1909 /* close socket and unregister */
1910 if (nsi->nsip.fd.data) {
1911 close(nsi->nsip.fd.fd);
1912 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001913 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001914 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001915}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001916
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001917/*! Destroy an entire NS instance
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001918 * \param nsi gprs_ns_inst that is to be destroyed
1919 *
1920 * This function releases all resources associated with the
1921 * NS-instance.
1922 */
1923void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1924{
1925 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001926 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001927 talloc_free(nsi);
1928}
1929
1930
1931/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1932 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1933
1934/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001935static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001936 struct sockaddr_in *saddr)
1937{
Harald Welteba4c6662010-05-19 15:38:10 +02001938 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001939 int ret = 0;
1940 socklen_t saddr_len = sizeof(*saddr);
1941
1942 if (!msg) {
1943 *error = -ENOMEM;
1944 return NULL;
1945 }
1946
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001947 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001948 (struct sockaddr *)saddr, &saddr_len);
1949 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001950 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001951 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001952 msgb_free(msg);
1953 *error = ret;
1954 return NULL;
1955 } else if (ret == 0) {
1956 msgb_free(msg);
1957 *error = ret;
1958 return NULL;
1959 }
1960
1961 msg->l2h = msg->data;
1962 msgb_put(msg, ret);
1963
1964 return msg;
1965}
1966
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001967static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001968{
1969 int error;
1970 struct sockaddr_in saddr;
1971 struct gprs_ns_inst *nsi = bfd->data;
1972 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1973
1974 if (!msg)
1975 return error;
1976
Harald Welteb3ee2652010-05-19 14:38:50 +02001977 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001978
1979 msgb_free(msg);
1980
1981 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001982}
1983
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001984static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001985{
1986 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1987 return -EIO;
1988}
1989
Harald Welteb3ee2652010-05-19 14:38:50 +02001990static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001991{
1992 int rc;
1993 struct gprs_ns_inst *nsi = nsvc->nsi;
1994 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1995
1996 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1997 (struct sockaddr *)daddr, sizeof(*daddr));
1998
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001999 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02002000
2001 return rc;
2002}
2003
2004/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02002005static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02002006{
2007 int rc = 0;
2008
2009 if (what & BSC_FD_READ)
2010 rc = handle_nsip_read(bfd);
2011 if (what & BSC_FD_WRITE)
2012 rc = handle_nsip_write(bfd);
2013
2014 return rc;
2015}
2016
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002017/*! Create a listening socket for GPRS NS/UDP/IP
Harald Weltea9f23c82011-11-23 15:01:31 +01002018 * \param[in] nsi NS protocol instance to listen
2019 * \returns >=0 (fd) in case of success, negative in case of error
2020 *
2021 * A call to this function will create a UDP socket bound to the port
2022 * number and IP address specified in the NS protocol instance. The
2023 * file descriptor of the socket will be stored in nsi->nsip.fd.
2024 */
Harald Welte7fb05232010-05-19 15:09:09 +02002025int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02002026{
Harald Welte73952e32012-06-16 14:59:56 +08002027 struct in_addr in;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002028 struct in_addr remote;
2029 char remote_str[INET_ADDRSTRLEN];
Harald Weltef030b212010-04-26 19:18:54 +02002030 int ret;
2031
Harald Weltebfe62e52017-05-15 12:48:30 +02002032 in.s_addr = osmo_htonl(nsi->nsip.local_ip);
Philipp Maier224a6dd2017-10-17 15:23:53 +02002033 remote.s_addr = osmo_htonl(nsi->nsip.remote_ip);
Harald Welte73952e32012-06-16 14:59:56 +08002034
2035 nsi->nsip.fd.cb = nsip_fd_cb;
2036 nsi->nsip.fd.data = nsi;
Philipp Maier224a6dd2017-10-17 15:23:53 +02002037
2038 if (nsi->nsip.remote_ip && nsi->nsip.remote_port) {
2039 /* connect to ensure only we only accept packets from the
2040 * configured remote end/peer */
2041 snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote));
2042 ret =
2043 osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
2044 IPPROTO_UDP, inet_ntoa(in),
2045 nsi->nsip.local_port, remote_str,
2046 nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
2047
2048 LOGP(DNS, LOGL_NOTICE,
2049 "Listening for nsip packets from %s:%u on %s:%u\n",
2050 remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port);
2051 } else {
2052 /* Accept UDP packets from any source IP/Port */
2053 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
2054 IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND);
2055
2056 LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port);
2057 }
2058
Stefan Sperlingf6538212018-10-10 16:09:32 +02002059 if (ret < 0) {
2060 nsi->nsip.fd.cb = NULL;
2061 nsi->nsip.fd.data = NULL;
Harald Weltef030b212010-04-26 19:18:54 +02002062 return ret;
Stefan Sperlingf6538212018-10-10 16:09:32 +02002063 }
Harald Weltef030b212010-04-26 19:18:54 +02002064
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01002065 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
2066 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
2067 if (ret < 0)
2068 LOGP(DNS, LOGL_ERROR,
2069 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
2070 nsi->nsip.dscp, ret, errno);
2071
Maxa19c1262017-10-20 15:39:45 +02002072 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 +02002073
2074 return ret;
2075}
Harald Welte3771d092010-04-30 20:26:32 +02002076
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002077/*! Initiate a RESET procedure
Harald Weltea9f23c82011-11-23 15:01:31 +01002078 * \param[in] nsvc NS-VC in which to start the procedure
2079 * \param[in] cause Numeric NS cause value
2080 *
2081 * This is a high-level function initiating a NS-RESET procedure. It
2082 * will not only send a NS-RESET, but also set the state to BLOCKED and
2083 * start the Tns-reset timer.
2084 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002085int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00002086{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002087 int rc;
2088
Harald Welte047f3872018-07-01 21:04:45 +02002089 ERR_IF_NSVC_USES_SNS(nsvc, "RESET procedure based on API request");
2090
Harald Welteb1020d52010-05-25 22:17:30 +02002091 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
2092 nsvc->nsei);
2093
Harald Welte731d1fc2010-05-14 11:53:08 +00002094 /* Mark NS-VC locally as blocked and dead */
Maxc513c0f2017-10-23 15:11:14 +02002095 ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET);
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02002096
Harald Welte731d1fc2010-05-14 11:53:08 +00002097 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002098 rc = gprs_ns_tx_reset(nsvc, cause);
2099 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00002100 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
2101 nsvc->nsei);
2102 }
2103 /* Start Tns-reset */
2104 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02002105
2106 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00002107}
2108
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002109/*! Establish a NS connection (from the BSS) to the SGSN
Harald Weltea9f23c82011-11-23 15:01:31 +01002110 * \param nsi NS-instance
2111 * \param[in] dest Destination IP/Port
2112 * \param[in] nsei NSEI of the to-be-established NS-VC
2113 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2114 * \returns struct gprs_nsvc representing the new NS-VC
2115 *
2116 * This function will establish a single NS/UDP/IP connection in uplink
2117 * (BSS to SGSN) direction.
2118 */
Harald Weltef5430362012-06-17 12:25:53 +08002119struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02002120 struct sockaddr_in *dest, uint16_t nsei,
2121 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02002122{
2123 struct gprs_nsvc *nsvc;
2124
Harald Welte047f3872018-07-01 21:04:45 +02002125 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00002126 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08002127 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00002128 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02002129 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02002130 nsvc->remote_end_is_sgsn = 1;
2131
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08002132 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
2133 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02002134}
Harald Weltea9f23c82011-11-23 15:01:31 +01002135
Harald Welte047f3872018-07-01 21:04:45 +02002136/*! Establish a NS connection (from the BSS) to the SGSN using SNS auto-configuration
2137 * \param nsi NS-instance
2138 * \param[in] dest Destination IP/Port
2139 * \param[in] nsei NSEI of the to-be-established NS-VC
2140 * \param[in] nsvci NSVCI of the to-be-established NS-VC
2141 * \returns struct gprs_nsvc representing the new NS-VC
2142 *
2143 * This function will establish a single NS/UDP/IP connection in uplink
2144 * (BSS to SGSN) direction. It will start with the SNS-SIZE procedure,
2145 * followed by BSS-originated SNS-CONFIG, then SGSN-originated SNS-CONFIG.
2146 *
2147 * Once configuration completes, the user will be notified by the S_SNS_CONFIGURED signal,
2148 * at which point he typically would want to initiate NS-RESET by means of gprs_nsvc_reset().
2149 */
2150struct gprs_nsvc *gprs_ns_nsip_connect_sns(struct gprs_ns_inst *nsi,
2151 struct sockaddr_in *dest, uint16_t nsei,
2152 uint16_t nsvci)
2153{
2154 struct gprs_nsvc *nsvc;
2155
2156 /* FIXME: We are getting the order wrong here. Normally, one would want
2157 * to start the SNS FSM *before* creating any NS-VC and then create the NS-VC
2158 * after the SNS layer has established the IP/port/etc. However, this would
2159 * require some massive code and API changes compared to existing libosmogb,
2160 * so let's keep the old logic. */
2161 nsvc = gprs_nsvc_by_rem_addr(nsi, dest);
Harald Welte94c9b442019-03-16 14:38:19 +01002162 if (!nsvc) {
2163 /* create NSVC with 0 data + signalling weight. This is illegal in SNS
2164 * and can hence only be created locally and serves as indication that
2165 * this NS-VC shall not be used for anything except SNS _unless_ it is
2166 * modified via SNS-{CONFIG,CHANGEWEIGHT,ADD} to become part of the
2167 * active NS-VCs */
2168 nsvc = gprs_nsvc_create2(nsi, nsvci, 0, 0);
2169 }
Harald Welte047f3872018-07-01 21:04:45 +02002170 nsvc->ip.bts_addr = *dest;
2171 nsvc->nsei = nsei;
2172 nsvc->remote_end_is_sgsn = 1;
2173 /* NSVCs are always UNBLOCKED in IP-SNS */
2174 ns_set_state(nsvc, 0);
2175
2176 if (nsi->bss_sns_fi)
2177 osmo_fsm_inst_term(nsi->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
2178 nsi->bss_sns_fi = gprs_sns_bss_fsm_alloc(nsi, nsvc, "NSIP");
2179 gprs_sns_bss_fsm_start(nsi);
2180 return nsvc;
2181}
2182
Harald Weltecca49632012-06-16 17:45:59 +08002183void gprs_ns_set_log_ss(int ss)
2184{
2185 DNS = ss;
2186}
2187
Daniel Willmannf1318fe2018-05-29 20:55:18 +02002188/*! Append the nsvc state to a talloc string
2189 * \param s The string to append to (allocated with talloc)
2190 * \param[in] nsvc The NS-VC to print the state of
2191 * \returns The new string with state information appended to it
2192 *
2193 * This function will append a comma-separated state of the NS-VC to the
2194 * string. The string needs to be allocated with talloc (e.g. talloc_strdup)
2195 */
2196char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc)
2197{
2198 s = talloc_asprintf_append(s,
2199 "%u,%u,%s,%s,%s,%s,%s\n",
2200 nsvc->nsei, nsvc->nsvci,
2201 NS_DESC_A(nsvc->state),
2202 NS_DESC_B(nsvc->state),
2203 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
2204 NS_DESC_A(nsvc->remote_state),
2205 NS_DESC_B(nsvc->remote_state));
2206
2207 return s;
2208}
2209
Harald Welte35042a22019-02-23 14:07:00 +01002210/*! Start the ALIVE timer procedure in all NS-VCs part of this NS Instance */
2211void gprs_nsvc_start_test(struct gprs_nsvc *nsvc)
2212{
Harald Welte94c9b442019-03-16 14:38:19 +01002213 /* skip the initial NS-VC unless it has explicitly been configured
2214 * via SNS-CONFIG from the SGSN */
2215 if (nsvc_is_not_used(nsvc))
2216 return;
Harald Welte35042a22019-02-23 14:07:00 +01002217 gprs_ns_tx_alive(nsvc);
2218 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
2219}
2220
Harald Welte047f3872018-07-01 21:04:45 +02002221void gprs_start_alive_all_nsvcs(struct gprs_ns_inst *nsi)
2222{
2223 struct gprs_nsvc *nsvc;
2224 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
2225 /* start the test procedure */
2226 gprs_nsvc_start_test(nsvc);
2227 }
2228}
2229
Harald Welte96e2a002017-06-12 21:44:18 +02002230/*! @} */