blob: 15b0bc501ab5d9e1d788119ba0016c2c43d04431 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_internal.h */
2
3#pragma once
4
5#include <stdbool.h>
6#include <stdint.h>
7
8#include <osmocom/gprs/protocol/gsm_08_16.h>
9#include <osmocom/gprs/gprs_ns2.h>
10
11struct osmo_fsm_inst;
12struct tlv_parsed;
13struct vty;
14
15struct gprs_ns2_vc_driver;
16struct gprs_ns2_vc_bind;
17
18
19
20#define NS_TIMERS_COUNT 8
21#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries|tsns-prov)"
22#define NS_TIMERS_HELP \
23 "(un)blocking Timer (Tns-block) timeout\n" \
24 "(un)blocking Timer (Tns-block) number of retries\n" \
25 "Reset Timer (Tns-reset) timeout\n" \
26 "Reset Timer (Tns-reset) number of retries\n" \
27 "Test Timer (Tns-test) timeout\n" \
28 "Alive Timer (Tns-alive) timeout\n" \
29 "Alive Timer (Tns-alive) number of retries\n" \
30 "SNS Provision Timer (Tsns-prov) timeout\n"
31
32/* Educated guess - LLC user payload is 1500 bytes plus possible headers */
33#define NS_ALLOC_SIZE 3072
34#define NS_ALLOC_HEADROOM 20
35
36enum ns2_timeout {
37 NS_TOUT_TNS_BLOCK,
38 NS_TOUT_TNS_BLOCK_RETRIES,
39 NS_TOUT_TNS_RESET,
40 NS_TOUT_TNS_RESET_RETRIES,
41 NS_TOUT_TNS_TEST,
42 NS_TOUT_TNS_ALIVE,
43 NS_TOUT_TNS_ALIVE_RETRIES,
44 NS_TOUT_TSNS_PROV,
45};
46
47enum nsvc_timer_mode {
48 /* standard timers */
49 NSVC_TIMER_TNS_TEST,
50 NSVC_TIMER_TNS_ALIVE,
51 NSVC_TIMER_TNS_RESET,
52 _NSVC_TIMER_NR,
53};
54
55enum ns_stat {
56 NS_STAT_ALIVE_DELAY,
57};
58
59/*! Osmocom NS link layer types */
60enum gprs_ns_ll {
61 GPRS_NS_LL_UDP, /*!< NS/UDP/IP */
62 GPRS_NS_LL_E1, /*!< NS/E1 */
63 GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */
64};
65
66/*! Osmocom NS2 VC create status */
67enum gprs_ns2_cs {
68 GPRS_NS2_CS_CREATED, /*!< A NSVC object has been created */
69 GPRS_NS2_CS_FOUND, /*!< A NSVC object has been found */
70 GPRS_NS2_CS_REJECTED, /*!< Rejected and answered message */
71 GPRS_NS2_CS_SKIPPED, /*!< Skipped message */
72 GPRS_NS2_CS_ERROR, /*!< Failed to process message */
73};
74
75
76#define NSE_S_BLOCKED 0x0001
77#define NSE_S_ALIVE 0x0002
78#define NSE_S_RESET 0x0004
79
80#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
81#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
82#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
83
84/*! An instance of the NS protocol stack */
85struct gprs_ns2_inst {
86 /*! callback to the user for incoming UNIT DATA IND */
87 osmo_prim_cb cb;
88
89 /*! callback data */
90 void *cb_data;
91
92 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
93 struct llist_head binding;
94
95 /*! linked lists of all NSVC in this instance */
96 struct llist_head nse;
97
98 /*! create dynamic NSE on receiving packages */
99 bool create_nse;
100
101 uint16_t timeout[NS_TIMERS_COUNT];
102
103 /*! workaround for rate counter until rate counter accepts char str as index */
104 uint32_t rate_ctr_idx;
105};
106
107/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
108struct gprs_ns2_nse {
109 uint16_t nsei;
110
111 /*! entry back to ns2_inst */
112 struct gprs_ns2_inst *nsi;
113
114 /*! llist entry for gprs_ns2_inst */
115 struct llist_head list;
116
117 /*! llist head to hold all nsvc */
118 struct llist_head nsvc;
119
120 /*! true if this NSE was created by VTY or pcu socket) */
121 bool persistent;
122
123 /*! true if this NSE has at least one alive VC */
124 bool alive;
125
126 struct osmo_fsm_inst *bss_sns_fi;
127};
128
129/*! Structure representing a single NS-VC */
130struct gprs_ns2_vc {
131 /*! list of NS-VCs within NSE */
132 struct llist_head list;
133
134 /*! list of NS-VCs within bind, bind is the owner! */
135 struct llist_head blist;
136
137 /*! pointer to NS Instance */
138 struct gprs_ns2_nse *nse;
139
140 /*! pointer to NS VL bind. bind own the memory of this instance */
141 struct gprs_ns2_vc_bind *bind;
142
143 /*! true if this NS was created by VTY or pcu socket) */
144 bool persistent;
145
146 /*! uniquely identifies NS-VC if VC contains nsvci */
147 uint16_t nsvci;
148
149 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
150 uint8_t sig_weight;
151
152 /*! signaling weight. 0 = don't use for user data (BVCI != 0) */
153 uint8_t data_weight;
154
155 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
156 void *priv;
157
158 bool nsvci_is_valid;
159 bool sns_only;
160
161 struct rate_ctr_group *ctrg;
162 struct osmo_stat_item_group *statg;
163
164 /*! which link-layer are we based on? */
165 enum gprs_ns_ll ll;
166 enum gprs_ns2_vc_mode mode;
167
168 struct osmo_fsm_inst *fi;
169};
170
171/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
172struct gprs_ns2_vc_bind {
173 /*! list entry in nsi */
174 struct llist_head list;
175 /*! list of all VC */
176 struct llist_head nsvc;
177 /*! driver private structure */
178 void *priv;
179 /*! a pointer back to the nsi */
180 struct gprs_ns2_inst *nsi;
181 struct gprs_ns2_vc_driver *driver;
182
183 /*! if VCs use reset/block/unblock method. IP shall not use this */
184 enum gprs_ns2_vc_mode vc_mode;
185
186 /*! send a msg over a VC */
187 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
188
189 /*! free the vc priv data */
190 void (*free_vc)(struct gprs_ns2_vc *nsvc);
191};
192
193
194struct gprs_ns2_vc_driver {
195 const char *name;
196 void *priv;
197 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
198};
199
200enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
201 struct msgb *msg,
202 const char *logname,
203 struct msgb **reject,
204 struct gprs_ns2_vc **success);
205
Alexander Couzensffd49d02020-09-24 00:47:17 +0200206int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200207 struct msgb *msg);
208
209struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
210 struct gprs_ns2_nse *nse,
211 bool initiater);
212
213struct msgb *gprs_ns2_msgb_alloc(void);
214
215void gprs_ns2_sns_dump_vty(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats);
216void ns2_prim_status_ind(struct gprs_ns2_inst *nsi,
217 uint16_t nsei, uint16_t bvci,
218 enum gprs_ns2_affecting_cause cause);
219void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
220
221/* message */
222int gprs_ns2_validate(struct gprs_ns2_vc *nsvc,
223 uint8_t pdu_type,
224 struct msgb *msg,
225 struct tlv_parsed *tp,
226 uint8_t *cause);
227
228/* SNS messages */
229int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
230 const struct gprs_ns_ie_ip4_elem *ip4_elems,
231 unsigned int num_ip4_elems,
232 const struct gprs_ns_ie_ip6_elem *ip6_elems,
233 unsigned int num_ip6_elems);
234int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
235 const struct gprs_ns_ie_ip4_elem *ip4_elems,
236 unsigned int num_ip4_elems,
237 const struct gprs_ns_ie_ip6_elem *ip6_elems,
238 unsigned int num_ip6_elems);
239int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
240int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
241 int ip4_ep_nr, int ip6_ep_nr);
242int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
243
244/* transmit message over a VC */
245int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
246int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
247
248int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
249int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
250
251int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
252int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
253
254int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
255int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
256
257int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
258 uint16_t bvci, uint8_t sducontrol,
259 struct msgb *msg);
260
261int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
262 uint16_t bvci, struct msgb *orig_msg);
263
264/* driver */
265struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
266 struct gprs_ns2_nse *nse,
267 struct osmo_sockaddr *remote);
268
269/* sns */
270int gprs_ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
271struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
272 const char *id);
273int ns2_sns_bss_fsm_start(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc,
274 struct osmo_sockaddr *remote);
275void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc);
276
277/* vc */
278struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
279 const char *id, bool initiate);
280int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
281int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
282int gprs_ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
283int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
284
285/* vty.c */
286void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind);
287
288/* nse */
289void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);