Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \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: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 60 | * - NSVCI 65535 and 65534 are reserved for internal use |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 61 | * - There are no BLOCK and UNBLOCK timers (yet?) |
| 62 | * |
| 63 | * \file gprs_ns2.c */ |
| 64 | |
| 65 | #include <stdlib.h> |
| 66 | #include <unistd.h> |
| 67 | #include <errno.h> |
| 68 | #include <stdint.h> |
| 69 | |
| 70 | #include <sys/types.h> |
| 71 | #include <sys/socket.h> |
| 72 | #include <arpa/inet.h> |
| 73 | |
| 74 | #include <osmocom/core/fsm.h> |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 75 | #include <osmocom/core/logging.h> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 76 | #include <osmocom/core/msgb.h> |
| 77 | #include <osmocom/core/rate_ctr.h> |
| 78 | #include <osmocom/core/socket.h> |
| 79 | #include <osmocom/core/sockaddr_str.h> |
| 80 | #include <osmocom/core/stats.h> |
| 81 | #include <osmocom/core/stat_item.h> |
| 82 | #include <osmocom/core/talloc.h> |
| 83 | #include <osmocom/gprs/gprs_msgb.h> |
| 84 | #include <osmocom/gsm/prim.h> |
| 85 | #include <osmocom/gsm/tlv.h> |
| 86 | |
| 87 | #include "gprs_ns2_internal.h" |
| 88 | |
| 89 | #define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__) |
| 90 | #define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__) |
| 91 | #define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED) |
| 92 | #define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED)); |
| 93 | #define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE) |
| 94 | #define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE)); |
| 95 | |
| 96 | /* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules. |
| 97 | * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it, |
| 98 | * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2. |
| 99 | * The TLV parser depends on 8bit tag in many places. |
| 100 | * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE. |
| 101 | */ |
| 102 | static const struct tlv_definition ns_att_tlvdef1 = { |
| 103 | .def = { |
| 104 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 105 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 106 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 107 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 108 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 109 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 110 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 111 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 112 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 113 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 114 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 115 | /* NS_IE_IP_ADDR in the IPv4 version */ |
| 116 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 }, |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | static const struct tlv_definition ns_att_tlvdef2 = { |
| 121 | .def = { |
| 122 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 123 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 124 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 125 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 126 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 127 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 128 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 129 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 130 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 131 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 132 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 133 | /* NS_IE_IP_ADDR in the IPv6 version */ |
| 134 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 }, |
| 135 | }, |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | /* Section 10.3.2, Table 13 */ |
Alexander Couzens | b3b837c | 2020-10-27 15:12:25 +0100 | [diff] [blame] | 140 | const struct value_string gprs_ns2_cause_strs[] = { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 141 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 142 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 143 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 144 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 145 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 146 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 147 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 148 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
| 149 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 150 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 151 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 152 | { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" }, |
| 153 | { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" }, |
| 154 | { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" }, |
| 155 | { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" }, |
| 156 | { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" }, |
| 157 | { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" }, |
| 158 | { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" }, |
| 159 | { 0, NULL } |
| 160 | }; |
| 161 | |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 162 | static const struct rate_ctr_desc ns_ctr_description[] = { |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 163 | [NS_CTR_PKTS_IN] = { "packets:in", "Packets at NS Level ( In)" }, |
| 164 | [NS_CTR_PKTS_OUT] = { "packets:out", "Packets at NS Level (Out)" }, |
Harald Welte | f22ae5a | 2021-01-30 22:01:28 +0100 | [diff] [blame] | 165 | [NS_CTR_PKTS_OUT_DROP] = { "packets:out:drop", "Dropped Packets (Out)" }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 166 | [NS_CTR_BYTES_IN] = { "bytes:in", "Bytes at NS Level ( In)" }, |
| 167 | [NS_CTR_BYTES_OUT] = { "bytes:out", "Bytes at NS Level (Out)" }, |
Harald Welte | f22ae5a | 2021-01-30 22:01:28 +0100 | [diff] [blame] | 168 | [NS_CTR_BYTES_OUT_DROP] = { "bytes:out:drop", "Dropped Bytes (Out)" }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 169 | [NS_CTR_BLOCKED] = { "blocked", "NS-VC Block count " }, |
Harald Welte | b40bf8b | 2021-01-30 22:43:01 +0100 | [diff] [blame] | 170 | [NS_CTR_UNBLOCKED] = { "unblocked", "NS-VC Unblock count " }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 171 | [NS_CTR_DEAD] = { "dead", "NS-VC gone dead count " }, |
| 172 | [NS_CTR_REPLACED] = { "replaced", "NS-VC replaced other count" }, |
| 173 | [NS_CTR_NSEI_CHG] = { "nsei-chg", "NS-VC changed NSEI count " }, |
| 174 | [NS_CTR_INV_VCI] = { "inv-nsvci", "NS-VCI was invalid count " }, |
| 175 | [NS_CTR_INV_NSEI] = { "inv-nsei", "NSEI was invalid count " }, |
| 176 | [NS_CTR_LOST_ALIVE] = { "lost:alive", "ALIVE ACK missing count " }, |
| 177 | [NS_CTR_LOST_RESET] = { "lost:reset", "RESET ACK missing count " }, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 180 | static const struct rate_ctr_group_desc nse_ctrg_desc = { |
| 181 | .group_name_prefix = "ns:nse", |
| 182 | .group_description = "NSE Peer Statistics", |
| 183 | .num_ctr = ARRAY_SIZE(ns_ctr_description), |
| 184 | .ctr_desc = ns_ctr_description, |
| 185 | .class_id = OSMO_STATS_CLASS_PEER, |
| 186 | }; |
| 187 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 188 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
| 189 | .group_name_prefix = "ns:nsvc", |
| 190 | .group_description = "NSVC Peer Statistics", |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 191 | .num_ctr = ARRAY_SIZE(ns_ctr_description), |
| 192 | .ctr_desc = ns_ctr_description, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 193 | .class_id = OSMO_STATS_CLASS_PEER, |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | static const struct osmo_stat_item_desc nsvc_stat_description[] = { |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 198 | [NS_STAT_ALIVE_DELAY] = { "alive.delay", "ALIVE response time ", "ms", 16, 0 }, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | static const struct osmo_stat_item_group_desc nsvc_statg_desc = { |
| 202 | .group_name_prefix = "ns.nsvc", |
| 203 | .group_description = "NSVC Peer Statistics", |
| 204 | .num_items = ARRAY_SIZE(nsvc_stat_description), |
| 205 | .item_desc = nsvc_stat_description, |
| 206 | .class_id = OSMO_STATS_CLASS_PEER, |
| 207 | }; |
| 208 | |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 209 | const struct osmo_stat_item_desc nsbind_stat_description[] = { |
| 210 | [NS2_BIND_STAT_BACKLOG_LEN] = { "tx_backlog_length", "Transmit backlog length", "packets", 16, 0 }, |
| 211 | }; |
| 212 | |
| 213 | static const struct osmo_stat_item_group_desc nsbind_statg_desc = { |
| 214 | .group_name_prefix = "ns.bind", |
| 215 | .group_description = "NS Bind Statistics", |
| 216 | .num_items = ARRAY_SIZE(nsbind_stat_description), |
| 217 | .item_desc = nsbind_stat_description, |
| 218 | .class_id = OSMO_STATS_CLASS_PEER, |
| 219 | }; |
| 220 | |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 221 | const struct value_string gprs_ns2_aff_cause_prim_strs[] = { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 222 | { GPRS_NS2_AFF_CAUSE_VC_FAILURE, "NSVC failure" }, |
| 223 | { GPRS_NS2_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" }, |
| 224 | { GPRS_NS2_AFF_CAUSE_FAILURE, "NSE failure" }, |
| 225 | { GPRS_NS2_AFF_CAUSE_RECOVERY, "NSE recovery" }, |
| 226 | { GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" }, |
| 227 | { GPRS_NS2_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" }, |
Alexander Couzens | b2de546 | 2021-02-16 16:24:30 +0100 | [diff] [blame] | 228 | { GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS, "NSE SNS no endpoints"}, |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 229 | { GPRS_NS2_AFF_CAUSE_MTU_CHANGE, "NSE MTU changed" }, |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 230 | { 0, NULL } |
| 231 | }; |
| 232 | |
Alexander Couzens | 0ab028c | 2020-11-04 02:41:44 +0100 | [diff] [blame] | 233 | const struct value_string gprs_ns2_prim_strs[] = { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 234 | { GPRS_NS2_PRIM_UNIT_DATA, "UNIT DATA" }, |
| 235 | { GPRS_NS2_PRIM_CONGESTION, "CONGESTION" }, |
| 236 | { GPRS_NS2_PRIM_STATUS, "STATUS" }, |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 237 | { 0, NULL } |
| 238 | }; |
| 239 | |
Harald Welte | a24e7ee | 2020-11-29 17:38:48 +0100 | [diff] [blame] | 240 | const struct value_string gprs_ns2_lltype_strs[] = { |
| 241 | { GPRS_NS2_LL_UDP, "UDP" }, |
| 242 | { GPRS_NS2_LL_FR_GRE, "FR_GRE" }, |
| 243 | { GPRS_NS2_LL_FR, "FR" }, |
| 244 | { 0, NULL } |
| 245 | }; |
| 246 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 247 | /*! string-format a given NS-VC into a user-supplied buffer. |
| 248 | * \param[in] buf user-allocated output buffer |
| 249 | * \param[in] buf_len size of user-allocated output buffer in bytes |
| 250 | * \param[in] nsvc NS-VC to be string-formatted |
| 251 | * \return pointer to buf on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 252 | char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc) |
| 253 | { |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 254 | const struct osmo_sockaddr *local; |
| 255 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 256 | struct osmo_sockaddr_str local_str; |
| 257 | struct osmo_sockaddr_str remote_str; |
| 258 | |
| 259 | if (!buf_len) |
Harald Welte | 92ad029 | 2020-09-18 22:34:24 +0200 | [diff] [blame] | 260 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 261 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 262 | switch (nsvc->nse->ll) { |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 263 | case GPRS_NS2_LL_UDP: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 264 | if (!gprs_ns2_is_ip_bind(nsvc->bind)) { |
| 265 | buf[0] = '\0'; |
| 266 | return buf; |
| 267 | } |
| 268 | |
| 269 | local = gprs_ns2_ip_bind_sockaddr(nsvc->bind); |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 270 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 271 | if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas)) |
| 272 | strcpy(local_str.ip, "invalid"); |
| 273 | if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas)) |
| 274 | strcpy(remote_str.ip, "invalid"); |
| 275 | |
| 276 | if (nsvc->nsvci_is_valid) |
| 277 | snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u", |
| 278 | local_str.ip, local_str.port, |
| 279 | nsvc->nsvci, |
| 280 | remote_str.ip, remote_str.port); |
| 281 | else |
| 282 | snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u", |
| 283 | local_str.ip, local_str.port, |
| 284 | remote_str.ip, remote_str.port); |
| 285 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 286 | case GPRS_NS2_LL_FR_GRE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 287 | snprintf(buf, buf_len, "frgre)"); |
| 288 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 289 | case GPRS_NS2_LL_FR: |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 290 | snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind), |
| 291 | gprs_ns2_fr_nsvc_dlci(nsvc)); |
| 292 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 293 | default: |
Harald Welte | 6188e00 | 2020-12-01 17:20:07 +0100 | [diff] [blame] | 294 | snprintf(buf, buf_len, "unknown)"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | buf[buf_len - 1] = '\0'; |
| 299 | |
| 300 | return buf; |
| 301 | } |
| 302 | |
| 303 | /* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */ |
| 304 | #define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8 |
| 305 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 306 | /*! string-format a given NS-VC to a thread-local static buffer. |
| 307 | * \param[in] nsvc NS-VC to be string-formatted |
| 308 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 309 | const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc) |
| 310 | { |
| 311 | static __thread char buf[NS2_LL_MAX_STR]; |
| 312 | return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc); |
| 313 | } |
| 314 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 315 | /*! string-format a given NS-VC to a dynamically allocated string. |
| 316 | * \param[in] ctx talloc context from which to allocate |
| 317 | * \param[in] nsvc NS-VC to be string-formatted |
| 318 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 319 | char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc) |
| 320 | { |
| 321 | char *buf = talloc_size(ctx, NS2_LL_MAX_STR); |
| 322 | if (!buf) |
| 323 | return buf; |
| 324 | return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc); |
| 325 | } |
| 326 | |
Daniel Willmann | f128654 | 2020-11-03 23:03:33 +0100 | [diff] [blame] | 327 | /*! Return the current state name of a given NS-VC to a thread-local static buffer. |
| 328 | * \param[in] nsvc NS-VC to return the state of |
| 329 | * \return pointer to the string on success; NULL on error */ |
| 330 | const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc) |
| 331 | { |
| 332 | return osmo_fsm_inst_state_name(nsvc->fi); |
| 333 | } |
| 334 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 335 | /* select a signalling NSVC and respect sig_counter |
| 336 | * param[out] reset_counter - all counter has to be resetted to their signal weight |
| 337 | * return the chosen nsvc or NULL |
| 338 | */ |
| 339 | static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse) |
| 340 | { |
| 341 | struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp; |
| 342 | |
| 343 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 344 | if (tmp->sig_weight == 0) |
| 345 | continue; |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 346 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 347 | continue; |
| 348 | if (tmp->sig_counter == 0) { |
| 349 | last = tmp; |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | tmp->sig_counter--; |
| 354 | nsvc = tmp; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | /* all counter were zero, but there are valid nsvc */ |
| 359 | if (!nsvc && last) { |
| 360 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 361 | tmp->sig_counter = tmp->sig_weight; |
| 362 | } |
| 363 | |
| 364 | last->sig_counter--; |
| 365 | return last; |
| 366 | } else { |
| 367 | return nsvc; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */ |
Alexander Couzens | 9e81a32 | 2021-02-16 17:36:25 +0100 | [diff] [blame] | 372 | static struct gprs_ns2_vc *ns2_load_sharing_modulo( |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 373 | struct gprs_ns2_nse *nse, |
| 374 | uint16_t bvci, |
| 375 | uint32_t load_selector) |
| 376 | { |
| 377 | struct gprs_ns2_vc *tmp; |
Alexander Couzens | 265a5eb | 2021-02-02 11:26:17 +0100 | [diff] [blame] | 378 | uint32_t mod; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 379 | uint32_t i = 0; |
| 380 | |
Alexander Couzens | 265a5eb | 2021-02-02 11:26:17 +0100 | [diff] [blame] | 381 | if (nse->nsvc_count == 0) |
| 382 | return NULL; |
| 383 | |
| 384 | mod = (bvci + load_selector) % nse->nsvc_count; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 385 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 386 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 387 | continue; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 388 | if (i == mod) |
| 389 | return tmp; |
| 390 | i++; |
| 391 | } |
| 392 | |
| 393 | return NULL; |
| 394 | } |
| 395 | |
Alexander Couzens | d8a8d98 | 2021-02-15 01:07:45 +0100 | [diff] [blame] | 396 | /* 4.4.2 Load Sharing function for the IP Sub-Network |
| 397 | * |
| 398 | * Implement a simple approach for UDP load sharing of data weight based on the modulo of the lsp. |
| 399 | * |
| 400 | * E.g. 3 NSVC: 1st weight 5, 2nd weight 3, 3rd weight 1, lsp = 3. |
| 401 | * sum all weights = 9 |
| 402 | * target_weight = lsp % sum = 3 |
| 403 | * |
| 404 | * 1st NSVC will be the target for 0-4 |
| 405 | * 2nd NSVC will be the target for 5-7 |
| 406 | * 3rd NSVC will be the target for 8 |
| 407 | * |
| 408 | * The 1st NSVC will be used. |
| 409 | * E.g. lsp = 7. The 2nd NSVC will used. |
| 410 | */ |
| 411 | static struct gprs_ns2_vc *ns2_load_sharing_weight_modulo( |
| 412 | struct gprs_ns2_nse *nse, |
| 413 | uint16_t bvci, |
| 414 | uint32_t load_selector) |
| 415 | { |
| 416 | struct gprs_ns2_vc *tmp; |
| 417 | uint32_t mod; |
| 418 | uint32_t i = 0; |
| 419 | |
| 420 | if (nse->nsvc_count == 0) |
| 421 | return NULL; |
| 422 | |
| 423 | mod = (bvci + load_selector) % nse->sum_data_weight; |
| 424 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Daniel Willmann | c7f547f | 2021-06-29 13:18:42 +0200 | [diff] [blame] | 425 | if (tmp->data_weight == 0) |
| 426 | continue; |
Alexander Couzens | d8a8d98 | 2021-02-15 01:07:45 +0100 | [diff] [blame] | 427 | if (!ns2_vc_is_unblocked(tmp)) |
| 428 | continue; |
| 429 | if (i == mod || mod < i + tmp->data_weight) |
| 430 | return tmp; |
| 431 | i += tmp->data_weight; |
| 432 | } |
| 433 | |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 437 | /* pick the first available data NSVC - no load sharing */ |
| 438 | struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse) |
| 439 | { |
| 440 | struct gprs_ns2_vc *nsvc = NULL, *tmp; |
| 441 | |
| 442 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 443 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 444 | continue; |
| 445 | if (tmp->data_weight == 0) |
| 446 | continue; |
| 447 | |
| 448 | nsvc = tmp; |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | return nsvc; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static struct gprs_ns2_vc *ns2_load_sharing( |
| 457 | struct gprs_ns2_nse *nse, |
| 458 | uint16_t bvci, |
| 459 | uint32_t link_selector) |
| 460 | { |
| 461 | struct gprs_ns2_vc *nsvc = NULL; |
| 462 | |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 463 | switch (nse->ll) { |
| 464 | case GPRS_NS2_LL_FR: |
Alexander Couzens | 9e81a32 | 2021-02-16 17:36:25 +0100 | [diff] [blame] | 465 | nsvc = ns2_load_sharing_modulo(nse, bvci, link_selector); |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 466 | break; |
| 467 | case GPRS_NS2_LL_UDP: |
| 468 | default: |
| 469 | if (bvci == 0) { |
| 470 | /* signalling */ |
| 471 | nsvc = ns2_load_sharing_signal(nse); |
| 472 | } else { |
| 473 | /* data with load sharing parameter */ |
Alexander Couzens | d8a8d98 | 2021-02-15 01:07:45 +0100 | [diff] [blame] | 474 | nsvc = ns2_load_sharing_weight_modulo(nse, bvci, link_selector); |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 475 | } |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 476 | break; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return nsvc; |
| 480 | } |
| 481 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 482 | /*! Receive a primitive from the NS User (Gb). |
| 483 | * \param[in] nsi NS instance to which the primitive is issued |
| 484 | * \param[in] oph The primitive |
| 485 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 486 | int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph) |
| 487 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 488 | /* TODO: implement resource distribution */ |
| 489 | /* TODO: check for empty PDUs which can be sent to Request/Confirm |
| 490 | * the IP endpoint */ |
| 491 | struct osmo_gprs_ns2_prim *nsp; |
| 492 | struct gprs_ns2_nse *nse = NULL; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 493 | struct gprs_ns2_vc *nsvc = NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 494 | uint16_t bvci, nsei; |
| 495 | uint8_t sducontrol = 0; |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 496 | int rc = 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 497 | |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 498 | if (oph->sap != SAP_NS) { |
| 499 | rc = -EINVAL; |
| 500 | goto out; |
| 501 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 502 | |
| 503 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 504 | |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 505 | if (oph->operation != PRIM_OP_REQUEST || oph->primitive != GPRS_NS2_PRIM_UNIT_DATA) { |
| 506 | rc = -EINVAL; |
| 507 | goto out; |
| 508 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 509 | |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 510 | if (!oph->msg) { |
| 511 | rc = -EINVAL; |
| 512 | goto out; |
| 513 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 514 | |
| 515 | bvci = nsp->bvci; |
| 516 | nsei = nsp->nsei; |
| 517 | |
| 518 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 519 | if (!nse) { |
| 520 | rc = -EINVAL; |
| 521 | goto out; |
| 522 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 523 | |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 524 | if (!nse->alive) { |
| 525 | goto out; |
| 526 | } |
Alexander Couzens | 265a5eb | 2021-02-02 11:26:17 +0100 | [diff] [blame] | 527 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 528 | nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 529 | |
| 530 | /* TODO: send a status primitive back */ |
| 531 | if (!nsvc) |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 532 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 533 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 534 | if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_REQUEST_CHANGE) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 535 | sducontrol = 1; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 536 | else if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_CONFIRM_CHANGE) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 537 | sducontrol = 2; |
| 538 | |
| 539 | return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg); |
Alexander Couzens | 6df1160 | 2021-02-14 23:39:40 +0100 | [diff] [blame] | 540 | |
| 541 | out: |
| 542 | msgb_free(oph->msg); |
| 543 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 546 | /*! Send a STATUS.ind primitive to the specified NS instance user. |
| 547 | * \param[in] nsi NS instance on which we operate |
| 548 | * \param[in] nsei NSEI to which the statue relates |
| 549 | * \param[in] bvci BVCI to which the status relates |
| 550 | * \param[in] cause The cause of the status */ |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 551 | void ns2_prim_status_ind(struct gprs_ns2_nse *nse, |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 552 | struct gprs_ns2_vc *nsvc, |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 553 | uint16_t bvci, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 554 | enum gprs_ns2_affecting_cause cause) |
| 555 | { |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 556 | char nsvc_str[NS2_LL_MAX_STR]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 557 | struct osmo_gprs_ns2_prim nsp = {}; |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 558 | nsp.nsei = nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 559 | nsp.bvci = bvci; |
| 560 | nsp.u.status.cause = cause; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 561 | nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci); |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 562 | nsp.u.status.first = nse->first; |
| 563 | nsp.u.status.persistent = nse->persistent; |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 564 | if (nse->mtu < 4) |
| 565 | nsp.u.status.mtu = 0; |
| 566 | else |
| 567 | nsp.u.status.mtu = nse->mtu - 4; /* 1 Byte NS PDU type, 1 Byte NS SDU control, 2 Byte BVCI */ |
| 568 | |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 569 | if (nsvc) { |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 570 | nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc); |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 571 | LOGNSVC(nsvc, LOGL_NOTICE, "NS-STATUS.ind(bvci=%05u): cause=%s, transfer=%d, first=%d, mtu=%d\n", |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 572 | nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause), |
Alexander Couzens | 31cf166 | 2021-07-02 17:36:17 +0200 | [diff] [blame] | 573 | nsp.u.status.transfer, nsp.u.status.first, nsp.u.status.mtu); |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 574 | } else { |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 575 | LOGNSE(nse, LOGL_NOTICE, "NS-STATUS.ind(bvci=%05u): cause=%s, transfer=%d, first=%d, mtu=%d\n", |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 576 | nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause), |
Alexander Couzens | 31cf166 | 2021-07-02 17:36:17 +0200 | [diff] [blame] | 577 | nsp.u.status.transfer, nsp.u.status.first, nsp.u.status.mtu); |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 578 | } |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 579 | |
Harald Welte | 8669043 | 2021-01-31 16:09:19 +0100 | [diff] [blame] | 580 | osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_STATUS, PRIM_OP_INDICATION, NULL); |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 581 | nse->nsi->cb(&nsp.oph, nse->nsi->cb_data); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 584 | /*! Allocate a NS-VC within the given bind + NSE. |
| 585 | * \param[in] bind The 'bind' on which we operate |
| 586 | * \param[in] nse The NS Entity on which we operate |
| 587 | * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater) |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 588 | * \param[in] id - human-readable identifier |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 589 | * \return newly allocated NS-VC on success; NULL on error */ |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 590 | struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater, |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 591 | enum gprs_ns2_vc_mode vc_mode, const char *id) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 592 | { |
Daniel Willmann | bb89905 | 2021-01-16 14:02:45 +0100 | [diff] [blame] | 593 | /* Sanity check */ |
| 594 | OSMO_ASSERT(bind->ll == nse->ll); |
| 595 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 596 | struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc); |
| 597 | |
| 598 | if (!nsvc) |
| 599 | return NULL; |
| 600 | |
| 601 | nsvc->bind = bind; |
| 602 | nsvc->nse = nse; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 603 | nsvc->mode = vc_mode; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 604 | nsvc->sig_weight = 1; |
| 605 | nsvc->data_weight = 1; |
| 606 | |
Harald Welte | 97ccbf7 | 2021-01-31 11:49:19 +0100 | [diff] [blame] | 607 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->nsvc_rate_ctr_idx); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 608 | if (!nsvc->ctrg) { |
| 609 | goto err; |
| 610 | } |
Harald Welte | 97ccbf7 | 2021-01-31 11:49:19 +0100 | [diff] [blame] | 611 | nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->nsvc_rate_ctr_idx); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 612 | if (!nsvc->statg) |
| 613 | goto err_group; |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 614 | if (!ns2_vc_fsm_alloc(nsvc, id, initiater)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 615 | goto err_statg; |
| 616 | |
Harald Welte | 97ccbf7 | 2021-01-31 11:49:19 +0100 | [diff] [blame] | 617 | bind->nsi->nsvc_rate_ctr_idx++; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 618 | |
Michael Iedema | a667d85 | 2021-06-09 10:04:57 +0000 | [diff] [blame] | 619 | rate_ctr_group_set_name(nsvc->ctrg, id); |
Pau Espin Pedrol | 05047b7 | 2021-05-31 13:19:13 +0200 | [diff] [blame] | 620 | osmo_stat_item_group_set_name(nsvc->statg, id); |
| 621 | |
Alexander Couzens | d5cd8c6 | 2021-06-15 20:59:03 +0200 | [diff] [blame] | 622 | llist_add_tail(&nsvc->list, &nse->nsvc); |
| 623 | llist_add_tail(&nsvc->blist, &bind->nsvc); |
Alexander Couzens | ca5ce0d | 2021-09-05 23:15:56 +0200 | [diff] [blame] | 624 | osmo_clock_gettime(CLOCK_MONOTONIC, &nsvc->ts_alive_change); |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 625 | ns2_nse_update_mtu(nse); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 626 | |
| 627 | return nsvc; |
| 628 | |
| 629 | err_statg: |
| 630 | osmo_stat_item_group_free(nsvc->statg); |
| 631 | err_group: |
| 632 | rate_ctr_group_free(nsvc->ctrg); |
| 633 | err: |
| 634 | talloc_free(nsvc); |
| 635 | |
| 636 | return NULL; |
| 637 | } |
| 638 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 639 | /*! Destroy/release given NS-VC. |
| 640 | * \param[in] nsvc NS-VC to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 641 | void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc) |
| 642 | { |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 643 | if (!nsvc || nsvc->freed) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 644 | return; |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 645 | nsvc->freed = true; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 646 | ns2_prim_status_ind(nsvc->nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 647 | |
| 648 | llist_del(&nsvc->list); |
| 649 | llist_del(&nsvc->blist); |
| 650 | |
| 651 | /* notify nse this nsvc is unavailable */ |
| 652 | ns2_nse_notify_unblocked(nsvc, false); |
| 653 | |
| 654 | /* check if sns is using this VC */ |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 655 | ns2_sns_replace_nsvc(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 656 | osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 657 | |
| 658 | /* let the driver/bind clean up it's internal state */ |
| 659 | if (nsvc->priv && nsvc->bind->free_vc) |
| 660 | nsvc->bind->free_vc(nsvc); |
| 661 | |
| 662 | osmo_stat_item_group_free(nsvc->statg); |
| 663 | rate_ctr_group_free(nsvc->ctrg); |
| 664 | |
| 665 | talloc_free(nsvc); |
| 666 | } |
| 667 | |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 668 | void ns2_free_nsvcs(struct gprs_ns2_nse *nse) |
| 669 | { |
| 670 | struct gprs_ns2_vc *nsvc; |
| 671 | |
| 672 | /* prevent recursive free() when the user reacts on a down event and free() a second time */ |
| 673 | while (!llist_empty(&nse->nsvc)) { |
| 674 | nsvc = llist_first_entry(&nse->nsvc, struct gprs_ns2_vc, list); |
| 675 | gprs_ns2_free_nsvc(nsvc); |
| 676 | } |
| 677 | } |
| 678 | |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 679 | /*! Destroy/release all NS-VC of given NSE |
| 680 | * \param[in] nse NSE |
| 681 | */ |
| 682 | void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse) |
| 683 | { |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 684 | if (!nse || nse->freed) |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 685 | return; |
| 686 | |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 687 | if (nse->bss_sns_fi) { |
| 688 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_FREE_NSVCS, NULL); |
| 689 | } else { |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 690 | ns2_free_nsvcs(nse); |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 694 | /*! Allocate a message buffer for use with the NS2 stack. */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 695 | struct msgb *ns2_msgb_alloc(void) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 696 | { |
| 697 | struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, |
| 698 | "GPRS/NS"); |
| 699 | if (!msg) { |
| 700 | LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n", |
| 701 | NS_ALLOC_SIZE); |
| 702 | } |
| 703 | return msg; |
| 704 | } |
| 705 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 706 | /*! Create a status message to be sent over a new connection. |
| 707 | * \param[in] orig_msg the original message |
| 708 | * \param[in] tp TLVP parsed of the original message |
| 709 | * \param[out] reject callee-allocated message buffer of the generated NS-STATUS |
| 710 | * \param[in] cause Cause for the rejection |
| 711 | * \return 0 on success */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 712 | static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause) |
| 713 | { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 714 | struct msgb *msg = ns2_msgb_alloc(); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 715 | struct gprs_ns_hdr *nsh; |
| 716 | bool have_vci = false; |
| 717 | uint8_t _cause = cause; |
| 718 | uint16_t nsei = 0; |
| 719 | |
| 720 | if (!msg) |
| 721 | return -ENOMEM; |
| 722 | |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 723 | if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 724 | nsei = tlvp_val16be(tp, NS_IE_NSEI); |
| 725 | |
| 726 | LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n", |
| 727 | nsei, gprs_ns2_cause_str(cause)); |
| 728 | } |
| 729 | |
| 730 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 731 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 732 | nsh->pdu_type = NS_PDUT_STATUS; |
| 733 | |
| 734 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause); |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 735 | have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 736 | |
| 737 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 738 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 739 | cause == NS_CAUSE_NSVC_UNKNOWN) { |
| 740 | if (!have_vci) { |
| 741 | msgb_free(msg); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
| 745 | msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI)); |
| 746 | } |
| 747 | |
| 748 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 749 | switch (cause) { |
| 750 | case NS_CAUSE_SEM_INCORR_PDU: |
| 751 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 752 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 753 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 754 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 755 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 756 | orig_msg->l2h); |
| 757 | break; |
| 758 | default: |
| 759 | break; |
| 760 | } |
| 761 | |
| 762 | *reject = msg; |
| 763 | return 0; |
| 764 | } |
| 765 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 766 | /*! Resolve a NS Entity based on its NSEI. |
| 767 | * \param[in] nsi NS Instance in which we do the look-up |
| 768 | * \param[in] nsei NSEI to look up |
| 769 | * \return NS Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 770 | struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei) |
| 771 | { |
| 772 | struct gprs_ns2_nse *nse; |
| 773 | |
| 774 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 775 | if (nse->nsei == nsei) |
| 776 | return nse; |
| 777 | } |
| 778 | |
| 779 | return NULL; |
| 780 | } |
| 781 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 782 | /*! Resolve a NS-VC Entity based on its NS-VCI. |
| 783 | * \param[in] nsi NS Instance in which we do the look-up |
| 784 | * \param[in] nsvci NS-VCI to look up |
| 785 | * \return NS-VC Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 786 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci) |
| 787 | { |
| 788 | struct gprs_ns2_nse *nse; |
| 789 | struct gprs_ns2_vc *nsvc; |
| 790 | |
| 791 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 792 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 793 | if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci) |
| 794 | return nsvc; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return NULL; |
| 799 | } |
| 800 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 801 | /*! Create a NS Entity within given NS instance. |
| 802 | * \param[in] nsi NS instance in which to create NS Entity |
| 803 | * \param[in] nsei NS Entity Identifier of to-be-created NSE |
Harald Welte | 5b034fb | 2021-03-04 14:16:49 +0100 | [diff] [blame] | 804 | * \param[in] ip_sns_role_sgsn Does local side implement SGSN role? |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 805 | * \returns newly-allocated NS-E in successful case; NULL on error */ |
Harald Welte | 5b034fb | 2021-03-04 14:16:49 +0100 | [diff] [blame] | 806 | struct gprs_ns2_nse *gprs_ns2_create_nse2(struct gprs_ns2_inst *nsi, uint16_t nsei, |
| 807 | enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect, |
| 808 | bool ip_sns_role_sgsn) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 809 | { |
| 810 | struct gprs_ns2_nse *nse; |
| 811 | |
| 812 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 813 | if (nse) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 814 | LOGNSE(nse, LOGL_ERROR, "Can not create a NSE with already taken NSEI\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 815 | return nse; |
| 816 | } |
| 817 | |
| 818 | nse = talloc_zero(nsi, struct gprs_ns2_nse); |
| 819 | if (!nse) |
| 820 | return NULL; |
Harald Welte | 06d9bf9 | 2021-03-05 10:20:11 +0100 | [diff] [blame] | 821 | nse->dialect = GPRS_NS2_DIALECT_UNDEF; |
Harald Welte | 5b034fb | 2021-03-04 14:16:49 +0100 | [diff] [blame] | 822 | nse->ip_sns_role_sgsn = ip_sns_role_sgsn; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 823 | |
Harald Welte | 06d9bf9 | 2021-03-05 10:20:11 +0100 | [diff] [blame] | 824 | if (ns2_nse_set_dialect(nse, dialect) < 0) { |
| 825 | talloc_free(nse); |
| 826 | return NULL; |
Alexander Couzens | 93ad499 | 2020-12-06 03:03:03 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 829 | nse->ctrg = rate_ctr_group_alloc(nse, &nse_ctrg_desc, nsei); |
| 830 | if (!nse->ctrg) { |
| 831 | talloc_free(nse); |
| 832 | return NULL; |
| 833 | } |
| 834 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 835 | nse->ll = linklayer; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 836 | nse->nsei = nsei; |
| 837 | nse->nsi = nsi; |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 838 | nse->first = true; |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 839 | nse->mtu = 0; |
Alexander Couzens | d5cd8c6 | 2021-06-15 20:59:03 +0200 | [diff] [blame] | 840 | llist_add_tail(&nse->list, &nsi->nse); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 841 | INIT_LLIST_HEAD(&nse->nsvc); |
Alexander Couzens | 2c64c25 | 2021-09-05 23:15:29 +0200 | [diff] [blame] | 842 | osmo_clock_gettime(CLOCK_MONOTONIC, &nse->ts_alive_change); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 843 | |
| 844 | return nse; |
| 845 | } |
| 846 | |
Harald Welte | 06d9bf9 | 2021-03-05 10:20:11 +0100 | [diff] [blame] | 847 | int ns2_nse_set_dialect(struct gprs_ns2_nse *nse, enum gprs_ns2_dialect dialect) |
| 848 | { |
| 849 | char sns[16]; |
| 850 | |
| 851 | if (nse->dialect == dialect) |
| 852 | return 0; |
| 853 | |
| 854 | switch (nse->dialect) { |
| 855 | case GPRS_NS2_DIALECT_UNDEF: |
| 856 | if (dialect == GPRS_NS2_DIALECT_SNS) { |
| 857 | snprintf(sns, sizeof(sns), "NSE%05u-SNS", nse->nsei); |
Harald Welte | 5b034fb | 2021-03-04 14:16:49 +0100 | [diff] [blame] | 858 | if (nse->ip_sns_role_sgsn) |
| 859 | nse->bss_sns_fi = ns2_sns_sgsn_fsm_alloc(nse, sns); |
| 860 | else |
| 861 | nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns); |
Harald Welte | 06d9bf9 | 2021-03-05 10:20:11 +0100 | [diff] [blame] | 862 | if (!nse->bss_sns_fi) |
| 863 | return -1; |
| 864 | } |
| 865 | nse->dialect = dialect; |
| 866 | break; |
| 867 | default: |
| 868 | if (dialect == GPRS_NS2_DIALECT_UNDEF) { |
| 869 | if (nse->bss_sns_fi) |
| 870 | osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 871 | nse->bss_sns_fi = NULL; |
| 872 | nse->dialect = GPRS_NS2_DIALECT_UNDEF; |
| 873 | } else { |
| 874 | /* we don't support arbitrary changes without going through UNDEF first */ |
| 875 | return -EPERM; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
Harald Welte | 5b034fb | 2021-03-04 14:16:49 +0100 | [diff] [blame] | 882 | /*! Create a NS Entity within given NS instance. |
| 883 | * \param[in] nsi NS instance in which to create NS Entity |
| 884 | * \param[in] nsei NS Entity Identifier of to-be-created NSE |
| 885 | * \returns newly-allocated NS-E in successful case; NULL on error */ |
| 886 | struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei, |
| 887 | enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect) |
| 888 | { |
| 889 | return gprs_ns2_create_nse2(nsi, nsei, linklayer, dialect, false); |
| 890 | } |
| 891 | |
Alexander Couzens | 05e7f7d | 2020-10-11 19:51:46 +0200 | [diff] [blame] | 892 | /*! Return the NSEI |
| 893 | * \param[in] nse NS Entity |
| 894 | * \return the nsei. |
| 895 | */ |
| 896 | uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse) |
| 897 | { |
| 898 | return nse->nsei; |
| 899 | } |
| 900 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 901 | /*! Destroy given NS Entity. |
| 902 | * \param[in] nse NS Entity to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 903 | void gprs_ns2_free_nse(struct gprs_ns2_nse *nse) |
| 904 | { |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 905 | if (!nse || nse->freed) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 906 | return; |
| 907 | |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 908 | nse->freed = true; |
Alexander Couzens | d1cd650 | 2021-01-15 02:16:23 +0100 | [diff] [blame] | 909 | nse->alive = false; |
Alexander Couzens | 2665388 | 2021-02-19 12:56:35 +0100 | [diff] [blame] | 910 | if (nse->bss_sns_fi) { |
| 911 | osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 912 | nse->bss_sns_fi = NULL; |
| 913 | } |
| 914 | |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 915 | gprs_ns2_free_nsvcs(nse); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 916 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE); |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 917 | rate_ctr_group_free(nse->ctrg); |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 918 | ns2_free_nsvcs(nse); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 919 | |
| 920 | llist_del(&nse->list); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 921 | talloc_free(nse); |
| 922 | } |
| 923 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 924 | void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi) |
| 925 | { |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 926 | struct gprs_ns2_nse *nse; |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 927 | |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 928 | /* prevent recursive free() when the user reacts on a down event and free() a second time */ |
| 929 | while (!llist_empty(&nsi->nse)) { |
| 930 | nse = llist_first_entry(&nsi->nse, struct gprs_ns2_nse, list); |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 931 | gprs_ns2_free_nse(nse); |
| 932 | } |
| 933 | } |
| 934 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 935 | static inline int ns2_tlv_parse(struct tlv_parsed *dec, |
| 936 | const uint8_t *buf, int buf_len, uint8_t lv_tag, |
| 937 | uint8_t lv_tag2) |
| 938 | { |
| 939 | /* workaround for NS_IE_IP_ADDR not following any known TLV rules. |
| 940 | * See comment of ns_att_tlvdef1. */ |
| 941 | int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2); |
| 942 | if (rc < 0) |
| 943 | return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2); |
| 944 | return rc; |
| 945 | } |
| 946 | |
Harald Welte | fe0266e | 2021-03-03 18:12:31 +0100 | [diff] [blame] | 947 | static enum ns2_cs ns2_create_vc_sns(struct gprs_ns2_vc_bind *bind, |
| 948 | const struct osmo_sockaddr *remote, |
| 949 | struct gprs_ns2_vc **success, uint16_t nsei) |
| 950 | { |
| 951 | struct gprs_ns2_vc *nsvc; |
| 952 | struct gprs_ns2_nse *nse; |
| 953 | |
| 954 | nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, remote); |
| 955 | /* ns2_create_vc() is only called if no NS-VC could be found */ |
| 956 | OSMO_ASSERT(!nsvc); |
| 957 | |
| 958 | nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 959 | if (!nse) { |
| 960 | if (!bind->accept_sns) { |
| 961 | struct osmo_sockaddr_str remote_str; |
| 962 | osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas); |
| 963 | /* no dynamic creation of IP-SNS NSE permitted */ |
| 964 | LOGP(DLNS, LOGL_ERROR, "[%s]:%u: Dynamic creation of NSE(%05u) via IP-SNS not " |
| 965 | "permitted. Check your config.\n", remote_str.ip, remote_str.port, nsei); |
| 966 | return NS2_CS_ERROR; |
| 967 | } |
| 968 | nse = gprs_ns2_create_nse2(bind->nsi, nsei, bind->ll, GPRS_NS2_DIALECT_SNS, true); |
| 969 | if (!nse) { |
| 970 | LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei); |
| 971 | return NS2_CS_ERROR; |
| 972 | } |
Harald Welte | d164ef8 | 2021-03-04 22:29:17 +0100 | [diff] [blame] | 973 | /* add configured list of default binds; if that fails, use only current bind */ |
| 974 | if (!ns2_sns_add_sns_default_binds(nse)) |
| 975 | gprs_ns2_sns_add_bind(nse, bind); |
Harald Welte | fe0266e | 2021-03-03 18:12:31 +0100 | [diff] [blame] | 976 | } else { |
| 977 | /* nsei already known */ |
| 978 | if (nse->ll != bind->ll) { |
| 979 | LOGNSE(nse, LOGL_ERROR, "Received NS-RESET with wrong linklayer(%s)" |
| 980 | " for already known NSE(%s)\n", gprs_ns2_lltype_str(bind->ll), |
| 981 | gprs_ns2_lltype_str(nse->ll)); |
| 982 | return NS2_CS_SKIPPED; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | nsvc = ns2_ip_bind_connect(bind, nse, remote); |
| 987 | if (!nsvc) |
| 988 | return NS2_CS_SKIPPED; |
| 989 | |
| 990 | nsvc->nsvci_is_valid = false; |
| 991 | |
| 992 | *success = nsvc; |
| 993 | |
| 994 | return NS2_CS_CREATED; |
| 995 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 996 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 997 | /*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE. |
| 998 | * \param[in] bind the bind through which msg was received |
| 999 | * \param[in] msg the actual received message |
Harald Welte | 7d0daac | 2021-03-02 23:08:43 +0100 | [diff] [blame] | 1000 | * \param[in] remote address of remote peer sending message |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1001 | * \param[in] logname A name to describe the VC. E.g. ip address pair |
| 1002 | * \param[out] reject A message filled to be sent back. Only used in failure cases. |
| 1003 | * \param[out] success A pointer which will be set to the new VC on success |
| 1004 | * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */ |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1005 | enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind, |
| 1006 | struct msgb *msg, |
Harald Welte | 7d0daac | 2021-03-02 23:08:43 +0100 | [diff] [blame] | 1007 | const struct osmo_sockaddr *remote, |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1008 | const char *logname, |
| 1009 | struct msgb **reject, |
| 1010 | struct gprs_ns2_vc **success) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1011 | { |
| 1012 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 1013 | struct tlv_parsed tp; |
| 1014 | struct gprs_ns2_vc *nsvc; |
| 1015 | struct gprs_ns2_nse *nse; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1016 | enum gprs_ns2_dialect dialect; |
| 1017 | enum gprs_ns2_vc_mode vc_mode; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1018 | uint16_t nsvci; |
| 1019 | uint16_t nsei; |
Alexander Couzens | d794806 | 2021-06-03 20:35:47 +0200 | [diff] [blame] | 1020 | const struct osmo_sockaddr *local; |
| 1021 | char idbuf[256], tmp[INET6_ADDRSTRLEN + 8]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1022 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 1023 | int rc, tlv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1024 | |
| 1025 | if (msg->len < sizeof(struct gprs_ns_hdr)) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1026 | return NS2_CS_ERROR; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1027 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 1028 | /* parse the tlv early to allow reject status msg to |
| 1029 | * work with valid tp. |
| 1030 | * Ignore the return code until the pdu type is parsed because |
| 1031 | * an unknown pdu type should be ignored */ |
| 1032 | tlv = ns2_tlv_parse(&tp, nsh->data, |
Alexander Couzens | bac5b01 | 2020-12-09 23:32:22 +0100 | [diff] [blame] | 1033 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Alexander Couzens | bac5b01 | 2020-12-09 23:32:22 +0100 | [diff] [blame] | 1034 | |
Harald Welte | fe0266e | 2021-03-03 18:12:31 +0100 | [diff] [blame] | 1035 | if (bind->ll == GPRS_NS2_LL_UDP && nsh->pdu_type == SNS_PDUT_SIZE && tlv >= 0) { |
| 1036 | uint16_t nsei; |
| 1037 | |
| 1038 | if (!TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) { |
| 1039 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE); |
| 1040 | if (rc < 0) |
| 1041 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
| 1042 | return NS2_CS_REJECTED; |
| 1043 | } |
| 1044 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
| 1045 | /* Create NS-VC, and if required, even NSE dynamically */ |
| 1046 | return ns2_create_vc_sns(bind, remote, success, nsei); |
| 1047 | } |
| 1048 | |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1049 | switch (nsh->pdu_type) { |
| 1050 | case NS_PDUT_STATUS: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1051 | /* Do not respond, see 3GPP TS 08.16, 7.5.1 */ |
| 1052 | LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s " |
| 1053 | "for non-existing NS-VC\n", |
| 1054 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1055 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1056 | case NS_PDUT_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1057 | /* Ignore this, see 3GPP TS 08.16, 7.4.1 */ |
| 1058 | LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s " |
| 1059 | "for non-existing NS-VC\n", |
| 1060 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1061 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1062 | case NS_PDUT_RESET_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1063 | /* Ignore this, see 3GPP TS 08.16, 7.3.1 */ |
| 1064 | LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s " |
| 1065 | "for non-existing NS-VC\n", |
| 1066 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1067 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1068 | case NS_PDUT_RESET: |
| 1069 | /* accept PDU RESET when vc_mode matches */ |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1070 | if (bind->accept_ipaccess) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1071 | dialect = GPRS_NS2_DIALECT_IPACCESS; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1072 | break; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1073 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1074 | |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1075 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 1076 | if (rc < 0) |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1077 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1078 | return NS2_CS_REJECTED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 1079 | default: |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 1080 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 1081 | if (rc < 0) |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 1082 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1083 | return NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 1086 | if (tlv < 0) { |
| 1087 | /* TODO: correct behaviour would checking what's wrong. |
| 1088 | * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE. |
| 1089 | * Otherwise ignore the non-essential TLV. */ |
| 1090 | LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 1091 | "TLV Parse\n", tlv); |
| 1092 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 1093 | if (rc < 0) |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 1094 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1095 | return NS2_CS_REJECTED; |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 1098 | if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) || |
| 1099 | !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) { |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 1100 | LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
| 1101 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 1102 | if (rc < 0) |
| 1103 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1104 | return NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1107 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1108 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 1109 | |
| 1110 | /* find or create NSE */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1111 | nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 1112 | if (!nse) { |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1113 | /* only create nse for udp & ipaccess */ |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1114 | if (bind->ll != GPRS_NS2_LL_UDP || dialect != GPRS_NS2_DIALECT_IPACCESS) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1115 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1116 | |
Alexander Couzens | 2e1a3a9 | 2021-01-27 20:44:41 +0100 | [diff] [blame] | 1117 | if (!bind->accept_ipaccess) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1118 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1119 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1120 | nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1121 | if (!nse) { |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1122 | LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1123 | return NS2_CS_ERROR; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1124 | } |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1125 | } else { |
| 1126 | /* nsei already known */ |
| 1127 | if (nse->ll != bind->ll) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1128 | LOGNSE(nse, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s)" |
| 1129 | " for already known NSE(%s)\n", nsvci, gprs_ns2_lltype_str(bind->ll), |
| 1130 | gprs_ns2_lltype_str(nse->ll)); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1131 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci); |
| 1136 | if (nsvc) { |
| 1137 | if (nsvc->persistent) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1138 | LOGNSVC(nsvc, LOGL_ERROR, "Received NS-RESET for a persistent NSE over wrong connection.\n"); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1139 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 1140 | } |
| 1141 | /* destroy old dynamic nsvc */ |
| 1142 | gprs_ns2_free_nsvc(nsvc); |
| 1143 | } |
| 1144 | |
| 1145 | /* do nse persistent check late to be more precise on the error message */ |
| 1146 | if (nse->persistent) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1147 | LOGNSE(nse, LOGL_ERROR, "Received NS-RESET for a persistent NSE but the unknown " |
| 1148 | "NS-VCI(%05u)\n", nsvci); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1149 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 1152 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1153 | vc_mode = ns2_dialect_to_vc_mode(dialect); |
Alexander Couzens | d794806 | 2021-06-03 20:35:47 +0200 | [diff] [blame] | 1154 | |
| 1155 | local = gprs_ns2_ip_bind_sockaddr(bind); |
| 1156 | osmo_sockaddr_to_str_buf(tmp, sizeof(tmp), local); |
| 1157 | snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-NSVC%05u-%s-%s", gprs_ns2_lltype_str(nse->ll), |
| 1158 | nse->nsei, nsvci, tmp, osmo_sockaddr_to_str(remote)); |
| 1159 | osmo_identifier_sanitize_buf(idbuf, NULL, '_'); |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 1160 | nsvc = ns2_vc_alloc(bind, nse, false, vc_mode, idbuf); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1161 | if (!nsvc) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1162 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1163 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1164 | nsvc->nsvci = nsvci; |
| 1165 | nsvc->nsvci_is_valid = true; |
| 1166 | |
| 1167 | *success = nsvc; |
| 1168 | |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 1169 | return NS2_CS_CREATED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1172 | /*! Create, and connect an inactive, new IP-based NS-VC |
| 1173 | * \param[in] bind bind in which the new NS-VC is to be created |
| 1174 | * \param[in] remote remote address to which to connect |
| 1175 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 1176 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 1177 | * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1178 | struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 1179 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1180 | struct gprs_ns2_nse *nse, |
| 1181 | uint16_t nsvci) |
| 1182 | { |
| 1183 | struct gprs_ns2_vc *nsvc; |
| 1184 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1185 | nsvc = ns2_ip_bind_connect(bind, nse, remote); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1186 | if (!nsvc) |
| 1187 | return NULL; |
| 1188 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1189 | if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1190 | nsvc->nsvci = nsvci; |
| 1191 | nsvc->nsvci_is_valid = true; |
| 1192 | } |
| 1193 | |
| 1194 | return nsvc; |
| 1195 | } |
| 1196 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1197 | /*! Create, connect and activate a new IP-based NS-VC |
| 1198 | * \param[in] bind bind in which the new NS-VC is to be created |
| 1199 | * \param[in] remote remote address to which to connect |
| 1200 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 1201 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 1202 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1203 | struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 1204 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1205 | struct gprs_ns2_nse *nse, |
| 1206 | uint16_t nsvci) |
| 1207 | { |
| 1208 | struct gprs_ns2_vc *nsvc; |
| 1209 | nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci); |
| 1210 | if (!nsvc) |
| 1211 | return NULL; |
| 1212 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1213 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1214 | |
| 1215 | return nsvc; |
| 1216 | } |
| 1217 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1218 | /*! Create, connect and activate a new IP-based NS-VC |
| 1219 | * \param[in] bind bind in which the new NS-VC is to be created |
| 1220 | * \param[in] remote remote address to which to connect |
| 1221 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 1222 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 1223 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1224 | struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 1225 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1226 | uint16_t nsei, |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1227 | uint16_t nsvci, |
| 1228 | enum gprs_ns2_dialect dialect) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1229 | { |
| 1230 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 1231 | |
| 1232 | if (!nse) { |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1233 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1234 | if (!nse) |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
| 1238 | return gprs_ns2_ip_connect(bind, remote, nse, nsvci); |
| 1239 | } |
| 1240 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1241 | /*! Find NS-VC for given socket address. |
| 1242 | * \param[in] nse NS Entity in which to search |
| 1243 | * \param[in] sockaddr socket address to search for |
| 1244 | * \return NS-VC matching sockaddr; NULL if none found */ |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 1245 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 1246 | const struct osmo_sockaddr *sockaddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1247 | { |
| 1248 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 1249 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1250 | |
| 1251 | OSMO_ASSERT(nse); |
| 1252 | OSMO_ASSERT(sockaddr); |
| 1253 | |
| 1254 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 1255 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1256 | if (!osmo_sockaddr_cmp(sockaddr, remote)) |
| 1257 | return nsvc; |
| 1258 | } |
| 1259 | |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | |
Alexander Couzens | 6cb5d5f | 2020-10-11 23:23:31 +0200 | [diff] [blame] | 1263 | /*! |
| 1264 | * Iterate over all nsvc of a NS Entity and call the callback. |
| 1265 | * If the callback returns < 0 it aborts the loop and returns the callback return code. |
| 1266 | * \param[in] nse NS Entity to iterate over all nsvcs |
| 1267 | * \param[in] cb the callback to call |
| 1268 | * \param[inout] cb_data the private data of the callback |
| 1269 | * \return 0 if the loop completes. If a callback returns < 0 it will returns this value. |
| 1270 | */ |
| 1271 | int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data) |
| 1272 | { |
| 1273 | struct gprs_ns2_vc *nsvc, *tmp; |
| 1274 | int rc = 0; |
| 1275 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
| 1276 | rc = cb(nsvc, cb_data); |
| 1277 | if (rc < 0) |
| 1278 | return rc; |
| 1279 | } |
| 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1285 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1286 | /*! Bottom-side entry-point for received NS PDU from the driver/bind |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1287 | * \param[in] nsvc NS-VC for which the message was received |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1288 | * \param msg the received message. Ownership is transferred, caller must not free it! |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1289 | * \return 0 on success; negative on error */ |
Alexander Couzens | ffd49d0 | 2020-09-24 00:47:17 +0200 | [diff] [blame] | 1290 | int ns2_recv_vc(struct gprs_ns2_vc *nsvc, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1291 | struct msgb *msg) |
| 1292 | { |
| 1293 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Alexander Couzens | ba63453 | 2021-01-25 13:46:38 +0100 | [diff] [blame] | 1294 | struct tlv_parsed tp = { }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1295 | int rc = 0; |
| 1296 | |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 1297 | log_set_context(LOG_CTX_GB_NSE, nsvc->nse); |
| 1298 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 1299 | |
Daniel Willmann | efa64f9 | 2021-07-09 20:35:00 +0200 | [diff] [blame] | 1300 | RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_IN); |
| 1301 | RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_IN, msg->len); |
Harald Welte | e5f55f7 | 2021-01-30 21:51:15 +0100 | [diff] [blame] | 1302 | |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1303 | if (msg->len < sizeof(struct gprs_ns_hdr)) { |
| 1304 | rc = -EINVAL; |
| 1305 | goto freemsg; |
| 1306 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1307 | |
Alexander Couzens | 6cf65d9 | 2021-01-18 17:55:35 +0100 | [diff] [blame] | 1308 | if (nsh->pdu_type != NS_PDUT_UNITDATA) |
| 1309 | LOG_NS_RX_SIGNAL(nsvc, nsh->pdu_type); |
| 1310 | else |
| 1311 | LOG_NS_DATA(nsvc, "Rx", nsh->pdu_type, LOGL_INFO, "\n"); |
| 1312 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1313 | switch (nsh->pdu_type) { |
| 1314 | case SNS_PDUT_CONFIG: |
| 1315 | /* one additional byte ('end flag') before the TLV part starts */ |
| 1316 | rc = ns2_tlv_parse(&tp, nsh->data+1, |
| 1317 | msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0); |
| 1318 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1319 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1320 | goto freemsg; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1321 | } |
| 1322 | /* All sub-network service related message types */ |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1323 | return ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1324 | case SNS_PDUT_ACK: |
| 1325 | case SNS_PDUT_ADD: |
| 1326 | case SNS_PDUT_CHANGE_WEIGHT: |
| 1327 | case SNS_PDUT_DELETE: |
| 1328 | /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */ |
Harald Welte | 36be9d8 | 2020-10-09 15:39:25 +0200 | [diff] [blame] | 1329 | rc = ns2_tlv_parse(&tp, nsh->data+5, |
| 1330 | msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1331 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1332 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1333 | goto freemsg; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1334 | } |
| 1335 | tp.lv[NS_IE_NSEI].val = nsh->data+2; |
| 1336 | tp.lv[NS_IE_NSEI].len = 2; |
| 1337 | tp.lv[NS_IE_TRANS_ID].val = nsh->data+4; |
| 1338 | tp.lv[NS_IE_TRANS_ID].len = 1; |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1339 | return ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1340 | case SNS_PDUT_CONFIG_ACK: |
| 1341 | case SNS_PDUT_SIZE: |
| 1342 | case SNS_PDUT_SIZE_ACK: |
| 1343 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1344 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1345 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1346 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1347 | goto freemsg; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1348 | } |
| 1349 | /* All sub-network service related message types */ |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1350 | return ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1351 | case NS_PDUT_UNITDATA: |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1352 | return ns2_vc_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1353 | default: |
| 1354 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1355 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1356 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1357 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1358 | if (nsh->pdu_type != NS_PDUT_STATUS) |
Alexander Couzens | d802f9a | 2021-09-23 16:19:32 +0200 | [diff] [blame] | 1359 | ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg, NULL); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1360 | return rc; |
| 1361 | } |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1362 | return ns2_vc_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1363 | } |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1364 | freemsg: |
| 1365 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1366 | |
| 1367 | return rc; |
| 1368 | } |
| 1369 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1370 | /* summarize all active data nsvcs */ |
| 1371 | void ns2_nse_data_sum(struct gprs_ns2_nse *nse) |
| 1372 | { |
| 1373 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1374 | |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 1375 | nse->nsvc_count = 0; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1376 | nse->sum_data_weight = 0; |
| 1377 | nse->sum_sig_weight = 0; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1378 | |
| 1379 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1380 | if (!ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1381 | continue; |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 1382 | |
| 1383 | nse->nsvc_count++; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1384 | nse->sum_data_weight += nsvc->data_weight; |
| 1385 | nse->sum_sig_weight += nsvc->sig_weight; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1389 | /*! Notify a nse about the change of a NS-VC. |
| 1390 | * \param[in] nsvc NS-VC which has detected the change (and shall not be notified). |
| 1391 | * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1392 | void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked) |
| 1393 | { |
| 1394 | struct gprs_ns2_nse *nse = nsvc->nse; |
Daniel Willmann | 334cf87 | 2021-11-10 16:44:56 +0100 | [diff] [blame] | 1395 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 1396 | uint16_t nsei = nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1397 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1398 | ns2_nse_data_sum(nse); |
Alexander Couzens | 616a949 | 2021-07-20 22:14:35 +0200 | [diff] [blame] | 1399 | ns2_sns_notify_alive(nse, nsvc, unblocked); |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1400 | |
Daniel Willmann | 334cf87 | 2021-11-10 16:44:56 +0100 | [diff] [blame] | 1401 | /* NSE could have been freed, try to get it again */ |
| 1402 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 1403 | |
| 1404 | if (!nse || unblocked == nse->alive) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1405 | return; |
| 1406 | |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1407 | /* wait until both data_weight and sig_weight are != 0 before declaring NSE as alive */ |
| 1408 | if (unblocked && nse->sum_data_weight && nse->sum_sig_weight) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1409 | nse->alive = true; |
Alexander Couzens | 2c64c25 | 2021-09-05 23:15:29 +0200 | [diff] [blame] | 1410 | osmo_clock_gettime(CLOCK_MONOTONIC, &nse->ts_alive_change); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1411 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_RECOVERY); |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 1412 | nse->first = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1413 | return; |
| 1414 | } |
| 1415 | |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1416 | if (nse->alive && (nse->sum_data_weight == 0 || nse->sum_sig_weight == 0)) { |
| 1417 | /* nse became unavailable */ |
| 1418 | nse->alive = false; |
Alexander Couzens | 2c64c25 | 2021-09-05 23:15:29 +0200 | [diff] [blame] | 1419 | osmo_clock_gettime(CLOCK_MONOTONIC, &nse->ts_alive_change); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1420 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1421 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /*! Create a new GPRS NS instance |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1425 | * \param[in] ctx a talloc context to allocate NS instance from |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 1426 | * \param[in] cb Call-back function for dispatching primitives to the user. The Call-back must free all msgb* given in the primitive. |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1427 | * \param[in] cb_data transparent user data passed to Call-back |
| 1428 | * \returns dynamically allocated gprs_ns_inst; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1429 | struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data) |
| 1430 | { |
| 1431 | struct gprs_ns2_inst *nsi; |
| 1432 | |
| 1433 | nsi = talloc_zero(ctx, struct gprs_ns2_inst); |
| 1434 | if (!nsi) |
| 1435 | return NULL; |
| 1436 | |
| 1437 | nsi->cb = cb; |
| 1438 | nsi->cb_data = cb_data; |
| 1439 | INIT_LLIST_HEAD(&nsi->binding); |
| 1440 | INIT_LLIST_HEAD(&nsi->nse); |
| 1441 | |
| 1442 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 1443 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 1444 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 1445 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 1446 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 1447 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 1448 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
| 1449 | nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */ |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1450 | nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3; |
| 1451 | nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1452 | nsi->timeout[NS_TOUT_TSNS_PROCEDURES_RETRIES] = 3; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1453 | |
| 1454 | return nsi; |
| 1455 | } |
| 1456 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1457 | /*! Destroy a NS Instance (including all its NSEs, binds, ...). |
| 1458 | * \param[in] nsi NS instance to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1459 | void gprs_ns2_free(struct gprs_ns2_inst *nsi) |
| 1460 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1461 | if (!nsi) |
| 1462 | return; |
| 1463 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 1464 | gprs_ns2_free_nses(nsi); |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1465 | gprs_ns2_free_binds(nsi); |
Alexander Couzens | 3531504 | 2020-10-10 03:28:17 +0200 | [diff] [blame] | 1466 | |
| 1467 | talloc_free(nsi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1470 | /*! Start the NS-ALIVE FSM in all NS-VCs of given NSE. |
| 1471 | * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1472 | void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse) |
| 1473 | { |
| 1474 | struct gprs_ns2_vc *nsvc; |
| 1475 | OSMO_ASSERT(nse); |
| 1476 | |
| 1477 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Harald Welte | c962a2e | 2021-03-05 08:09:08 +0100 | [diff] [blame] | 1478 | /* A pre-configured endpoint shall not be used for NSE data or signalling traffic |
| 1479 | * (with the exception of Size and Configuration procedures) unless it is |
| 1480 | * configured by the SGSN using the auto-configuration procedures */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1481 | if (nsvc->sns_only) |
| 1482 | continue; |
| 1483 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1484 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1488 | /*! Destroy a given bind. |
| 1489 | * \param[in] bind the bind we want to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1490 | void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind) |
| 1491 | { |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 1492 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1493 | struct gprs_ns2_nse *nse; |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 1494 | if (!bind || bind->freed) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1495 | return; |
Alexander Couzens | 41589a3 | 2021-07-19 03:40:02 +0200 | [diff] [blame] | 1496 | bind->freed = true; |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 1497 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1498 | if (gprs_ns2_is_ip_bind(bind)) { |
| 1499 | llist_for_each_entry(nse, &bind->nsi->nse, list) { |
| 1500 | gprs_ns2_sns_del_bind(nse, bind); |
| 1501 | } |
| 1502 | } |
| 1503 | |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 1504 | /* prevent recursive free() when the user reacts on a down event and free() a second time */ |
| 1505 | while (!llist_empty(&bind->nsvc)) { |
| 1506 | nsvc = llist_first_entry(&bind->nsvc, struct gprs_ns2_vc, blist); |
Alexander Couzens | 4bfcce6 | 2021-09-03 22:22:39 +0200 | [diff] [blame] | 1507 | gprs_ns2_free_nsvc(nsvc); |
| 1508 | } |
| 1509 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1510 | if (bind->driver->free_bind) |
| 1511 | bind->driver->free_bind(bind); |
| 1512 | |
| 1513 | llist_del(&bind->list); |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 1514 | osmo_stat_item_group_free(bind->statg); |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1515 | talloc_free((char *)bind->name); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1516 | talloc_free(bind); |
| 1517 | } |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1518 | |
| 1519 | void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi) |
| 1520 | { |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 1521 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1522 | |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 1523 | /* prevent recursive free() when the user reacts on a down event and free() a second time */ |
| 1524 | while (!llist_empty(&nsi->binding)) { |
| 1525 | bind = llist_first_entry(&nsi->binding, struct gprs_ns2_vc_bind, list); |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1526 | gprs_ns2_free_bind(bind); |
| 1527 | } |
| 1528 | } |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1529 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1530 | /*! Search for a bind with a unique name |
| 1531 | * \param[in] nsi NS instance on which we operate |
| 1532 | * \param[in] name The unique bind name to search for |
| 1533 | * \return the bind or NULL if not found |
| 1534 | */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1535 | struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(struct gprs_ns2_inst *nsi, const char *name) |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1536 | { |
| 1537 | struct gprs_ns2_vc_bind *bind; |
| 1538 | |
| 1539 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 1540 | if (!strcmp(bind->name, name)) |
| 1541 | return bind; |
| 1542 | } |
| 1543 | |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1547 | enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect) |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1548 | { |
| 1549 | switch (dialect) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1550 | case GPRS_NS2_DIALECT_SNS: |
| 1551 | case GPRS_NS2_DIALECT_STATIC_ALIVE: |
| 1552 | return GPRS_NS2_VC_MODE_ALIVE; |
| 1553 | case GPRS_NS2_DIALECT_STATIC_RESETBLOCK: |
| 1554 | case GPRS_NS2_DIALECT_IPACCESS: |
| 1555 | return GPRS_NS2_VC_MODE_BLOCKRESET; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1556 | default: |
| 1557 | return -1; |
| 1558 | } |
| 1559 | } |
| 1560 | |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1561 | static void add_bind_array(struct gprs_ns2_vc_bind **array, |
| 1562 | struct gprs_ns2_vc_bind *bind, int size) |
| 1563 | { |
| 1564 | int i; |
| 1565 | for (i=0; i < size; i++) { |
| 1566 | if (array[i] == bind) |
| 1567 | return; |
| 1568 | if (!array[i]) |
| 1569 | break; |
| 1570 | } |
| 1571 | |
| 1572 | if (i == size) |
| 1573 | return; |
| 1574 | |
| 1575 | array[i] = bind; |
| 1576 | } |
| 1577 | |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 1578 | void ns2_nse_update_mtu(struct gprs_ns2_nse *nse) |
| 1579 | { |
| 1580 | struct gprs_ns2_vc *nsvc; |
| 1581 | int mtu = 0; |
| 1582 | |
| 1583 | if (llist_empty(&nse->nsvc)) { |
| 1584 | nse->mtu = 0; |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1589 | if (mtu == 0) |
| 1590 | mtu = nsvc->bind->mtu; |
| 1591 | else if (mtu > nsvc->bind->mtu) |
| 1592 | mtu = nsvc->bind->mtu; |
| 1593 | } |
| 1594 | |
| 1595 | if (nse->mtu == mtu) |
| 1596 | return; |
| 1597 | |
| 1598 | nse->mtu = mtu; |
| 1599 | if (nse->alive) |
Alexander Couzens | db7b2ab | 2021-07-02 16:12:28 +0200 | [diff] [blame] | 1600 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_MTU_CHANGE); |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1603 | /*! calculate the transfer capabilities for a nse |
| 1604 | * \param nse the nse to count the transfer capability |
| 1605 | * \param bvci a bvci - unused |
| 1606 | * \return the transfer capability in mbit. On error < 0. |
| 1607 | */ |
| 1608 | int ns2_count_transfer_cap(struct gprs_ns2_nse *nse, |
| 1609 | uint16_t bvci) |
| 1610 | { |
| 1611 | struct gprs_ns2_vc *nsvc; |
| 1612 | struct gprs_ns2_vc_bind **active_binds; |
| 1613 | int i, active_nsvcs = 0, transfer_cap = 0; |
| 1614 | |
| 1615 | /* calculate the transfer capabilities based on the binds. |
| 1616 | * A bind has a transfer capability which is shared across all NSVCs. |
| 1617 | * Take care the bind cap is not counted twice within a NSE. |
| 1618 | * This should be accurate for FR and UDP but not for FR/GRE. */ |
| 1619 | |
| 1620 | if (!nse->alive) |
| 1621 | return 0; |
| 1622 | |
| 1623 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1624 | if (ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1625 | active_nsvcs++; |
| 1626 | } |
Alexander Couzens | 3255668 | 2021-02-28 23:42:53 +0100 | [diff] [blame] | 1627 | |
| 1628 | if (!active_nsvcs) |
| 1629 | return 0; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1630 | |
| 1631 | active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs); |
| 1632 | if (!active_binds) |
| 1633 | return -ENOMEM; |
| 1634 | |
| 1635 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1636 | if (!ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1637 | continue; |
| 1638 | add_bind_array(active_binds, nsvc->bind, active_nsvcs); |
| 1639 | } |
| 1640 | |
| 1641 | /* TODO: change calcuation for FR/GRE */ |
| 1642 | for (i = 0; i < active_nsvcs; i++) { |
| 1643 | if (active_binds[i]) |
| 1644 | transfer_cap += active_binds[i]->transfer_capability; |
| 1645 | } |
| 1646 | |
| 1647 | talloc_free(active_binds); |
| 1648 | return transfer_cap; |
| 1649 | } |
| 1650 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1651 | /*! common allocation + low-level initialization of a bind. Called by vc-drivers */ |
| 1652 | int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name, |
| 1653 | struct gprs_ns2_vc_bind **result) |
| 1654 | { |
| 1655 | struct gprs_ns2_vc_bind *bind; |
| 1656 | |
| 1657 | if (!name) |
| 1658 | return -EINVAL; |
| 1659 | |
| 1660 | if (gprs_ns2_bind_by_name(nsi, name)) |
| 1661 | return -EALREADY; |
| 1662 | |
| 1663 | bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
| 1664 | if (!bind) |
Harald Welte | bdfb8b9 | 2021-01-31 11:44:57 +0100 | [diff] [blame] | 1665 | return -ENOMEM; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1666 | |
| 1667 | bind->name = talloc_strdup(bind, name); |
| 1668 | if (!bind->name) { |
| 1669 | talloc_free(bind); |
Harald Welte | bdfb8b9 | 2021-01-31 11:44:57 +0100 | [diff] [blame] | 1670 | return -ENOMEM; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 1673 | bind->statg = osmo_stat_item_group_alloc(bind, &nsbind_statg_desc, nsi->bind_rate_ctr_idx); |
| 1674 | if (!bind->statg) { |
| 1675 | talloc_free(bind); |
| 1676 | return -ENOMEM; |
| 1677 | } |
| 1678 | |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 1679 | bind->sns_sig_weight = 1; |
| 1680 | bind->sns_data_weight = 1; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1681 | bind->nsi = nsi; |
| 1682 | INIT_LLIST_HEAD(&bind->nsvc); |
Alexander Couzens | d5cd8c6 | 2021-06-15 20:59:03 +0200 | [diff] [blame] | 1683 | llist_add_tail(&bind->list, &nsi->binding); |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1684 | |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 1685 | nsi->bind_rate_ctr_idx++; |
| 1686 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 1687 | if (result) |
| 1688 | *result = bind; |
| 1689 | |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1693 | /*! @} */ |