blob: c9d2fca26349029c6a83720314aabdb331583b3d [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
Daniel Willmanna8b61652021-02-12 05:05:14 +010014#include <osmocom/gprs/frame_relay.h>
Alexander Couzens951e1332020-09-22 13:21:46 +020015#include <osmocom/gprs/gprs_ns2.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020016#include <osmocom/vty/command.h>
17
18#include <sys/types.h>
19#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010020#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020021
22#define GBPROXY_INIT_VU_GEN_TX 256
Daniel Willmannee834af2020-12-14 16:22:39 +010023#define GBPROXY_MAX_NR_SGSN 16
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020024
Daniel Willmanna8b61652021-02-12 05:05:14 +010025/* Set conservative default BSSGP SDU (FR SDU - size of NS UNITDATA IEs) */
26#define DEFAULT_NSE_SDU (FRAME_RELAY_SDU - 4)
27
Daniel Willmann8f407b12020-12-02 19:33:50 +010028/* BVCI uses 16 bits */
29#define BVC_LOG_CTX_FLAG (1<<17)
30
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020031struct rate_ctr_group;
32struct gprs_gb_parse_context;
33struct tlv_parsed;
34
35enum gbproxy_global_ctr {
36 GBPROX_GLOB_CTR_INV_BVCI,
37 GBPROX_GLOB_CTR_INV_LAI,
38 GBPROX_GLOB_CTR_INV_RAI,
39 GBPROX_GLOB_CTR_INV_NSEI,
40 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
41 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
42 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
43 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
44 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
45 GBPROX_GLOB_CTR_TX_ERR_SGSN,
46 GBPROX_GLOB_CTR_OTHER_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020047};
48
Harald Welte560bdb32020-12-04 22:24:47 +010049enum gbproxy_bvc_ctr {
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020050 GBPROX_PEER_CTR_BLOCKED,
51 GBPROX_PEER_CTR_UNBLOCKED,
52 GBPROX_PEER_CTR_DROPPED,
53 GBPROX_PEER_CTR_INV_NSEI,
54 GBPROX_PEER_CTR_TX_ERR,
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020055 GBPROX_PEER_CTR_LAST,
56};
57
Harald Welte4a8769a2019-03-22 08:26:45 +010058/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020059struct gbproxy_config {
Harald Welte4a8769a2019-03-22 08:26:45 +010060 /* NS instance of libosmogb */
Alexander Couzens951e1332020-09-22 13:21:46 +020061 struct gprs_ns2_inst *nsi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020062
Harald Welte209dc9f2020-12-12 19:02:16 +010063 struct {
64 /* percentage of BVC flow control advertised to each SGSN in the pool */
65 uint8_t bvc_fc_ratio;
Daniel Willmannee834af2020-12-14 16:22:39 +010066 /* NRI bitlen and usable NULL-NRI ranges */
67 uint8_t nri_bitlen;
68 struct osmo_nri_ranges *null_nri_ranges;
Daniel Willmannb387c1e2020-12-27 18:14:39 +010069
70 /* Used for testing: If not NULL then this SGSN is returned by
71 * gbproxy_sgsn_by_tlli() */
72 struct gbproxy_sgsn *nsf_override;
Harald Welte209dc9f2020-12-12 19:02:16 +010073 } pool;
74
Daniel Willmannee834af2020-12-14 16:22:39 +010075 /* hash table of all BSS side Gb peers */
Harald Welted2fef952020-12-05 00:31:07 +010076 DECLARE_HASHTABLE(bss_nses, 8);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020077
Harald Weltee5209642020-12-05 19:59:45 +010078 /* hash table of all SGSN-side Gb peers */
79 DECLARE_HASHTABLE(sgsn_nses, 8);
80
81 /* hash table of all gbproxy_cell */
82 DECLARE_HASHTABLE(cells, 8);
83
Daniel Willmann77493b12020-12-29 21:13:31 +010084 /* tlli<->nse cache used to map SUSPEND/RESUME (N)ACKS */
85 struct {
86 DECLARE_HASHTABLE(entries, 10);
87 struct osmo_timer_list timer;
88 /* Time in seconds that the entries should be valid */
89 uint8_t timeout;
90 } tlli_cache;
91
Daniel Willmannc8a50092021-01-17 13:11:41 +010092 /* imsi<->nse cache used for PAGING REJECT */
93 struct {
94 DECLARE_HASHTABLE(entries, 10);
95 struct osmo_timer_list timer;
96 /* Time in seconds that the entries should be valid */
97 uint8_t timeout;
98 } imsi_cache;
99
Daniel Willmannee834af2020-12-14 16:22:39 +0100100 /* List of all SGSNs */
101 struct llist_head sgsns;
102
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200103 /* Counter */
104 struct rate_ctr_group *ctrg;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200105};
106
Harald Weltee5209642020-12-05 19:59:45 +0100107/* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */
108struct gbproxy_cell {
109 /* linked to gbproxy_config.cells hashtable */
110 struct hlist_node list;
111
112 /* point back to the config */
113 struct gbproxy_config *cfg;
114
115 /* BVCI of PTP BVCs associated to this cell */
116 uint16_t bvci;
117
Philipp Maierda3af942021-02-04 21:54:09 +0100118 struct {
119 /* Routing Area that this cell is part of */
120 struct gprs_ra_id raid;
121
122 /* Cell ID of this cell */
123 uint16_t cid;
124 } id;
Harald Weltee5209642020-12-05 19:59:45 +0100125
126 /* pointer to the BSS-side BVC */
127 struct gbproxy_bvc *bss_bvc;
128
129 /* pointers to SGSN-side BVC (one for each pool member) */
Daniel Willmannee834af2020-12-14 16:22:39 +0100130 struct gbproxy_bvc *sgsn_bvc[GBPROXY_MAX_NR_SGSN];
Harald Weltee5209642020-12-05 19:59:45 +0100131};
132
Daniel Willmanne50550e2020-11-26 18:19:21 +0100133/* One BVC inside an NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100134struct gbproxy_bvc {
135 /* linked to gbproxy_nse.bvcs */
Harald Welte8b4c7942020-12-05 10:14:49 +0100136 struct hlist_node list;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200137
Harald Welte560bdb32020-12-04 22:24:47 +0100138 /* The NSE this BVC belongs to */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100139 struct gbproxy_nse *nse;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200140
Harald Welte560bdb32020-12-04 22:24:47 +0100141 /* PTP BVCI of this BVC */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200142 uint16_t bvci;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200143
Philipp Maierda3af942021-02-04 21:54:09 +0100144 /* Routing Area that this BVC is part of */
145 struct gprs_ra_id raid;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200146
147 /* Counter */
148 struct rate_ctr_group *ctrg;
Harald Weltee5209642020-12-05 19:59:45 +0100149
150 /* the cell to which this BVC belongs */
151 struct gbproxy_cell *cell;
152
153 /* per-BVC FSM instance */
154 struct osmo_fsm_inst *fi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200155};
156
Harald Welte560bdb32020-12-04 22:24:47 +0100157/* one NS Entity that we interact with (BSS/PCU) */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100158struct gbproxy_nse {
Harald Welte2b7b1bb2020-12-04 23:58:20 +0100159 /* linked to gbproxy_config.bss_nses */
Harald Welted2fef952020-12-05 00:31:07 +0100160 struct hlist_node list;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100161
162 /* point back to the config */
163 struct gbproxy_config *cfg;
164
Harald Welte560bdb32020-12-04 22:24:47 +0100165 /* NSEI of the NSE */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100166 uint16_t nsei;
167
Daniel Willmanna8b61652021-02-12 05:05:14 +0100168 /* Maximum side of the NS-UNITDATA NS SDU that can be transported by the NSE */
169 uint16_t max_sdu_len;
170
Harald Weltee5209642020-12-05 19:59:45 +0100171 /* Are we facing towards a SGSN (true) or BSS (false) */
172 bool sgsn_facing;
173
Daniel Willmanne50550e2020-11-26 18:19:21 +0100174 /* List of all BVCs in this NSE */
Harald Welte8b4c7942020-12-05 10:14:49 +0100175 DECLARE_HASHTABLE(bvcs, 10);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100176};
177
Daniel Willmannee834af2020-12-14 16:22:39 +0100178/* SGSN configuration such as pool options (only for NSE where sgsn_facing == true) */
179struct gbproxy_sgsn {
180 /* linked to gbproxy_config.sgsns */
181 struct llist_head list;
182
183 /* The NSE belonging to this SGSN */
184 struct gbproxy_nse *nse;
185
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100186 /* Name of the SGSN */
187 char *name;
188
Daniel Willmannee834af2020-12-14 16:22:39 +0100189 /* Pool configuration for the sgsn (only valid if sgsn_facing == true) */
190 struct {
191 bool allow_attach;
192 struct osmo_nri_ranges *nri_ranges;
193 } pool;
194};
195
Daniel Willmann77493b12020-12-29 21:13:31 +0100196/* TLLI cache */
197struct gbproxy_tlli_cache_entry {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100198 /* linked to gbproxy_config.tlli_cache.entries */
Daniel Willmann77493b12020-12-29 21:13:31 +0100199 struct hlist_node list;
200
201 /* TLLI of the entry */
202 uint32_t tlli;
203 /* When was this entry last seen */
204 time_t tstamp;
205 /* The Cell this TLLI was last seen */
206 struct gbproxy_nse *nse;
207};
208
Daniel Willmannc8a50092021-01-17 13:11:41 +0100209/* IMSI cache */
210struct gbproxy_imsi_cache_entry {
211 /* linked to gbproxy_config.imsi_cache.entries */
212 struct hlist_node list;
213
214 /* IMSI of the entry */
215 char imsi[OSMO_IMSI_BUF_SIZE];
216 /* When was this entry last seen */
217 time_t tstamp;
218 /* The SGSN where the request came from */
219 struct gbproxy_nse *nse;
220};
221
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100222/* Convenience logging macros for NSE/BVC */
223#define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100224 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \
225 (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100226#define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \
227 LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS)
228
229#define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Weltee5209642020-12-05 19:59:45 +0100230 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \
231 (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \
232 osmo_fsm_inst_state_name((BVC)->fi), ## ARGS)
Daniel Willmann066c4cb2020-12-01 16:36:29 +0100233#define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \
234 LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS)
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200235
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100236#define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \
237 LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS)
238#define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \
239 LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS)
240
Daniel Willmannee834af2020-12-14 16:22:39 +0100241#define LOGPSGSN_CAT(SGSN, SUBSYS, LEVEL, FMT, ARGS...) \
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100242 LOGP(SUBSYS, LEVEL, "NSE(%05u)-SGSN(%s) " FMT, (SGSN)->nse->nsei, (SGSN)->name, ## ARGS)
Daniel Willmannee834af2020-12-14 16:22:39 +0100243#define LOGPSGSN(SGSN, LEVEL, FMT, ARGS...) \
244 LOGPSGSN_CAT(SGSN, DGPRS, LEVEL, FMT, ## ARGS)
245
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200246/* gb_proxy_vty .c */
247
248int gbproxy_vty_init(void);
249int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
250
Daniel Willmann13404b72018-06-01 07:21:20 +0200251/* gb_proxy_ctrl.c */
252int gb_ctrl_cmds_install(void);
253
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200254
255/* gb_proxy.c */
256int gbproxy_init_config(struct gbproxy_config *cfg);
257
258/* Main input function for Gb proxy */
Alexander Couzens951e1332020-09-22 13:21:46 +0200259int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200260
261int gbprox_signal(unsigned int subsys, unsigned int signal,
262 void *handler_data, void *signal_data);
263
Alexander Couzens951e1332020-09-22 13:21:46 +0200264
265int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200266
267void gbprox_reset(struct gbproxy_config *cfg);
268
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200269/* Peer handling */
Harald Weltee5209642020-12-05 19:59:45 +0100270#define NSE_F_SGSN 0x0001
271#define NSE_F_BSS 0x0002
272
273struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100274struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci);
275void gbproxy_bvc_free(struct gbproxy_bvc *bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100276int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci);
277
Philipp Maiere4597ec2021-02-09 16:02:00 +0100278struct 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 +0100279struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci);
Daniel Willmannfccbef02021-01-19 17:59:19 +0100280struct 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 +0100281void gbproxy_cell_free(struct gbproxy_cell *cell);
282bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200283
Daniel Willmanne50550e2020-11-26 18:19:21 +0100284/* NSE handling */
Harald Weltee5209642020-12-05 19:59:45 +0100285struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100286void gbproxy_nse_free(struct gbproxy_nse *nse);
Harald Weltee5209642020-12-05 19:59:45 +0100287struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags);
288struct 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 +0100289struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100290struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi);
Daniel Willmann77493b12020-12-29 21:13:31 +0100291
292/* TLLI cache */
293void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli);
294void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli);
295int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100296
Daniel Willmannc8a50092021-01-17 13:11:41 +0100297/* IMSI cache */
298void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi);
299void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi);
300int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg);
301
Daniel Willmannee834af2020-12-14 16:22:39 +0100302/* SGSN handling */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100303struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100304void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100305struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name);
Daniel Willmannee834af2020-12-14 16:22:39 +0100306struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei);
307struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei);
308struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100309struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
310 uint32_t tlli);
Daniel Willmannee834af2020-12-14 16:22:39 +0100311
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200312#endif