blob: ad5bb27f312146a3942ed95e5512d02eab01194a [file] [log] [blame]
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +02001#ifndef _GB_PROXY_H
2#define _GB_PROXY_H
3
4
5#include <osmocom/core/msgb.h>
Alexander Couzens951e1332020-09-22 13:21:46 +02006#include <osmocom/core/timer.h>
Harald Weltee5209642020-12-05 19:59:45 +01007#include <osmocom/core/fsm.h>
Harald Welted2fef952020-12-05 00:31:07 +01008#include <osmocom/core/hashtable.h>
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +01009#include <osmocom/gsm/gsm23003.h>
Daniel Willmannee834af2020-12-14 16:22:39 +010010#include <osmocom/gsm/gsm23236.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020011
Alexander Couzens951e1332020-09-22 13:21:46 +020012#include <osmocom/gprs/gprs_ns2.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020013#include <osmocom/vty/command.h>
14
15#include <sys/types.h>
16#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010017#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020018
19#define GBPROXY_INIT_VU_GEN_TX 256
Daniel Willmannee834af2020-12-14 16:22:39 +010020#define GBPROXY_MAX_NR_SGSN 16
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020021
Daniel Willmann8f407b12020-12-02 19:33:50 +010022/* BVCI uses 16 bits */
23#define BVC_LOG_CTX_FLAG (1<<17)
24
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020025struct rate_ctr_group;
26struct gprs_gb_parse_context;
27struct tlv_parsed;
28
29enum gbproxy_global_ctr {
30 GBPROX_GLOB_CTR_INV_BVCI,
31 GBPROX_GLOB_CTR_INV_LAI,
32 GBPROX_GLOB_CTR_INV_RAI,
33 GBPROX_GLOB_CTR_INV_NSEI,
34 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
35 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
36 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
37 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
38 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
39 GBPROX_GLOB_CTR_TX_ERR_SGSN,
40 GBPROX_GLOB_CTR_OTHER_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020041};
42
Harald Welte560bdb32020-12-04 22:24:47 +010043enum gbproxy_bvc_ctr {
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020044 GBPROX_PEER_CTR_BLOCKED,
45 GBPROX_PEER_CTR_UNBLOCKED,
46 GBPROX_PEER_CTR_DROPPED,
47 GBPROX_PEER_CTR_INV_NSEI,
48 GBPROX_PEER_CTR_TX_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020049 GBPROX_PEER_CTR_LAST,
50};
51
Harald Welte4a8769a2019-03-22 08:26:45 +010052/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020053struct gbproxy_config {
Harald Welte4a8769a2019-03-22 08:26:45 +010054 /* NS instance of libosmogb */
Alexander Couzens951e1332020-09-22 13:21:46 +020055 struct gprs_ns2_inst *nsi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020056
Harald Welte209dc9f2020-12-12 19:02:16 +010057 struct {
58 /* percentage of BVC flow control advertised to each SGSN in the pool */
59 uint8_t bvc_fc_ratio;
Daniel Willmannee834af2020-12-14 16:22:39 +010060 /* NRI bitlen and usable NULL-NRI ranges */
61 uint8_t nri_bitlen;
62 struct osmo_nri_ranges *null_nri_ranges;
Daniel Willmannb387c1e2020-12-27 18:14:39 +010063
64 /* Used for testing: If not NULL then this SGSN is returned by
65 * gbproxy_sgsn_by_tlli() */
66 struct gbproxy_sgsn *nsf_override;
Harald Welte209dc9f2020-12-12 19:02:16 +010067 } pool;
68
Daniel Willmannee834af2020-12-14 16:22:39 +010069 /* hash table of all BSS side Gb peers */
Harald Welted2fef952020-12-05 00:31:07 +010070 DECLARE_HASHTABLE(bss_nses, 8);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020071
Harald Weltee5209642020-12-05 19:59:45 +010072 /* hash table of all SGSN-side Gb peers */
73 DECLARE_HASHTABLE(sgsn_nses, 8);
74
75 /* hash table of all gbproxy_cell */
76 DECLARE_HASHTABLE(cells, 8);
77
Daniel Willmannee834af2020-12-14 16:22:39 +010078 /* List of all SGSNs */
79 struct llist_head sgsns;
80
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020081 /* Counter */
82 struct rate_ctr_group *ctrg;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020083};
84
Harald Weltee5209642020-12-05 19:59:45 +010085/* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */
86struct gbproxy_cell {
87 /* linked to gbproxy_config.cells hashtable */
88 struct hlist_node list;
89
90 /* point back to the config */
91 struct gbproxy_config *cfg;
92
93 /* BVCI of PTP BVCs associated to this cell */
94 uint16_t bvci;
95
96 /* Routing Area that this BVC is part of (raw 04.08 encoding) */
97 uint8_t ra[6];
98
99 /* pointer to the BSS-side BVC */
100 struct gbproxy_bvc *bss_bvc;
101
102 /* pointers to SGSN-side BVC (one for each pool member) */
Daniel Willmannee834af2020-12-14 16:22:39 +0100103 struct gbproxy_bvc *sgsn_bvc[GBPROXY_MAX_NR_SGSN];
Harald Weltee5209642020-12-05 19:59:45 +0100104};
105
Daniel Willmanne50550e2020-11-26 18:19:21 +0100106/* One BVC inside an NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100107struct gbproxy_bvc {
108 /* linked to gbproxy_nse.bvcs */
Harald Welte8b4c7942020-12-05 10:14:49 +0100109 struct hlist_node list;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200110
Harald Welte560bdb32020-12-04 22:24:47 +0100111 /* The NSE this BVC belongs to */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100112 struct gbproxy_nse *nse;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200113
Harald Welte560bdb32020-12-04 22:24:47 +0100114 /* PTP BVCI of this BVC */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200115 uint16_t bvci;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200116
Harald Welte560bdb32020-12-04 22:24:47 +0100117 /* Routing Area that this BVC is part of (raw 04.08 encoding) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200118 uint8_t ra[6];
119
120 /* Counter */
121 struct rate_ctr_group *ctrg;
Harald Weltee5209642020-12-05 19:59:45 +0100122
123 /* the cell to which this BVC belongs */
124 struct gbproxy_cell *cell;
125
126 /* per-BVC FSM instance */
127 struct osmo_fsm_inst *fi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200128};
129
Harald Welte560bdb32020-12-04 22:24:47 +0100130/* one NS Entity that we interact with (BSS/PCU) */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100131struct gbproxy_nse {
Harald Welte2b7b1bb2020-12-04 23:58:20 +0100132 /* linked to gbproxy_config.bss_nses */
Harald Welted2fef952020-12-05 00:31:07 +0100133 struct hlist_node list;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100134
135 /* point back to the config */
136 struct gbproxy_config *cfg;
137
Harald Welte560bdb32020-12-04 22:24:47 +0100138 /* NSEI of the NSE */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100139 uint16_t nsei;
140
Harald Weltee5209642020-12-05 19:59:45 +0100141 /* Are we facing towards a SGSN (true) or BSS (false) */
142 bool sgsn_facing;
143
Daniel Willmanne50550e2020-11-26 18:19:21 +0100144 /* List of all BVCs in this NSE */
Harald Welte8b4c7942020-12-05 10:14:49 +0100145 DECLARE_HASHTABLE(bvcs, 10);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100146};
147
Daniel Willmannee834af2020-12-14 16:22:39 +0100148/* SGSN configuration such as pool options (only for NSE where sgsn_facing == true) */
149struct gbproxy_sgsn {
150 /* linked to gbproxy_config.sgsns */
151 struct llist_head list;
152
153 /* The NSE belonging to this SGSN */
154 struct gbproxy_nse *nse;
155
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100156 /* Name of the SGSN */
157 char *name;
158
Daniel Willmannee834af2020-12-14 16:22:39 +0100159 /* Pool configuration for the sgsn (only valid if sgsn_facing == true) */
160 struct {
161 bool allow_attach;
162 struct osmo_nri_ranges *nri_ranges;
163 } pool;
164};
165
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100166/* Convenience logging macros for NSE/BVC */
167#define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100168 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \
169 (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100170#define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \
171 LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS)
172
173#define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100174 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \
175 (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \
176 osmo_fsm_inst_state_name((BVC)->fi), ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100177#define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \
178 LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS)
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200179
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100180#define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \
181 LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS)
182#define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \
183 LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS)
184
Daniel Willmannee834af2020-12-14 16:22:39 +0100185#define LOGPSGSN_CAT(SGSN, SUBSYS, LEVEL, FMT, ARGS...) \
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100186 LOGP(SUBSYS, LEVEL, "NSE(%05u)-SGSN(%s) " FMT, (SGSN)->nse->nsei, (SGSN)->name, ## ARGS)
Daniel Willmannee834af2020-12-14 16:22:39 +0100187#define LOGPSGSN(SGSN, LEVEL, FMT, ARGS...) \
188 LOGPSGSN_CAT(SGSN, DGPRS, LEVEL, FMT, ## ARGS)
189
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200190/* gb_proxy_vty .c */
191
192int gbproxy_vty_init(void);
193int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
194
Daniel Willmann13404b72018-06-01 07:21:20 +0200195/* gb_proxy_ctrl.c */
196int gb_ctrl_cmds_install(void);
197
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200198
199/* gb_proxy.c */
200int gbproxy_init_config(struct gbproxy_config *cfg);
201
202/* Main input function for Gb proxy */
Alexander Couzens951e1332020-09-22 13:21:46 +0200203int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200204
205int gbprox_signal(unsigned int subsys, unsigned int signal,
206 void *handler_data, void *signal_data);
207
Alexander Couzens951e1332020-09-22 13:21:46 +0200208
209int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200210
211void gbprox_reset(struct gbproxy_config *cfg);
212
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200213/* Peer handling */
Harald Weltee5209642020-12-05 19:59:45 +0100214#define NSE_F_SGSN 0x0001
215#define NSE_F_BSS 0x0002
216
217struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100218struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci);
219void gbproxy_bvc_free(struct gbproxy_bvc *bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100220int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci);
221
222struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci);
223struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci);
224void gbproxy_cell_free(struct gbproxy_cell *cell);
225bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200226
Daniel Willmanne50550e2020-11-26 18:19:21 +0100227/* NSE handling */
Harald Weltee5209642020-12-05 19:59:45 +0100228struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100229void gbproxy_nse_free(struct gbproxy_nse *nse);
Harald Weltee5209642020-12-05 19:59:45 +0100230struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags);
231struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100232
Daniel Willmannee834af2020-12-14 16:22:39 +0100233/* SGSN handling */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100234struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100235void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100236struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100237struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei);
238struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei);
239struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100240struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
241 uint32_t tlli);
Daniel Willmannee834af2020-12-14 16:22:39 +0100242
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200243#endif