blob: 8892cf4dfd0871fb85595f97362319176c786e74 [file] [log] [blame]
Harald Welteb77c6972010-05-01 11:28:43 +02001#ifndef _GB_PROXY_H
2#define _GB_PROXY_H
3
Harald Welteb77c6972010-05-01 11:28:43 +02004
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +01005#include <osmocom/core/msgb.h>
Alexander Couzens82182d02020-09-22 13:21:46 +02006#include <osmocom/core/timer.h>
Harald Welte5687ae62020-12-05 19:59:45 +01007#include <osmocom/core/fsm.h>
Harald Welte78db2442020-12-05 00:31:07 +01008#include <osmocom/core/hashtable.h>
Neels Hofmeyr10719b72018-02-21 00:39:36 +01009#include <osmocom/gsm/gsm23003.h>
Harald Welteb77c6972010-05-01 11:28:43 +020010
Alexander Couzens82182d02020-09-22 13:21:46 +020011#include <osmocom/gprs/gprs_ns2.h>
Harald Welte4b037e42010-05-19 19:45:32 +020012#include <osmocom/vty/command.h>
Harald Welteb77c6972010-05-01 11:28:43 +020013
Holger Hans Peter Freyther3fa26442014-08-04 16:27:11 +020014#include <sys/types.h>
15#include <regex.h>
Harald Welteaed46ec2019-03-22 09:44:42 +010016#include <stdbool.h>
Holger Hans Peter Freyther3fa26442014-08-04 16:27:11 +020017
Jacob Erlbeck65750282014-09-22 15:41:21 +020018#define GBPROXY_INIT_VU_GEN_TX 256
19
Daniel Willmanne8c8ec92020-12-02 19:33:50 +010020/* BVCI uses 16 bits */
21#define BVC_LOG_CTX_FLAG (1<<17)
22
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020023struct rate_ctr_group;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020024struct gprs_gb_parse_context;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +020025struct tlv_parsed;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020026
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020027enum 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 Erlbeck9114bee2014-08-19 12:21:01 +020039};
40
Harald Welte8cd74402020-12-04 22:24:47 +010041enum gbproxy_bvc_ctr {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020042 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 Freyther98fa3dc2015-11-09 14:30:22 +010047 GBPROX_PEER_CTR_LAST,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020048};
49
Harald Welted3dcaf02019-03-22 08:26:45 +010050/* global gb-proxy configuration */
Harald Welteb77c6972010-05-01 11:28:43 +020051struct gbproxy_config {
Harald Welted3dcaf02019-03-22 08:26:45 +010052 /* NS instance of libosmogb */
Alexander Couzens82182d02020-09-22 13:21:46 +020053 struct gprs_ns2_inst *nsi;
Jacob Erlbeck67a44452014-05-19 10:14:58 +020054
Harald Welte0e1b7912020-12-04 23:58:20 +010055 /* Linked list of all BSS side Gb peers */
Harald Welte78db2442020-12-05 00:31:07 +010056 DECLARE_HASHTABLE(bss_nses, 8);
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020057
Harald Welte5687ae62020-12-05 19:59:45 +010058 /* 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 Freythera7027a02014-08-04 11:19:56 +020064 /* Counter */
65 struct rate_ctr_group *ctrg;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020066};
67
Harald Welte5687ae62020-12-05 19:59:45 +010068/* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */
69struct 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 Willmann447ad442020-11-26 18:19:21 +010089/* One BVC inside an NSE */
Harald Welte8cd74402020-12-04 22:24:47 +010090struct gbproxy_bvc {
91 /* linked to gbproxy_nse.bvcs */
Harald Welte993d3f42020-12-05 10:14:49 +010092 struct hlist_node list;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020093
Harald Welte8cd74402020-12-04 22:24:47 +010094 /* The NSE this BVC belongs to */
Daniel Willmann447ad442020-11-26 18:19:21 +010095 struct gbproxy_nse *nse;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020096
Harald Welte8cd74402020-12-04 22:24:47 +010097 /* PTP BVCI of this BVC */
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020098 uint16_t bvci;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020099
Harald Welte8cd74402020-12-04 22:24:47 +0100100 /* Routing Area that this BVC is part of (raw 04.08 encoding) */
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200101 uint8_t ra[6];
102
103 /* Counter */
104 struct rate_ctr_group *ctrg;
Harald Welte5687ae62020-12-05 19:59:45 +0100105
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 Freyther18739ea2014-08-04 11:10:09 +0200111};
112
Harald Welte8cd74402020-12-04 22:24:47 +0100113/* one NS Entity that we interact with (BSS/PCU) */
Daniel Willmann447ad442020-11-26 18:19:21 +0100114struct gbproxy_nse {
Harald Welte0e1b7912020-12-04 23:58:20 +0100115 /* linked to gbproxy_config.bss_nses */
Harald Welte78db2442020-12-05 00:31:07 +0100116 struct hlist_node list;
Daniel Willmann447ad442020-11-26 18:19:21 +0100117
118 /* point back to the config */
119 struct gbproxy_config *cfg;
120
Harald Welte8cd74402020-12-04 22:24:47 +0100121 /* NSEI of the NSE */
Daniel Willmann447ad442020-11-26 18:19:21 +0100122 uint16_t nsei;
123
Harald Welte5687ae62020-12-05 19:59:45 +0100124 /* Are we facing towards a SGSN (true) or BSS (false) */
125 bool sgsn_facing;
126
Daniel Willmann447ad442020-11-26 18:19:21 +0100127 /* List of all BVCs in this NSE */
Harald Welte993d3f42020-12-05 10:14:49 +0100128 DECLARE_HASHTABLE(bvcs, 10);
Daniel Willmann447ad442020-11-26 18:19:21 +0100129};
130
Daniel Willmann74187972020-12-01 16:36:29 +0100131/* Convenience logging macros for NSE/BVC */
132#define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \
Harald Welte5687ae62020-12-05 19:59:45 +0100133 LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \
134 (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS)
Daniel Willmann74187972020-12-01 16:36:29 +0100135#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 Welte5687ae62020-12-05 19:59:45 +0100139 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 Willmann74187972020-12-01 16:36:29 +0100142#define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \
143 LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS)
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200144
Harald Welte7cb76a42020-12-07 12:03:10 +0100145#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 Welteb77c6972010-05-01 11:28:43 +0200150/* gb_proxy_vty .c */
151
152int gbproxy_vty_init(void);
153int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
154
Daniel Willmannc977afa2018-06-01 07:21:20 +0200155/* gb_proxy_ctrl.c */
156int gb_ctrl_cmds_install(void);
157
Harald Welteb77c6972010-05-01 11:28:43 +0200158
159/* gb_proxy.c */
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200160int gbproxy_init_config(struct gbproxy_config *cfg);
Harald Welteb77c6972010-05-01 11:28:43 +0200161
162/* Main input function for Gb proxy */
Alexander Couzens82182d02020-09-22 13:21:46 +0200163int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Harald Welteb77c6972010-05-01 11:28:43 +0200164
Harald Weltec1c1dd22010-05-11 06:34:24 +0200165int gbprox_signal(unsigned int subsys, unsigned int signal,
166 void *handler_data, void *signal_data);
Harald Welte1ccbf442010-05-14 11:53:08 +0000167
Alexander Couzens82182d02020-09-22 13:21:46 +0200168
169int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Harald Welte1ccbf442010-05-14 11:53:08 +0000170
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200171void gbprox_reset(struct gbproxy_config *cfg);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200172
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200173/* Peer handling */
Harald Welte5687ae62020-12-05 19:59:45 +0100174#define NSE_F_SGSN 0x0001
175#define NSE_F_BSS 0x0002
176
177struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci);
Harald Welte8cd74402020-12-04 22:24:47 +0100178struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci);
179void gbproxy_bvc_free(struct gbproxy_bvc *bvc);
Harald Welte5687ae62020-12-05 19:59:45 +0100180int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci);
181
182struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci);
183struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci);
184void gbproxy_cell_free(struct gbproxy_cell *cell);
185bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc);
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200186
Daniel Willmann447ad442020-11-26 18:19:21 +0100187/* NSE handling */
Harald Welte5687ae62020-12-05 19:59:45 +0100188struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmann447ad442020-11-26 18:19:21 +0100189void gbproxy_nse_free(struct gbproxy_nse *nse);
Harald Welte5687ae62020-12-05 19:59:45 +0100190struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags);
191struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing);
Daniel Willmann447ad442020-11-26 18:19:21 +0100192
Harald Welteb77c6972010-05-01 11:28:43 +0200193#endif