blob: bae943861835e5d8024aa899adb00760d2ccc7ab [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
206char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
207{
208 struct osmo_sockaddr *local;
209 struct osmo_sockaddr *remote;
210 struct osmo_sockaddr_str local_str;
211 struct osmo_sockaddr_str remote_str;
212
213 if (!buf_len)
214 return 0;
215
216 switch (nsvc->ll) {
217 case GPRS_NS_LL_UDP:
218 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
219 buf[0] = '\0';
220 return buf;
221 }
222
223 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
224 remote = gprs_ns2_ip_vc_sockaddr(nsvc);
225 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
226 strcpy(local_str.ip, "invalid");
227 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
228 strcpy(remote_str.ip, "invalid");
229
230 if (nsvc->nsvci_is_valid)
231 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
232 local_str.ip, local_str.port,
233 nsvc->nsvci,
234 remote_str.ip, remote_str.port);
235 else
236 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
237 local_str.ip, local_str.port,
238 remote_str.ip, remote_str.port);
239 break;
240 case GPRS_NS_LL_FR_GRE:
241 snprintf(buf, buf_len, "frgre)");
242 break;
243 case GPRS_NS_LL_E1:
244 snprintf(buf, buf_len, "e1)");
245 break;
246 default:
247 buf[0] = '\0';
248 break;
249 }
250
251 buf[buf_len - 1] = '\0';
252
253 return buf;
254}
255
256/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
257#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
258
259const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
260{
261 static __thread char buf[NS2_LL_MAX_STR];
262 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
263}
264
265char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
266{
267 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
268 if (!buf)
269 return buf;
270 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
271}
272
273/*!
274 * Receive a primitive from the NS User (Gb)
275 * \param nsi
276 * \param oph The primitive.
277 * \return 0 on success
278 */
279int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
280{
281 /* TODO: implement load distribution function */
282 /* TODO: implement resource distribution */
283 /* TODO: check for empty PDUs which can be sent to Request/Confirm
284 * the IP endpoint */
285 struct osmo_gprs_ns2_prim *nsp;
286 struct gprs_ns2_nse *nse = NULL;
287 struct gprs_ns2_vc *nsvc = NULL, *tmp;
288 uint16_t bvci, nsei;
289 uint8_t sducontrol = 0;
290
291 if (oph->sap != SAP_NS)
292 return -EINVAL;
293
294 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
295
296 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
297 return -EINVAL;
298
299 if (!oph->msg)
300 return -EINVAL;
301
302 bvci = nsp->bvci;
303 nsei = nsp->nsei;
304
305 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
306 if (!nse)
307 return -EINVAL;
308
309 llist_for_each_entry(tmp, &nse->nsvc, list) {
310 if (!gprs_ns2_vc_is_unblocked(tmp))
311 continue;
312 if (bvci == 0 && tmp->sig_weight == 0)
313 continue;
314 if (bvci != 0 && tmp->data_weight == 0)
315 continue;
316
317 nsvc = tmp;
318 }
319
320 /* TODO: send a status primitive back */
321 if (!nsvc)
322 return 0;
323
324 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
325 sducontrol = 1;
326 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
327 sducontrol = 2;
328
329 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
330}
331
332void ns2_prim_status_ind(struct gprs_ns2_inst *nsi,
333 uint16_t nsei, uint16_t bvci,
334 enum gprs_ns2_affecting_cause cause)
335{
336 struct osmo_gprs_ns2_prim nsp = {};
337 nsp.nsei = nsei;
338 nsp.bvci = bvci;
339 nsp.u.status.cause = cause;
340 nsp.u.status.transfer = -1;
341 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
342 PRIM_OP_INDICATION, NULL);
343 nsi->cb(&nsp.oph, nsi->cb_data);
344}
345
346/*!
347 * \brief ns2_vc_alloc
348 * \param bind
349 * \param nse
350 * \param initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
351 * \return
352 */
353struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
354{
355 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
356
357 if (!nsvc)
358 return NULL;
359
360 nsvc->bind = bind;
361 nsvc->nse = nse;
362 nsvc->mode = bind->vc_mode;
363 nsvc->sig_weight = 1;
364 nsvc->data_weight = 1;
365
366 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
367 if (!nsvc->ctrg) {
368 goto err;
369 }
370 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
371 if (!nsvc->statg)
372 goto err_group;
373 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
374 goto err_statg;
375
376 bind->nsi->rate_ctr_idx++;
377
378 llist_add(&nsvc->list, &nse->nsvc);
379 llist_add(&nsvc->blist, &bind->nsvc);
380
381 return nsvc;
382
383err_statg:
384 osmo_stat_item_group_free(nsvc->statg);
385err_group:
386 rate_ctr_group_free(nsvc->ctrg);
387err:
388 talloc_free(nsvc);
389
390 return NULL;
391}
392
393
394void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
395{
396 if (!nsvc)
397 return;
398
399 ns2_prim_status_ind(nsvc->nse->nsi, nsvc->nse->nsei,
400 0, NS_AFF_CAUSE_VC_FAILURE);
401
402 llist_del(&nsvc->list);
403 llist_del(&nsvc->blist);
404
405 /* notify nse this nsvc is unavailable */
406 ns2_nse_notify_unblocked(nsvc, false);
407
408 /* check if sns is using this VC */
409 ns2_sns_free_nsvc(nsvc);
410 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
411
412 /* let the driver/bind clean up it's internal state */
413 if (nsvc->priv && nsvc->bind->free_vc)
414 nsvc->bind->free_vc(nsvc);
415
416 osmo_stat_item_group_free(nsvc->statg);
417 rate_ctr_group_free(nsvc->ctrg);
418
419 talloc_free(nsvc);
420}
421
422struct msgb *gprs_ns2_msgb_alloc(void)
423{
424 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
425 "GPRS/NS");
426 if (!msg) {
427 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
428 NS_ALLOC_SIZE);
429 }
430 return msg;
431}
432
433/*!
434 * Create a status message to be sent over a new connection.
435 * \param[in] orig_msg the original message
436 * \param[in] tp TLVP parsed of the original message
437 * \param[out] reject
438 * \param[in] cause
439 * \return 0 on success
440 */
441static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
442{
443 struct msgb *msg = gprs_ns2_msgb_alloc();
444 struct gprs_ns_hdr *nsh;
445 bool have_vci = false;
446 uint8_t _cause = cause;
447 uint16_t nsei = 0;
448
449 if (!msg)
450 return -ENOMEM;
451
452 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
453 nsei = tlvp_val16be(tp, NS_IE_NSEI);
454
455 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
456 nsei, gprs_ns2_cause_str(cause));
457 }
458
459 msg->l2h = msgb_put(msg, sizeof(*nsh));
460 nsh = (struct gprs_ns_hdr *) msg->l2h;
461 nsh->pdu_type = NS_PDUT_STATUS;
462
463 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
464 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
465
466 /* Section 9.2.7.1: Static conditions for NS-VCI */
467 if (cause == NS_CAUSE_NSVC_BLOCKED ||
468 cause == NS_CAUSE_NSVC_UNKNOWN) {
469 if (!have_vci) {
470 msgb_free(msg);
471 return -EINVAL;
472 }
473
474 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
475 }
476
477 /* Section 9.2.7.2: Static conditions for NS PDU */
478 switch (cause) {
479 case NS_CAUSE_SEM_INCORR_PDU:
480 case NS_CAUSE_PDU_INCOMP_PSTATE:
481 case NS_CAUSE_PROTO_ERR_UNSPEC:
482 case NS_CAUSE_INVAL_ESSENT_IE:
483 case NS_CAUSE_MISSING_ESSENT_IE:
484 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
485 orig_msg->l2h);
486 break;
487 default:
488 break;
489 }
490
491 *reject = msg;
492 return 0;
493}
494
495struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
496{
497 struct gprs_ns2_nse *nse;
498
499 llist_for_each_entry(nse, &nsi->nse, list) {
500 if (nse->nsei == nsei)
501 return nse;
502 }
503
504 return NULL;
505}
506
507struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
508{
509 struct gprs_ns2_nse *nse;
510 struct gprs_ns2_vc *nsvc;
511
512 llist_for_each_entry(nse, &nsi->nse, list) {
513 llist_for_each_entry(nsvc, &nse->nsvc, list) {
514 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
515 return nsvc;
516 }
517 }
518
519 return NULL;
520}
521
522struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
523{
524 struct gprs_ns2_nse *nse;
525
526 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
527 if (nse) {
528 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
529 return nse;
530 }
531
532 nse = talloc_zero(nsi, struct gprs_ns2_nse);
533 if (!nse)
534 return NULL;
535
536 nse->nsei = nsei;
537 nse->nsi = nsi;
538 llist_add(&nse->list, &nsi->nse);
539 INIT_LLIST_HEAD(&nse->nsvc);
540
541 return nse;
542}
543
544void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
545{
546 struct gprs_ns2_vc *nsvc, *tmp;
547
548 if (!nse)
549 return;
550
551 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
552 gprs_ns2_free_nsvc(nsvc);
553 }
554
555 ns2_prim_status_ind(nse->nsi, nse->nsei,
556 0, NS_AFF_CAUSE_FAILURE);
557
558 llist_del(&nse->list);
559 if (nse->bss_sns_fi)
560 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
561 talloc_free(nse);
562}
563
564static inline int ns2_tlv_parse(struct tlv_parsed *dec,
565 const uint8_t *buf, int buf_len, uint8_t lv_tag,
566 uint8_t lv_tag2)
567{
568 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
569 * See comment of ns_att_tlvdef1. */
570 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
571 if (rc < 0)
572 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
573 return rc;
574}
575
576
577/*!
578 * Create a new VC based on a message. Depending on the bind it might create NSE.
579 * \param[in] bind
580 * \param[in] msg
581 * \param[in] logname A name to describe the VC. E.g. ip address pair
582 * \param[out] reject A message filled to be sent back. Only used in failure cases.
583 * \param[out] success A pointer which will be set to the new VC on success
584 * \return
585 */
586enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
587 struct msgb *msg,
588 const char *logname,
589 struct msgb **reject,
590 struct gprs_ns2_vc **success)
591{
592 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
593 struct tlv_parsed tp;
594 struct gprs_ns2_vc *nsvc;
595 struct gprs_ns2_nse *nse;
596 uint16_t nsvci;
597 uint16_t nsei;
598
599 int rc;
600
601 if (msg->len < sizeof(struct gprs_ns_hdr))
602 return GPRS_NS2_CS_ERROR;
603
604 if (nsh->pdu_type == NS_PDUT_STATUS) {
605 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
606 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
607 "for non-existing NS-VC\n",
608 logname);
609 return GPRS_NS2_CS_SKIPPED;
610 }
611
612 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
613 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
614 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
615 "for non-existing NS-VC\n",
616 logname);
617 return GPRS_NS2_CS_SKIPPED;
618 }
619
620 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
621 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
622 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
623 "for non-existing NS-VC\n",
624 logname);
625 return GPRS_NS2_CS_SKIPPED;
626 }
627
628 /* Only the RESET procedure creates a new NSVC */
629 if (nsh->pdu_type != NS_PDUT_RESET) {
630 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
631
632 if (rc < 0) {
633 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
634 return rc;
635 }
636 return GPRS_NS2_CS_REJECTED;
637 }
638
639 rc = ns2_tlv_parse(&tp, nsh->data,
640 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
641 if (rc < 0) {
642 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
643 "TLV Parse\n", rc);
644 /* TODO: send invalid message back */
645 return GPRS_NS2_CS_REJECTED;
646 }
647
648 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
649 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
650 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
651 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
652 return GPRS_NS2_CS_REJECTED;
653 }
654
655 /* find or create NSE */
656 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
657 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
658 if (!nse) {
659 if (!bind->nsi->create_nse) {
660 return GPRS_NS2_CS_SKIPPED;
661 }
662
663 nse = gprs_ns2_create_nse(bind->nsi, nsei);
664 if (!nse) {
665 return GPRS_NS2_CS_ERROR;
666 }
667 }
668
669 nsvc = ns2_vc_alloc(bind, nse, false);
670 if (!nsvc)
671 return GPRS_NS2_CS_SKIPPED;
672
673 nsvc->ll = GPRS_NS_LL_UDP;
674
675 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
676 nsvc->nsvci = nsvci;
677 nsvc->nsvci_is_valid = true;
678
679 *success = nsvc;
680
681 return GPRS_NS2_CS_CREATED;
682}
683
684struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
685 struct osmo_sockaddr *remote,
686 struct gprs_ns2_nse *nse,
687 uint16_t nsvci)
688{
689 struct gprs_ns2_vc *nsvc;
690
691 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
692 if (!nsvc)
693 return NULL;
694
695 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
696 nsvc->nsvci = nsvci;
697 nsvc->nsvci_is_valid = true;
698 }
699
700 return nsvc;
701}
702
703/*!
704 * Create a new IP-based NSVC
705 * \param bind
706 * \param remote
707 * \param nse
708 * \param nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
709 * \return
710 */
711struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
712 struct osmo_sockaddr *remote,
713 struct gprs_ns2_nse *nse,
714 uint16_t nsvci)
715{
716 struct gprs_ns2_vc *nsvc;
717 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
718 if (!nsvc)
719 return NULL;
720
721 gprs_ns2_vc_fsm_start(nsvc);
722
723 return nsvc;
724}
725
726/*!
727 * Create a new IP-based NSVC
728 * \param bind
729 * \param remote
730 * \param nsei
731 * \param nsvci only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
732 * \return
733 */
734struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
735 struct osmo_sockaddr *remote,
736 uint16_t nsei,
737 uint16_t nsvci)
738{
739 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
740
741 if (!nse) {
742 nse = gprs_ns2_create_nse(bind->nsi, nsei);
743 if (!nse)
744 return NULL;
745 }
746
747 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
748}
749
750/*!
751 * Create a new IP SNS NSE
752 * \param bind
753 * \param remote
754 * \param nsei
755 * \return 0 on success
756 */
757int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
758 struct osmo_sockaddr *remote,
759 uint16_t nsei)
760{
761 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
762 struct gprs_ns2_vc *nsvc;
763
764 if (!nse) {
765 nse = gprs_ns2_create_nse(bind->nsi, nsei);
766 if (!nse)
767 return -1;
768 }
769
770 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
771 if (!nsvc)
772 return -1;
773
774 if (!nse->bss_sns_fi)
775 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
776
777 if (!nse->bss_sns_fi)
778 return -1;
779
780 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
781}
782
783struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr(struct gprs_ns2_nse *nse,
784 struct osmo_sockaddr *sockaddr)
785{
786 struct gprs_ns2_vc *nsvc;
787 struct osmo_sockaddr *remote;
788
789 OSMO_ASSERT(nse);
790 OSMO_ASSERT(sockaddr);
791
792 llist_for_each_entry(nsvc, &nse->nsvc, list) {
793 remote = gprs_ns2_ip_vc_sockaddr(nsvc);
794 if (!osmo_sockaddr_cmp(sockaddr, remote))
795 return nsvc;
796 }
797
798 return NULL;
799}
800
801
802/*!
803 * \brief gprs_ns2_recv_vc entrypoint of received NS PDU from the driver/bind
804 * \param nsi
805 * \param vc
806 * \param msg the received message. Must not be freeded.
807 * \return
808 */
809int ns2_recv_vc(struct gprs_ns2_inst *nsi,
810 struct gprs_ns2_vc *nsvc,
811 struct msgb *msg)
812{
813 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
814 struct tlv_parsed tp;
815 int rc = 0;
816
817 if (msg->len < sizeof(struct gprs_ns_hdr))
818 return -EINVAL;
819
820 switch (nsh->pdu_type) {
821 case SNS_PDUT_CONFIG:
822 /* one additional byte ('end flag') before the TLV part starts */
823 rc = ns2_tlv_parse(&tp, nsh->data+1,
824 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
825 if (rc < 0) {
826 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
827 return rc;
828 }
829 /* All sub-network service related message types */
830 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
831 break;
832 case SNS_PDUT_ACK:
833 case SNS_PDUT_ADD:
834 case SNS_PDUT_CHANGE_WEIGHT:
835 case SNS_PDUT_DELETE:
836 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
837 rc = ns2_tlv_parse(&tp, nsh->data+1,
838 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
839 if (rc < 0) {
840 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
841 return rc;
842 }
843 tp.lv[NS_IE_NSEI].val = nsh->data+2;
844 tp.lv[NS_IE_NSEI].len = 2;
845 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
846 tp.lv[NS_IE_TRANS_ID].len = 1;
847 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
848 break;
849 case SNS_PDUT_CONFIG_ACK:
850 case SNS_PDUT_SIZE:
851 case SNS_PDUT_SIZE_ACK:
852 rc = ns2_tlv_parse(&tp, nsh->data,
853 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
854 if (rc < 0) {
855 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
856 return rc;
857 }
858 /* All sub-network service related message types */
859 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
860 break;
861
862 case NS_PDUT_UNITDATA:
863 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
864 break;
865 default:
866 rc = ns2_tlv_parse(&tp, nsh->data,
867 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
868 if (rc < 0) {
869 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
870 if (nsh->pdu_type != NS_PDUT_STATUS)
871 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
872 return rc;
873 }
874 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
875 break;
876 }
877
878 return rc;
879}
880
881/* notify a nse about the change of a nsvc */
882void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
883{
884 struct gprs_ns2_nse *nse = nsvc->nse;
885 struct gprs_ns2_vc *tmp;
886
887 if (unblocked == nse->alive)
888 return;
889
890 if (unblocked) {
891 /* this is the first unblocked NSVC on an unavailable NSE */
892 nse->alive = true;
893 ns2_prim_status_ind(nse->nsi, nse->nsei,
894 0, NS_AFF_CAUSE_RECOVERY);
895 return;
896 }
897
898 /* check if there are any remaining alive vcs */
899 llist_for_each_entry(tmp, &nse->nsvc, list) {
900 if (tmp == nsvc)
901 continue;
902
903 if (gprs_ns2_vc_is_unblocked(tmp)) {
904 /* there is at least one remaining alive NSVC */
905 return;
906 }
907 }
908
909 /* nse became unavailable */
910 nse->alive = false;
911 ns2_prim_status_ind(nse->nsi, nse->nsei,
912 0, NS_AFF_CAUSE_FAILURE);
913}
914
915/*! Create a new GPRS NS instance
916 * \param[in] ctx a talloc context
917 * \param[in] cb Call-back function for incoming BSSGP data
918 * \param[in] cb_data Call-back data
919 * \returns dynamically allocated gprs_ns_inst
920 */
921struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
922{
923 struct gprs_ns2_inst *nsi;
924
925 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
926 if (!nsi)
927 return NULL;
928
929 nsi->cb = cb;
930 nsi->cb_data = cb_data;
931 INIT_LLIST_HEAD(&nsi->binding);
932 INIT_LLIST_HEAD(&nsi->nse);
933
934 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
935 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
936 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
937 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
938 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
939 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
940 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
941 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
942
943 return nsi;
944}
945
946void gprs_ns2_free(struct gprs_ns2_inst *nsi)
947{
948 struct gprs_ns2_vc_bind *bind, *tbind;
949 struct gprs_ns2_nse *nse, *ntmp;
950
951 if (!nsi)
952 return;
953
954 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
955 gprs_ns2_free_nse(nse);
956 }
957
958 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
959 gprs_ns2_free_bind(bind);
960 }
961}
962
963/*!
964 * \brief gprs_ns2_dynamic_create_nse
965 * \param nsi the instance to modify
966 * \param create_nse if NSE can be created on receiving package. SGSN set this.
967 * \return
968 */
969int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
970{
971 nsi->create_nse = create_nse;
972
973 return 0;
974}
975
976void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
977{
978 struct gprs_ns2_vc *nsvc;
979 OSMO_ASSERT(nse);
980
981 llist_for_each_entry(nsvc, &nse->nsvc, list) {
982 if (nsvc->sns_only)
983 continue;
984
985 gprs_ns2_vc_fsm_start(nsvc);
986 }
987}
988
989void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
990{
991 bind->vc_mode = mode;
992}
993
994void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
995{
996 struct gprs_ns2_vc *nsvc, *tmp;
997 if (!bind)
998 return;
999
1000 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1001 gprs_ns2_free_nsvc(nsvc);
1002 }
1003
1004 if (bind->driver->free_bind)
1005 bind->driver->free_bind(bind);
1006
1007 llist_del(&bind->list);
1008 talloc_free(bind);
1009}
1010/*! @} */