Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 1 | #ifndef _GB_PROXY_H |
| 2 | #define _GB_PROXY_H |
| 3 | |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 4 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 5 | #include <osmocom/core/msgb.h> |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 6 | #include <osmocom/core/timer.h> |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 7 | #include <osmocom/core/fsm.h> |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 8 | #include <osmocom/core/hashtable.h> |
Neels Hofmeyr | 10719b7 | 2018-02-21 00:39:36 +0100 | [diff] [blame] | 9 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 10 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 11 | #include <osmocom/gprs/gprs_ns2.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 12 | #include <osmocom/vty/command.h> |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 13 | |
Holger Hans Peter Freyther | 3fa2644 | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 14 | #include <sys/types.h> |
| 15 | #include <regex.h> |
Harald Welte | aed46ec | 2019-03-22 09:44:42 +0100 | [diff] [blame] | 16 | #include <stdbool.h> |
Holger Hans Peter Freyther | 3fa2644 | 2014-08-04 16:27:11 +0200 | [diff] [blame] | 17 | |
Jacob Erlbeck | 6575028 | 2014-09-22 15:41:21 +0200 | [diff] [blame] | 18 | #define GBPROXY_INIT_VU_GEN_TX 256 |
| 19 | |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 20 | /* BVCI uses 16 bits */ |
| 21 | #define BVC_LOG_CTX_FLAG (1<<17) |
| 22 | |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 23 | struct rate_ctr_group; |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 24 | struct gprs_gb_parse_context; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 25 | struct tlv_parsed; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 26 | |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 27 | enum gbproxy_global_ctr { |
| 28 | GBPROX_GLOB_CTR_INV_BVCI, |
| 29 | GBPROX_GLOB_CTR_INV_LAI, |
| 30 | GBPROX_GLOB_CTR_INV_RAI, |
| 31 | GBPROX_GLOB_CTR_INV_NSEI, |
| 32 | GBPROX_GLOB_CTR_PROTO_ERR_BSS, |
| 33 | GBPROX_GLOB_CTR_PROTO_ERR_SGSN, |
| 34 | GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS, |
| 35 | GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN, |
| 36 | GBPROX_GLOB_CTR_RESTART_RESET_SGSN, |
| 37 | GBPROX_GLOB_CTR_TX_ERR_SGSN, |
| 38 | GBPROX_GLOB_CTR_OTHER_ERR, |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 41 | enum gbproxy_bvc_ctr { |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 42 | GBPROX_PEER_CTR_BLOCKED, |
| 43 | GBPROX_PEER_CTR_UNBLOCKED, |
| 44 | GBPROX_PEER_CTR_DROPPED, |
| 45 | GBPROX_PEER_CTR_INV_NSEI, |
| 46 | GBPROX_PEER_CTR_TX_ERR, |
Holger Hans Peter Freyther | 98fa3dc | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 47 | GBPROX_PEER_CTR_LAST, |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Harald Welte | d3dcaf0 | 2019-03-22 08:26:45 +0100 | [diff] [blame] | 50 | /* global gb-proxy configuration */ |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 51 | struct gbproxy_config { |
Harald Welte | d3dcaf0 | 2019-03-22 08:26:45 +0100 | [diff] [blame] | 52 | /* NS instance of libosmogb */ |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 53 | struct gprs_ns2_inst *nsi; |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 54 | |
Harald Welte | 0e1b791 | 2020-12-04 23:58:20 +0100 | [diff] [blame] | 55 | /* Linked list of all BSS side Gb peers */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 56 | DECLARE_HASHTABLE(bss_nses, 8); |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 57 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 58 | /* hash table of all SGSN-side Gb peers */ |
| 59 | DECLARE_HASHTABLE(sgsn_nses, 8); |
| 60 | |
| 61 | /* hash table of all gbproxy_cell */ |
| 62 | DECLARE_HASHTABLE(cells, 8); |
| 63 | |
Holger Hans Peter Freyther | a7027a0 | 2014-08-04 11:19:56 +0200 | [diff] [blame] | 64 | /* Counter */ |
| 65 | struct rate_ctr_group *ctrg; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 68 | /* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */ |
| 69 | struct gbproxy_cell { |
| 70 | /* linked to gbproxy_config.cells hashtable */ |
| 71 | struct hlist_node list; |
| 72 | |
| 73 | /* point back to the config */ |
| 74 | struct gbproxy_config *cfg; |
| 75 | |
| 76 | /* BVCI of PTP BVCs associated to this cell */ |
| 77 | uint16_t bvci; |
| 78 | |
| 79 | /* Routing Area that this BVC is part of (raw 04.08 encoding) */ |
| 80 | uint8_t ra[6]; |
| 81 | |
| 82 | /* pointer to the BSS-side BVC */ |
| 83 | struct gbproxy_bvc *bss_bvc; |
| 84 | |
| 85 | /* pointers to SGSN-side BVC (one for each pool member) */ |
| 86 | struct gbproxy_bvc *sgsn_bvc[16]; |
| 87 | }; |
| 88 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 89 | /* One BVC inside an NSE */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 90 | struct gbproxy_bvc { |
| 91 | /* linked to gbproxy_nse.bvcs */ |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 92 | struct hlist_node list; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 93 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 94 | /* The NSE this BVC belongs to */ |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 95 | struct gbproxy_nse *nse; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 96 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 97 | /* PTP BVCI of this BVC */ |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 98 | uint16_t bvci; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 99 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 100 | /* Routing Area that this BVC is part of (raw 04.08 encoding) */ |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 101 | uint8_t ra[6]; |
| 102 | |
| 103 | /* Counter */ |
| 104 | struct rate_ctr_group *ctrg; |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 105 | |
| 106 | /* the cell to which this BVC belongs */ |
| 107 | struct gbproxy_cell *cell; |
| 108 | |
| 109 | /* per-BVC FSM instance */ |
| 110 | struct osmo_fsm_inst *fi; |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 113 | /* one NS Entity that we interact with (BSS/PCU) */ |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 114 | struct gbproxy_nse { |
Harald Welte | 0e1b791 | 2020-12-04 23:58:20 +0100 | [diff] [blame] | 115 | /* linked to gbproxy_config.bss_nses */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 116 | struct hlist_node list; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 117 | |
| 118 | /* point back to the config */ |
| 119 | struct gbproxy_config *cfg; |
| 120 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 121 | /* NSEI of the NSE */ |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 122 | uint16_t nsei; |
| 123 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 124 | /* Are we facing towards a SGSN (true) or BSS (false) */ |
| 125 | bool sgsn_facing; |
| 126 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 127 | /* List of all BVCs in this NSE */ |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 128 | DECLARE_HASHTABLE(bvcs, 10); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
Daniel Willmann | 7418797 | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 131 | /* Convenience logging macros for NSE/BVC */ |
| 132 | #define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \ |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 133 | LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \ |
| 134 | (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS) |
Daniel Willmann | 7418797 | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 135 | #define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \ |
| 136 | LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS) |
| 137 | |
| 138 | #define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \ |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 139 | LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \ |
| 140 | (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \ |
| 141 | osmo_fsm_inst_state_name((BVC)->fi), ## ARGS) |
Daniel Willmann | 7418797 | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 142 | #define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \ |
| 143 | LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS) |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 144 | |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame^] | 145 | #define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \ |
| 146 | LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS) |
| 147 | #define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \ |
| 148 | LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS) |
| 149 | |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 150 | /* gb_proxy_vty .c */ |
| 151 | |
| 152 | int gbproxy_vty_init(void); |
| 153 | int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg); |
| 154 | |
Daniel Willmann | c977afa | 2018-06-01 07:21:20 +0200 | [diff] [blame] | 155 | /* gb_proxy_ctrl.c */ |
| 156 | int gb_ctrl_cmds_install(void); |
| 157 | |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 158 | |
| 159 | /* gb_proxy.c */ |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 160 | int gbproxy_init_config(struct gbproxy_config *cfg); |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 161 | |
| 162 | /* Main input function for Gb proxy */ |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 163 | int gbprox_rcvmsg(void *ctx, struct msgb *msg); |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 164 | |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 165 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 166 | void *handler_data, void *signal_data); |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 167 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 168 | |
| 169 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx); |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 170 | |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 171 | void gbprox_reset(struct gbproxy_config *cfg); |
Jacob Erlbeck | 7368528 | 2014-05-23 20:48:07 +0200 | [diff] [blame] | 172 | |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 173 | /* Peer handling */ |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 174 | #define NSE_F_SGSN 0x0001 |
| 175 | #define NSE_F_BSS 0x0002 |
| 176 | |
| 177 | struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 178 | struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci); |
| 179 | void gbproxy_bvc_free(struct gbproxy_bvc *bvc); |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 180 | int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci); |
| 181 | |
| 182 | struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci); |
| 183 | struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci); |
| 184 | void gbproxy_cell_free(struct gbproxy_cell *cell); |
| 185 | bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 186 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 187 | /* NSE handling */ |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 188 | struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 189 | void gbproxy_nse_free(struct gbproxy_nse *nse); |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 190 | struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags); |
| 191 | struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 192 | |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 193 | #endif |