blob: d988cae7ce7fb5df93ca761a79df0fea097b0272 [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>
Daniel Willmannc8a50092021-01-17 13:11:41 +010011#include <osmocom/gsm/protocol/gsm_23_003.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020012
Alexander Couzens951e1332020-09-22 13:21:46 +020013#include <osmocom/gprs/gprs_ns2.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020014#include <osmocom/vty/command.h>
15
16#include <sys/types.h>
17#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010018#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020019
20#define GBPROXY_INIT_VU_GEN_TX 256
Daniel Willmannee834af2020-12-14 16:22:39 +010021#define GBPROXY_MAX_NR_SGSN 16
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020022
Daniel Willmann8f407b12020-12-02 19:33:50 +010023/* BVCI uses 16 bits */
24#define BVC_LOG_CTX_FLAG (1<<17)
25
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020026struct rate_ctr_group;
27struct gprs_gb_parse_context;
28struct tlv_parsed;
29
30enum gbproxy_global_ctr {
31 GBPROX_GLOB_CTR_INV_BVCI,
32 GBPROX_GLOB_CTR_INV_LAI,
33 GBPROX_GLOB_CTR_INV_RAI,
34 GBPROX_GLOB_CTR_INV_NSEI,
35 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
36 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
37 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
38 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
39 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
40 GBPROX_GLOB_CTR_TX_ERR_SGSN,
41 GBPROX_GLOB_CTR_OTHER_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020042};
43
Harald Welte560bdb32020-12-04 22:24:47 +010044enum gbproxy_bvc_ctr {
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020045 GBPROX_PEER_CTR_BLOCKED,
46 GBPROX_PEER_CTR_UNBLOCKED,
47 GBPROX_PEER_CTR_DROPPED,
48 GBPROX_PEER_CTR_INV_NSEI,
49 GBPROX_PEER_CTR_TX_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020050 GBPROX_PEER_CTR_LAST,
51};
52
Harald Welte4a8769a2019-03-22 08:26:45 +010053/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020054struct gbproxy_config {
Harald Welte4a8769a2019-03-22 08:26:45 +010055 /* NS instance of libosmogb */
Alexander Couzens951e1332020-09-22 13:21:46 +020056 struct gprs_ns2_inst *nsi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020057
Harald Welte209dc9f2020-12-12 19:02:16 +010058 struct {
59 /* percentage of BVC flow control advertised to each SGSN in the pool */
60 uint8_t bvc_fc_ratio;
Daniel Willmannee834af2020-12-14 16:22:39 +010061 /* NRI bitlen and usable NULL-NRI ranges */
62 uint8_t nri_bitlen;
63 struct osmo_nri_ranges *null_nri_ranges;
Daniel Willmannb387c1e2020-12-27 18:14:39 +010064
65 /* Used for testing: If not NULL then this SGSN is returned by
66 * gbproxy_sgsn_by_tlli() */
67 struct gbproxy_sgsn *nsf_override;
Harald Welte209dc9f2020-12-12 19:02:16 +010068 } pool;
69
Daniel Willmannee834af2020-12-14 16:22:39 +010070 /* hash table of all BSS side Gb peers */
Harald Welted2fef952020-12-05 00:31:07 +010071 DECLARE_HASHTABLE(bss_nses, 8);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020072
Harald Weltee5209642020-12-05 19:59:45 +010073 /* hash table of all SGSN-side Gb peers */
74 DECLARE_HASHTABLE(sgsn_nses, 8);
75
76 /* hash table of all gbproxy_cell */
77 DECLARE_HASHTABLE(cells, 8);
78
Daniel Willmann77493b12020-12-29 21:13:31 +010079 /* tlli<->nse cache used to map SUSPEND/RESUME (N)ACKS */
80 struct {
81 DECLARE_HASHTABLE(entries, 10);
82 struct osmo_timer_list timer;
83 /* Time in seconds that the entries should be valid */
84 uint8_t timeout;
85 } tlli_cache;
86
Daniel Willmannc8a50092021-01-17 13:11:41 +010087 /* imsi<->nse cache used for PAGING REJECT */
88 struct {
89 DECLARE_HASHTABLE(entries, 10);
90 struct osmo_timer_list timer;
91 /* Time in seconds that the entries should be valid */
92 uint8_t timeout;
93 } imsi_cache;
94
Daniel Willmannee834af2020-12-14 16:22:39 +010095 /* List of all SGSNs */
96 struct llist_head sgsns;
97
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020098 /* Counter */
99 struct rate_ctr_group *ctrg;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200100};
101
Harald Weltee5209642020-12-05 19:59:45 +0100102/* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */
103struct gbproxy_cell {
104 /* linked to gbproxy_config.cells hashtable */
105 struct hlist_node list;
106
107 /* point back to the config */
108 struct gbproxy_config *cfg;
109
110 /* BVCI of PTP BVCs associated to this cell */
111 uint16_t bvci;
112
113 /* Routing Area that this BVC is part of (raw 04.08 encoding) */
114 uint8_t ra[6];
115
116 /* pointer to the BSS-side BVC */
117 struct gbproxy_bvc *bss_bvc;
118
119 /* pointers to SGSN-side BVC (one for each pool member) */
Daniel Willmannee834af2020-12-14 16:22:39 +0100120 struct gbproxy_bvc *sgsn_bvc[GBPROXY_MAX_NR_SGSN];
Harald Weltee5209642020-12-05 19:59:45 +0100121};
122
Daniel Willmanne50550e2020-11-26 18:19:21 +0100123/* One BVC inside an NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100124struct gbproxy_bvc {
125 /* linked to gbproxy_nse.bvcs */
Harald Welte8b4c7942020-12-05 10:14:49 +0100126 struct hlist_node list;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200127
Harald Welte560bdb32020-12-04 22:24:47 +0100128 /* The NSE this BVC belongs to */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100129 struct gbproxy_nse *nse;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200130
Harald Welte560bdb32020-12-04 22:24:47 +0100131 /* PTP BVCI of this BVC */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200132 uint16_t bvci;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200133
Harald Welte560bdb32020-12-04 22:24:47 +0100134 /* Routing Area that this BVC is part of (raw 04.08 encoding) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200135 uint8_t ra[6];
136
137 /* Counter */
138 struct rate_ctr_group *ctrg;
Harald Weltee5209642020-12-05 19:59:45 +0100139
140 /* the cell to which this BVC belongs */
141 struct gbproxy_cell *cell;
142
143 /* per-BVC FSM instance */
144 struct osmo_fsm_inst *fi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200145};
146
Harald Welte560bdb32020-12-04 22:24:47 +0100147/* one NS Entity that we interact with (BSS/PCU) */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100148struct gbproxy_nse {
Harald Welte2b7b1bb2020-12-04 23:58:20 +0100149 /* linked to gbproxy_config.bss_nses */
Harald Welted2fef952020-12-05 00:31:07 +0100150 struct hlist_node list;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100151
152 /* point back to the config */
153 struct gbproxy_config *cfg;
154
Harald Welte560bdb32020-12-04 22:24:47 +0100155 /* NSEI of the NSE */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100156 uint16_t nsei;
157
Harald Weltee5209642020-12-05 19:59:45 +0100158 /* Are we facing towards a SGSN (true) or BSS (false) */
159 bool sgsn_facing;
160
Daniel Willmanne50550e2020-11-26 18:19:21 +0100161 /* List of all BVCs in this NSE */
Harald Welte8b4c7942020-12-05 10:14:49 +0100162 DECLARE_HASHTABLE(bvcs, 10);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100163};
164
Daniel Willmannee834af2020-12-14 16:22:39 +0100165/* SGSN configuration such as pool options (only for NSE where sgsn_facing == true) */
166struct gbproxy_sgsn {
167 /* linked to gbproxy_config.sgsns */
168 struct llist_head list;
169
170 /* The NSE belonging to this SGSN */
171 struct gbproxy_nse *nse;
172
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100173 /* Name of the SGSN */
174 char *name;
175
Daniel Willmannee834af2020-12-14 16:22:39 +0100176 /* Pool configuration for the sgsn (only valid if sgsn_facing == true) */
177 struct {
178 bool allow_attach;
179 struct osmo_nri_ranges *nri_ranges;
180 } pool;
181};
182
Daniel Willmann77493b12020-12-29 21:13:31 +0100183/* TLLI cache */
184struct gbproxy_tlli_cache_entry {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100185 /* linked to gbproxy_config.tlli_cache.entries */
Daniel Willmann77493b12020-12-29 21:13:31 +0100186 struct hlist_node list;
187
188 /* TLLI of the entry */
189 uint32_t tlli;
190 /* When was this entry last seen */
191 time_t tstamp;
192 /* The Cell this TLLI was last seen */
193 struct gbproxy_nse *nse;
194};
195
Daniel Willmannc8a50092021-01-17 13:11:41 +0100196/* IMSI cache */
197struct gbproxy_imsi_cache_entry {
198 /* linked to gbproxy_config.imsi_cache.entries */
199 struct hlist_node list;
200
201 /* IMSI of the entry */
202 char imsi[OSMO_IMSI_BUF_SIZE];
203 /* When was this entry last seen */
204 time_t tstamp;
205 /* The SGSN where the request came from */
206 struct gbproxy_nse *nse;
207};
208
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100209/* Convenience logging macros for NSE/BVC */
210#define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100211 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \
212 (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100213#define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \
214 LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS)
215
216#define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100217 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \
218 (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \
219 osmo_fsm_inst_state_name((BVC)->fi), ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100220#define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \
221 LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS)
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200222
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100223#define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \
224 LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS)
225#define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \
226 LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS)
227
Daniel Willmannee834af2020-12-14 16:22:39 +0100228#define LOGPSGSN_CAT(SGSN, SUBSYS, LEVEL, FMT, ARGS...) \
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100229 LOGP(SUBSYS, LEVEL, "NSE(%05u)-SGSN(%s) " FMT, (SGSN)->nse->nsei, (SGSN)->name, ## ARGS)
Daniel Willmannee834af2020-12-14 16:22:39 +0100230#define LOGPSGSN(SGSN, LEVEL, FMT, ARGS...) \
231 LOGPSGSN_CAT(SGSN, DGPRS, LEVEL, FMT, ## ARGS)
232
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200233/* gb_proxy_vty .c */
234
235int gbproxy_vty_init(void);
236int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
237
Daniel Willmann13404b72018-06-01 07:21:20 +0200238/* gb_proxy_ctrl.c */
239int gb_ctrl_cmds_install(void);
240
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200241
242/* gb_proxy.c */
243int gbproxy_init_config(struct gbproxy_config *cfg);
244
245/* Main input function for Gb proxy */
Alexander Couzens951e1332020-09-22 13:21:46 +0200246int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200247
248int gbprox_signal(unsigned int subsys, unsigned int signal,
249 void *handler_data, void *signal_data);
250
Alexander Couzens951e1332020-09-22 13:21:46 +0200251
252int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200253
254void gbprox_reset(struct gbproxy_config *cfg);
255
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200256/* Peer handling */
Harald Weltee5209642020-12-05 19:59:45 +0100257#define NSE_F_SGSN 0x0001
258#define NSE_F_BSS 0x0002
259
260struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100261struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci);
262void gbproxy_bvc_free(struct gbproxy_bvc *bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100263int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci);
264
265struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci);
266struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci);
267void gbproxy_cell_free(struct gbproxy_cell *cell);
268bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200269
Daniel Willmanne50550e2020-11-26 18:19:21 +0100270/* NSE handling */
Harald Weltee5209642020-12-05 19:59:45 +0100271struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100272void gbproxy_nse_free(struct gbproxy_nse *nse);
Harald Weltee5209642020-12-05 19:59:45 +0100273struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags);
274struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmann77493b12020-12-29 21:13:31 +0100275struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100276struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi);
Daniel Willmann77493b12020-12-29 21:13:31 +0100277
278/* TLLI cache */
279void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli);
280void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli);
281int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100282
Daniel Willmannc8a50092021-01-17 13:11:41 +0100283/* IMSI cache */
284void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi);
285void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi);
286int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg);
287
Daniel Willmannee834af2020-12-14 16:22:39 +0100288/* SGSN handling */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100289struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100290void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100291struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100292struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei);
293struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei);
294struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100295struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
296 uint32_t tlli);
Daniel Willmannee834af2020-12-14 16:22:39 +0100297
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200298#endif