blob: a36f3051016e351651e52301c486a048c42fef9c [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>
Philipp Maierda3af942021-02-04 21:54:09 +010011#include <osmocom/gsm/gsm48.h>
Daniel Willmannc8a50092021-01-17 13:11:41 +010012#include <osmocom/gsm/protocol/gsm_23_003.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020013
Alexander Couzens951e1332020-09-22 13:21:46 +020014#include <osmocom/gprs/gprs_ns2.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020015#include <osmocom/vty/command.h>
16
17#include <sys/types.h>
18#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010019#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020020
21#define GBPROXY_INIT_VU_GEN_TX 256
Daniel Willmannee834af2020-12-14 16:22:39 +010022#define GBPROXY_MAX_NR_SGSN 16
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020023
Daniel Willmann8f407b12020-12-02 19:33:50 +010024/* BVCI uses 16 bits */
25#define BVC_LOG_CTX_FLAG (1<<17)
26
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020027struct rate_ctr_group;
28struct gprs_gb_parse_context;
29struct tlv_parsed;
30
31enum gbproxy_global_ctr {
32 GBPROX_GLOB_CTR_INV_BVCI,
33 GBPROX_GLOB_CTR_INV_LAI,
34 GBPROX_GLOB_CTR_INV_RAI,
35 GBPROX_GLOB_CTR_INV_NSEI,
36 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
37 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
38 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
39 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
40 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
41 GBPROX_GLOB_CTR_TX_ERR_SGSN,
42 GBPROX_GLOB_CTR_OTHER_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020043};
44
Harald Welte560bdb32020-12-04 22:24:47 +010045enum gbproxy_bvc_ctr {
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020046 GBPROX_PEER_CTR_BLOCKED,
47 GBPROX_PEER_CTR_UNBLOCKED,
48 GBPROX_PEER_CTR_DROPPED,
49 GBPROX_PEER_CTR_INV_NSEI,
50 GBPROX_PEER_CTR_TX_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020051 GBPROX_PEER_CTR_LAST,
52};
53
Harald Welte4a8769a2019-03-22 08:26:45 +010054/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020055struct gbproxy_config {
Harald Welte4a8769a2019-03-22 08:26:45 +010056 /* NS instance of libosmogb */
Alexander Couzens951e1332020-09-22 13:21:46 +020057 struct gprs_ns2_inst *nsi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020058
Harald Welte209dc9f2020-12-12 19:02:16 +010059 struct {
60 /* percentage of BVC flow control advertised to each SGSN in the pool */
61 uint8_t bvc_fc_ratio;
Daniel Willmannee834af2020-12-14 16:22:39 +010062 /* NRI bitlen and usable NULL-NRI ranges */
63 uint8_t nri_bitlen;
64 struct osmo_nri_ranges *null_nri_ranges;
Daniel Willmannb387c1e2020-12-27 18:14:39 +010065
66 /* Used for testing: If not NULL then this SGSN is returned by
67 * gbproxy_sgsn_by_tlli() */
68 struct gbproxy_sgsn *nsf_override;
Harald Welte209dc9f2020-12-12 19:02:16 +010069 } pool;
70
Daniel Willmannee834af2020-12-14 16:22:39 +010071 /* hash table of all BSS side Gb peers */
Harald Welted2fef952020-12-05 00:31:07 +010072 DECLARE_HASHTABLE(bss_nses, 8);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020073
Harald Weltee5209642020-12-05 19:59:45 +010074 /* hash table of all SGSN-side Gb peers */
75 DECLARE_HASHTABLE(sgsn_nses, 8);
76
77 /* hash table of all gbproxy_cell */
78 DECLARE_HASHTABLE(cells, 8);
79
Daniel Willmann77493b12020-12-29 21:13:31 +010080 /* tlli<->nse cache used to map SUSPEND/RESUME (N)ACKS */
81 struct {
82 DECLARE_HASHTABLE(entries, 10);
83 struct osmo_timer_list timer;
84 /* Time in seconds that the entries should be valid */
85 uint8_t timeout;
86 } tlli_cache;
87
Daniel Willmannc8a50092021-01-17 13:11:41 +010088 /* imsi<->nse cache used for PAGING REJECT */
89 struct {
90 DECLARE_HASHTABLE(entries, 10);
91 struct osmo_timer_list timer;
92 /* Time in seconds that the entries should be valid */
93 uint8_t timeout;
94 } imsi_cache;
95
Daniel Willmannee834af2020-12-14 16:22:39 +010096 /* List of all SGSNs */
97 struct llist_head sgsns;
98
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020099 /* Counter */
100 struct rate_ctr_group *ctrg;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200101};
102
Harald Weltee5209642020-12-05 19:59:45 +0100103/* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */
104struct gbproxy_cell {
105 /* linked to gbproxy_config.cells hashtable */
106 struct hlist_node list;
107
108 /* point back to the config */
109 struct gbproxy_config *cfg;
110
111 /* BVCI of PTP BVCs associated to this cell */
112 uint16_t bvci;
113
Philipp Maierda3af942021-02-04 21:54:09 +0100114 struct {
115 /* Routing Area that this cell is part of */
116 struct gprs_ra_id raid;
117
118 /* Cell ID of this cell */
119 uint16_t cid;
120 } id;
Harald Weltee5209642020-12-05 19:59:45 +0100121
122 /* pointer to the BSS-side BVC */
123 struct gbproxy_bvc *bss_bvc;
124
125 /* pointers to SGSN-side BVC (one for each pool member) */
Daniel Willmannee834af2020-12-14 16:22:39 +0100126 struct gbproxy_bvc *sgsn_bvc[GBPROXY_MAX_NR_SGSN];
Harald Weltee5209642020-12-05 19:59:45 +0100127};
128
Daniel Willmanne50550e2020-11-26 18:19:21 +0100129/* One BVC inside an NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100130struct gbproxy_bvc {
131 /* linked to gbproxy_nse.bvcs */
Harald Welte8b4c7942020-12-05 10:14:49 +0100132 struct hlist_node list;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200133
Harald Welte560bdb32020-12-04 22:24:47 +0100134 /* The NSE this BVC belongs to */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100135 struct gbproxy_nse *nse;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200136
Harald Welte560bdb32020-12-04 22:24:47 +0100137 /* PTP BVCI of this BVC */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200138 uint16_t bvci;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200139
Philipp Maierda3af942021-02-04 21:54:09 +0100140 /* Routing Area that this BVC is part of */
141 struct gprs_ra_id raid;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200142
143 /* Counter */
144 struct rate_ctr_group *ctrg;
Harald Weltee5209642020-12-05 19:59:45 +0100145
146 /* the cell to which this BVC belongs */
147 struct gbproxy_cell *cell;
148
149 /* per-BVC FSM instance */
150 struct osmo_fsm_inst *fi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200151};
152
Harald Welte560bdb32020-12-04 22:24:47 +0100153/* one NS Entity that we interact with (BSS/PCU) */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100154struct gbproxy_nse {
Harald Welte2b7b1bb2020-12-04 23:58:20 +0100155 /* linked to gbproxy_config.bss_nses */
Harald Welted2fef952020-12-05 00:31:07 +0100156 struct hlist_node list;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100157
158 /* point back to the config */
159 struct gbproxy_config *cfg;
160
Harald Welte560bdb32020-12-04 22:24:47 +0100161 /* NSEI of the NSE */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100162 uint16_t nsei;
163
Harald Weltee5209642020-12-05 19:59:45 +0100164 /* Are we facing towards a SGSN (true) or BSS (false) */
165 bool sgsn_facing;
166
Daniel Willmanne50550e2020-11-26 18:19:21 +0100167 /* List of all BVCs in this NSE */
Harald Welte8b4c7942020-12-05 10:14:49 +0100168 DECLARE_HASHTABLE(bvcs, 10);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100169};
170
Daniel Willmannee834af2020-12-14 16:22:39 +0100171/* SGSN configuration such as pool options (only for NSE where sgsn_facing == true) */
172struct gbproxy_sgsn {
173 /* linked to gbproxy_config.sgsns */
174 struct llist_head list;
175
176 /* The NSE belonging to this SGSN */
177 struct gbproxy_nse *nse;
178
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100179 /* Name of the SGSN */
180 char *name;
181
Daniel Willmannee834af2020-12-14 16:22:39 +0100182 /* Pool configuration for the sgsn (only valid if sgsn_facing == true) */
183 struct {
184 bool allow_attach;
185 struct osmo_nri_ranges *nri_ranges;
186 } pool;
187};
188
Daniel Willmann77493b12020-12-29 21:13:31 +0100189/* TLLI cache */
190struct gbproxy_tlli_cache_entry {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100191 /* linked to gbproxy_config.tlli_cache.entries */
Daniel Willmann77493b12020-12-29 21:13:31 +0100192 struct hlist_node list;
193
194 /* TLLI of the entry */
195 uint32_t tlli;
196 /* When was this entry last seen */
197 time_t tstamp;
198 /* The Cell this TLLI was last seen */
199 struct gbproxy_nse *nse;
200};
201
Daniel Willmannc8a50092021-01-17 13:11:41 +0100202/* IMSI cache */
203struct gbproxy_imsi_cache_entry {
204 /* linked to gbproxy_config.imsi_cache.entries */
205 struct hlist_node list;
206
207 /* IMSI of the entry */
208 char imsi[OSMO_IMSI_BUF_SIZE];
209 /* When was this entry last seen */
210 time_t tstamp;
211 /* The SGSN where the request came from */
212 struct gbproxy_nse *nse;
213};
214
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100215/* Convenience logging macros for NSE/BVC */
216#define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100217 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \
218 (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100219#define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \
220 LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS)
221
222#define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100223 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \
224 (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \
225 osmo_fsm_inst_state_name((BVC)->fi), ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100226#define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \
227 LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS)
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200228
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100229#define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \
230 LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS)
231#define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \
232 LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS)
233
Daniel Willmannee834af2020-12-14 16:22:39 +0100234#define LOGPSGSN_CAT(SGSN, SUBSYS, LEVEL, FMT, ARGS...) \
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100235 LOGP(SUBSYS, LEVEL, "NSE(%05u)-SGSN(%s) " FMT, (SGSN)->nse->nsei, (SGSN)->name, ## ARGS)
Daniel Willmannee834af2020-12-14 16:22:39 +0100236#define LOGPSGSN(SGSN, LEVEL, FMT, ARGS...) \
237 LOGPSGSN_CAT(SGSN, DGPRS, LEVEL, FMT, ## ARGS)
238
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200239/* gb_proxy_vty .c */
240
241int gbproxy_vty_init(void);
242int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
243
Daniel Willmann13404b72018-06-01 07:21:20 +0200244/* gb_proxy_ctrl.c */
245int gb_ctrl_cmds_install(void);
246
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200247
248/* gb_proxy.c */
249int gbproxy_init_config(struct gbproxy_config *cfg);
250
251/* Main input function for Gb proxy */
Alexander Couzens951e1332020-09-22 13:21:46 +0200252int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200253
254int gbprox_signal(unsigned int subsys, unsigned int signal,
255 void *handler_data, void *signal_data);
256
Alexander Couzens951e1332020-09-22 13:21:46 +0200257
258int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200259
260void gbprox_reset(struct gbproxy_config *cfg);
261
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200262/* Peer handling */
Harald Weltee5209642020-12-05 19:59:45 +0100263#define NSE_F_SGSN 0x0001
264#define NSE_F_BSS 0x0002
265
266struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100267struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci);
268void gbproxy_bvc_free(struct gbproxy_bvc *bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100269int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci);
270
Philipp Maiere4597ec2021-02-09 16:02:00 +0100271struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci, const struct gprs_ra_id *raid, uint16_t cid);
Harald Weltee5209642020-12-05 19:59:45 +0100272struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci);
Daniel Willmannfccbef02021-01-19 17:59:19 +0100273struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid);
Harald Weltee5209642020-12-05 19:59:45 +0100274void gbproxy_cell_free(struct gbproxy_cell *cell);
275bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200276
Daniel Willmanne50550e2020-11-26 18:19:21 +0100277/* NSE handling */
Harald Weltee5209642020-12-05 19:59:45 +0100278struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100279void gbproxy_nse_free(struct gbproxy_nse *nse);
Harald Weltee5209642020-12-05 19:59:45 +0100280struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags);
281struct 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 +0100282struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100283struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi);
Daniel Willmann77493b12020-12-29 21:13:31 +0100284
285/* TLLI cache */
286void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli);
287void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli);
288int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100289
Daniel Willmannc8a50092021-01-17 13:11:41 +0100290/* IMSI cache */
291void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi);
292void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi);
293int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg);
294
Daniel Willmannee834af2020-12-14 16:22:39 +0100295/* SGSN handling */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100296struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100297void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100298struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100299struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei);
300struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei);
301struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100302struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
303 uint32_t tlli);
Daniel Willmannee834af2020-12-14 16:22:39 +0100304
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200305#endif