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