blob: 002ea1d2bdd8d57feb9d1590f917b2425555906d [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2.c
2 * GPRS Networks Service (NS) messages on the Gb interface.
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
6/* (C) 2009-2018 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2016-2017,2020 sysmocom - s.f.m.c. GmbH
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 *
11 * All Rights Reserved
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30/*! \addtogroup libgb
31 * @{
32 *
33 * GPRS Networks Service (NS) messages on the Gb interface
34 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
35 *
36 * Some introduction into NS: NS is used typically on top of frame relay,
37 * but in the ip.access world it is encapsulated in UDP packets. It serves
38 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
39 * do much, apart from providing congestion notification and status indication.
40 *
41 * Terms:
42 *
43 * NS Network Service
44 * NSVC NS Virtual Connection
45 * NSEI NS Entity Identifier
46 * NSVL NS Virtual Link
47 * NSVLI NS Virtual Link Identifier
48 * BVC BSSGP Virtual Connection
49 * BVCI BSSGP Virtual Connection Identifier
50 * NSVCG NS Virtual Connection Goup
51 * Blocked NS-VC cannot be used for user traffic
52 * Alive Ability of a NS-VC to provide communication
53 *
54 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
55 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
56 * NS then has to figure out which NSVC's are responsible for this BVCI.
57 * Those mappings are administratively configured.
58 *
59 * This implementation has the following limitations:
60 * - Only one NS-VC for each NSE: No load-sharing function
61 * - NSVCI 65535 and 65534 are reserved for internal use
62 * - Only UDP is supported as of now, no frame relay support
63 * - There are no BLOCK and UNBLOCK timers (yet?)
64 *
65 * \file gprs_ns2.c */
66
67#include <stdlib.h>
68#include <unistd.h>
69#include <errno.h>
70#include <stdint.h>
71
72#include <sys/types.h>
73#include <sys/socket.h>
74#include <arpa/inet.h>
75
76#include <osmocom/core/fsm.h>
77#include <osmocom/core/msgb.h>
78#include <osmocom/core/rate_ctr.h>
79#include <osmocom/core/socket.h>
80#include <osmocom/core/sockaddr_str.h>
81#include <osmocom/core/stats.h>
82#include <osmocom/core/stat_item.h>
83#include <osmocom/core/talloc.h>
84#include <osmocom/gprs/gprs_msgb.h>
85#include <osmocom/gsm/prim.h>
86#include <osmocom/gsm/tlv.h>
87
88#include "gprs_ns2_internal.h"
89
90#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
91#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
92#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
93#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
94#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
95#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
96
97/* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules.
98 * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it,
99 * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2.
100 * The TLV parser depends on 8bit tag in many places.
101 * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE.
102 */
103static const struct tlv_definition ns_att_tlvdef1 = {
104 .def = {
105 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
106 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
107 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
108 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
109 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
110 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
111 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
112 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
113 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
114 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
115 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
116 /* NS_IE_IP_ADDR in the IPv4 version */
117 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
118 },
119};
120
121static const struct tlv_definition ns_att_tlvdef2 = {
122 .def = {
123 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
124 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
125 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
126 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
127 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
128 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
129 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
130 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
131 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
132 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
133 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
134 /* NS_IE_IP_ADDR in the IPv6 version */
135 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 },
136 },
137};
138
139
140/* Section 10.3.2, Table 13 */
141static const struct value_string ns2_cause_str[] = {
142 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
143 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
144 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
145 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
146 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
147 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
148 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
149 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
150 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
151 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
152 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
153 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
154 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
155 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
156 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
157 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
158 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
159 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
160 { 0, NULL }
161};
162
163/*! Obtain a human-readable string for NS cause value */
164const char *gprs_ns2_cause_str(int cause)
165{
166 enum ns_cause _cause = cause;
167 return get_value_string(ns2_cause_str, _cause);
168}
169
170static const struct rate_ctr_desc nsvc_ctr_description[] = {
171 { "packets:in", "Packets at NS Level ( In)" },
172 { "packets:out","Packets at NS Level (Out)" },
173 { "bytes:in", "Bytes at NS Level ( In)" },
174 { "bytes:out", "Bytes at NS Level (Out)" },
175 { "blocked", "NS-VC Block count " },
176 { "dead", "NS-VC gone dead count " },
177 { "replaced", "NS-VC replaced other count" },
178 { "nsei-chg", "NS-VC changed NSEI count " },
179 { "inv-nsvci", "NS-VCI was invalid count " },
180 { "inv-nsei", "NSEI was invalid count " },
181 { "lost:alive", "ALIVE ACK missing count " },
182 { "lost:reset", "RESET ACK missing count " },
183};
184
185static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
186 .group_name_prefix = "ns:nsvc",
187 .group_description = "NSVC Peer Statistics",
188 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
189 .ctr_desc = nsvc_ctr_description,
190 .class_id = OSMO_STATS_CLASS_PEER,
191};
192
193
194static const struct osmo_stat_item_desc nsvc_stat_description[] = {
195 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
196};
197
198static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
199 .group_name_prefix = "ns.nsvc",
200 .group_description = "NSVC Peer Statistics",
201 .num_items = ARRAY_SIZE(nsvc_stat_description),
202 .item_desc = nsvc_stat_description,
203 .class_id = OSMO_STATS_CLASS_PEER,
204};
205
Harald Welte5bef2cc2020-09-18 22:33:24 +0200206/*! string-format a given NS-VC into a user-supplied buffer.
207 * \param[in] buf user-allocated output buffer
208 * \param[in] buf_len size of user-allocated output buffer in bytes
209 * \param[in] nsvc NS-VC to be string-formatted
210 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200211char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
212{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200213 const struct osmo_sockaddr *local;
214 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200215 struct osmo_sockaddr_str local_str;
216 struct osmo_sockaddr_str remote_str;
217
218 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200219 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200220
221 switch (nsvc->ll) {
222 case GPRS_NS_LL_UDP:
223 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
224 buf[0] = '\0';
225 return buf;
226 }
227
228 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200229 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200230 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
231 strcpy(local_str.ip, "invalid");
232 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
233 strcpy(remote_str.ip, "invalid");
234
235 if (nsvc->nsvci_is_valid)
236 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
237 local_str.ip, local_str.port,
238 nsvc->nsvci,
239 remote_str.ip, remote_str.port);
240 else
241 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
242 local_str.ip, local_str.port,
243 remote_str.ip, remote_str.port);
244 break;
245 case GPRS_NS_LL_FR_GRE:
246 snprintf(buf, buf_len, "frgre)");
247 break;
248 case GPRS_NS_LL_E1:
249 snprintf(buf, buf_len, "e1)");
250 break;
251 default:
252 buf[0] = '\0';
253 break;
254 }
255
256 buf[buf_len - 1] = '\0';
257
258 return buf;
259}
260
261/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
262#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
263
Harald Welte5bef2cc2020-09-18 22:33:24 +0200264/*! string-format a given NS-VC to a thread-local static buffer.
265 * \param[in] nsvc NS-VC to be string-formatted
266 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200267const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
268{
269 static __thread char buf[NS2_LL_MAX_STR];
270 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
271}
272
Harald Welte5bef2cc2020-09-18 22:33:24 +0200273/*! string-format a given NS-VC to a dynamically allocated string.
274 * \param[in] ctx talloc context from which to allocate
275 * \param[in] nsvc NS-VC to be string-formatted
276 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200277char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
278{
279 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
280 if (!buf)
281 return buf;
282 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
283}
284
Harald Welte5bef2cc2020-09-18 22:33:24 +0200285/*! Receive a primitive from the NS User (Gb).
286 * \param[in] nsi NS instance to which the primitive is issued
287 * \param[in] oph The primitive
288 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200289int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
290{
291 /* TODO: implement load distribution function */
292 /* TODO: implement resource distribution */
293 /* TODO: check for empty PDUs which can be sent to Request/Confirm
294 * the IP endpoint */
295 struct osmo_gprs_ns2_prim *nsp;
296 struct gprs_ns2_nse *nse = NULL;
297 struct gprs_ns2_vc *nsvc = NULL, *tmp;
298 uint16_t bvci, nsei;
299 uint8_t sducontrol = 0;
300
301 if (oph->sap != SAP_NS)
302 return -EINVAL;
303
304 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
305
306 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
307 return -EINVAL;
308
309 if (!oph->msg)
310 return -EINVAL;
311
312 bvci = nsp->bvci;
313 nsei = nsp->nsei;
314
315 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
316 if (!nse)
317 return -EINVAL;
318
319 llist_for_each_entry(tmp, &nse->nsvc, list) {
320 if (!gprs_ns2_vc_is_unblocked(tmp))
321 continue;
322 if (bvci == 0 && tmp->sig_weight == 0)
323 continue;
324 if (bvci != 0 && tmp->data_weight == 0)
325 continue;
326
327 nsvc = tmp;
328 }
329
330 /* TODO: send a status primitive back */
331 if (!nsvc)
332 return 0;
333
334 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
335 sducontrol = 1;
336 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
337 sducontrol = 2;
338
339 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
340}
341
Harald Welte5bef2cc2020-09-18 22:33:24 +0200342/*! Send a STATUS.ind primitive to the specified NS instance user.
343 * \param[in] nsi NS instance on which we operate
344 * \param[in] nsei NSEI to which the statue relates
345 * \param[in] bvci BVCI to which the status relates
346 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200347void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
348 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 enum gprs_ns2_affecting_cause cause)
350{
351 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200352 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200353 nsp.bvci = bvci;
354 nsp.u.status.cause = cause;
355 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200356 nsp.u.status.first = nse->first;
357 nsp.u.status.persistent = nse->persistent;
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
359 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200360 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200361}
362
Harald Welte5bef2cc2020-09-18 22:33:24 +0200363/*! Allocate a NS-VC within the given bind + NSE.
364 * \param[in] bind The 'bind' on which we operate
365 * \param[in] nse The NS Entity on which we operate
366 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
367 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200368struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
369{
370 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
371
372 if (!nsvc)
373 return NULL;
374
375 nsvc->bind = bind;
376 nsvc->nse = nse;
377 nsvc->mode = bind->vc_mode;
378 nsvc->sig_weight = 1;
379 nsvc->data_weight = 1;
380
381 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
382 if (!nsvc->ctrg) {
383 goto err;
384 }
385 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
386 if (!nsvc->statg)
387 goto err_group;
388 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
389 goto err_statg;
390
391 bind->nsi->rate_ctr_idx++;
392
393 llist_add(&nsvc->list, &nse->nsvc);
394 llist_add(&nsvc->blist, &bind->nsvc);
395
396 return nsvc;
397
398err_statg:
399 osmo_stat_item_group_free(nsvc->statg);
400err_group:
401 rate_ctr_group_free(nsvc->ctrg);
402err:
403 talloc_free(nsvc);
404
405 return NULL;
406}
407
Harald Welte5bef2cc2020-09-18 22:33:24 +0200408/*! Destroy/release given NS-VC.
409 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200410void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
411{
412 if (!nsvc)
413 return;
414
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200415 ns2_prim_status_ind(nsvc->nse, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200416
417 llist_del(&nsvc->list);
418 llist_del(&nsvc->blist);
419
420 /* notify nse this nsvc is unavailable */
421 ns2_nse_notify_unblocked(nsvc, false);
422
423 /* check if sns is using this VC */
424 ns2_sns_free_nsvc(nsvc);
425 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
426
427 /* let the driver/bind clean up it's internal state */
428 if (nsvc->priv && nsvc->bind->free_vc)
429 nsvc->bind->free_vc(nsvc);
430
431 osmo_stat_item_group_free(nsvc->statg);
432 rate_ctr_group_free(nsvc->ctrg);
433
434 talloc_free(nsvc);
435}
436
Harald Welte5bef2cc2020-09-18 22:33:24 +0200437/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200438struct msgb *gprs_ns2_msgb_alloc(void)
439{
440 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
441 "GPRS/NS");
442 if (!msg) {
443 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
444 NS_ALLOC_SIZE);
445 }
446 return msg;
447}
448
Harald Welte5bef2cc2020-09-18 22:33:24 +0200449/*! Create a status message to be sent over a new connection.
450 * \param[in] orig_msg the original message
451 * \param[in] tp TLVP parsed of the original message
452 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
453 * \param[in] cause Cause for the rejection
454 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200455static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
456{
457 struct msgb *msg = gprs_ns2_msgb_alloc();
458 struct gprs_ns_hdr *nsh;
459 bool have_vci = false;
460 uint8_t _cause = cause;
461 uint16_t nsei = 0;
462
463 if (!msg)
464 return -ENOMEM;
465
466 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
467 nsei = tlvp_val16be(tp, NS_IE_NSEI);
468
469 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
470 nsei, gprs_ns2_cause_str(cause));
471 }
472
473 msg->l2h = msgb_put(msg, sizeof(*nsh));
474 nsh = (struct gprs_ns_hdr *) msg->l2h;
475 nsh->pdu_type = NS_PDUT_STATUS;
476
477 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
478 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
479
480 /* Section 9.2.7.1: Static conditions for NS-VCI */
481 if (cause == NS_CAUSE_NSVC_BLOCKED ||
482 cause == NS_CAUSE_NSVC_UNKNOWN) {
483 if (!have_vci) {
484 msgb_free(msg);
485 return -EINVAL;
486 }
487
488 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
489 }
490
491 /* Section 9.2.7.2: Static conditions for NS PDU */
492 switch (cause) {
493 case NS_CAUSE_SEM_INCORR_PDU:
494 case NS_CAUSE_PDU_INCOMP_PSTATE:
495 case NS_CAUSE_PROTO_ERR_UNSPEC:
496 case NS_CAUSE_INVAL_ESSENT_IE:
497 case NS_CAUSE_MISSING_ESSENT_IE:
498 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
499 orig_msg->l2h);
500 break;
501 default:
502 break;
503 }
504
505 *reject = msg;
506 return 0;
507}
508
Harald Welte5bef2cc2020-09-18 22:33:24 +0200509/*! Resolve a NS Entity based on its NSEI.
510 * \param[in] nsi NS Instance in which we do the look-up
511 * \param[in] nsei NSEI to look up
512 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200513struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
514{
515 struct gprs_ns2_nse *nse;
516
517 llist_for_each_entry(nse, &nsi->nse, list) {
518 if (nse->nsei == nsei)
519 return nse;
520 }
521
522 return NULL;
523}
524
Harald Welte5bef2cc2020-09-18 22:33:24 +0200525/*! Resolve a NS-VC Entity based on its NS-VCI.
526 * \param[in] nsi NS Instance in which we do the look-up
527 * \param[in] nsvci NS-VCI to look up
528 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200529struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
530{
531 struct gprs_ns2_nse *nse;
532 struct gprs_ns2_vc *nsvc;
533
534 llist_for_each_entry(nse, &nsi->nse, list) {
535 llist_for_each_entry(nsvc, &nse->nsvc, list) {
536 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
537 return nsvc;
538 }
539 }
540
541 return NULL;
542}
543
Harald Welte5bef2cc2020-09-18 22:33:24 +0200544/*! Create a NS Entity within given NS instance.
545 * \param[in] nsi NS instance in which to create NS Entity
546 * \param[in] nsei NS Entity Identifier of to-be-created NSE
547 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200548struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
549{
550 struct gprs_ns2_nse *nse;
551
552 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
553 if (nse) {
554 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
555 return nse;
556 }
557
558 nse = talloc_zero(nsi, struct gprs_ns2_nse);
559 if (!nse)
560 return NULL;
561
562 nse->nsei = nsei;
563 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200564 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200565 llist_add(&nse->list, &nsi->nse);
566 INIT_LLIST_HEAD(&nse->nsvc);
567
568 return nse;
569}
570
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200571/*! Return the NSEI
572 * \param[in] nse NS Entity
573 * \return the nsei.
574 */
575uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
576{
577 return nse->nsei;
578}
579
Harald Welte5bef2cc2020-09-18 22:33:24 +0200580/*! Destroy given NS Entity.
581 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200582void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
583{
584 struct gprs_ns2_vc *nsvc, *tmp;
585
586 if (!nse)
587 return;
588
589 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
590 gprs_ns2_free_nsvc(nsvc);
591 }
592
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200593 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200594
595 llist_del(&nse->list);
596 if (nse->bss_sns_fi)
597 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
598 talloc_free(nse);
599}
600
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200601void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
602{
603 struct gprs_ns2_nse *nse, *ntmp;
604
605 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
606 gprs_ns2_free_nse(nse);
607 }
608}
609
Alexander Couzens6a161492020-07-12 13:45:50 +0200610static inline int ns2_tlv_parse(struct tlv_parsed *dec,
611 const uint8_t *buf, int buf_len, uint8_t lv_tag,
612 uint8_t lv_tag2)
613{
614 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
615 * See comment of ns_att_tlvdef1. */
616 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
617 if (rc < 0)
618 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
619 return rc;
620}
621
622
Harald Welte5bef2cc2020-09-18 22:33:24 +0200623/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
624 * \param[in] bind the bind through which msg was received
625 * \param[in] msg the actual received message
626 * \param[in] logname A name to describe the VC. E.g. ip address pair
627 * \param[out] reject A message filled to be sent back. Only used in failure cases.
628 * \param[out] success A pointer which will be set to the new VC on success
629 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200630enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
631 struct msgb *msg,
632 const char *logname,
633 struct msgb **reject,
634 struct gprs_ns2_vc **success)
635{
636 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
637 struct tlv_parsed tp;
638 struct gprs_ns2_vc *nsvc;
639 struct gprs_ns2_nse *nse;
640 uint16_t nsvci;
641 uint16_t nsei;
642
643 int rc;
644
645 if (msg->len < sizeof(struct gprs_ns_hdr))
646 return GPRS_NS2_CS_ERROR;
647
648 if (nsh->pdu_type == NS_PDUT_STATUS) {
649 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
650 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
651 "for non-existing NS-VC\n",
652 logname);
653 return GPRS_NS2_CS_SKIPPED;
654 }
655
656 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
657 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
658 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
659 "for non-existing NS-VC\n",
660 logname);
661 return GPRS_NS2_CS_SKIPPED;
662 }
663
664 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
665 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
666 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
667 "for non-existing NS-VC\n",
668 logname);
669 return GPRS_NS2_CS_SKIPPED;
670 }
671
Alexander Couzens48f63862020-09-12 02:19:19 +0200672 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
673 /* Only the RESET procedure creates a new NSVC */
674 if (nsh->pdu_type != NS_PDUT_RESET) {
675 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200676
Alexander Couzens48f63862020-09-12 02:19:19 +0200677 if (rc < 0) {
678 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
679 return rc;
680 }
681 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200682 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200683 } else { /* NS2_VC_MODE_ALIVE */
684 /* Only the ALIVE procedure creates a new NSVC */
685 if (nsh->pdu_type != NS_PDUT_ALIVE) {
686 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
687
688 if (rc < 0) {
689 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
690 return rc;
691 }
692 return GPRS_NS2_CS_REJECTED;
693 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200694 }
695
696 rc = ns2_tlv_parse(&tp, nsh->data,
697 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
698 if (rc < 0) {
699 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
700 "TLV Parse\n", rc);
701 /* TODO: send invalid message back */
702 return GPRS_NS2_CS_REJECTED;
703 }
704
Alexander Couzens48f63862020-09-12 02:19:19 +0200705 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
706 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
707 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
708 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
709 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
710 return GPRS_NS2_CS_REJECTED;
711 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200712 }
713
714 /* find or create NSE */
715 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
716 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
717 if (!nse) {
718 if (!bind->nsi->create_nse) {
719 return GPRS_NS2_CS_SKIPPED;
720 }
721
722 nse = gprs_ns2_create_nse(bind->nsi, nsei);
723 if (!nse) {
724 return GPRS_NS2_CS_ERROR;
725 }
726 }
727
728 nsvc = ns2_vc_alloc(bind, nse, false);
729 if (!nsvc)
730 return GPRS_NS2_CS_SKIPPED;
731
732 nsvc->ll = GPRS_NS_LL_UDP;
733
734 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
735 nsvc->nsvci = nsvci;
736 nsvc->nsvci_is_valid = true;
737
738 *success = nsvc;
739
740 return GPRS_NS2_CS_CREATED;
741}
742
Harald Welte5bef2cc2020-09-18 22:33:24 +0200743/*! Create, and connect an inactive, new IP-based NS-VC
744 * \param[in] bind bind in which the new NS-VC is to be created
745 * \param[in] remote remote address to which to connect
746 * \param[in] nse NS Entity in which the NS-VC is to be created
747 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
748 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200749struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700750 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200751 struct gprs_ns2_nse *nse,
752 uint16_t nsvci)
753{
754 struct gprs_ns2_vc *nsvc;
755
756 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
757 if (!nsvc)
758 return NULL;
759
760 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
761 nsvc->nsvci = nsvci;
762 nsvc->nsvci_is_valid = true;
763 }
764
765 return nsvc;
766}
767
Harald Welte5bef2cc2020-09-18 22:33:24 +0200768/*! Create, connect and activate a new IP-based NS-VC
769 * \param[in] bind bind in which the new NS-VC is to be created
770 * \param[in] remote remote address to which to connect
771 * \param[in] nse NS Entity in which the NS-VC is to be created
772 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
773 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200774struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700775 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200776 struct gprs_ns2_nse *nse,
777 uint16_t nsvci)
778{
779 struct gprs_ns2_vc *nsvc;
780 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
781 if (!nsvc)
782 return NULL;
783
784 gprs_ns2_vc_fsm_start(nsvc);
785
786 return nsvc;
787}
788
Harald Welte5bef2cc2020-09-18 22:33:24 +0200789/*! Create, connect and activate a new IP-based NS-VC
790 * \param[in] bind bind in which the new NS-VC is to be created
791 * \param[in] remote remote address to which to connect
792 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
793 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
794 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200795struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700796 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200797 uint16_t nsei,
798 uint16_t nsvci)
799{
800 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
801
802 if (!nse) {
803 nse = gprs_ns2_create_nse(bind->nsi, nsei);
804 if (!nse)
805 return NULL;
806 }
807
808 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
809}
810
Harald Welte5bef2cc2020-09-18 22:33:24 +0200811/*! Create, connect and activate a new IP-SNS NSE.
812 * \param[in] bind bind in which the new NS-VC is to be created
813 * \param[in] remote remote address to which to connect
814 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
815 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200816int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700817 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200818 uint16_t nsei)
819{
820 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
821 struct gprs_ns2_vc *nsvc;
822
823 if (!nse) {
824 nse = gprs_ns2_create_nse(bind->nsi, nsei);
825 if (!nse)
826 return -1;
827 }
828
829 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
830 if (!nsvc)
831 return -1;
832
833 if (!nse->bss_sns_fi)
834 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
835
836 if (!nse->bss_sns_fi)
837 return -1;
838
839 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
840}
841
Harald Welte5bef2cc2020-09-18 22:33:24 +0200842/*! Find NS-VC for given socket address.
843 * \param[in] nse NS Entity in which to search
844 * \param[in] sockaddr socket address to search for
845 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200846struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700847 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200848{
849 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200850 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200851
852 OSMO_ASSERT(nse);
853 OSMO_ASSERT(sockaddr);
854
855 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200856 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200857 if (!osmo_sockaddr_cmp(sockaddr, remote))
858 return nsvc;
859 }
860
861 return NULL;
862}
863
864
Harald Welte5bef2cc2020-09-18 22:33:24 +0200865/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200866 * \param[in] nsvc NS-VC for which the message was received
867 * \param msg the received message. Ownership is trasnferred, caller must not free it!
868 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200869int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200870 struct msgb *msg)
871{
872 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
873 struct tlv_parsed tp;
874 int rc = 0;
875
876 if (msg->len < sizeof(struct gprs_ns_hdr))
877 return -EINVAL;
878
879 switch (nsh->pdu_type) {
880 case SNS_PDUT_CONFIG:
881 /* one additional byte ('end flag') before the TLV part starts */
882 rc = ns2_tlv_parse(&tp, nsh->data+1,
883 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
884 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200885 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200886 return rc;
887 }
888 /* All sub-network service related message types */
889 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
890 break;
891 case SNS_PDUT_ACK:
892 case SNS_PDUT_ADD:
893 case SNS_PDUT_CHANGE_WEIGHT:
894 case SNS_PDUT_DELETE:
895 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200896 rc = ns2_tlv_parse(&tp, nsh->data+5,
897 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200898 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200899 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200900 return rc;
901 }
902 tp.lv[NS_IE_NSEI].val = nsh->data+2;
903 tp.lv[NS_IE_NSEI].len = 2;
904 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
905 tp.lv[NS_IE_TRANS_ID].len = 1;
906 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
907 break;
908 case SNS_PDUT_CONFIG_ACK:
909 case SNS_PDUT_SIZE:
910 case SNS_PDUT_SIZE_ACK:
911 rc = ns2_tlv_parse(&tp, nsh->data,
912 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
913 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200914 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200915 return rc;
916 }
917 /* All sub-network service related message types */
918 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
919 break;
920
921 case NS_PDUT_UNITDATA:
922 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
923 break;
924 default:
925 rc = ns2_tlv_parse(&tp, nsh->data,
926 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
927 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200928 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200929 if (nsh->pdu_type != NS_PDUT_STATUS)
930 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
931 return rc;
932 }
933 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
934 break;
935 }
936
937 return rc;
938}
939
Harald Welte5bef2cc2020-09-18 22:33:24 +0200940/*! Notify a nse about the change of a NS-VC.
941 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
942 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200943void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
944{
945 struct gprs_ns2_nse *nse = nsvc->nse;
946 struct gprs_ns2_vc *tmp;
947
948 if (unblocked == nse->alive)
949 return;
950
951 if (unblocked) {
952 /* this is the first unblocked NSVC on an unavailable NSE */
953 nse->alive = true;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200954 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200955 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200956 return;
957 }
958
959 /* check if there are any remaining alive vcs */
960 llist_for_each_entry(tmp, &nse->nsvc, list) {
961 if (tmp == nsvc)
962 continue;
963
964 if (gprs_ns2_vc_is_unblocked(tmp)) {
965 /* there is at least one remaining alive NSVC */
966 return;
967 }
968 }
969
970 /* nse became unavailable */
971 nse->alive = false;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200972 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200973}
974
975/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +0200976 * \param[in] ctx a talloc context to allocate NS instance from
977 * \param[in] cb Call-back function for dispatching primitives to the user
978 * \param[in] cb_data transparent user data passed to Call-back
979 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200980struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
981{
982 struct gprs_ns2_inst *nsi;
983
984 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
985 if (!nsi)
986 return NULL;
987
988 nsi->cb = cb;
989 nsi->cb_data = cb_data;
990 INIT_LLIST_HEAD(&nsi->binding);
991 INIT_LLIST_HEAD(&nsi->nse);
992
993 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
994 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
995 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
996 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
997 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
998 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
999 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1000 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1001
1002 return nsi;
1003}
1004
Harald Welte5bef2cc2020-09-18 22:33:24 +02001005/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1006 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001007void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1008{
Alexander Couzens6a161492020-07-12 13:45:50 +02001009 if (!nsi)
1010 return;
1011
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001012 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001013 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001014
1015 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001016}
1017
Harald Welte5bef2cc2020-09-18 22:33:24 +02001018/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1019 * \param nsi the instance to modify
1020 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1021 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001022 */
1023int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1024{
1025 nsi->create_nse = create_nse;
1026
1027 return 0;
1028}
1029
Harald Welte5bef2cc2020-09-18 22:33:24 +02001030/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1031 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001032void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1033{
1034 struct gprs_ns2_vc *nsvc;
1035 OSMO_ASSERT(nse);
1036
1037 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1038 if (nsvc->sns_only)
1039 continue;
1040
1041 gprs_ns2_vc_fsm_start(nsvc);
1042 }
1043}
1044
Harald Welte5bef2cc2020-09-18 22:33:24 +02001045/*! Set the mode of given bind.
1046 * \param[in] bind the bind we want to set the mode of
1047 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001048void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1049{
1050 bind->vc_mode = mode;
1051}
1052
Harald Welte5bef2cc2020-09-18 22:33:24 +02001053/*! Destroy a given bind.
1054 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001055void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1056{
1057 struct gprs_ns2_vc *nsvc, *tmp;
1058 if (!bind)
1059 return;
1060
1061 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1062 gprs_ns2_free_nsvc(nsvc);
1063 }
1064
1065 if (bind->driver->free_bind)
1066 bind->driver->free_bind(bind);
1067
1068 llist_del(&bind->list);
1069 talloc_free(bind);
1070}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001071
1072void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1073{
1074 struct gprs_ns2_vc_bind *bind, *tbind;
1075
1076 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1077 gprs_ns2_free_bind(bind);
1078 }
1079}
Alexander Couzens6a161492020-07-12 13:45:50 +02001080/*! @} */