Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_sns.c |
| 2 | * NS Sub-Network Service Protocol implementation |
| 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 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 6 | /* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 7 | * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 8 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 9 | * |
| 10 | * All Rights Reserved |
| 11 | * |
| 12 | * SPDX-License-Identifier: GPL-2.0+ |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | /* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures |
| 30 | * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and |
| 31 | * associated weights. The BSS then uses this to establish a full mesh |
| 32 | * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports. |
| 33 | * |
| 34 | * Known limitation/expectation/bugs: |
| 35 | * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time. |
| 36 | * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs. |
| 37 | * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK. |
| 38 | */ |
| 39 | |
| 40 | #include <errno.h> |
| 41 | |
| 42 | #include <netinet/in.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <stdint.h> |
| 45 | |
| 46 | #include <osmocom/core/fsm.h> |
| 47 | #include <osmocom/core/msgb.h> |
| 48 | #include <osmocom/core/socket.h> |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 49 | #include <osmocom/core/sockaddr_str.h> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 50 | #include <osmocom/gsm/tlv.h> |
| 51 | #include <osmocom/gprs/gprs_msgb.h> |
| 52 | #include <osmocom/gprs/gprs_ns2.h> |
| 53 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 54 | |
| 55 | #include "gprs_ns2_internal.h" |
| 56 | |
| 57 | #define S(x) (1 << (x)) |
| 58 | |
| 59 | enum ns2_sns_type { |
| 60 | IPv4, |
| 61 | IPv6, |
| 62 | }; |
| 63 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 64 | enum ns2_sns_role { |
| 65 | GPRS_SNS_ROLE_BSS, |
| 66 | GPRS_SNS_ROLE_SGSN, |
| 67 | }; |
| 68 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 69 | /* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 70 | enum gprs_sns_bss_state { |
| 71 | GPRS_SNS_ST_UNCONFIGURED, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 72 | GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */ |
| 73 | GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */ |
| 74 | GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 75 | GPRS_SNS_ST_CONFIGURED, |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 76 | GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */ |
| 77 | GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | enum gprs_sns_event { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 81 | GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */ |
| 82 | GPRS_SNS_EV_RX_SIZE, |
| 83 | GPRS_SNS_EV_RX_SIZE_ACK, |
| 84 | GPRS_SNS_EV_RX_CONFIG, |
| 85 | GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */ |
| 86 | GPRS_SNS_EV_RX_CONFIG_ACK, |
| 87 | GPRS_SNS_EV_RX_ADD, |
| 88 | GPRS_SNS_EV_RX_DELETE, |
| 89 | GPRS_SNS_EV_RX_CHANGE_WEIGHT, |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 90 | GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */ |
Harald Welte | 04647e1 | 2021-03-02 18:50:40 +0100 | [diff] [blame] | 91 | GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */ |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 92 | GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */ |
Harald Welte | 04647e1 | 2021-03-02 18:50:40 +0100 | [diff] [blame] | 93 | GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */ |
| 94 | GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const struct value_string gprs_sns_event_names[] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 98 | { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" }, |
| 99 | { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" }, |
| 100 | { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" }, |
| 101 | { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" }, |
| 102 | { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" }, |
| 103 | { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" }, |
| 104 | { GPRS_SNS_EV_RX_ADD, "RX_ADD" }, |
| 105 | { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" }, |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 106 | { GPRS_SNS_EV_RX_ACK, "RX_ACK" }, |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 107 | { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" }, |
| 108 | { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" }, |
| 109 | { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"}, |
| 110 | { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"}, |
| 111 | { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"}, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 112 | { 0, NULL } |
| 113 | }; |
| 114 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 115 | struct sns_endpoint { |
| 116 | struct llist_head list; |
| 117 | struct osmo_sockaddr saddr; |
| 118 | }; |
| 119 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 120 | struct ns2_sns_bind { |
| 121 | struct llist_head list; |
| 122 | struct gprs_ns2_vc_bind *bind; |
| 123 | }; |
| 124 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 125 | struct ns2_sns_elems { |
| 126 | struct gprs_ns_ie_ip4_elem *ip4; |
| 127 | unsigned int num_ip4; |
| 128 | struct gprs_ns_ie_ip6_elem *ip6; |
| 129 | unsigned int num_ip6; |
| 130 | }; |
| 131 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 132 | struct ns2_sns_state { |
| 133 | struct gprs_ns2_nse *nse; |
| 134 | |
| 135 | enum ns2_sns_type ip; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 136 | enum ns2_sns_role role; /* local role: BSS or SGSN */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 137 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 138 | /* holds the list of initial SNS endpoints */ |
| 139 | struct llist_head sns_endpoints; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 140 | /* list of used struct ns2_sns_bind */ |
| 141 | struct llist_head binds; |
| 142 | /* pointer to the bind which was used to initiate the SNS connection */ |
| 143 | struct ns2_sns_bind *initial_bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 144 | /* prevent recursive reselection */ |
| 145 | bool reselection_running; |
| 146 | |
| 147 | /* The current initial SNS endpoints. |
| 148 | * The initial connection will be moved into the NSE |
| 149 | * if configured via SNS. Otherwise it will be removed |
| 150 | * in configured state. */ |
| 151 | struct sns_endpoint *initial; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 152 | /* all SNS PDU will be sent over this nsvc */ |
| 153 | struct gprs_ns2_vc *sns_nsvc; |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 154 | /* timer N */ |
| 155 | int N; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 156 | /* true if at least one nsvc is alive */ |
| 157 | bool alive; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 158 | |
| 159 | /* local configuration to send to the remote end */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 160 | struct ns2_sns_elems local; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 161 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 162 | /* remote configuration as received */ |
| 163 | struct ns2_sns_elems remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 164 | |
| 165 | /* local configuration about our capabilities in terms of connections to |
| 166 | * remote (SGSN) side */ |
| 167 | size_t num_max_nsvcs; |
| 168 | size_t num_max_ip4_remote; |
| 169 | size_t num_max_ip6_remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi) |
| 173 | { |
| 174 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 175 | return gss->nse; |
| 176 | } |
| 177 | |
| 178 | /* helper function to compute the sum of all (data or signaling) weights */ |
| 179 | static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num, |
| 180 | bool data_weight) |
| 181 | { |
| 182 | unsigned int i; |
| 183 | int weight_sum = 0; |
| 184 | |
| 185 | for (i = 0; i < num; i++) { |
| 186 | if (data_weight) |
| 187 | weight_sum += ip4[i].data_weight; |
| 188 | else |
| 189 | weight_sum += ip4[i].sig_weight; |
| 190 | } |
| 191 | return weight_sum; |
| 192 | } |
| 193 | #define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true) |
| 194 | #define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false) |
| 195 | |
| 196 | /* helper function to compute the sum of all (data or signaling) weights */ |
| 197 | static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num, |
| 198 | bool data_weight) |
| 199 | { |
| 200 | unsigned int i; |
| 201 | int weight_sum = 0; |
| 202 | |
| 203 | for (i = 0; i < num; i++) { |
| 204 | if (data_weight) |
| 205 | weight_sum += ip6[i].data_weight; |
| 206 | else |
| 207 | weight_sum += ip6[i].sig_weight; |
| 208 | } |
| 209 | return weight_sum; |
| 210 | } |
| 211 | #define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true) |
| 212 | #define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false) |
| 213 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 214 | static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight) |
| 215 | { |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 216 | return ip4_weight_sum(nss->remote.ip4, nss->remote.num_ip4, data_weight) + |
| 217 | ip6_weight_sum(nss->remote.ip6, nss->remote.num_ip6, data_weight); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 218 | } |
| 219 | #define nss_weight_sum_data(nss) nss_weight_sum(nss, true) |
| 220 | #define nss_weight_sum_sig(nss) nss_weight_sum(nss, false) |
| 221 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 222 | static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse, |
| 223 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 224 | { |
| 225 | struct osmo_sockaddr sa; |
| 226 | /* copy over. Both data structures use network byte order */ |
| 227 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 228 | sa.u.sin.sin_port = ip4->udp_port; |
| 229 | sa.u.sin.sin_family = AF_INET; |
| 230 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 231 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse, |
| 235 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 236 | { |
| 237 | struct osmo_sockaddr sa; |
| 238 | /* copy over. Both data structures use network byte order */ |
| 239 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 240 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 241 | sa.u.sin6.sin6_family = AF_INET; |
| 242 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 243 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 246 | /*! Return the initial SNS remote socket address |
| 247 | * \param nse NS Entity |
| 248 | * \return address of the initial SNS connection; NULL in case of error |
| 249 | */ |
| 250 | const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse) |
| 251 | { |
| 252 | struct ns2_sns_state *gss; |
| 253 | |
| 254 | if (!nse->bss_sns_fi) |
| 255 | return NULL; |
| 256 | |
| 257 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 258 | return &gss->initial->saddr; |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 261 | /*! called when a nsvc is beeing freed or the nsvc became dead */ |
| 262 | void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 263 | { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 264 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 265 | struct gprs_ns2_vc *tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 266 | struct osmo_fsm_inst *fi = nse->bss_sns_fi; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 267 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 268 | |
| 269 | if (!fi) |
| 270 | return; |
| 271 | |
| 272 | gss = (struct ns2_sns_state *) fi->priv; |
| 273 | if (nsvc != gss->sns_nsvc) |
| 274 | return; |
| 275 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 276 | gss->sns_nsvc = NULL; |
| 277 | if (gss->alive) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 278 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 279 | if (ns2_vc_is_unblocked(tmp)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 280 | gss->sns_nsvc = tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 281 | return; |
| 282 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 283 | } |
| 284 | } else { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 285 | /* the SNS is waiting for its first NS-VC to come up |
| 286 | * choose any other nsvc */ |
| 287 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 288 | if (nsvc != tmp) { |
| 289 | gss->sns_nsvc = tmp; |
| 290 | return; |
| 291 | } |
| 292 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 293 | } |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 294 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 295 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 298 | static void ns2_clear_elems(struct ns2_sns_elems *elems) |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 299 | { |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 300 | TALLOC_FREE(elems->ip4); |
| 301 | TALLOC_FREE(elems->ip6); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 302 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 303 | elems->num_ip4 = 0; |
| 304 | elems->num_ip6 = 0; |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 307 | static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote, |
| 308 | uint8_t sig_weight, uint8_t data_weight) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 309 | { |
| 310 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 311 | struct gprs_ns2_vc *nsvc; |
| 312 | struct gprs_ns2_vc_bind *bind; |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 313 | |
| 314 | /* for every bind, create a connection if bind type == IP */ |
| 315 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 316 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 317 | continue; |
| 318 | /* ignore failed connection */ |
| 319 | nsvc = gprs_ns2_ip_connect_inactive(bind, |
| 320 | remote, |
| 321 | nse, 0); |
| 322 | if (!nsvc) { |
| 323 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n"); |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | nsvc->sig_weight = sig_weight; |
| 328 | nsvc->data_weight = data_weight; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi, |
| 333 | struct gprs_ns2_nse *nse, |
| 334 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 335 | { |
Alexander Couzens | c068d86 | 2020-10-12 04:11:51 +0200 | [diff] [blame] | 336 | struct osmo_sockaddr remote = { }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 337 | /* copy over. Both data structures use network byte order */ |
| 338 | remote.u.sin.sin_family = AF_INET; |
| 339 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 340 | remote.u.sin.sin_port = ip4->udp_port; |
| 341 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 342 | ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi, |
| 346 | struct gprs_ns2_nse *nse, |
| 347 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 348 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 349 | struct osmo_sockaddr remote = {}; |
| 350 | /* copy over. Both data structures use network byte order */ |
| 351 | remote.u.sin6.sin6_family = AF_INET6; |
| 352 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 353 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 354 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 355 | ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 358 | static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse, |
| 359 | struct gprs_ns2_vc_bind *bind, |
| 360 | const struct osmo_sockaddr *remote) |
| 361 | { |
| 362 | struct gprs_ns2_vc *nsvc; |
| 363 | |
| 364 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 365 | if (nsvc->bind != bind) |
| 366 | continue; |
| 367 | |
| 368 | if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc))) |
| 369 | return nsvc; |
| 370 | } |
| 371 | return NULL; |
| 372 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 373 | |
| 374 | static int create_missing_nsvcs(struct osmo_fsm_inst *fi) |
| 375 | { |
| 376 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 377 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 378 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 379 | struct ns2_sns_bind *sbind; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 380 | struct osmo_sockaddr remote = { }; |
| 381 | unsigned int i; |
| 382 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 383 | /* iterate over all remote IPv4 endpoints */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 384 | for (i = 0; i < gss->remote.num_ip4; i++) { |
| 385 | const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 386 | |
| 387 | remote.u.sin.sin_family = AF_INET; |
| 388 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 389 | remote.u.sin.sin_port = ip4->udp_port; |
| 390 | |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 391 | /* iterate over all local binds within this SNS */ |
| 392 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 393 | struct gprs_ns2_vc_bind *bind = sbind->bind; |
| 394 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 395 | /* we only care about UDP binds */ |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 396 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 397 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 398 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 399 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 400 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 401 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 402 | if (!nsvc) { |
| 403 | /* TODO: add to a list to send back a NS-STATUS */ |
| 404 | continue; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* update data / signalling weight */ |
| 409 | nsvc->data_weight = ip4->data_weight; |
| 410 | nsvc->sig_weight = ip4->sig_weight; |
| 411 | nsvc->sns_only = false; |
| 412 | } |
| 413 | } |
| 414 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 415 | /* iterate over all remote IPv4 endpoints */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 416 | for (i = 0; i < gss->remote.num_ip6; i++) { |
| 417 | const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 418 | |
| 419 | remote.u.sin6.sin6_family = AF_INET6; |
| 420 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 421 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 422 | |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 423 | /* iterate over all local binds within this SNS */ |
| 424 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 425 | struct gprs_ns2_vc_bind *bind = sbind->bind; |
| 426 | |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 427 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 428 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 429 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 430 | /* we only care about UDP binds */ |
| 431 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 432 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 433 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 434 | if (!nsvc) { |
| 435 | /* TODO: add to a list to send back a NS-STATUS */ |
| 436 | continue; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* update data / signalling weight */ |
| 441 | nsvc->data_weight = ip6->data_weight; |
| 442 | nsvc->sig_weight = ip6->sig_weight; |
| 443 | nsvc->sns_only = false; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Add a given remote IPv4 element to gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 452 | static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 453 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 454 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 455 | /* check for duplicates */ |
Alexander Couzens | cc56ddc | 2021-06-06 03:33:35 +0200 | [diff] [blame^] | 456 | for (unsigned int i = 0; i < elems->num_ip4; i++) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 457 | if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 458 | continue; |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 459 | return -1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 462 | elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem, |
| 463 | elems->num_ip4+1); |
| 464 | elems->ip4[elems->num_ip4] = *ip4; |
| 465 | elems->num_ip4 += 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /* Remove a given remote IPv4 element from gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 470 | static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 471 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 472 | { |
| 473 | unsigned int i; |
| 474 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 475 | for (i = 0; i < elems->num_ip4; i++) { |
| 476 | if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 477 | continue; |
| 478 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 479 | memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1); |
| 480 | elems->num_ip4 -= 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | /* update the weights for specified remote IPv4 */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 487 | static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 488 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 489 | { |
| 490 | unsigned int i; |
| 491 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 492 | for (i = 0; i < elems->num_ip4; i++) { |
| 493 | if (elems->ip4[i].ip_addr != ip4->ip_addr || |
| 494 | elems->ip4[i].udp_port != ip4->udp_port) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 495 | continue; |
| 496 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 497 | elems->ip4[i].sig_weight = ip4->sig_weight; |
| 498 | elems->ip4[i].data_weight = ip4->data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | /* Add a given remote IPv6 element to gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 505 | static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 506 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 507 | { |
Alexander Couzens | cc56ddc | 2021-06-06 03:33:35 +0200 | [diff] [blame^] | 508 | /* check for duplicates */ |
| 509 | for (unsigned int i = 0; i < elems->num_ip6; i++) { |
| 510 | if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) || |
| 511 | elems->ip6[i].udp_port != ip6->udp_port) |
| 512 | continue; |
| 513 | return -1; |
| 514 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 515 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 516 | elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem, |
| 517 | elems->num_ip6+1); |
| 518 | elems->ip6[elems->num_ip6] = *ip6; |
| 519 | elems->num_ip6 += 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /* Remove a given remote IPv6 element from gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 524 | static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 525 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 526 | { |
| 527 | unsigned int i; |
| 528 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 529 | for (i = 0; i < elems->num_ip6; i++) { |
| 530 | if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 531 | continue; |
| 532 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 533 | memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1); |
| 534 | elems->num_ip6 -= 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | /* update the weights for specified remote IPv6 */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 541 | static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 542 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 543 | { |
| 544 | unsigned int i; |
| 545 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 546 | for (i = 0; i < elems->num_ip6; i++) { |
| 547 | if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) || |
| 548 | elems->ip6[i].udp_port != ip6->udp_port) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 549 | continue; |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 550 | elems->ip6[i].sig_weight = ip6->sig_weight; |
| 551 | elems->ip6[i].data_weight = ip6->data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4, const struct gprs_ns_ie_ip6_elem *ip6) |
| 558 | { |
| 559 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 560 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 561 | struct gprs_ns2_vc *nsvc; |
| 562 | struct osmo_sockaddr sa = {}; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 563 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 564 | uint8_t new_signal; |
| 565 | uint8_t new_data; |
| 566 | |
| 567 | /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the |
| 568 | * signalling weights of all the peer IP endpoints configured for this NSE is |
| 569 | * equal to zero or if the resulting sum of the data weights of all the peer IP |
| 570 | * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an |
| 571 | * SNS-ACK PDU with a cause code of "Invalid weights". */ |
| 572 | |
| 573 | if (ip4) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 574 | if (update_ip4_elem(gss, &gss->remote, ip4)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 575 | return -NS_CAUSE_UNKN_IP_EP; |
| 576 | |
| 577 | /* copy over. Both data structures use network byte order */ |
| 578 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 579 | sa.u.sin.sin_port = ip4->udp_port; |
| 580 | sa.u.sin.sin_family = AF_INET; |
| 581 | new_signal = ip4->sig_weight; |
| 582 | new_data = ip4->data_weight; |
| 583 | } else if (ip6) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 584 | if (update_ip6_elem(gss, &gss->remote, ip6)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 585 | return -NS_CAUSE_UNKN_IP_EP; |
| 586 | |
| 587 | /* copy over. Both data structures use network byte order */ |
| 588 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 589 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 590 | sa.u.sin6.sin6_family = AF_INET6; |
| 591 | new_signal = ip6->sig_weight; |
| 592 | new_data = ip6->data_weight; |
| 593 | } else { |
| 594 | OSMO_ASSERT(false); |
| 595 | } |
| 596 | |
| 597 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 598 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 599 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 600 | OSMO_ASSERT(remote); |
| 601 | |
| 602 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 603 | continue; |
| 604 | |
| 605 | LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n", |
| 606 | gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data, |
| 607 | nsvc->sig_weight, new_signal); |
| 608 | |
| 609 | nsvc->data_weight = new_data; |
| 610 | nsvc->sig_weight = new_signal; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int do_sns_delete(struct osmo_fsm_inst *fi, |
| 617 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 618 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 619 | { |
| 620 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 621 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 622 | struct gprs_ns2_vc *nsvc, *tmp; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 623 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 624 | struct osmo_sockaddr sa = {}; |
| 625 | |
| 626 | if (ip4) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 627 | if (remove_ip4_elem(gss, &gss->remote, ip4) < 0) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 628 | return -NS_CAUSE_UNKN_IP_EP; |
| 629 | /* copy over. Both data structures use network byte order */ |
| 630 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 631 | sa.u.sin.sin_port = ip4->udp_port; |
| 632 | sa.u.sin.sin_family = AF_INET; |
| 633 | } else if (ip6) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 634 | if (remove_ip6_elem(gss, &gss->remote, ip6)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 635 | return -NS_CAUSE_UNKN_IP_EP; |
| 636 | |
| 637 | /* copy over. Both data structures use network byte order */ |
| 638 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 639 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 640 | sa.u.sin6.sin6_family = AF_INET6; |
| 641 | } else { |
| 642 | OSMO_ASSERT(false); |
| 643 | } |
| 644 | |
| 645 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 646 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 647 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 648 | OSMO_ASSERT(remote); |
| 649 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 650 | continue; |
| 651 | |
| 652 | LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc)); |
| 653 | gprs_ns2_free_nsvc(nsvc); |
| 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int do_sns_add(struct osmo_fsm_inst *fi, |
| 660 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 661 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 662 | { |
| 663 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 664 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 665 | struct gprs_ns2_vc *nsvc; |
| 666 | int rc = 0; |
| 667 | |
| 668 | /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints |
| 669 | * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send |
| 670 | * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */ |
| 671 | switch (gss->ip) { |
| 672 | case IPv4: |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 673 | if (gss->remote.num_ip4 >= gss->num_max_ip4_remote) |
| 674 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 675 | /* TODO: log message duplicate */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 676 | rc = add_ip4_elem(gss, &gss->remote, ip4); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 677 | break; |
| 678 | case IPv6: |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 679 | if (gss->remote.num_ip6 >= gss->num_max_ip6_remote) |
| 680 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 681 | /* TODO: log message duplicate */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 682 | rc = add_ip6_elem(gss, &gss->remote, ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 683 | break; |
| 684 | default: |
| 685 | /* the gss->ip is initialized with the bss */ |
| 686 | OSMO_ASSERT(false); |
| 687 | } |
| 688 | |
| 689 | if (rc) |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 690 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 691 | |
| 692 | /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the |
| 693 | * NSE shall send an SNS-ACK PDU with the cause code "Protocol error - |
| 694 | * unspecified" */ |
| 695 | switch (gss->ip) { |
| 696 | case IPv4: |
| 697 | nsvc = nsvc_by_ip4_elem(nse, ip4); |
| 698 | if (nsvc) { |
| 699 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 700 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 701 | } |
| 702 | |
| 703 | /* TODO: failure case */ |
| 704 | ns2_nsvc_create_ip4(fi, nse, ip4); |
| 705 | break; |
| 706 | case IPv6: |
| 707 | nsvc = nsvc_by_ip6_elem(nse, ip6); |
| 708 | if (nsvc) { |
| 709 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 710 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 711 | } |
| 712 | |
| 713 | /* TODO: failure case */ |
| 714 | ns2_nsvc_create_ip6(fi, nse, ip6); |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 724 | static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 725 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 726 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 727 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 728 | /* empty state - SNS Select will start by ns2_sns_st_all_action() */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 731 | static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 732 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 733 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 734 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 735 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 736 | struct tlv_parsed *tp = NULL; |
| 737 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 738 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 739 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 740 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 741 | case GPRS_SNS_EV_RX_SIZE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 742 | tp = data; |
| 743 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 744 | LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n", |
| 745 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 746 | /* TODO: What to do? */ |
| 747 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 748 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 749 | nsi->timeout[NS_TOUT_TSNS_PROV], 2); |
| 750 | } |
| 751 | break; |
| 752 | default: |
| 753 | OSMO_ASSERT(0); |
| 754 | } |
| 755 | } |
| 756 | |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 757 | static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype) |
| 758 | { |
| 759 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 760 | struct ns2_sns_bind *sbind; |
| 761 | int count = 0; |
| 762 | |
| 763 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 764 | const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 765 | if (!sa) |
| 766 | continue; |
| 767 | |
| 768 | switch (stype) { |
| 769 | case IPv4: |
| 770 | if (sa->u.sas.ss_family == AF_INET) |
| 771 | count++; |
| 772 | break; |
| 773 | case IPv6: |
| 774 | if (sa->u.sas.ss_family == AF_INET6) |
| 775 | count++; |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | return count; |
| 780 | } |
| 781 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 782 | static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 783 | { |
| 784 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 785 | struct gprs_ns_ie_ip4_elem *ip4_elems; |
| 786 | struct gprs_ns_ie_ip6_elem *ip6_elems; |
| 787 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 788 | struct ns2_sns_bind *sbind; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 789 | const struct osmo_sockaddr *remote; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 790 | const struct osmo_sockaddr *sa; |
| 791 | struct osmo_sockaddr local; |
| 792 | int count; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 793 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 794 | ns2_clear_elems(&gss->local); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 795 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 796 | /* no initial available */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 797 | if (gss->role == GPRS_SNS_ROLE_BSS) { |
| 798 | if (!gss->initial) |
| 799 | return; |
| 800 | remote = &gss->initial->saddr; |
| 801 | } else |
| 802 | remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 803 | |
| 804 | /* count how many bindings are available (only UDP binds) */ |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 805 | count = llist_count(&gss->binds); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 806 | if (count == 0) { |
Harald Welte | 0599287 | 2021-03-04 15:49:21 +0100 | [diff] [blame] | 807 | LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n"); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 811 | switch (gss->ip) { |
| 812 | case IPv4: |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 813 | ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 814 | if (!ip4_elems) |
| 815 | return; |
| 816 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 817 | gss->local.ip4 = ip4_elems; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 818 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 819 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 820 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 821 | if (!sa) |
| 822 | continue; |
| 823 | |
| 824 | if (sa->u.sas.ss_family != AF_INET) |
| 825 | continue; |
| 826 | |
| 827 | /* check if this is an specific bind */ |
| 828 | if (sa->u.sin.sin_addr.s_addr == 0) { |
| 829 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 830 | continue; |
| 831 | |
| 832 | ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr; |
| 833 | } else { |
| 834 | ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr; |
| 835 | } |
| 836 | |
| 837 | ip4_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 838 | ip4_elems->sig_weight = bind->sns_sig_weight; |
| 839 | ip4_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 840 | ip4_elems++; |
| 841 | } |
| 842 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 843 | gss->local.num_ip4 = count; |
| 844 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 845 | break; |
| 846 | case IPv6: |
| 847 | /* IPv6 */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 848 | ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 849 | if (!ip6_elems) |
| 850 | return; |
| 851 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 852 | gss->local.ip6 = ip6_elems; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 853 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 854 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 855 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 856 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 857 | if (!sa) |
| 858 | continue; |
| 859 | |
| 860 | if (sa->u.sas.ss_family != AF_INET6) |
| 861 | continue; |
| 862 | |
| 863 | /* check if this is an specific bind */ |
| 864 | if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) { |
| 865 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 866 | continue; |
| 867 | |
| 868 | ip6_elems->ip_addr = local.u.sin6.sin6_addr; |
| 869 | } else { |
| 870 | ip6_elems->ip_addr = sa->u.sin6.sin6_addr; |
| 871 | } |
| 872 | |
| 873 | ip6_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 874 | ip6_elems->sig_weight = bind->sns_sig_weight; |
| 875 | ip6_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 876 | |
| 877 | ip6_elems++; |
| 878 | } |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 879 | gss->local.num_ip6 = count; |
| 880 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 881 | break; |
| 882 | } |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Alexander Couzens | 6608ce9 | 2021-04-26 20:39:46 +0200 | [diff] [blame] | 885 | static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss) |
| 886 | { |
| 887 | /* take the first bind or take the next bind */ |
| 888 | if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds) |
| 889 | gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list); |
| 890 | else |
| 891 | gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list); |
| 892 | } |
| 893 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 894 | /* setup all dynamic SNS settings, create a new nsvc and send the SIZE */ |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 895 | static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 896 | { |
| 897 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 898 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 899 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 900 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 901 | /* on a generic failure, the timer callback will recover */ |
| 902 | if (old_state != GPRS_SNS_ST_UNCONFIGURED) |
| 903 | ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE); |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 904 | if (old_state != GPRS_SNS_ST_BSS_SIZE) |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 905 | gss->N = 0; |
| 906 | |
| 907 | gss->alive = false; |
| 908 | |
| 909 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 6608ce9 | 2021-04-26 20:39:46 +0200 | [diff] [blame] | 910 | ns2_sns_choose_next_bind(gss); |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 911 | |
| 912 | /* setup the NSVC */ |
| 913 | if (!gss->sns_nsvc) { |
| 914 | struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind; |
| 915 | struct osmo_sockaddr *remote = &gss->initial->saddr; |
| 916 | gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote); |
| 917 | if (!gss->sns_nsvc) |
| 918 | return; |
Harald Welte | c962a2e | 2021-03-05 08:09:08 +0100 | [diff] [blame] | 919 | /* A pre-configured endpoint shall not be used for NSE data or signalling traffic |
| 920 | * (with the exception of Size and Configuration procedures) unless it is configured |
| 921 | * by the SGSN using the auto-configuration procedures */ |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 922 | gss->sns_nsvc->sns_only = true; |
| 923 | } |
| 924 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 925 | if (gss->num_max_ip4_remote > 0) |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 926 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 927 | else |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 928 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 931 | static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 932 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 933 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 934 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 935 | struct tlv_parsed *tp = NULL; |
| 936 | |
| 937 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 938 | |
| 939 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 940 | case GPRS_SNS_EV_RX_CONFIG_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 941 | tp = (struct tlv_parsed *) data; |
| 942 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 943 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n", |
| 944 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 945 | /* TODO: What to do? */ |
| 946 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 947 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 948 | } |
| 949 | break; |
| 950 | default: |
| 951 | OSMO_ASSERT(0); |
| 952 | } |
| 953 | } |
| 954 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 955 | static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 956 | { |
| 957 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 958 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 959 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 960 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 961 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 962 | gss->N = 0; |
| 963 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 964 | /* Transmit SNS-CONFIG */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 965 | switch (gss->ip) { |
| 966 | case IPv4: |
| 967 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 968 | gss->local.ip4, gss->local.num_ip4, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 969 | NULL, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 970 | break; |
| 971 | case IPv6: |
| 972 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 973 | NULL, 0, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 974 | gss->local.ip6, gss->local.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 975 | break; |
| 976 | } |
| 977 | } |
| 978 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 979 | /* calculate the timeout of the configured state. the configured |
| 980 | * state will fail if not at least one NS-VC is alive within X second. |
| 981 | */ |
| 982 | static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi) |
| 983 | { |
| 984 | int secs; |
| 985 | struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi; |
| 986 | secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]; |
| 987 | secs += nsi->timeout[NS_TOUT_TNS_TEST]; |
| 988 | |
| 989 | return secs; |
| 990 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 991 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 992 | /* append the remote endpoints from the parsed TLV array to the ns2_sns_state */ |
| 993 | static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 994 | { |
| 995 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 996 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 997 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 998 | const struct gprs_ns_ie_ip4_elem *v4_list; |
| 999 | unsigned int num_v4; |
| 1000 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1001 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1002 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1003 | if (num_v4 && gss->remote.ip6) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1004 | return -NS_CAUSE_INVAL_NR_IPv4_EP; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1005 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1006 | /* realloc to the new size */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1007 | gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4, |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1008 | struct gprs_ns_ie_ip4_elem, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1009 | gss->remote.num_ip4 + num_v4); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1010 | /* append the new entries to the end of the list */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1011 | memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list)); |
| 1012 | gss->remote.num_ip4 += num_v4; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1013 | |
| 1014 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n", |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1015 | gss->remote.num_ip4); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1016 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1017 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1018 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1019 | const struct gprs_ns_ie_ip6_elem *v6_list; |
| 1020 | unsigned int num_v6; |
| 1021 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1022 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
| 1023 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1024 | if (num_v6 && gss->remote.ip4) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1025 | return -NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1026 | |
| 1027 | /* realloc to the new size */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1028 | gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6, |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1029 | struct gprs_ns_ie_ip6_elem, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1030 | gss->remote.num_ip6 + num_v6); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1031 | /* append the new entries to the end of the list */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1032 | memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list)); |
| 1033 | gss->remote.num_ip6 += num_v6; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1034 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1035 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n", |
| 1036 | gss->remote.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1037 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1038 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1039 | return 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1042 | static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1043 | { |
| 1044 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1045 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1046 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1047 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1048 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1049 | gss->N = 0; |
| 1050 | } |
| 1051 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1052 | static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1053 | { |
| 1054 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1055 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1056 | uint8_t cause; |
| 1057 | int rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1058 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1059 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1060 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1061 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1062 | case GPRS_SNS_EV_RX_CONFIG_END: |
| 1063 | case GPRS_SNS_EV_RX_CONFIG: |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1064 | rc = ns_sns_append_remote_eps(fi, data); |
| 1065 | if (rc < 0) { |
| 1066 | cause = -rc; |
| 1067 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1068 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1069 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1070 | } |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1071 | if (event == GPRS_SNS_EV_RX_CONFIG_END) { |
| 1072 | /* check if sum of data / sig weights == 0 */ |
| 1073 | if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) { |
| 1074 | cause = NS_CAUSE_INVAL_WEIGH; |
| 1075 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1076 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1077 | return; |
| 1078 | } |
| 1079 | create_missing_nsvcs(fi); |
| 1080 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1081 | /* start the test procedure on ALL NSVCs! */ |
| 1082 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 1083 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0); |
| 1084 | } else { |
| 1085 | /* just send CONFIG-ACK */ |
| 1086 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1087 | osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1088 | } |
| 1089 | break; |
| 1090 | default: |
| 1091 | OSMO_ASSERT(0); |
| 1092 | } |
| 1093 | } |
| 1094 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1095 | /* called when receiving GPRS_SNS_EV_RX_ADD in state configure */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1096 | static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi, |
| 1097 | struct ns2_sns_state *gss, |
| 1098 | struct tlv_parsed *tp) |
| 1099 | { |
| 1100 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1101 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1102 | int num_v4 = 0, num_v6 = 0; |
| 1103 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1104 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1105 | int rc = 0; |
| 1106 | |
| 1107 | /* TODO: refactor EV_ADD/CHANGE/REMOVE by |
| 1108 | * check uniqueness within the lists (no doublicate entries) |
| 1109 | * check not-known-by-us and sent back a list of unknown/known values |
| 1110 | * (abnormal behaviour according to 48.016) |
| 1111 | */ |
| 1112 | |
| 1113 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1114 | if (gss->ip == IPv4) { |
| 1115 | if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1116 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1117 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1122 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1123 | for (i = 0; i < num_v4; i++) { |
| 1124 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1125 | rc = do_sns_add(fi, &v4_list[i], NULL); |
| 1126 | if (rc < 0) { |
| 1127 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1128 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1129 | do_sns_delete(fi, &v4_list[j], NULL); |
| 1130 | cause = -rc; |
| 1131 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | } else { /* IPv6 */ |
| 1136 | if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1137 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1138 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1143 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1144 | for (i = 0; i < num_v6; i++) { |
| 1145 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1146 | rc = do_sns_add(fi, NULL, &v6_list[i]); |
| 1147 | if (rc < 0) { |
| 1148 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1149 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1150 | do_sns_delete(fi, NULL, &v6_list[j]); |
| 1151 | cause = -rc; |
| 1152 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1153 | break; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */ |
| 1159 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1160 | } |
| 1161 | |
| 1162 | static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi, |
| 1163 | struct ns2_sns_state *gss, |
| 1164 | struct tlv_parsed *tp) |
| 1165 | { |
| 1166 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1167 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1168 | int num_v4 = 0, num_v6 = 0; |
| 1169 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1170 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1171 | int rc = 0; |
| 1172 | |
| 1173 | /* TODO: split up delete into v4 + v6 |
| 1174 | * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa |
| 1175 | * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time |
| 1176 | */ |
| 1177 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1178 | if (gss->ip == IPv4) { |
| 1179 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1180 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1181 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1182 | for ( i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1183 | rc = do_sns_delete(fi, &v4_list[i], NULL); |
| 1184 | if (rc < 0) { |
| 1185 | cause = -rc; |
| 1186 | /* continue to delete others */ |
| 1187 | } |
| 1188 | } |
| 1189 | if (cause != 0xff) { |
| 1190 | /* TODO: create list of not-deleted and return it */ |
| 1191 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) { |
| 1196 | /* delete all NS-VCs for given IPv4 address */ |
| 1197 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1198 | struct gprs_ns_ie_ip4_elem *ip4_remote; |
| 1199 | uint32_t ip_addr = *(uint32_t *)(ie+1); |
| 1200 | if (ie[0] != 0x01) { /* Address Type != IPv4 */ |
| 1201 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1202 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1203 | return; |
| 1204 | } |
| 1205 | /* make a copy as do_sns_delete() will change the array underneath us */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1206 | ip4_remote = talloc_memdup(fi, gss->remote.ip4, |
| 1207 | gss->remote.num_ip4 * sizeof(*v4_list)); |
| 1208 | for (i = 0; i < gss->remote.num_ip4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1209 | if (ip4_remote[i].ip_addr == ip_addr) { |
| 1210 | rc = do_sns_delete(fi, &ip4_remote[i], NULL); |
| 1211 | if (rc < 0) { |
| 1212 | cause = -rc; |
| 1213 | /* continue to delete others */ |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | talloc_free(ip4_remote); |
| 1218 | if (cause != 0xff) { |
| 1219 | /* TODO: create list of not-deleted and return it */ |
| 1220 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1221 | return; |
| 1222 | } |
| 1223 | } else { |
| 1224 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1225 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1226 | return; |
| 1227 | } |
| 1228 | } else { /* IPv6 */ |
| 1229 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1230 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1231 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1232 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1233 | rc = do_sns_delete(fi, NULL, &v6_list[i]); |
| 1234 | if (rc < 0) { |
| 1235 | cause = -rc; |
| 1236 | /* continue to delete others */ |
| 1237 | } |
| 1238 | } |
| 1239 | if (cause != 0xff) { |
| 1240 | /* TODO: create list of not-deleted and return it */ |
| 1241 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1242 | return; |
| 1243 | } |
| 1244 | } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) { |
| 1245 | /* delete all NS-VCs for given IPv4 address */ |
| 1246 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1247 | struct gprs_ns_ie_ip6_elem *ip6_remote; |
| 1248 | struct in6_addr ip6_addr; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1249 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1250 | if (ie[0] != 0x02) { /* Address Type != IPv6 */ |
| 1251 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1252 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1253 | return; |
| 1254 | } |
| 1255 | memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr)); |
| 1256 | /* make a copy as do_sns_delete() will change the array underneath us */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1257 | ip6_remote = talloc_memdup(fi, gss->remote.ip6, |
| 1258 | gss->remote.num_ip6 * sizeof(*v4_list)); |
| 1259 | for (i = 0; i < gss->remote.num_ip6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1260 | if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) { |
| 1261 | rc = do_sns_delete(fi, NULL, &ip6_remote[i]); |
| 1262 | if (rc < 0) { |
| 1263 | cause = -rc; |
| 1264 | /* continue to delete others */ |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | talloc_free(ip6_remote); |
| 1270 | if (cause != 0xff) { |
| 1271 | /* TODO: create list of not-deleted and return it */ |
| 1272 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1273 | return; |
| 1274 | } |
| 1275 | } else { |
| 1276 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1277 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1278 | return; |
| 1279 | } |
| 1280 | } |
| 1281 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1282 | } |
| 1283 | |
| 1284 | static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi, |
| 1285 | struct ns2_sns_state *gss, |
| 1286 | struct tlv_parsed *tp) |
| 1287 | { |
| 1288 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1289 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1290 | int num_v4 = 0, num_v6 = 0; |
| 1291 | uint8_t trans_id, cause = 0xff; |
| 1292 | int rc = 0; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1293 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1294 | |
| 1295 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1296 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1297 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1298 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1299 | for (i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1300 | rc = do_sns_change_weight(fi, &v4_list[i], NULL); |
| 1301 | if (rc < 0) { |
| 1302 | cause = -rc; |
| 1303 | /* continue to others */ |
| 1304 | } |
| 1305 | } |
| 1306 | if (cause != 0xff) { |
| 1307 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1308 | return; |
| 1309 | } |
| 1310 | } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1311 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1312 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1313 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1314 | rc = do_sns_change_weight(fi, NULL, &v6_list[i]); |
| 1315 | if (rc < 0) { |
| 1316 | cause = -rc; |
| 1317 | /* continue to others */ |
| 1318 | } |
| 1319 | } |
| 1320 | if (cause != 0xff) { |
| 1321 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1322 | return; |
| 1323 | } |
| 1324 | } else { |
| 1325 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1326 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1327 | return; |
| 1328 | } |
| 1329 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1330 | } |
| 1331 | |
| 1332 | static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1333 | { |
| 1334 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1335 | struct tlv_parsed *tp = data; |
| 1336 | |
| 1337 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1338 | case GPRS_SNS_EV_RX_ADD: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1339 | ns2_sns_st_configured_add(fi, gss, tp); |
| 1340 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1341 | case GPRS_SNS_EV_RX_DELETE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1342 | ns2_sns_st_configured_delete(fi, gss, tp); |
| 1343 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1344 | case GPRS_SNS_EV_RX_CHANGE_WEIGHT: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1345 | ns2_sns_st_configured_change(fi, gss, tp); |
| 1346 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1347 | case GPRS_SNS_EV_REQ_NSVC_ALIVE: |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1348 | osmo_timer_del(&fi->timer); |
| 1349 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 1354 | { |
Alexander Couzens | cdb2baa | 2021-04-01 15:29:16 +0200 | [diff] [blame] | 1355 | struct gprs_ns2_vc *nsvc; |
| 1356 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1357 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | cdb2baa | 2021-04-01 15:29:16 +0200 | [diff] [blame] | 1358 | /* NS-VC status updates are only parsed in ST_CONFIGURED. |
| 1359 | * Do an initial check if there are any nsvc alive atm */ |
| 1360 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1361 | if (ns2_vc_is_unblocked(nsvc)) { |
| 1362 | gss->alive = true; |
| 1363 | osmo_timer_del(&fi->timer); |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | |
Alexander Couzens | 53e7009 | 2021-04-06 15:45:47 +0200 | [diff] [blame] | 1368 | /* remove the initial NSVC if the NSVC isn't part of the configuration */ |
| 1369 | if (gss->sns_nsvc->sns_only) |
| 1370 | gprs_ns2_free_nsvc(gss->sns_nsvc); |
| 1371 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1372 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static const struct osmo_fsm_state ns2_sns_bss_states[] = { |
| 1376 | [GPRS_SNS_ST_UNCONFIGURED] = { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1377 | .in_event_mask = 0, /* handled by all_state_action */ |
Alexander Couzens | 0a7c5ee | 2021-04-10 18:20:21 +0200 | [diff] [blame] | 1378 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 1379 | S(GPRS_SNS_ST_BSS_SIZE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1380 | .name = "UNCONFIGURED", |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1381 | .action = ns2_sns_st_bss_unconfigured, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1382 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1383 | [GPRS_SNS_ST_BSS_SIZE] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1384 | .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1385 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1386 | S(GPRS_SNS_ST_BSS_SIZE) | |
| 1387 | S(GPRS_SNS_ST_BSS_CONFIG_BSS), |
| 1388 | .name = "BSS_SIZE", |
| 1389 | .action = ns2_sns_st_bss_size, |
| 1390 | .onenter = ns2_sns_st_bss_size_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1391 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1392 | [GPRS_SNS_ST_BSS_CONFIG_BSS] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1393 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1394 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1395 | S(GPRS_SNS_ST_BSS_CONFIG_BSS) | |
| 1396 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
| 1397 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1398 | .name = "BSS_CONFIG_BSS", |
| 1399 | .action = ns2_sns_st_bss_config_bss, |
| 1400 | .onenter = ns2_sns_st_bss_config_bss_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1401 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1402 | [GPRS_SNS_ST_BSS_CONFIG_SGSN] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1403 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) | |
| 1404 | S(GPRS_SNS_EV_RX_CONFIG_END), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1405 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1406 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1407 | S(GPRS_SNS_ST_CONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1408 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1409 | .name = "BSS_CONFIG_SGSN", |
| 1410 | .action = ns2_sns_st_bss_config_sgsn, |
| 1411 | .onenter = ns2_sns_st_bss_config_sgsn_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1412 | }, |
| 1413 | [GPRS_SNS_ST_CONFIGURED] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1414 | .in_event_mask = S(GPRS_SNS_EV_RX_ADD) | |
| 1415 | S(GPRS_SNS_EV_RX_DELETE) | |
| 1416 | S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) | |
| 1417 | S(GPRS_SNS_EV_REQ_NSVC_ALIVE), |
Alexander Couzens | e03d863 | 2020-12-06 03:31:44 +0100 | [diff] [blame] | 1418 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1419 | S(GPRS_SNS_ST_BSS_SIZE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1420 | .name = "CONFIGURED", |
| 1421 | .action = ns2_sns_st_configured, |
| 1422 | .onenter = ns2_sns_st_configured_onenter, |
| 1423 | }, |
| 1424 | }; |
| 1425 | |
| 1426 | static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi) |
| 1427 | { |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1428 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1429 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1430 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 1431 | |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1432 | gss->N++; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1433 | switch (fi->T) { |
| 1434 | case 1: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1435 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) { |
| 1436 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1437 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1438 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1439 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1440 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1441 | break; |
| 1442 | case 2: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1443 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1444 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: BSS Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1445 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1446 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1447 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1448 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1449 | break; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1450 | case 3: |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1451 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 1452 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1453 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1454 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1455 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1456 | } |
| 1457 | break; |
| 1458 | case 4: |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1459 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1460 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1461 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1462 | } |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1466 | /* common allstate-action for both roles */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1467 | static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1468 | { |
| 1469 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1470 | struct ns2_sns_bind *sbind; |
| 1471 | struct gprs_ns2_vc *nsvc, *nsvc2; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1472 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1473 | switch (event) { |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1474 | case GPRS_SNS_EV_REQ_ADD_BIND: |
| 1475 | sbind = data; |
| 1476 | switch (fi->state) { |
| 1477 | case GPRS_SNS_ST_UNCONFIGURED: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1478 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1479 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1480 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1481 | /* TODO: add the ip4 element to the list */ |
| 1482 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1483 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 1484 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1485 | case GPRS_SNS_ST_CONFIGURED: |
| 1486 | /* TODO: add to SNS-IP procedure queue & add nsvc() */ |
| 1487 | break; |
| 1488 | } |
| 1489 | break; |
| 1490 | case GPRS_SNS_EV_REQ_DELETE_BIND: |
| 1491 | sbind = data; |
| 1492 | switch (fi->state) { |
| 1493 | case GPRS_SNS_ST_UNCONFIGURED: |
| 1494 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1495 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1496 | /* TODO: remove the ip4 element from the list */ |
| 1497 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 1498 | if (nsvc->bind == sbind->bind) { |
| 1499 | gprs_ns2_free_nsvc(nsvc); |
| 1500 | } |
| 1501 | } |
| 1502 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1503 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 1504 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1505 | case GPRS_SNS_ST_CONFIGURED: |
| 1506 | /* TODO: do an delete SNS-IP procedure */ |
| 1507 | /* TODO: remove the ip4 element to the list */ |
| 1508 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 1509 | if (nsvc->bind == sbind->bind) { |
| 1510 | gprs_ns2_free_nsvc(nsvc); |
| 1511 | } |
| 1512 | } |
| 1513 | break; |
| 1514 | } |
| 1515 | /* if this is the last bind, the free_nsvc() will trigger a reselection */ |
| 1516 | talloc_free(sbind); |
| 1517 | break; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1518 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Alexander Couzens | 31d52e1 | 2021-06-05 20:04:04 +0200 | [diff] [blame] | 1521 | /* validate the bss configuration (sns endpoint and binds) |
| 1522 | * - no endpoints -> invalid |
| 1523 | * - no binds -> invalid |
| 1524 | * - only v4 sns endpoints, only v6 binds -> invalid |
| 1525 | * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ... |
| 1526 | */ |
| 1527 | static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss) |
| 1528 | { |
| 1529 | struct ns2_sns_bind *sbind; |
| 1530 | struct sns_endpoint *endpoint; |
| 1531 | const struct osmo_sockaddr *addr; |
| 1532 | int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0; |
| 1533 | bool v4_endpoints = false; |
| 1534 | bool v6_endpoints = false; |
| 1535 | |
| 1536 | if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) |
| 1537 | return 0; |
| 1538 | |
| 1539 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 1540 | addr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 1541 | if (!addr) |
| 1542 | continue; |
| 1543 | switch (addr->u.sa.sa_family) { |
| 1544 | case AF_INET: |
| 1545 | v4_sig += sbind->bind->sns_sig_weight; |
| 1546 | v4_data += sbind->bind->sns_data_weight; |
| 1547 | break; |
| 1548 | case AF_INET6: |
| 1549 | v6_sig += sbind->bind->sns_sig_weight; |
| 1550 | v6_data += sbind->bind->sns_data_weight; |
| 1551 | break; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) { |
| 1556 | switch (endpoint->saddr.u.sa.sa_family) { |
| 1557 | case AF_INET: |
| 1558 | v4_endpoints = true; |
| 1559 | break; |
| 1560 | case AF_INET6: |
| 1561 | v6_endpoints = true; |
| 1562 | break; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data); |
| 1567 | } |
| 1568 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1569 | /* allstate-action for BSS role */ |
| 1570 | static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1571 | { |
| 1572 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1573 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1574 | |
| 1575 | /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */ |
| 1576 | switch (event) { |
| 1577 | case GPRS_SNS_EV_REQ_NO_NSVC: |
| 1578 | /* ignore reselection running */ |
| 1579 | if (gss->reselection_running) |
| 1580 | break; |
| 1581 | |
| 1582 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei); |
| 1583 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
| 1584 | break; |
| 1585 | case GPRS_SNS_EV_REQ_SELECT_ENDPOINT: |
| 1586 | /* tear down previous state |
| 1587 | * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */ |
| 1588 | gss->reselection_running = true; |
| 1589 | gprs_ns2_free_nsvcs(nse); |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 1590 | ns2_clear_elems(&gss->local); |
| 1591 | ns2_clear_elems(&gss->remote); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* Choose the next sns endpoint. */ |
Alexander Couzens | 31d52e1 | 2021-06-05 20:04:04 +0200 | [diff] [blame] | 1594 | if (!ns2_sns_bss_valid_configuration(gss)) { |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1595 | gss->initial = NULL; |
| 1596 | ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS); |
| 1597 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3); |
| 1598 | return; |
| 1599 | } else if (!gss->initial) { |
| 1600 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 1601 | } else if (gss->initial->list.next == &gss->sns_endpoints) { |
| 1602 | /* last entry, continue with first */ |
| 1603 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 1604 | } else { |
| 1605 | /* next element is an entry */ |
| 1606 | gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list); |
| 1607 | } |
| 1608 | |
| 1609 | gss->reselection_running = false; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1610 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1611 | break; |
| 1612 | default: |
| 1613 | ns2_sns_st_all_action(fi, event, data); |
| 1614 | break; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1618 | static struct osmo_fsm gprs_ns2_sns_bss_fsm = { |
| 1619 | .name = "GPRS-NS2-SNS-BSS", |
| 1620 | .states = ns2_sns_bss_states, |
| 1621 | .num_states = ARRAY_SIZE(ns2_sns_bss_states), |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1622 | .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) | |
| 1623 | S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1624 | S(GPRS_SNS_EV_REQ_ADD_BIND) | |
| 1625 | S(GPRS_SNS_EV_REQ_DELETE_BIND), |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1626 | .allstate_action = ns2_sns_st_all_action_bss, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1627 | .cleanup = NULL, |
| 1628 | .timer_cb = ns2_sns_fsm_bss_timer_cb, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1629 | .event_names = gprs_sns_event_names, |
| 1630 | .pre_term = NULL, |
| 1631 | .log_subsys = DLNS, |
| 1632 | }; |
| 1633 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1634 | /*! Allocate an IP-SNS FSM for the BSS side. |
| 1635 | * \param[in] nse NS Entity in which the FSM runs |
| 1636 | * \param[in] id string identifier |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 1637 | * \returns FSM instance on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1638 | struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse, |
| 1639 | const char *id) |
| 1640 | { |
| 1641 | struct osmo_fsm_inst *fi; |
| 1642 | struct ns2_sns_state *gss; |
| 1643 | |
| 1644 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id); |
| 1645 | if (!fi) |
| 1646 | return fi; |
| 1647 | |
| 1648 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 1649 | if (!gss) |
| 1650 | goto err; |
| 1651 | |
| 1652 | fi->priv = gss; |
| 1653 | gss->nse = nse; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1654 | gss->role = GPRS_SNS_ROLE_BSS; |
Harald Welte | 24f4df5 | 2021-03-04 18:02:54 +0100 | [diff] [blame] | 1655 | /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */ |
| 1656 | gss->num_max_ip4_remote = 8192; |
| 1657 | gss->num_max_ip6_remote = 8192; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1658 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1659 | INIT_LLIST_HEAD(&gss->binds); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1660 | |
| 1661 | return fi; |
| 1662 | err: |
| 1663 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 1664 | return NULL; |
| 1665 | } |
| 1666 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1667 | /*! main entry point for receiving SNS messages from the network. |
| 1668 | * \param[in] nsvc NS-VC on which the message was received |
| 1669 | * \param[in] msg message buffer of the IP-SNS message |
| 1670 | * \param[in] tp parsed TLV structure of message |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 1671 | * \returns 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1672 | int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1673 | { |
| 1674 | struct gprs_ns2_nse *nse = nsvc->nse; |
| 1675 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1676 | uint16_t nsei = nsvc->nse->nsei; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1677 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1678 | struct osmo_fsm_inst *fi; |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1679 | int rc = 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1680 | |
| 1681 | if (!nse->bss_sns_fi) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1682 | LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n", |
| 1683 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1684 | rc = -EINVAL; |
| 1685 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1686 | } |
| 1687 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1688 | /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */ |
| 1689 | fi = nse->bss_sns_fi; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1690 | gss = (struct ns2_sns_state *) fi->priv; |
| 1691 | if (!gss->sns_nsvc) |
| 1692 | gss->sns_nsvc = nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1693 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1694 | LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei, |
| 1695 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 1696 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1697 | switch (nsh->pdu_type) { |
| 1698 | case SNS_PDUT_SIZE: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1699 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1700 | break; |
| 1701 | case SNS_PDUT_SIZE_ACK: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1702 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1703 | break; |
| 1704 | case SNS_PDUT_CONFIG: |
| 1705 | if (nsh->data[0] & 0x01) |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1706 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1707 | else |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1708 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1709 | break; |
| 1710 | case SNS_PDUT_CONFIG_ACK: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1711 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1712 | break; |
| 1713 | case SNS_PDUT_ADD: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1714 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1715 | break; |
| 1716 | case SNS_PDUT_DELETE: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1717 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1718 | break; |
| 1719 | case SNS_PDUT_CHANGE_WEIGHT: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1720 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1721 | break; |
| 1722 | case SNS_PDUT_ACK: |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 1723 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1724 | break; |
| 1725 | default: |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1726 | LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei, |
| 1727 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1728 | rc = -EINVAL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1729 | } |
| 1730 | |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 1731 | out: |
| 1732 | msgb_free(msg); |
| 1733 | |
| 1734 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | #include <osmocom/vty/vty.h> |
| 1738 | #include <osmocom/vty/misc.h> |
| 1739 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1740 | static void vty_dump_sns_ip4(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1741 | { |
| 1742 | struct in_addr in = { .s_addr = ip4->ip_addr }; |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1743 | vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1744 | inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE); |
| 1745 | } |
| 1746 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1747 | static void vty_dump_sns_ip6(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1748 | { |
| 1749 | char ip_addr[INET6_ADDRSTRLEN] = {}; |
| 1750 | if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN))) |
| 1751 | strcpy(ip_addr, "Invalid IPv6"); |
| 1752 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1753 | vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1754 | ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE); |
| 1755 | } |
| 1756 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1757 | /*! Dump the IP-SNS state to a vty. |
| 1758 | * \param[in] vty VTY to which the state shall be printed |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1759 | * \param[in] prefix prefix to print at start of each line (typically indenting) |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1760 | * \param[in] nse NS Entity whose IP-SNS state shall be printed |
| 1761 | * \param[in] stats Whether or not statistics shall also be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1762 | void ns2_sns_dump_vty(struct vty *vty, const char *prefix, const struct gprs_ns2_nse *nse, bool stats) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1763 | { |
| 1764 | struct ns2_sns_state *gss; |
| 1765 | unsigned int i; |
| 1766 | |
| 1767 | if (!nse->bss_sns_fi) |
| 1768 | return; |
| 1769 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1770 | vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1771 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 1772 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1773 | vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s", |
| 1774 | prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1775 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1776 | if (gss->local.num_ip4 && gss->remote.num_ip4) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1777 | vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1778 | for (i = 0; i < gss->local.num_ip4; i++) |
| 1779 | vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1780 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1781 | vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1782 | for (i = 0; i < gss->remote.num_ip4; i++) |
| 1783 | vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1784 | } |
| 1785 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1786 | if (gss->local.num_ip6 && gss->remote.num_ip6) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1787 | vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1788 | for (i = 0; i < gss->local.num_ip6; i++) |
| 1789 | vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1790 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1791 | vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1792 | for (i = 0; i < gss->remote.num_ip6; i++) |
| 1793 | vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 1797 | /*! write IP-SNS to a vty |
| 1798 | * \param[in] vty VTY to which the state shall be printed |
| 1799 | * \param[in] nse NS Entity whose IP-SNS state shall be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1800 | void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse) |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 1801 | { |
| 1802 | struct ns2_sns_state *gss; |
| 1803 | struct osmo_sockaddr_str addr_str; |
| 1804 | struct sns_endpoint *endpoint; |
| 1805 | |
| 1806 | if (!nse->bss_sns_fi) |
| 1807 | return; |
| 1808 | |
| 1809 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 1810 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) { |
Vadim Yanitskiy | d8b7003 | 2021-01-05 14:24:09 +0100 | [diff] [blame] | 1811 | /* It's unlikely that an error happens, but let's better be safe. */ |
| 1812 | if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0) |
| 1813 | addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" }; |
Alexander Couzens | 5fa431c | 2021-02-08 23:21:54 +0100 | [diff] [blame] | 1814 | vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE); |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1818 | static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state, |
| 1819 | const struct osmo_sockaddr *saddr) |
| 1820 | { |
| 1821 | struct sns_endpoint *endpoint; |
| 1822 | |
| 1823 | llist_for_each_entry(endpoint, &state->sns_endpoints, list) { |
| 1824 | if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr)) |
| 1825 | return endpoint; |
| 1826 | } |
| 1827 | |
| 1828 | return NULL; |
| 1829 | } |
| 1830 | |
| 1831 | /*! gprs_ns2_sns_add_endpoint |
| 1832 | * \param[in] nse |
| 1833 | * \param[in] sockaddr |
| 1834 | * \return |
| 1835 | */ |
| 1836 | int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse, |
| 1837 | const struct osmo_sockaddr *saddr) |
| 1838 | { |
| 1839 | struct ns2_sns_state *gss; |
| 1840 | struct sns_endpoint *endpoint; |
| 1841 | bool do_selection = false; |
| 1842 | |
| 1843 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1844 | return -EINVAL; |
| 1845 | } |
| 1846 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1847 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1848 | return -EINVAL; |
| 1849 | } |
| 1850 | |
| 1851 | gss = nse->bss_sns_fi->priv; |
| 1852 | |
| 1853 | if (ns2_get_sns_endpoint(gss, saddr)) |
| 1854 | return -EADDRINUSE; |
| 1855 | |
| 1856 | endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint); |
| 1857 | if (!endpoint) |
| 1858 | return -ENOMEM; |
| 1859 | |
| 1860 | endpoint->saddr = *saddr; |
| 1861 | if (llist_empty(&gss->sns_endpoints)) |
| 1862 | do_selection = true; |
| 1863 | |
| 1864 | llist_add_tail(&endpoint->list, &gss->sns_endpoints); |
| 1865 | if (do_selection) |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1866 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | /*! gprs_ns2_sns_del_endpoint |
| 1872 | * \param[in] nse |
| 1873 | * \param[in] sockaddr |
| 1874 | * \return 0 on success, otherwise < 0 |
| 1875 | */ |
| 1876 | int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse, |
| 1877 | const struct osmo_sockaddr *saddr) |
| 1878 | { |
| 1879 | struct ns2_sns_state *gss; |
| 1880 | struct sns_endpoint *endpoint; |
| 1881 | |
| 1882 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1883 | return -EINVAL; |
| 1884 | } |
| 1885 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1886 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | |
| 1890 | gss = nse->bss_sns_fi->priv; |
| 1891 | endpoint = ns2_get_sns_endpoint(gss, saddr); |
| 1892 | if (!endpoint) |
| 1893 | return -ENOENT; |
| 1894 | |
| 1895 | /* if this is an unused SNS endpoint it's done */ |
| 1896 | if (gss->initial != endpoint) { |
| 1897 | llist_del(&endpoint->list); |
| 1898 | talloc_free(endpoint); |
| 1899 | return 0; |
| 1900 | } |
| 1901 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1902 | /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1903 | * and restart SNS SIZE procedure which selects a new initial */ |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1904 | LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed." |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1905 | "Closing all NS-VC and restart SNS-SIZE procedure" |
| 1906 | "with a remaining SNS endpoint.\n"); |
| 1907 | |
| 1908 | /* Continue with the next endpoint in the list. |
| 1909 | * Special case if the endpoint is at the start or end of the list */ |
| 1910 | if (endpoint->list.prev == &gss->sns_endpoints || |
| 1911 | endpoint->list.next == &gss->sns_endpoints) |
| 1912 | gss->initial = NULL; |
| 1913 | else |
| 1914 | gss->initial = llist_entry(endpoint->list.next->prev, |
| 1915 | struct sns_endpoint, |
| 1916 | list); |
| 1917 | |
| 1918 | llist_del(&endpoint->list); |
| 1919 | gprs_ns2_free_nsvcs(nse); |
| 1920 | talloc_free(endpoint); |
| 1921 | |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | /*! gprs_ns2_sns_count |
| 1926 | * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed |
| 1927 | * \return the count of endpoints or < 0 if NSE doesn't contain sns. |
| 1928 | */ |
| 1929 | int gprs_ns2_sns_count(struct gprs_ns2_nse *nse) |
| 1930 | { |
| 1931 | struct ns2_sns_state *gss; |
| 1932 | struct sns_endpoint *endpoint; |
| 1933 | int count = 0; |
| 1934 | |
| 1935 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1936 | return -EINVAL; |
| 1937 | } |
| 1938 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1939 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1940 | return -EINVAL; |
| 1941 | } |
| 1942 | |
| 1943 | gss = nse->bss_sns_fi->priv; |
| 1944 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) |
| 1945 | count++; |
| 1946 | |
| 1947 | return count; |
| 1948 | } |
| 1949 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1950 | void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive) |
| 1951 | { |
| 1952 | struct ns2_sns_state *gss; |
| 1953 | struct gprs_ns2_vc *tmp; |
| 1954 | |
| 1955 | if (!nse->bss_sns_fi) |
| 1956 | return; |
| 1957 | |
| 1958 | gss = nse->bss_sns_fi->priv; |
| 1959 | if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED) |
| 1960 | return; |
| 1961 | |
| 1962 | if (alive == gss->alive) |
| 1963 | return; |
| 1964 | |
| 1965 | /* check if this is the current SNS NS-VC */ |
| 1966 | if (nsvc == gss->sns_nsvc) { |
| 1967 | /* only replace the SNS NS-VC if there are other alive NS-VC. |
| 1968 | * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED |
| 1969 | * and couldn't confirm yet if the NS-VC comes up */ |
| 1970 | if (gss->alive && !alive) |
| 1971 | ns2_sns_replace_nsvc(nsvc); |
| 1972 | } |
| 1973 | |
| 1974 | if (alive) { |
| 1975 | gss->alive = true; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1976 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1977 | } else { |
| 1978 | /* is there at least another alive nsvc? */ |
| 1979 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 1980 | if (ns2_vc_is_unblocked(tmp)) |
| 1981 | return; |
| 1982 | } |
| 1983 | |
| 1984 | /* all NS-VC have failed */ |
| 1985 | gss->alive = false; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1986 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1990 | int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse, |
| 1991 | struct gprs_ns2_vc_bind *bind) |
| 1992 | { |
| 1993 | struct ns2_sns_state *gss; |
| 1994 | struct ns2_sns_bind *tmp; |
| 1995 | |
| 1996 | OSMO_ASSERT(nse->bss_sns_fi); |
| 1997 | gss = nse->bss_sns_fi->priv; |
| 1998 | |
| 1999 | if (!gprs_ns2_is_ip_bind(bind)) { |
| 2000 | return -EINVAL; |
| 2001 | } |
| 2002 | |
| 2003 | if (!llist_empty(&gss->binds)) { |
| 2004 | llist_for_each_entry(tmp, &gss->binds, list) { |
| 2005 | if (tmp->bind == bind) |
| 2006 | return -EALREADY; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | tmp = talloc_zero(gss, struct ns2_sns_bind); |
| 2011 | if (!tmp) |
| 2012 | return -ENOMEM; |
| 2013 | tmp->bind = bind; |
| 2014 | llist_add_tail(&tmp->list, &gss->binds); |
| 2015 | |
| 2016 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp); |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | /* Remove a bind from the SNS. All assosiated NSVC must be removed. */ |
| 2021 | int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse, |
| 2022 | struct gprs_ns2_vc_bind *bind) |
| 2023 | { |
| 2024 | struct ns2_sns_state *gss; |
| 2025 | struct ns2_sns_bind *tmp, *tmp2; |
| 2026 | bool found = false; |
| 2027 | |
| 2028 | if (!nse->bss_sns_fi) |
| 2029 | return -EINVAL; |
| 2030 | |
| 2031 | gss = nse->bss_sns_fi->priv; |
| 2032 | if (gss->initial_bind && gss->initial_bind->bind == bind) { |
| 2033 | if (gss->initial_bind->list.prev == &gss->binds) |
| 2034 | gss->initial_bind = NULL; |
| 2035 | else |
| 2036 | gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list); |
| 2037 | } |
| 2038 | |
| 2039 | llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) { |
| 2040 | if (tmp->bind == bind) { |
| 2041 | llist_del(&tmp->list); |
| 2042 | found = true; |
Alexander Couzens | a35c296 | 2021-04-19 03:30:15 +0200 | [diff] [blame] | 2043 | break; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | if (!found) |
| 2048 | return -ENOENT; |
| 2049 | |
| 2050 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp); |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2054 | /* Update SNS weights for a bind (local endpoint). |
| 2055 | * \param[in] bind the bind which has been updated |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 2056 | */ |
| 2057 | void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind) |
| 2058 | { |
| 2059 | /* TODO: implement weights after binds per sns implemented */ |
| 2060 | } |
| 2061 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2062 | |
| 2063 | |
| 2064 | |
| 2065 | /*********************************************************************** |
| 2066 | * SGSN role |
| 2067 | ***********************************************************************/ |
| 2068 | |
| 2069 | static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2070 | { |
| 2071 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2072 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2073 | /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */ |
| 2074 | } |
| 2075 | |
| 2076 | /* We're waiting for inbound SNS-CONFIG from the BSS */ |
| 2077 | static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2078 | { |
| 2079 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2080 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2081 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2082 | uint8_t cause; |
| 2083 | int rc; |
| 2084 | |
| 2085 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2086 | |
| 2087 | switch (event) { |
| 2088 | case GPRS_SNS_EV_RX_CONFIG: |
| 2089 | case GPRS_SNS_EV_RX_CONFIG_END: |
| 2090 | rc = ns_sns_append_remote_eps(fi, data); |
| 2091 | if (rc < 0) { |
| 2092 | cause = -rc; |
| 2093 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2094 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2095 | return; |
| 2096 | } |
| 2097 | /* only change state if last CONFIG was received */ |
| 2098 | if (event == GPRS_SNS_EV_RX_CONFIG_END) { |
| 2099 | /* ensure sum of data weight / sig weights is > 0 */ |
| 2100 | if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) { |
| 2101 | cause = NS_CAUSE_INVAL_WEIGH; |
| 2102 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2103 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2104 | break; |
| 2105 | } |
| 2106 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2107 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2108 | } else { |
| 2109 | /* just send CONFIG-ACK */ |
| 2110 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2111 | osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0); |
| 2112 | } |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 2118 | { |
| 2119 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2120 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2121 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2122 | /* transmit SGSN-oriented SNS-CONFIG */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2123 | ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4, |
| 2124 | gss->local.ip6, gss->local.num_ip6); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */ |
| 2128 | static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2129 | { |
| 2130 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2131 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2132 | struct tlv_parsed *tp = NULL; |
| 2133 | |
| 2134 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2135 | |
| 2136 | switch (event) { |
| 2137 | case GPRS_SNS_EV_RX_CONFIG_ACK: |
| 2138 | tp = data; |
| 2139 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 2140 | LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n", |
| 2141 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 2142 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2143 | break; |
| 2144 | } |
| 2145 | /* we currently only send one SNS-CONFIG with END FLAG */ |
| 2146 | if (true) { |
| 2147 | create_missing_nsvcs(fi); |
| 2148 | /* start the test procedure on ALL NSVCs! */ |
| 2149 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 2150 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4); |
| 2151 | } |
| 2152 | break; |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | /* SGSN-side SNS state machine */ |
| 2157 | static const struct osmo_fsm_state ns2_sns_sgsn_states[] = { |
| 2158 | [GPRS_SNS_ST_UNCONFIGURED] = { |
| 2159 | .in_event_mask = 0, /* handled by all_state_action */ |
| 2160 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2161 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG), |
| 2162 | .name = "UNCONFIGURED", |
| 2163 | .action = ns2_sns_st_sgsn_unconfigured, |
| 2164 | }, |
| 2165 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = { |
| 2166 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) | |
| 2167 | S(GPRS_SNS_EV_RX_CONFIG_END), |
| 2168 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2169 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) | |
| 2170 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK), |
| 2171 | .name = "SGSN_WAIT_CONFIG", |
| 2172 | .action = ns2_sns_st_sgsn_wait_config, |
| 2173 | }, |
| 2174 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = { |
| 2175 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK), |
| 2176 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2177 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) | |
| 2178 | S(GPRS_SNS_ST_CONFIGURED), |
| 2179 | .name = "SGSN_WAIT_CONFIG_ACK", |
| 2180 | .action = ns2_sns_st_sgsn_wait_config_ack, |
| 2181 | .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter, |
| 2182 | }, |
| 2183 | [GPRS_SNS_ST_CONFIGURED] = { |
| 2184 | .in_event_mask = S(GPRS_SNS_EV_RX_ADD) | |
| 2185 | S(GPRS_SNS_EV_RX_DELETE) | |
| 2186 | S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) | |
| 2187 | S(GPRS_SNS_EV_REQ_NSVC_ALIVE), |
| 2188 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED), |
| 2189 | .name = "CONFIGURED", |
| 2190 | /* shared with BSS side; once configured there's no difference */ |
| 2191 | .action = ns2_sns_st_configured, |
| 2192 | .onenter = ns2_sns_st_configured_onenter, |
| 2193 | }, |
| 2194 | }; |
| 2195 | |
| 2196 | static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi) |
| 2197 | { |
| 2198 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2199 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2200 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2201 | |
| 2202 | gss->N++; |
| 2203 | switch (fi->T) { |
| 2204 | case 3: |
| 2205 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 2206 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei); |
| 2207 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2208 | } else { |
| 2209 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2210 | } |
| 2211 | break; |
| 2212 | case 4: |
| 2213 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei); |
| 2214 | break; |
| 2215 | } |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | |
| 2220 | /* allstate-action for SGSN role */ |
| 2221 | static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2222 | { |
| 2223 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2224 | struct tlv_parsed *tp = NULL; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2225 | size_t num_local_eps, num_remote_eps; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2226 | uint8_t flag; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2227 | uint8_t cause; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2228 | |
| 2229 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2230 | |
| 2231 | switch (event) { |
| 2232 | case GPRS_SNS_EV_RX_SIZE: |
| 2233 | tp = (struct tlv_parsed *) data; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2234 | /* check for mandatory / conditional IEs */ |
| 2235 | if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) || |
| 2236 | !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) { |
| 2237 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
| 2238 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2239 | break; |
| 2240 | } |
| 2241 | if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) && |
| 2242 | !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) { |
| 2243 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2244 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2245 | break; |
| 2246 | } |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2247 | if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2)) |
| 2248 | gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR); |
| 2249 | if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) |
| 2250 | gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR); |
| 2251 | /* decide if we go for IPv4 or IPv6 */ |
| 2252 | if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) { |
| 2253 | gss->ip = IPv6; |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2254 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2255 | num_local_eps = gss->local.num_ip6; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2256 | num_remote_eps = gss->num_max_ip6_remote; |
| 2257 | } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) { |
| 2258 | gss->ip = IPv4; |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2259 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2260 | num_local_eps = gss->local.num_ip4; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2261 | num_remote_eps = gss->num_max_ip4_remote; |
| 2262 | } else { |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2263 | if (gss->local.num_ip4 && !gss->num_max_ip4_remote) |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2264 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 2265 | else |
| 2266 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 2267 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2268 | break; |
| 2269 | } |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2270 | /* ensure number of NS-VCs is sufficient for full mesh */ |
| 2271 | gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC); |
| 2272 | if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) { |
| 2273 | LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, " |
| 2274 | "but BSS supports only %zu maximum NS-VCs\n", num_local_eps, |
| 2275 | num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs); |
| 2276 | cause = NS_CAUSE_INVAL_NR_NS_VC; |
| 2277 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2278 | break; |
| 2279 | } |
| 2280 | /* perform state reset, if requested */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2281 | flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG); |
| 2282 | if (flag & 1) { |
| 2283 | struct gprs_ns2_vc *nsvc, *nsvc2; |
| 2284 | /* clear all state */ |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 2285 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2286 | gss->N = 0; |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 2287 | ns2_clear_elems(&gss->local); |
| 2288 | ns2_clear_elems(&gss->remote); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2289 | llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) { |
| 2290 | if (nsvc == gss->sns_nsvc) { |
| 2291 | /* keep the NSVC we need for SNS, but unconfigure it */ |
| 2292 | nsvc->sig_weight = 0; |
| 2293 | nsvc->data_weight = 0; |
| 2294 | ns2_vc_force_unconfigured(nsvc); |
| 2295 | } else { |
| 2296 | /* free all other NS-VCs */ |
| 2297 | gprs_ns2_free_nsvc(nsvc); |
| 2298 | } |
| 2299 | } |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2300 | ns2_sns_compute_local_ep_from_binds(fi); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2301 | } |
| 2302 | /* send SIZE_ACK */ |
| 2303 | ns2_tx_sns_size_ack(gss->sns_nsvc, NULL); |
| 2304 | /* only wait for SNS-CONFIG in case of Reset flag */ |
| 2305 | if (flag & 1) |
| 2306 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0); |
| 2307 | break; |
| 2308 | default: |
| 2309 | ns2_sns_st_all_action(fi, event, data); |
| 2310 | break; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = { |
| 2315 | .name = "GPRS-NS2-SNS-SGSN", |
| 2316 | .states = ns2_sns_sgsn_states, |
| 2317 | .num_states = ARRAY_SIZE(ns2_sns_sgsn_states), |
| 2318 | .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) | |
| 2319 | S(GPRS_SNS_EV_REQ_NO_NSVC) | |
| 2320 | S(GPRS_SNS_EV_REQ_ADD_BIND) | |
| 2321 | S(GPRS_SNS_EV_REQ_DELETE_BIND), |
| 2322 | .allstate_action = ns2_sns_st_all_action_sgsn, |
| 2323 | .cleanup = NULL, |
| 2324 | .timer_cb = ns2_sns_fsm_sgsn_timer_cb, |
| 2325 | .event_names = gprs_sns_event_names, |
| 2326 | .pre_term = NULL, |
| 2327 | .log_subsys = DLNS, |
| 2328 | }; |
| 2329 | |
| 2330 | /*! Allocate an IP-SNS FSM for the SGSN side. |
| 2331 | * \param[in] nse NS Entity in which the FSM runs |
| 2332 | * \param[in] id string identifier |
| 2333 | * \returns FSM instance on success; NULL on error */ |
| 2334 | struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id) |
| 2335 | { |
| 2336 | struct osmo_fsm_inst *fi; |
| 2337 | struct ns2_sns_state *gss; |
| 2338 | |
| 2339 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id); |
| 2340 | if (!fi) |
| 2341 | return fi; |
| 2342 | |
| 2343 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 2344 | if (!gss) |
| 2345 | goto err; |
| 2346 | |
| 2347 | fi->priv = gss; |
| 2348 | gss->nse = nse; |
| 2349 | gss->role = GPRS_SNS_ROLE_SGSN; |
| 2350 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
| 2351 | INIT_LLIST_HEAD(&gss->binds); |
| 2352 | |
| 2353 | return fi; |
| 2354 | err: |
| 2355 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 2356 | return NULL; |
| 2357 | } |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2362 | /* initialize osmo_ctx on main tread */ |
| 2363 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 2364 | { |
| 2365 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2366 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2367 | } |