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