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> |
| 75 | #include <osmocom/core/msgb.h> |
| 76 | #include <osmocom/core/rate_ctr.h> |
| 77 | #include <osmocom/core/socket.h> |
| 78 | #include <osmocom/core/sockaddr_str.h> |
| 79 | #include <osmocom/core/stats.h> |
| 80 | #include <osmocom/core/stat_item.h> |
| 81 | #include <osmocom/core/talloc.h> |
| 82 | #include <osmocom/gprs/gprs_msgb.h> |
| 83 | #include <osmocom/gsm/prim.h> |
| 84 | #include <osmocom/gsm/tlv.h> |
| 85 | |
| 86 | #include "gprs_ns2_internal.h" |
| 87 | |
| 88 | #define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__) |
| 89 | #define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__) |
| 90 | #define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED) |
| 91 | #define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED)); |
| 92 | #define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE) |
| 93 | #define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE)); |
| 94 | |
| 95 | /* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules. |
| 96 | * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it, |
| 97 | * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2. |
| 98 | * The TLV parser depends on 8bit tag in many places. |
| 99 | * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE. |
| 100 | */ |
| 101 | static const struct tlv_definition ns_att_tlvdef1 = { |
| 102 | .def = { |
| 103 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 104 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 105 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 106 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 107 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 108 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 109 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 110 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 111 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 112 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 113 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 114 | /* NS_IE_IP_ADDR in the IPv4 version */ |
| 115 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 }, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | static const struct tlv_definition ns_att_tlvdef2 = { |
| 120 | .def = { |
| 121 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 122 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 123 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 124 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 125 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 126 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 127 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 128 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 129 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 130 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 131 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 132 | /* NS_IE_IP_ADDR in the IPv6 version */ |
| 133 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 }, |
| 134 | }, |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | /* Section 10.3.2, Table 13 */ |
Alexander Couzens | b3b837c | 2020-10-27 15:12:25 +0100 | [diff] [blame] | 139 | const struct value_string gprs_ns2_cause_strs[] = { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 140 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 141 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 142 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 143 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 144 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 145 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 146 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 147 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
| 148 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 149 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 150 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 151 | { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" }, |
| 152 | { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" }, |
| 153 | { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" }, |
| 154 | { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" }, |
| 155 | { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" }, |
| 156 | { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" }, |
| 157 | { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" }, |
| 158 | { 0, NULL } |
| 159 | }; |
| 160 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 161 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
| 162 | { "packets:in", "Packets at NS Level ( In)" }, |
| 163 | { "packets:out","Packets at NS Level (Out)" }, |
| 164 | { "bytes:in", "Bytes at NS Level ( In)" }, |
| 165 | { "bytes:out", "Bytes at NS Level (Out)" }, |
| 166 | { "blocked", "NS-VC Block count " }, |
| 167 | { "dead", "NS-VC gone dead count " }, |
| 168 | { "replaced", "NS-VC replaced other count" }, |
| 169 | { "nsei-chg", "NS-VC changed NSEI count " }, |
| 170 | { "inv-nsvci", "NS-VCI was invalid count " }, |
| 171 | { "inv-nsei", "NSEI was invalid count " }, |
| 172 | { "lost:alive", "ALIVE ACK missing count " }, |
| 173 | { "lost:reset", "RESET ACK missing count " }, |
| 174 | }; |
| 175 | |
| 176 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
| 177 | .group_name_prefix = "ns:nsvc", |
| 178 | .group_description = "NSVC Peer Statistics", |
| 179 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 180 | .ctr_desc = nsvc_ctr_description, |
| 181 | .class_id = OSMO_STATS_CLASS_PEER, |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | static const struct osmo_stat_item_desc nsvc_stat_description[] = { |
| 186 | { "alive.delay", "ALIVE response time ", "ms", 16, 0 }, |
| 187 | }; |
| 188 | |
| 189 | static const struct osmo_stat_item_group_desc nsvc_statg_desc = { |
| 190 | .group_name_prefix = "ns.nsvc", |
| 191 | .group_description = "NSVC Peer Statistics", |
| 192 | .num_items = ARRAY_SIZE(nsvc_stat_description), |
| 193 | .item_desc = nsvc_stat_description, |
| 194 | .class_id = OSMO_STATS_CLASS_PEER, |
| 195 | }; |
| 196 | |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 197 | const struct value_string gprs_ns2_aff_cause_prim_strs[] = { |
| 198 | { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" }, |
| 199 | { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" }, |
| 200 | { NS_AFF_CAUSE_FAILURE, "NSE failure" }, |
| 201 | { NS_AFF_CAUSE_RECOVERY, "NSE recovery" }, |
| 202 | { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" }, |
| 203 | { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" }, |
| 204 | { 0, NULL } |
| 205 | }; |
| 206 | |
Alexander Couzens | 0ab028c | 2020-11-04 02:41:44 +0100 | [diff] [blame] | 207 | const struct value_string gprs_ns2_prim_strs[] = { |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 208 | { PRIM_NS_UNIT_DATA, "UNIT DATA" }, |
| 209 | { PRIM_NS_CONGESTION, "CONGESTION" }, |
| 210 | { PRIM_NS_STATUS, "STATUS" }, |
| 211 | { 0, NULL } |
| 212 | }; |
| 213 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 214 | /*! string-format a given NS-VC into a user-supplied buffer. |
| 215 | * \param[in] buf user-allocated output buffer |
| 216 | * \param[in] buf_len size of user-allocated output buffer in bytes |
| 217 | * \param[in] nsvc NS-VC to be string-formatted |
| 218 | * \return pointer to buf on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 219 | char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc) |
| 220 | { |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 221 | const struct osmo_sockaddr *local; |
| 222 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 223 | struct osmo_sockaddr_str local_str; |
| 224 | struct osmo_sockaddr_str remote_str; |
| 225 | |
| 226 | if (!buf_len) |
Harald Welte | 92ad029 | 2020-09-18 22:34:24 +0200 | [diff] [blame] | 227 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 228 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 229 | switch (nsvc->nse->ll) { |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 230 | case GPRS_NS2_LL_UDP: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 231 | if (!gprs_ns2_is_ip_bind(nsvc->bind)) { |
| 232 | buf[0] = '\0'; |
| 233 | return buf; |
| 234 | } |
| 235 | |
| 236 | local = gprs_ns2_ip_bind_sockaddr(nsvc->bind); |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 237 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 238 | if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas)) |
| 239 | strcpy(local_str.ip, "invalid"); |
| 240 | if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas)) |
| 241 | strcpy(remote_str.ip, "invalid"); |
| 242 | |
| 243 | if (nsvc->nsvci_is_valid) |
| 244 | snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u", |
| 245 | local_str.ip, local_str.port, |
| 246 | nsvc->nsvci, |
| 247 | remote_str.ip, remote_str.port); |
| 248 | else |
| 249 | snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u", |
| 250 | local_str.ip, local_str.port, |
| 251 | remote_str.ip, remote_str.port); |
| 252 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 253 | case GPRS_NS2_LL_FR_GRE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 254 | snprintf(buf, buf_len, "frgre)"); |
| 255 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 256 | case GPRS_NS2_LL_FR: |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 257 | if (!gprs_ns2_is_frgre_bind(nsvc->bind)) { |
| 258 | buf[0] = '\0'; |
| 259 | return buf; |
| 260 | } |
| 261 | |
| 262 | snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind), |
| 263 | gprs_ns2_fr_nsvc_dlci(nsvc)); |
| 264 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 265 | default: |
| 266 | buf[0] = '\0'; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | buf[buf_len - 1] = '\0'; |
| 271 | |
| 272 | return buf; |
| 273 | } |
| 274 | |
| 275 | /* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */ |
| 276 | #define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8 |
| 277 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 278 | /*! string-format a given NS-VC to a thread-local static buffer. |
| 279 | * \param[in] nsvc NS-VC to be string-formatted |
| 280 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 281 | const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc) |
| 282 | { |
| 283 | static __thread char buf[NS2_LL_MAX_STR]; |
| 284 | return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc); |
| 285 | } |
| 286 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 287 | /*! string-format a given NS-VC to a dynamically allocated string. |
| 288 | * \param[in] ctx talloc context from which to allocate |
| 289 | * \param[in] nsvc NS-VC to be string-formatted |
| 290 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 291 | char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc) |
| 292 | { |
| 293 | char *buf = talloc_size(ctx, NS2_LL_MAX_STR); |
| 294 | if (!buf) |
| 295 | return buf; |
| 296 | return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc); |
| 297 | } |
| 298 | |
Daniel Willmann | f128654 | 2020-11-03 23:03:33 +0100 | [diff] [blame] | 299 | /*! Return the current state name of a given NS-VC to a thread-local static buffer. |
| 300 | * \param[in] nsvc NS-VC to return the state of |
| 301 | * \return pointer to the string on success; NULL on error */ |
| 302 | const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc) |
| 303 | { |
| 304 | return osmo_fsm_inst_state_name(nsvc->fi); |
| 305 | } |
| 306 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 307 | /* select a signalling NSVC and respect sig_counter |
| 308 | * param[out] reset_counter - all counter has to be resetted to their signal weight |
| 309 | * return the chosen nsvc or NULL |
| 310 | */ |
| 311 | static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse) |
| 312 | { |
| 313 | struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp; |
| 314 | |
| 315 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 316 | if (tmp->sig_weight == 0) |
| 317 | continue; |
| 318 | if (!gprs_ns2_vc_is_unblocked(tmp)) |
| 319 | continue; |
| 320 | if (tmp->sig_counter == 0) { |
| 321 | last = tmp; |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | tmp->sig_counter--; |
| 326 | nsvc = tmp; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | /* all counter were zero, but there are valid nsvc */ |
| 331 | if (!nsvc && last) { |
| 332 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 333 | tmp->sig_counter = tmp->sig_weight; |
| 334 | } |
| 335 | |
| 336 | last->sig_counter--; |
| 337 | return last; |
| 338 | } else { |
| 339 | return nsvc; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */ |
| 344 | static struct gprs_ns2_vc *ns2_load_sharing_modulor( |
| 345 | struct gprs_ns2_nse *nse, |
| 346 | uint16_t bvci, |
| 347 | uint32_t load_selector) |
| 348 | { |
| 349 | struct gprs_ns2_vc *tmp; |
| 350 | uint32_t mod = (bvci + load_selector) % nse->nsvc_data_count; |
| 351 | uint32_t i = 0; |
| 352 | |
| 353 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 354 | if (!gprs_ns2_vc_is_unblocked(tmp)) |
| 355 | continue; |
| 356 | if (tmp->data_weight == 0) |
| 357 | continue; |
| 358 | |
| 359 | if (i == mod) |
| 360 | return tmp; |
| 361 | i++; |
| 362 | } |
| 363 | |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | /* pick the first available data NSVC - no load sharing */ |
| 368 | struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse) |
| 369 | { |
| 370 | struct gprs_ns2_vc *nsvc = NULL, *tmp; |
| 371 | |
| 372 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 373 | if (!gprs_ns2_vc_is_unblocked(tmp)) |
| 374 | continue; |
| 375 | if (tmp->data_weight == 0) |
| 376 | continue; |
| 377 | |
| 378 | nsvc = tmp; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | return nsvc; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | static struct gprs_ns2_vc *ns2_load_sharing( |
| 387 | struct gprs_ns2_nse *nse, |
| 388 | uint16_t bvci, |
| 389 | uint32_t link_selector) |
| 390 | { |
| 391 | struct gprs_ns2_vc *nsvc = NULL; |
| 392 | |
| 393 | if (bvci == 0) { |
| 394 | /* signalling */ |
| 395 | nsvc = ns2_load_sharing_signal(nse); |
| 396 | } else { |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 397 | /* data with load sharing parameter */ |
| 398 | if (llist_empty(&nse->nsvc)) |
| 399 | return NULL; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 400 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 401 | switch (nse->ll) { |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 402 | case GPRS_NS2_LL_FR: |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 403 | nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector); |
| 404 | break; |
| 405 | default: |
| 406 | nsvc = ns2_load_sharing_first(nse); |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return nsvc; |
| 412 | } |
| 413 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 414 | /*! Receive a primitive from the NS User (Gb). |
| 415 | * \param[in] nsi NS instance to which the primitive is issued |
| 416 | * \param[in] oph The primitive |
| 417 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 418 | int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph) |
| 419 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 420 | /* TODO: implement resource distribution */ |
| 421 | /* TODO: check for empty PDUs which can be sent to Request/Confirm |
| 422 | * the IP endpoint */ |
| 423 | struct osmo_gprs_ns2_prim *nsp; |
| 424 | struct gprs_ns2_nse *nse = NULL; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 425 | struct gprs_ns2_vc *nsvc = NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 426 | uint16_t bvci, nsei; |
| 427 | uint8_t sducontrol = 0; |
| 428 | |
| 429 | if (oph->sap != SAP_NS) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 433 | |
| 434 | if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA) |
| 435 | return -EINVAL; |
| 436 | |
| 437 | if (!oph->msg) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | bvci = nsp->bvci; |
| 441 | nsei = nsp->nsei; |
| 442 | |
| 443 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 444 | if (!nse) |
| 445 | return -EINVAL; |
| 446 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 447 | nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 448 | |
| 449 | /* TODO: send a status primitive back */ |
| 450 | if (!nsvc) |
| 451 | return 0; |
| 452 | |
| 453 | if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE) |
| 454 | sducontrol = 1; |
| 455 | else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE) |
| 456 | sducontrol = 2; |
| 457 | |
| 458 | return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg); |
| 459 | } |
| 460 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 461 | /*! Send a STATUS.ind primitive to the specified NS instance user. |
| 462 | * \param[in] nsi NS instance on which we operate |
| 463 | * \param[in] nsei NSEI to which the statue relates |
| 464 | * \param[in] bvci BVCI to which the status relates |
| 465 | * \param[in] cause The cause of the status */ |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 466 | void ns2_prim_status_ind(struct gprs_ns2_nse *nse, |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 467 | struct gprs_ns2_vc *nsvc, |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 468 | uint16_t bvci, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 469 | enum gprs_ns2_affecting_cause cause) |
| 470 | { |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 471 | char nsvc_str[NS2_LL_MAX_STR]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 472 | struct osmo_gprs_ns2_prim nsp = {}; |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 473 | nsp.nsei = nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 474 | nsp.bvci = bvci; |
| 475 | nsp.u.status.cause = cause; |
| 476 | nsp.u.status.transfer = -1; |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 477 | nsp.u.status.first = nse->first; |
| 478 | nsp.u.status.persistent = nse->persistent; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 479 | if (nsvc) |
| 480 | nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc); |
| 481 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 482 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS, |
| 483 | PRIM_OP_INDICATION, NULL); |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 484 | nse->nsi->cb(&nsp.oph, nse->nsi->cb_data); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 487 | /*! Allocate a NS-VC within the given bind + NSE. |
| 488 | * \param[in] bind The 'bind' on which we operate |
| 489 | * \param[in] nse The NS Entity on which we operate |
| 490 | * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater) |
| 491 | * \return newly allocated NS-VC on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 492 | struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater) |
| 493 | { |
| 494 | struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc); |
| 495 | |
| 496 | if (!nsvc) |
| 497 | return NULL; |
| 498 | |
| 499 | nsvc->bind = bind; |
| 500 | nsvc->nse = nse; |
| 501 | nsvc->mode = bind->vc_mode; |
| 502 | nsvc->sig_weight = 1; |
| 503 | nsvc->data_weight = 1; |
| 504 | |
| 505 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx); |
| 506 | if (!nsvc->ctrg) { |
| 507 | goto err; |
| 508 | } |
| 509 | nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx); |
| 510 | if (!nsvc->statg) |
| 511 | goto err_group; |
| 512 | if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater)) |
| 513 | goto err_statg; |
| 514 | |
| 515 | bind->nsi->rate_ctr_idx++; |
| 516 | |
| 517 | llist_add(&nsvc->list, &nse->nsvc); |
| 518 | llist_add(&nsvc->blist, &bind->nsvc); |
| 519 | |
| 520 | return nsvc; |
| 521 | |
| 522 | err_statg: |
| 523 | osmo_stat_item_group_free(nsvc->statg); |
| 524 | err_group: |
| 525 | rate_ctr_group_free(nsvc->ctrg); |
| 526 | err: |
| 527 | talloc_free(nsvc); |
| 528 | |
| 529 | return NULL; |
| 530 | } |
| 531 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 532 | /*! Destroy/release given NS-VC. |
| 533 | * \param[in] nsvc NS-VC to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 534 | void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc) |
| 535 | { |
| 536 | if (!nsvc) |
| 537 | return; |
| 538 | |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 539 | ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 540 | |
| 541 | llist_del(&nsvc->list); |
| 542 | llist_del(&nsvc->blist); |
| 543 | |
| 544 | /* notify nse this nsvc is unavailable */ |
| 545 | ns2_nse_notify_unblocked(nsvc, false); |
| 546 | |
| 547 | /* check if sns is using this VC */ |
| 548 | ns2_sns_free_nsvc(nsvc); |
| 549 | osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 550 | |
| 551 | /* let the driver/bind clean up it's internal state */ |
| 552 | if (nsvc->priv && nsvc->bind->free_vc) |
| 553 | nsvc->bind->free_vc(nsvc); |
| 554 | |
| 555 | osmo_stat_item_group_free(nsvc->statg); |
| 556 | rate_ctr_group_free(nsvc->ctrg); |
| 557 | |
| 558 | talloc_free(nsvc); |
| 559 | } |
| 560 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 561 | /*! Allocate a message buffer for use with the NS2 stack. */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 562 | struct msgb *gprs_ns2_msgb_alloc(void) |
| 563 | { |
| 564 | struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, |
| 565 | "GPRS/NS"); |
| 566 | if (!msg) { |
| 567 | LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n", |
| 568 | NS_ALLOC_SIZE); |
| 569 | } |
| 570 | return msg; |
| 571 | } |
| 572 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 573 | /*! Create a status message to be sent over a new connection. |
| 574 | * \param[in] orig_msg the original message |
| 575 | * \param[in] tp TLVP parsed of the original message |
| 576 | * \param[out] reject callee-allocated message buffer of the generated NS-STATUS |
| 577 | * \param[in] cause Cause for the rejection |
| 578 | * \return 0 on success */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 579 | static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause) |
| 580 | { |
| 581 | struct msgb *msg = gprs_ns2_msgb_alloc(); |
| 582 | struct gprs_ns_hdr *nsh; |
| 583 | bool have_vci = false; |
| 584 | uint8_t _cause = cause; |
| 585 | uint16_t nsei = 0; |
| 586 | |
| 587 | if (!msg) |
| 588 | return -ENOMEM; |
| 589 | |
| 590 | if (TLVP_PRESENT(tp, NS_IE_NSEI)) { |
| 591 | nsei = tlvp_val16be(tp, NS_IE_NSEI); |
| 592 | |
| 593 | LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n", |
| 594 | nsei, gprs_ns2_cause_str(cause)); |
| 595 | } |
| 596 | |
| 597 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 598 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 599 | nsh->pdu_type = NS_PDUT_STATUS; |
| 600 | |
| 601 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause); |
| 602 | have_vci = TLVP_PRESENT(tp, NS_IE_VCI); |
| 603 | |
| 604 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 605 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 606 | cause == NS_CAUSE_NSVC_UNKNOWN) { |
| 607 | if (!have_vci) { |
| 608 | msgb_free(msg); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
| 612 | msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI)); |
| 613 | } |
| 614 | |
| 615 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 616 | switch (cause) { |
| 617 | case NS_CAUSE_SEM_INCORR_PDU: |
| 618 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 619 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 620 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 621 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 622 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 623 | orig_msg->l2h); |
| 624 | break; |
| 625 | default: |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | *reject = msg; |
| 630 | return 0; |
| 631 | } |
| 632 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 633 | /*! Resolve a NS Entity based on its NSEI. |
| 634 | * \param[in] nsi NS Instance in which we do the look-up |
| 635 | * \param[in] nsei NSEI to look up |
| 636 | * \return NS Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 637 | struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei) |
| 638 | { |
| 639 | struct gprs_ns2_nse *nse; |
| 640 | |
| 641 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 642 | if (nse->nsei == nsei) |
| 643 | return nse; |
| 644 | } |
| 645 | |
| 646 | return NULL; |
| 647 | } |
| 648 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 649 | /*! Resolve a NS-VC Entity based on its NS-VCI. |
| 650 | * \param[in] nsi NS Instance in which we do the look-up |
| 651 | * \param[in] nsvci NS-VCI to look up |
| 652 | * \return NS-VC Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 653 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci) |
| 654 | { |
| 655 | struct gprs_ns2_nse *nse; |
| 656 | struct gprs_ns2_vc *nsvc; |
| 657 | |
| 658 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 659 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 660 | if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci) |
| 661 | return nsvc; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | return NULL; |
| 666 | } |
| 667 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 668 | /*! Create a NS Entity within given NS instance. |
| 669 | * \param[in] nsi NS instance in which to create NS Entity |
| 670 | * \param[in] nsei NS Entity Identifier of to-be-created NSE |
| 671 | * \returns newly-allocated NS-E in successful case; NULL on error */ |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 672 | struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei, enum gprs_ns2_ll linklayer) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 673 | { |
| 674 | struct gprs_ns2_nse *nse; |
| 675 | |
| 676 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 677 | if (nse) { |
| 678 | LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei); |
| 679 | return nse; |
| 680 | } |
| 681 | |
| 682 | nse = talloc_zero(nsi, struct gprs_ns2_nse); |
| 683 | if (!nse) |
| 684 | return NULL; |
| 685 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 686 | nse->ll = linklayer; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 687 | nse->nsei = nsei; |
| 688 | nse->nsi = nsi; |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 689 | nse->first = true; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 690 | llist_add(&nse->list, &nsi->nse); |
| 691 | INIT_LLIST_HEAD(&nse->nsvc); |
| 692 | |
| 693 | return nse; |
| 694 | } |
| 695 | |
Alexander Couzens | 05e7f7d | 2020-10-11 19:51:46 +0200 | [diff] [blame] | 696 | /*! Return the NSEI |
| 697 | * \param[in] nse NS Entity |
| 698 | * \return the nsei. |
| 699 | */ |
| 700 | uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse) |
| 701 | { |
| 702 | return nse->nsei; |
| 703 | } |
| 704 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 705 | /*! Destroy given NS Entity. |
| 706 | * \param[in] nse NS Entity to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 707 | void gprs_ns2_free_nse(struct gprs_ns2_nse *nse) |
| 708 | { |
| 709 | struct gprs_ns2_vc *nsvc, *tmp; |
| 710 | |
| 711 | if (!nse) |
| 712 | return; |
| 713 | |
| 714 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
| 715 | gprs_ns2_free_nsvc(nsvc); |
| 716 | } |
| 717 | |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 718 | ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 719 | |
| 720 | llist_del(&nse->list); |
| 721 | if (nse->bss_sns_fi) |
| 722 | osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 723 | talloc_free(nse); |
| 724 | } |
| 725 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 726 | void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi) |
| 727 | { |
| 728 | struct gprs_ns2_nse *nse, *ntmp; |
| 729 | |
| 730 | llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) { |
| 731 | gprs_ns2_free_nse(nse); |
| 732 | } |
| 733 | } |
| 734 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 735 | static inline int ns2_tlv_parse(struct tlv_parsed *dec, |
| 736 | const uint8_t *buf, int buf_len, uint8_t lv_tag, |
| 737 | uint8_t lv_tag2) |
| 738 | { |
| 739 | /* workaround for NS_IE_IP_ADDR not following any known TLV rules. |
| 740 | * See comment of ns_att_tlvdef1. */ |
| 741 | int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2); |
| 742 | if (rc < 0) |
| 743 | return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2); |
| 744 | return rc; |
| 745 | } |
| 746 | |
| 747 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 748 | /*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE. |
| 749 | * \param[in] bind the bind through which msg was received |
| 750 | * \param[in] msg the actual received message |
| 751 | * \param[in] logname A name to describe the VC. E.g. ip address pair |
| 752 | * \param[out] reject A message filled to be sent back. Only used in failure cases. |
| 753 | * \param[out] success A pointer which will be set to the new VC on success |
| 754 | * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 755 | enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind, |
| 756 | struct msgb *msg, |
| 757 | const char *logname, |
| 758 | struct msgb **reject, |
| 759 | struct gprs_ns2_vc **success) |
| 760 | { |
| 761 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 762 | struct tlv_parsed tp; |
| 763 | struct gprs_ns2_vc *nsvc; |
| 764 | struct gprs_ns2_nse *nse; |
| 765 | uint16_t nsvci; |
| 766 | uint16_t nsei; |
| 767 | |
| 768 | int rc; |
| 769 | |
| 770 | if (msg->len < sizeof(struct gprs_ns_hdr)) |
| 771 | return GPRS_NS2_CS_ERROR; |
| 772 | |
| 773 | if (nsh->pdu_type == NS_PDUT_STATUS) { |
| 774 | /* Do not respond, see 3GPP TS 08.16, 7.5.1 */ |
| 775 | LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s " |
| 776 | "for non-existing NS-VC\n", |
| 777 | logname); |
| 778 | return GPRS_NS2_CS_SKIPPED; |
| 779 | } |
| 780 | |
| 781 | if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) { |
| 782 | /* Ignore this, see 3GPP TS 08.16, 7.4.1 */ |
| 783 | LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s " |
| 784 | "for non-existing NS-VC\n", |
| 785 | logname); |
| 786 | return GPRS_NS2_CS_SKIPPED; |
| 787 | } |
| 788 | |
| 789 | if (nsh->pdu_type == NS_PDUT_RESET_ACK) { |
| 790 | /* Ignore this, see 3GPP TS 08.16, 7.3.1 */ |
| 791 | LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s " |
| 792 | "for non-existing NS-VC\n", |
| 793 | logname); |
| 794 | return GPRS_NS2_CS_SKIPPED; |
| 795 | } |
| 796 | |
Alexander Couzens | 48f6386 | 2020-09-12 02:19:19 +0200 | [diff] [blame] | 797 | if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) { |
| 798 | /* Only the RESET procedure creates a new NSVC */ |
| 799 | if (nsh->pdu_type != NS_PDUT_RESET) { |
| 800 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 801 | |
Alexander Couzens | 48f6386 | 2020-09-12 02:19:19 +0200 | [diff] [blame] | 802 | if (rc < 0) { |
| 803 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
| 804 | return rc; |
| 805 | } |
| 806 | return GPRS_NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 807 | } |
Alexander Couzens | 48f6386 | 2020-09-12 02:19:19 +0200 | [diff] [blame] | 808 | } else { /* NS2_VC_MODE_ALIVE */ |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 809 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 48f6386 | 2020-09-12 02:19:19 +0200 | [diff] [blame] | 810 | |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 811 | if (rc < 0) { |
| 812 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
| 813 | return rc; |
Alexander Couzens | 48f6386 | 2020-09-12 02:19:19 +0200 | [diff] [blame] | 814 | } |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 815 | return GPRS_NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 819 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 820 | if (rc < 0) { |
| 821 | LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 822 | "TLV Parse\n", rc); |
| 823 | /* TODO: send invalid message back */ |
| 824 | return GPRS_NS2_CS_REJECTED; |
| 825 | } |
| 826 | |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 827 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 828 | !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 829 | LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
| 830 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE); |
| 831 | return GPRS_NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* find or create NSE */ |
| 835 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
| 836 | nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 837 | if (!nse) { |
| 838 | if (!bind->nsi->create_nse) { |
| 839 | return GPRS_NS2_CS_SKIPPED; |
| 840 | } |
| 841 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 842 | nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 843 | if (!nse) { |
| 844 | return GPRS_NS2_CS_ERROR; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | nsvc = ns2_vc_alloc(bind, nse, false); |
| 849 | if (!nsvc) |
| 850 | return GPRS_NS2_CS_SKIPPED; |
| 851 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 852 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 853 | nsvc->nsvci = nsvci; |
| 854 | nsvc->nsvci_is_valid = true; |
| 855 | |
| 856 | *success = nsvc; |
| 857 | |
| 858 | return GPRS_NS2_CS_CREATED; |
| 859 | } |
| 860 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 861 | /*! Create, and connect an inactive, new IP-based NS-VC |
| 862 | * \param[in] bind bind in which the new NS-VC is to be created |
| 863 | * \param[in] remote remote address to which to connect |
| 864 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 865 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 866 | * \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] | 867 | 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] | 868 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 869 | struct gprs_ns2_nse *nse, |
| 870 | uint16_t nsvci) |
| 871 | { |
| 872 | struct gprs_ns2_vc *nsvc; |
| 873 | |
| 874 | nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote); |
| 875 | if (!nsvc) |
| 876 | return NULL; |
| 877 | |
| 878 | if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) { |
| 879 | nsvc->nsvci = nsvci; |
| 880 | nsvc->nsvci_is_valid = true; |
| 881 | } |
| 882 | |
| 883 | return nsvc; |
| 884 | } |
| 885 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 886 | /*! Create, connect and activate a new IP-based NS-VC |
| 887 | * \param[in] bind bind in which the new NS-VC is to be created |
| 888 | * \param[in] remote remote address to which to connect |
| 889 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 890 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 891 | * \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] | 892 | 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] | 893 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 894 | struct gprs_ns2_nse *nse, |
| 895 | uint16_t nsvci) |
| 896 | { |
| 897 | struct gprs_ns2_vc *nsvc; |
| 898 | nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci); |
| 899 | if (!nsvc) |
| 900 | return NULL; |
| 901 | |
| 902 | gprs_ns2_vc_fsm_start(nsvc); |
| 903 | |
| 904 | return nsvc; |
| 905 | } |
| 906 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 907 | /*! Create, connect and activate a new IP-based NS-VC |
| 908 | * \param[in] bind bind in which the new NS-VC is to be created |
| 909 | * \param[in] remote remote address to which to connect |
| 910 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 911 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 912 | * \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] | 913 | 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] | 914 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 915 | uint16_t nsei, |
| 916 | uint16_t nsvci) |
| 917 | { |
| 918 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 919 | |
| 920 | if (!nse) { |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 921 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 922 | if (!nse) |
| 923 | return NULL; |
| 924 | } |
| 925 | |
| 926 | return gprs_ns2_ip_connect(bind, remote, nse, nsvci); |
| 927 | } |
| 928 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 929 | /*! Create, connect and activate a new IP-SNS NSE. |
| 930 | * \param[in] bind bind in which the new NS-VC is to be created |
| 931 | * \param[in] remote remote address to which to connect |
| 932 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 933 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 934 | int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 935 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 936 | uint16_t nsei) |
| 937 | { |
| 938 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 939 | struct gprs_ns2_vc *nsvc; |
| 940 | |
| 941 | if (!nse) { |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 942 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 943 | if (!nse) |
| 944 | return -1; |
| 945 | } |
| 946 | |
| 947 | nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote); |
| 948 | if (!nsvc) |
| 949 | return -1; |
| 950 | |
| 951 | if (!nse->bss_sns_fi) |
| 952 | nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL); |
| 953 | |
| 954 | if (!nse->bss_sns_fi) |
| 955 | return -1; |
| 956 | |
| 957 | return ns2_sns_bss_fsm_start(nse, nsvc, remote); |
| 958 | } |
| 959 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 960 | /*! Find NS-VC for given socket address. |
| 961 | * \param[in] nse NS Entity in which to search |
| 962 | * \param[in] sockaddr socket address to search for |
| 963 | * \return NS-VC matching sockaddr; NULL if none found */ |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 964 | 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] | 965 | const struct osmo_sockaddr *sockaddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 966 | { |
| 967 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 968 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 969 | |
| 970 | OSMO_ASSERT(nse); |
| 971 | OSMO_ASSERT(sockaddr); |
| 972 | |
| 973 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 974 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 975 | if (!osmo_sockaddr_cmp(sockaddr, remote)) |
| 976 | return nsvc; |
| 977 | } |
| 978 | |
| 979 | return NULL; |
| 980 | } |
| 981 | |
Alexander Couzens | 6cb5d5f | 2020-10-11 23:23:31 +0200 | [diff] [blame] | 982 | /*! |
| 983 | * Iterate over all nsvc of a NS Entity and call the callback. |
| 984 | * If the callback returns < 0 it aborts the loop and returns the callback return code. |
| 985 | * \param[in] nse NS Entity to iterate over all nsvcs |
| 986 | * \param[in] cb the callback to call |
| 987 | * \param[inout] cb_data the private data of the callback |
| 988 | * \return 0 if the loop completes. If a callback returns < 0 it will returns this value. |
| 989 | */ |
| 990 | int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data) |
| 991 | { |
| 992 | struct gprs_ns2_vc *nsvc, *tmp; |
| 993 | int rc = 0; |
| 994 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
| 995 | rc = cb(nsvc, cb_data); |
| 996 | if (rc < 0) |
| 997 | return rc; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1004 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1005 | /*! Bottom-side entry-point for received NS PDU from the driver/bind |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1006 | * \param[in] nsvc NS-VC for which the message was received |
| 1007 | * \param msg the received message. Ownership is trasnferred, caller must not free it! |
| 1008 | * \return 0 on success; negative on error */ |
Alexander Couzens | ffd49d0 | 2020-09-24 00:47:17 +0200 | [diff] [blame] | 1009 | int ns2_recv_vc(struct gprs_ns2_vc *nsvc, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1010 | struct msgb *msg) |
| 1011 | { |
| 1012 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1013 | struct tlv_parsed tp; |
| 1014 | int rc = 0; |
| 1015 | |
| 1016 | if (msg->len < sizeof(struct gprs_ns_hdr)) |
| 1017 | return -EINVAL; |
| 1018 | |
| 1019 | switch (nsh->pdu_type) { |
| 1020 | case SNS_PDUT_CONFIG: |
| 1021 | /* one additional byte ('end flag') before the TLV part starts */ |
| 1022 | rc = ns2_tlv_parse(&tp, nsh->data+1, |
| 1023 | msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0); |
| 1024 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1025 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1026 | return rc; |
| 1027 | } |
| 1028 | /* All sub-network service related message types */ |
| 1029 | rc = gprs_ns2_sns_rx(nsvc, msg, &tp); |
| 1030 | break; |
| 1031 | case SNS_PDUT_ACK: |
| 1032 | case SNS_PDUT_ADD: |
| 1033 | case SNS_PDUT_CHANGE_WEIGHT: |
| 1034 | case SNS_PDUT_DELETE: |
| 1035 | /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */ |
Harald Welte | 36be9d8 | 2020-10-09 15:39:25 +0200 | [diff] [blame] | 1036 | rc = ns2_tlv_parse(&tp, nsh->data+5, |
| 1037 | msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1038 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1039 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1040 | return rc; |
| 1041 | } |
| 1042 | tp.lv[NS_IE_NSEI].val = nsh->data+2; |
| 1043 | tp.lv[NS_IE_NSEI].len = 2; |
| 1044 | tp.lv[NS_IE_TRANS_ID].val = nsh->data+4; |
| 1045 | tp.lv[NS_IE_TRANS_ID].len = 1; |
| 1046 | rc = gprs_ns2_sns_rx(nsvc, msg, &tp); |
| 1047 | break; |
| 1048 | case SNS_PDUT_CONFIG_ACK: |
| 1049 | case SNS_PDUT_SIZE: |
| 1050 | case SNS_PDUT_SIZE_ACK: |
| 1051 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1052 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1053 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1054 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1055 | return rc; |
| 1056 | } |
| 1057 | /* All sub-network service related message types */ |
| 1058 | rc = gprs_ns2_sns_rx(nsvc, msg, &tp); |
| 1059 | break; |
| 1060 | |
| 1061 | case NS_PDUT_UNITDATA: |
| 1062 | rc = gprs_ns2_vc_rx(nsvc, msg, NULL); |
| 1063 | break; |
| 1064 | default: |
| 1065 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1066 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1067 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1068 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1069 | if (nsh->pdu_type != NS_PDUT_STATUS) |
| 1070 | ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg); |
| 1071 | return rc; |
| 1072 | } |
| 1073 | rc = gprs_ns2_vc_rx(nsvc, msg, &tp); |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | return rc; |
| 1078 | } |
| 1079 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1080 | /* summarize all active data nsvcs */ |
| 1081 | void ns2_nse_data_sum(struct gprs_ns2_nse *nse) |
| 1082 | { |
| 1083 | struct gprs_ns2_vc *nsvc; |
| 1084 | nse->nsvc_data_count = 0; |
| 1085 | |
| 1086 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1087 | if (!gprs_ns2_vc_is_unblocked(nsvc)) |
| 1088 | continue; |
| 1089 | if (nsvc->data_weight > 0) |
| 1090 | nse->nsvc_data_count++; |
| 1091 | } |
| 1092 | } |
| 1093 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1094 | /*! Notify a nse about the change of a NS-VC. |
| 1095 | * \param[in] nsvc NS-VC which has detected the change (and shall not be notified). |
| 1096 | * \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] | 1097 | void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked) |
| 1098 | { |
| 1099 | struct gprs_ns2_nse *nse = nsvc->nse; |
| 1100 | struct gprs_ns2_vc *tmp; |
| 1101 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1102 | ns2_nse_data_sum(nse); |
| 1103 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1104 | if (unblocked == nse->alive) |
| 1105 | return; |
| 1106 | |
| 1107 | if (unblocked) { |
| 1108 | /* this is the first unblocked NSVC on an unavailable NSE */ |
| 1109 | nse->alive = true; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 1110 | ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY); |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 1111 | nse->first = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | /* check if there are any remaining alive vcs */ |
| 1116 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 1117 | if (tmp == nsvc) |
| 1118 | continue; |
| 1119 | |
| 1120 | if (gprs_ns2_vc_is_unblocked(tmp)) { |
| 1121 | /* there is at least one remaining alive NSVC */ |
| 1122 | return; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /* nse became unavailable */ |
| 1127 | nse->alive = false; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 1128 | ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /*! Create a new GPRS NS instance |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1132 | * \param[in] ctx a talloc context to allocate NS instance from |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 1133 | * \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] | 1134 | * \param[in] cb_data transparent user data passed to Call-back |
| 1135 | * \returns dynamically allocated gprs_ns_inst; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1136 | struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data) |
| 1137 | { |
| 1138 | struct gprs_ns2_inst *nsi; |
| 1139 | |
| 1140 | nsi = talloc_zero(ctx, struct gprs_ns2_inst); |
| 1141 | if (!nsi) |
| 1142 | return NULL; |
| 1143 | |
| 1144 | nsi->cb = cb; |
| 1145 | nsi->cb_data = cb_data; |
| 1146 | INIT_LLIST_HEAD(&nsi->binding); |
| 1147 | INIT_LLIST_HEAD(&nsi->nse); |
| 1148 | |
| 1149 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 1150 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 1151 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 1152 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 1153 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 1154 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 1155 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
| 1156 | nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */ |
| 1157 | |
| 1158 | return nsi; |
| 1159 | } |
| 1160 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1161 | /*! Destroy a NS Instance (including all its NSEs, binds, ...). |
| 1162 | * \param[in] nsi NS instance to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1163 | void gprs_ns2_free(struct gprs_ns2_inst *nsi) |
| 1164 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1165 | if (!nsi) |
| 1166 | return; |
| 1167 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 1168 | gprs_ns2_free_nses(nsi); |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1169 | gprs_ns2_free_binds(nsi); |
Alexander Couzens | 3531504 | 2020-10-10 03:28:17 +0200 | [diff] [blame] | 1170 | |
| 1171 | talloc_free(nsi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1174 | /*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic. |
| 1175 | * \param nsi the instance to modify |
| 1176 | * \param create_nse if NSE can be created on receiving package. SGSN set this. |
| 1177 | * \return 0 on success; negative on error |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1178 | */ |
| 1179 | int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse) |
| 1180 | { |
| 1181 | nsi->create_nse = create_nse; |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1186 | /*! Start the NS-ALIVE FSM in all NS-VCs of given NSE. |
| 1187 | * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1188 | void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse) |
| 1189 | { |
| 1190 | struct gprs_ns2_vc *nsvc; |
| 1191 | OSMO_ASSERT(nse); |
| 1192 | |
| 1193 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1194 | if (nsvc->sns_only) |
| 1195 | continue; |
| 1196 | |
| 1197 | gprs_ns2_vc_fsm_start(nsvc); |
| 1198 | } |
| 1199 | } |
| 1200 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1201 | /*! Set the mode of given bind. |
| 1202 | * \param[in] bind the bind we want to set the mode of |
| 1203 | * \param[in] modde mode to set bind to */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1204 | void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode) |
| 1205 | { |
| 1206 | bind->vc_mode = mode; |
| 1207 | } |
| 1208 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1209 | /*! Destroy a given bind. |
| 1210 | * \param[in] bind the bind we want to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1211 | void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind) |
| 1212 | { |
| 1213 | struct gprs_ns2_vc *nsvc, *tmp; |
| 1214 | if (!bind) |
| 1215 | return; |
| 1216 | |
| 1217 | llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) { |
| 1218 | gprs_ns2_free_nsvc(nsvc); |
| 1219 | } |
| 1220 | |
| 1221 | if (bind->driver->free_bind) |
| 1222 | bind->driver->free_bind(bind); |
| 1223 | |
| 1224 | llist_del(&bind->list); |
| 1225 | talloc_free(bind); |
| 1226 | } |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1227 | |
| 1228 | void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi) |
| 1229 | { |
| 1230 | struct gprs_ns2_vc_bind *bind, *tbind; |
| 1231 | |
| 1232 | llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) { |
| 1233 | gprs_ns2_free_bind(bind); |
| 1234 | } |
| 1235 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1236 | /*! @} */ |