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