blob: 6c0433271216454db6adc523193e138ac7727539 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp.h */
2
Sylvain Munaut12ba7782014-06-16 10:13:40 +02003#pragma once
Harald Welte9ba50052010-03-14 15:45:01 +08004
Harald Welte8f9a3ee2010-05-02 11:26:34 +02005#include <stdint.h>
Harald Welted11c0592012-09-06 21:57:11 +02006#include <osmocom/core/timer.h>
7#include <osmocom/core/linuxlist.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +02008
Harald Welte73952e32012-06-16 14:59:56 +08009#include <osmocom/gsm/gsm48.h>
Harald Welte15a36432012-06-17 12:16:31 +080010#include <osmocom/gsm/prim.h>
Harald Welte73952e32012-06-16 14:59:56 +080011
Harald Welte8648e492012-06-17 13:12:51 +080012#include <osmocom/gprs/protocol/gsm_08_18.h>
Philipp Maierbd10c212020-12-14 22:28:19 +010013#include <osmocom/gprs/protocol/gsm_24_301.h>
Philipp Maier7741bc32021-01-07 21:55:48 +010014#include <osmocom/gprs/gprs_bssgp_rim.h>
Harald Welte8648e492012-06-17 13:12:51 +080015
Harald Welteaf086782010-05-11 10:01:17 +020016/* gprs_bssgp_util.c */
Alexander Couzens85a8fd32020-07-18 15:57:07 +020017
Harald Welte8f825282020-12-04 18:50:11 +010018#define BSSGP_PDUF_UL 0x0001 /* PDU may occur in uplink */
19#define BSSGP_PDUF_DL 0x0002 /* PDU may occur in downlink */
20#define BSSGP_PDUF_SIG 0x0004 /* PDU may occur on Signaling BVC */
21#define BSSGP_PDUF_PTP 0x0008 /* PDU may occur on PTP BVC */
22#define BSSGP_PDUF_PTM 0x0010 /* PDU may occur on PTM BVC */
23
24extern const struct osmo_tlv_prot_def osmo_pdef_bssgp;
25
26/*! return the PDU type flags (UL/DL/SIG/PTP/PTM) of specified PDU type */
27static inline uint32_t bssgp_pdu_type_flags(uint8_t pdu_type) {
28 return osmo_tlv_prot_msgt_flags(&osmo_pdef_bssgp, pdu_type);
29}
30
31typedef int (*bssgp_bvc_send)(void *ctx, struct msgb *msg);
Harald Welteaf086782010-05-11 10:01:17 +020032extern struct gprs_ns_inst *bssgp_nsi;
Alexander Couzens85a8fd32020-07-18 15:57:07 +020033void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data);
Harald Welteaf086782010-05-11 10:01:17 +020034struct msgb *bssgp_msgb_alloc(void);
Jacob Erlbeckf78ec5c2015-11-17 09:53:23 +010035struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name);
Harald Welteaf086782010-05-11 10:01:17 +020036const char *bssgp_cause_str(enum gprs_bssgp_cause cause);
Maxc0d9a6c2016-03-09 12:29:23 +010037const char *bssgp_pdu_str(enum bssgp_pdu_type pdu);
Daniel Willmann2d42b902020-09-26 09:11:05 +020038int bssgp_tx_bvc_reset_nsei_bvci(uint16_t nsei, uint16_t bvci, enum gprs_bssgp_cause cause, const struct gprs_ra_id *ra_id, uint16_t cell_id);
Harald Welteaf086782010-05-11 10:01:17 +020039/* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */
40int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei,
41 uint16_t bvci, uint16_t ns_bvci);
42/* Chapter 10.4.14: Status */
43int bssgp_tx_status(uint8_t cause, uint16_t *bvci, struct msgb *orig_msg);
44
Harald Welte15a36432012-06-17 12:16:31 +080045enum bssgp_prim {
46 PRIM_BSSGP_DL_UD,
47 PRIM_BSSGP_UL_UD,
48 PRIM_BSSGP_PTM_UD,
49
50 PRIM_BSSGP_GMM_SUSPEND,
51 PRIM_BSSGP_GMM_RESUME,
52 PRIM_BSSGP_GMM_PAGING,
53
54 PRIM_NM_FLUSH_LL,
55 PRIM_NM_LLC_DISCARDED,
56 PRIM_NM_BVC_RESET,
57 PRIM_NM_BVC_BLOCK,
58 PRIM_NM_BVC_UNBLOCK,
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010059 PRIM_NM_STATUS,
Philipp Maier1eaa7bc2020-12-16 21:07:04 +010060
61 PRIM_BSSGP_RIM_PDU_TRANSFER,
Harald Welte15a36432012-06-17 12:16:31 +080062};
63
64struct osmo_bssgp_prim {
65 struct osmo_prim_hdr oph;
66
67 /* common fields */
68 uint16_t nsei;
69 uint16_t bvci;
70 uint32_t tlli;
71 struct tlv_parsed *tp;
72 struct gprs_ra_id *ra_id;
73
74 /* specific fields */
75 union {
76 struct {
Holger Hans Peter Freytherd296f422012-08-03 09:59:20 +020077 uint8_t suspend_ref;
Harald Welte15a36432012-06-17 12:16:31 +080078 } resume;
Philipp Maier7741bc32021-01-07 21:55:48 +010079 struct bssgp_ran_information_pdu rim_pdu;
Harald Welte15a36432012-06-17 12:16:31 +080080 } u;
81};
82
Harald Welteaf086782010-05-11 10:01:17 +020083/* gprs_bssgp.c */
84
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085/*! BSSGP flow control (SGSN side) According to Section 8.2 */
Harald Welted11c0592012-09-06 21:57:11 +020086struct bssgp_flow_control {
Harald Weltebb826222012-09-07 10:22:01 +020087 uint32_t bucket_size_max; /*!< maximum size of the bucket (octets) */
88 uint32_t bucket_leak_rate; /*!< leak rate of the bucket (octets/sec) */
Harald Welted11c0592012-09-06 21:57:11 +020089
90 uint32_t bucket_counter; /*!< number of tokens in the bucket */
91 struct timeval time_last_pdu; /*!< timestamp of last PDU sent */
92
93 /* the built-in queue */
Harald Weltebb826222012-09-07 10:22:01 +020094 uint32_t max_queue_depth; /*!< how many packets to queue (mgs) */
95 uint32_t queue_depth; /*!< current length of queue (msgs) */
Harald Welted11c0592012-09-06 21:57:11 +020096 struct llist_head queue; /*!< linked list of msgb's */
97 struct osmo_timer_list timer; /*!< timer-based dequeueing */
98
99 /*! callback to be called at output of flow control */
100 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
101 uint32_t llc_pdu_len, void *priv);
102};
103
Harald Weltea78b9c22010-05-17 23:02:42 +0200104#define BVC_S_BLOCKED 0x0001
105
106/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
107struct bssgp_bvc_ctx {
108 struct llist_head list;
109
Harald Welteeb3ccf62011-11-25 08:15:42 +0100110 struct gprs_ra_id ra_id; /*!< parsed RA ID of the remote BTS */
111 uint16_t cell_id; /*!< Cell ID of the remote BTS */
Harald Weltea78b9c22010-05-17 23:02:42 +0200112
113 /* NSEI and BVCI of underlying Gb link. Together they
114 * uniquely identify a link to a BTS (5.4.4) */
115 uint16_t bvci;
116 uint16_t nsei;
117
118 uint32_t state;
119
120 struct rate_ctr_group *ctrg;
121
Harald Welted8b47692012-09-07 11:29:32 +0200122 struct bssgp_flow_control *fc;
Harald Weltebb826222012-09-07 10:22:01 +0200123 /*! default maximum size of per-MS bucket in octets */
Harald Welted11c0592012-09-06 21:57:11 +0200124 uint32_t bmax_default_ms;
Harald Weltebb826222012-09-07 10:22:01 +0200125 /*! default bucket leak rate of per-MS bucket in octests/s */
Harald Welted11c0592012-09-06 21:57:11 +0200126 uint32_t r_default_ms;
127
Alexander Couzens7819ff62021-02-17 02:30:33 +0100128 /*! BSS or SGSN. This defines the local state. */
129 bool is_sgsn;
Harald Weltea78b9c22010-05-17 23:02:42 +0200130 /* we might want to add this as a shortcut later, avoiding the NSVC
131 * lookup for every packet, similar to a routing cache */
132 //struct gprs_nsvc *nsvc;
133};
134extern struct llist_head bssgp_bvc_ctxts;
Daniel Willmann07a923f2020-10-16 16:34:54 +0200135/* Create a BTS Context with BVCI+NSEI */
136struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei);
Harald Weltea78b9c22010-05-17 23:02:42 +0200137/* Find a BTS Context based on parsed RA ID and Cell ID */
138struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid);
139/* Find a BTS context based on BVCI+NSEI tuple */
140struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700141/* Free a given BTS context */
142void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx);
Harald Weltea78b9c22010-05-17 23:02:42 +0200143
Harald Welte85fc3142011-11-25 08:58:40 +0100144#define BVC_F_BLOCKED 0x0001
145
146enum bssgp_ctr {
147 BSSGP_CTR_PKTS_IN,
148 BSSGP_CTR_PKTS_OUT,
149 BSSGP_CTR_BYTES_IN,
150 BSSGP_CTR_BYTES_OUT,
151 BSSGP_CTR_BLOCKED,
152 BSSGP_CTR_DISCARDED,
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100153 BSSGP_CTR_STATUS,
Harald Welte85fc3142011-11-25 08:58:40 +0100154};
155
156
Pablo Neira Ayusoff663232011-03-22 16:47:59 +0100157#include <osmocom/gsm/tlv.h>
Harald Welte605ac5d2012-06-16 16:09:52 +0800158#include <osmocom/gprs/gprs_msgb.h>
Harald Welte3771d092010-04-30 20:26:32 +0200159
Harald Welte61112342010-05-13 19:25:59 +0200160/* BSSGP-UL-UNITDATA.ind */
Harald Weltede4599c2012-06-17 13:04:02 +0800161int bssgp_rcvmsg(struct msgb *msg);
Harald Welte61112342010-05-13 19:25:59 +0200162
163/* BSSGP-DL-UNITDATA.req */
Harald Welte8ef54d12012-06-17 09:31:16 +0800164struct bssgp_lv {
165 uint16_t len;
166 uint8_t *v;
167};
168/* parameters for BSSGP downlink userdata transmission */
169struct bssgp_dl_ud_par {
170 uint32_t *tlli;
171 char *imsi;
Harald Welted11c0592012-09-06 21:57:11 +0200172 struct bssgp_flow_control *fc;
Harald Welte8ef54d12012-06-17 09:31:16 +0800173 uint16_t drx_parms;
174 /* FIXME: priority */
175 struct bssgp_lv ms_ra_cap;
176 uint8_t qos_profile[3];
177};
Harald Weltede4599c2012-06-17 13:04:02 +0800178int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
179 struct bssgp_dl_ud_par *dup);
Harald Welte61112342010-05-13 19:25:59 +0200180
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200181uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf);
Harald Welte85fc3142011-11-25 08:58:40 +0100182int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
183 uint16_t cid);
Harald Welte9ba50052010-03-14 15:45:01 +0800184
Harald Welte3771d092010-04-30 20:26:32 +0200185/* Wrapper around TLV parser to parse BSSGP IEs */
Harald Welte465c9a52020-11-18 11:52:14 +0100186static inline int bssgp_tlv_parse(struct tlv_parsed *tp, const uint8_t *buf, int len)
Harald Welte3771d092010-04-30 20:26:32 +0200187{
188 return tlv_parse(tp, &tvlv_att_def, buf, len, 0, 0);
189}
190
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200191/*! BSSGP Paging mode */
Harald Welte68b4f032010-06-09 16:22:28 +0200192enum bssgp_paging_mode {
193 BSSGP_PAGING_PS,
194 BSSGP_PAGING_CS,
195};
196
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200197/*! BSSGP Paging scope */
Harald Welte68b4f032010-06-09 16:22:28 +0200198enum bssgp_paging_scope {
Harald Welteeb3ccf62011-11-25 08:15:42 +0100199 BSSGP_PAGING_BSS_AREA, /*!< all cells in BSS */
200 BSSGP_PAGING_LOCATION_AREA, /*!< all cells in LA */
201 BSSGP_PAGING_ROUTEING_AREA, /*!< all cells in RA */
202 BSSGP_PAGING_BVCI, /*!< one cell */
Harald Welte68b4f032010-06-09 16:22:28 +0200203};
204
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200205/*! BSSGP paging information */
Harald Welte68b4f032010-06-09 16:22:28 +0200206struct bssgp_paging_info {
Harald Welteeb3ccf62011-11-25 08:15:42 +0100207 enum bssgp_paging_mode mode; /*!< CS or PS paging */
208 enum bssgp_paging_scope scope; /*!< bssgp_paging_scope */
209 struct gprs_ra_id raid; /*!< RA Identifier */
210 uint16_t bvci; /*!< BVCI */
Harald Welte85fc3142011-11-25 08:58:40 +0100211 char *imsi; /*!< IMSI, if any */
Harald Welteeb3ccf62011-11-25 08:15:42 +0100212 uint32_t *ptmsi; /*!< P-TMSI, if any */
213 uint16_t drx_params; /*!< DRX parameters */
214 uint8_t qos[3]; /*!< QoS parameters */
Harald Welte68b4f032010-06-09 16:22:28 +0200215};
216
217/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800218int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
219 struct bssgp_paging_info *pinfo);
Harald Welte68b4f032010-06-09 16:22:28 +0200220
Harald Weltebb826222012-09-07 10:22:01 +0200221void bssgp_fc_init(struct bssgp_flow_control *fc,
222 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
223 uint32_t max_queue_depth,
224 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
225 uint32_t llc_pdu_len, void *priv));
226
Harald Welted11c0592012-09-06 21:57:11 +0200227/* input function of the flow control implementation, called first
228 * for the MM flow control, and then as the MM flow control output
229 * callback in order to perform BVC flow control */
230int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
231 uint32_t llc_pdu_len, void *priv);
232
233/* Initialize the Flow Control parameters for a new MS according to
234 * default values for the BVC specified by BVCI and NSEI */
235int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200236 uint16_t nsei, uint32_t max_queue_depth);
Harald Welted11c0592012-09-06 21:57:11 +0200237
Harald Weltee61d4592022-11-03 11:05:58 +0100238void bssgp_flush_all_queues(void);
Alexander Couzensacc0a072018-08-07 11:22:28 +0200239void bssgp_fc_flush_queue(struct bssgp_flow_control *fc);
240
Harald Weltefdc73882010-05-18 08:02:36 +0200241/* gprs_bssgp_vty.c */
Harald Weltede4599c2012-06-17 13:04:02 +0800242int bssgp_vty_init(void);
Harald Weltefde19ed2020-12-07 21:43:51 +0100243void bssgp_set_log_ss(int ss) OSMO_DEPRECATED("Use DLBSSGP instead!\n");
Harald Weltefdc73882010-05-18 08:02:36 +0200244
Harald Welte15a36432012-06-17 12:16:31 +0800245int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx);