Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_internal.h */ |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <stdbool.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 9 | #include <osmocom/gprs/gprs_ns2.h> |
| 10 | |
| 11 | struct osmo_fsm_inst; |
| 12 | struct tlv_parsed; |
| 13 | struct vty; |
| 14 | |
| 15 | struct gprs_ns2_vc_driver; |
| 16 | struct gprs_ns2_vc_bind; |
| 17 | |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 18 | #define NS_TIMERS_COUNT 10 |
Harald Welte | 3a44d17 | 2020-12-16 12:02:51 +0100 | [diff] [blame] | 19 | #define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries|tsns-prov|tsns-size-retries|tsns-config-retries)" |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 20 | #define NS_TIMERS_HELP \ |
| 21 | "(un)blocking Timer (Tns-block) timeout\n" \ |
| 22 | "(un)blocking Timer (Tns-block) number of retries\n" \ |
| 23 | "Reset Timer (Tns-reset) timeout\n" \ |
| 24 | "Reset Timer (Tns-reset) number of retries\n" \ |
| 25 | "Test Timer (Tns-test) timeout\n" \ |
| 26 | "Alive Timer (Tns-alive) timeout\n" \ |
| 27 | "Alive Timer (Tns-alive) number of retries\n" \ |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 28 | "SNS Provision Timer (Tsns-prov) timeout\n" \ |
| 29 | "SNS Size number of retries\n" \ |
| 30 | "SNS Config number of retries\n" \ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 31 | |
| 32 | /* Educated guess - LLC user payload is 1500 bytes plus possible headers */ |
| 33 | #define NS_ALLOC_SIZE 3072 |
| 34 | #define NS_ALLOC_HEADROOM 20 |
| 35 | |
| 36 | enum ns2_timeout { |
| 37 | NS_TOUT_TNS_BLOCK, |
| 38 | NS_TOUT_TNS_BLOCK_RETRIES, |
| 39 | NS_TOUT_TNS_RESET, |
| 40 | NS_TOUT_TNS_RESET_RETRIES, |
| 41 | NS_TOUT_TNS_TEST, |
| 42 | NS_TOUT_TNS_ALIVE, |
| 43 | NS_TOUT_TNS_ALIVE_RETRIES, |
| 44 | NS_TOUT_TSNS_PROV, |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 45 | NS_TOUT_TSNS_SIZE_RETRIES, |
| 46 | NS_TOUT_TSNS_CONFIG_RETRIES, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | enum nsvc_timer_mode { |
| 50 | /* standard timers */ |
| 51 | NSVC_TIMER_TNS_TEST, |
| 52 | NSVC_TIMER_TNS_ALIVE, |
| 53 | NSVC_TIMER_TNS_RESET, |
| 54 | _NSVC_TIMER_NR, |
| 55 | }; |
| 56 | |
| 57 | enum ns_stat { |
| 58 | NS_STAT_ALIVE_DELAY, |
| 59 | }; |
| 60 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 61 | /*! Osmocom NS2 VC create status */ |
| 62 | enum gprs_ns2_cs { |
| 63 | GPRS_NS2_CS_CREATED, /*!< A NSVC object has been created */ |
| 64 | GPRS_NS2_CS_FOUND, /*!< A NSVC object has been found */ |
| 65 | GPRS_NS2_CS_REJECTED, /*!< Rejected and answered message */ |
| 66 | GPRS_NS2_CS_SKIPPED, /*!< Skipped message */ |
| 67 | GPRS_NS2_CS_ERROR, /*!< Failed to process message */ |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | #define NSE_S_BLOCKED 0x0001 |
| 72 | #define NSE_S_ALIVE 0x0002 |
| 73 | #define NSE_S_RESET 0x0004 |
| 74 | |
| 75 | #define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED") |
| 76 | #define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD") |
| 77 | #define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET") |
| 78 | |
| 79 | /*! An instance of the NS protocol stack */ |
| 80 | struct gprs_ns2_inst { |
| 81 | /*! callback to the user for incoming UNIT DATA IND */ |
| 82 | osmo_prim_cb cb; |
| 83 | |
| 84 | /*! callback data */ |
| 85 | void *cb_data; |
| 86 | |
| 87 | /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */ |
| 88 | struct llist_head binding; |
| 89 | |
| 90 | /*! linked lists of all NSVC in this instance */ |
| 91 | struct llist_head nse; |
| 92 | |
| 93 | /*! create dynamic NSE on receiving packages */ |
| 94 | bool create_nse; |
| 95 | |
| 96 | uint16_t timeout[NS_TIMERS_COUNT]; |
| 97 | |
| 98 | /*! workaround for rate counter until rate counter accepts char str as index */ |
| 99 | uint32_t rate_ctr_idx; |
Harald Welte | 53a2fde | 2020-12-01 22:21:14 +0100 | [diff] [blame] | 100 | |
| 101 | /*! libmnl netlink socket for link state monitoring */ |
| 102 | struct osmo_mnl *linkmon_mnl; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Harald Welte | 53a2fde | 2020-12-01 22:21:14 +0100 | [diff] [blame] | 105 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 106 | /*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */ |
| 107 | struct gprs_ns2_nse { |
| 108 | uint16_t nsei; |
| 109 | |
| 110 | /*! entry back to ns2_inst */ |
| 111 | struct gprs_ns2_inst *nsi; |
| 112 | |
| 113 | /*! llist entry for gprs_ns2_inst */ |
| 114 | struct llist_head list; |
| 115 | |
| 116 | /*! llist head to hold all nsvc */ |
| 117 | struct llist_head nsvc; |
| 118 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 119 | /*! count all active NSVCs with data capabilities */ |
| 120 | int nsvc_data_count; |
| 121 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 122 | /*! true if this NSE was created by VTY or pcu socket) */ |
| 123 | bool persistent; |
| 124 | |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 125 | /*! true if this NSE wasn't yet alive at all. |
| 126 | * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */ |
| 127 | bool first; |
| 128 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 129 | /*! true if this NSE has at least one alive VC */ |
| 130 | bool alive; |
| 131 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 132 | /*! which link-layer are we based on? */ |
| 133 | enum gprs_ns2_ll ll; |
| 134 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 135 | /*! which dialect does this NSE speaks? */ |
| 136 | enum gprs_ns2_dialect dialect; |
| 137 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 138 | struct osmo_fsm_inst *bss_sns_fi; |
| 139 | }; |
| 140 | |
| 141 | /*! Structure representing a single NS-VC */ |
| 142 | struct gprs_ns2_vc { |
| 143 | /*! list of NS-VCs within NSE */ |
| 144 | struct llist_head list; |
| 145 | |
| 146 | /*! list of NS-VCs within bind, bind is the owner! */ |
| 147 | struct llist_head blist; |
| 148 | |
| 149 | /*! pointer to NS Instance */ |
| 150 | struct gprs_ns2_nse *nse; |
| 151 | |
| 152 | /*! pointer to NS VL bind. bind own the memory of this instance */ |
| 153 | struct gprs_ns2_vc_bind *bind; |
| 154 | |
| 155 | /*! true if this NS was created by VTY or pcu socket) */ |
| 156 | bool persistent; |
| 157 | |
| 158 | /*! uniquely identifies NS-VC if VC contains nsvci */ |
| 159 | uint16_t nsvci; |
| 160 | |
| 161 | /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/ |
| 162 | uint8_t sig_weight; |
| 163 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 164 | /*! signalling packet counter for the load sharing function */ |
| 165 | uint8_t sig_counter; |
| 166 | |
| 167 | /*! data weight. 0 = don't use for user data (BVCI != 0) */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 168 | uint8_t data_weight; |
| 169 | |
| 170 | /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */ |
| 171 | void *priv; |
| 172 | |
| 173 | bool nsvci_is_valid; |
| 174 | bool sns_only; |
| 175 | |
| 176 | struct rate_ctr_group *ctrg; |
| 177 | struct osmo_stat_item_group *statg; |
| 178 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 179 | enum gprs_ns2_vc_mode mode; |
| 180 | |
| 181 | struct osmo_fsm_inst *fi; |
| 182 | }; |
| 183 | |
| 184 | /*! Structure repesenting a bind instance. E.g. IPv4 listen port. */ |
| 185 | struct gprs_ns2_vc_bind { |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 186 | /*! unique name */ |
| 187 | const char *name; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 188 | /*! list entry in nsi */ |
| 189 | struct llist_head list; |
| 190 | /*! list of all VC */ |
| 191 | struct llist_head nsvc; |
| 192 | /*! driver private structure */ |
| 193 | void *priv; |
| 194 | /*! a pointer back to the nsi */ |
| 195 | struct gprs_ns2_inst *nsi; |
| 196 | struct gprs_ns2_vc_driver *driver; |
| 197 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 198 | bool accept_ipaccess; |
| 199 | bool accept_sns; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 200 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 201 | /*! which link-layer are we based on? */ |
| 202 | enum gprs_ns2_ll ll; |
| 203 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 204 | /*! send a msg over a VC */ |
| 205 | int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg); |
| 206 | |
| 207 | /*! free the vc priv data */ |
| 208 | void (*free_vc)(struct gprs_ns2_vc *nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 209 | |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 210 | /*! allow to show information for the vty */ |
| 211 | void (*dump_vty)(const struct gprs_ns2_vc_bind *bind, |
| 212 | struct vty *vty, bool stats); |
| 213 | }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 214 | |
| 215 | struct gprs_ns2_vc_driver { |
| 216 | const char *name; |
| 217 | void *priv; |
| 218 | void (*free_bind)(struct gprs_ns2_vc_bind *driver); |
| 219 | }; |
| 220 | |
| 221 | enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind, |
| 222 | struct msgb *msg, |
| 223 | const char *logname, |
| 224 | struct msgb **reject, |
| 225 | struct gprs_ns2_vc **success); |
| 226 | |
Alexander Couzens | ffd49d0 | 2020-09-24 00:47:17 +0200 | [diff] [blame] | 227 | int ns2_recv_vc(struct gprs_ns2_vc *nsvc, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 228 | struct msgb *msg); |
| 229 | |
| 230 | struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, |
| 231 | struct gprs_ns2_nse *nse, |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 232 | bool initiater, |
| 233 | enum gprs_ns2_vc_mode vc_mode); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 234 | |
| 235 | struct msgb *gprs_ns2_msgb_alloc(void); |
| 236 | |
| 237 | void gprs_ns2_sns_dump_vty(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats); |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 238 | void ns2_prim_status_ind(struct gprs_ns2_nse *nse, |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 239 | struct gprs_ns2_vc *nsvc, |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 240 | uint16_t bvci, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 241 | enum gprs_ns2_affecting_cause cause); |
| 242 | void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive); |
| 243 | |
| 244 | /* message */ |
| 245 | int gprs_ns2_validate(struct gprs_ns2_vc *nsvc, |
| 246 | uint8_t pdu_type, |
| 247 | struct msgb *msg, |
| 248 | struct tlv_parsed *tp, |
| 249 | uint8_t *cause); |
| 250 | |
| 251 | /* SNS messages */ |
| 252 | int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause, |
| 253 | const struct gprs_ns_ie_ip4_elem *ip4_elems, |
| 254 | unsigned int num_ip4_elems, |
| 255 | const struct gprs_ns_ie_ip6_elem *ip6_elems, |
| 256 | unsigned int num_ip6_elems); |
| 257 | int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag, |
| 258 | const struct gprs_ns_ie_ip4_elem *ip4_elems, |
| 259 | unsigned int num_ip4_elems, |
| 260 | const struct gprs_ns_ie_ip6_elem *ip6_elems, |
| 261 | unsigned int num_ip6_elems); |
| 262 | int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause); |
| 263 | int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc, |
| 264 | int ip4_ep_nr, int ip6_ep_nr); |
| 265 | int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause); |
| 266 | |
| 267 | /* transmit message over a VC */ |
| 268 | int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause); |
| 269 | int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc); |
| 270 | |
| 271 | int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause); |
| 272 | int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc); |
| 273 | |
| 274 | int ns2_tx_unblock(struct gprs_ns2_vc *nsvc); |
| 275 | int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc); |
| 276 | |
| 277 | int ns2_tx_alive(struct gprs_ns2_vc *nsvc); |
| 278 | int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc); |
| 279 | |
| 280 | int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc, |
| 281 | uint16_t bvci, uint8_t sducontrol, |
| 282 | struct msgb *msg); |
| 283 | |
| 284 | int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause, |
| 285 | uint16_t bvci, struct msgb *orig_msg); |
| 286 | |
| 287 | /* driver */ |
| 288 | struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind, |
| 289 | struct gprs_ns2_nse *nse, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 290 | const struct osmo_sockaddr *remote); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 291 | int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote); |
| 292 | struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi, |
| 293 | struct osmo_sockaddr *remote, |
| 294 | int index); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 295 | |
| 296 | /* sns */ |
| 297 | int gprs_ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp); |
| 298 | struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse, |
| 299 | const char *id); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 300 | void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc); |
| 301 | |
| 302 | /* vc */ |
| 303 | struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc, |
| 304 | const char *id, bool initiate); |
| 305 | int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc); |
Daniel Willmann | ed0c982 | 2020-11-18 14:08:07 +0100 | [diff] [blame] | 306 | int gprs_ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 307 | int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp); |
| 308 | int gprs_ns2_vc_is_alive(struct gprs_ns2_vc *nsvc); |
| 309 | int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc); |
| 310 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 311 | /* nse */ |
| 312 | void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked); |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 313 | enum gprs_ns2_vc_mode gprs_ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect); |