blob: 6ea2fcc2114ccd0523327b48698a315eb1df290a [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 */
Alexander Couzens841817e2020-11-19 00:41:29 +010063 GPRS_NS_LL_FR, /*!< NS/FR */
Alexander Couzens6a161492020-07-12 13:45:50 +020064 GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */
65};
66
67/*! Osmocom NS2 VC create status */
68enum gprs_ns2_cs {
69 GPRS_NS2_CS_CREATED, /*!< A NSVC object has been created */
70 GPRS_NS2_CS_FOUND, /*!< A NSVC object has been found */
71 GPRS_NS2_CS_REJECTED, /*!< Rejected and answered message */
72 GPRS_NS2_CS_SKIPPED, /*!< Skipped message */
73 GPRS_NS2_CS_ERROR, /*!< Failed to process message */
74};
75
76
77#define NSE_S_BLOCKED 0x0001
78#define NSE_S_ALIVE 0x0002
79#define NSE_S_RESET 0x0004
80
81#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
82#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
83#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
84
85/*! An instance of the NS protocol stack */
86struct gprs_ns2_inst {
87 /*! callback to the user for incoming UNIT DATA IND */
88 osmo_prim_cb cb;
89
90 /*! callback data */
91 void *cb_data;
92
93 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
94 struct llist_head binding;
95
96 /*! linked lists of all NSVC in this instance */
97 struct llist_head nse;
98
99 /*! create dynamic NSE on receiving packages */
100 bool create_nse;
101
102 uint16_t timeout[NS_TIMERS_COUNT];
103
104 /*! workaround for rate counter until rate counter accepts char str as index */
105 uint32_t rate_ctr_idx;
106};
107
108/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
109struct gprs_ns2_nse {
110 uint16_t nsei;
111
112 /*! entry back to ns2_inst */
113 struct gprs_ns2_inst *nsi;
114
115 /*! llist entry for gprs_ns2_inst */
116 struct llist_head list;
117
118 /*! llist head to hold all nsvc */
119 struct llist_head nsvc;
120
121 /*! true if this NSE was created by VTY or pcu socket) */
122 bool persistent;
123
Alexander Couzensda0a2852020-10-01 23:24:07 +0200124 /*! true if this NSE wasn't yet alive at all.
125 * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */
126 bool first;
127
Alexander Couzens6a161492020-07-12 13:45:50 +0200128 /*! true if this NSE has at least one alive VC */
129 bool alive;
130
131 struct osmo_fsm_inst *bss_sns_fi;
132};
133
134/*! Structure representing a single NS-VC */
135struct gprs_ns2_vc {
136 /*! list of NS-VCs within NSE */
137 struct llist_head list;
138
139 /*! list of NS-VCs within bind, bind is the owner! */
140 struct llist_head blist;
141
142 /*! pointer to NS Instance */
143 struct gprs_ns2_nse *nse;
144
145 /*! pointer to NS VL bind. bind own the memory of this instance */
146 struct gprs_ns2_vc_bind *bind;
147
148 /*! true if this NS was created by VTY or pcu socket) */
149 bool persistent;
150
151 /*! uniquely identifies NS-VC if VC contains nsvci */
152 uint16_t nsvci;
153
154 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
155 uint8_t sig_weight;
156
157 /*! signaling weight. 0 = don't use for user data (BVCI != 0) */
158 uint8_t data_weight;
159
160 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
161 void *priv;
162
163 bool nsvci_is_valid;
164 bool sns_only;
165
166 struct rate_ctr_group *ctrg;
167 struct osmo_stat_item_group *statg;
168
169 /*! which link-layer are we based on? */
170 enum gprs_ns_ll ll;
171 enum gprs_ns2_vc_mode mode;
172
173 struct osmo_fsm_inst *fi;
174};
175
176/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
177struct gprs_ns2_vc_bind {
178 /*! list entry in nsi */
179 struct llist_head list;
180 /*! list of all VC */
181 struct llist_head nsvc;
182 /*! driver private structure */
183 void *priv;
184 /*! a pointer back to the nsi */
185 struct gprs_ns2_inst *nsi;
186 struct gprs_ns2_vc_driver *driver;
187
188 /*! if VCs use reset/block/unblock method. IP shall not use this */
189 enum gprs_ns2_vc_mode vc_mode;
190
191 /*! send a msg over a VC */
192 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
193
194 /*! free the vc priv data */
195 void (*free_vc)(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200196
Alexander Couzens22f34712020-10-02 02:34:39 +0200197 /*! allow to show information for the vty */
198 void (*dump_vty)(const struct gprs_ns2_vc_bind *bind,
199 struct vty *vty, bool stats);
200};
Alexander Couzens6a161492020-07-12 13:45:50 +0200201
202struct gprs_ns2_vc_driver {
203 const char *name;
204 void *priv;
205 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
206};
207
208enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
209 struct msgb *msg,
210 const char *logname,
211 struct msgb **reject,
212 struct gprs_ns2_vc **success);
213
Alexander Couzensffd49d02020-09-24 00:47:17 +0200214int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200215 struct msgb *msg);
216
217struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
218 struct gprs_ns2_nse *nse,
219 bool initiater);
220
221struct msgb *gprs_ns2_msgb_alloc(void);
222
223void gprs_ns2_sns_dump_vty(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200224void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100225 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200226 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200227 enum gprs_ns2_affecting_cause cause);
228void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
229
230/* message */
231int gprs_ns2_validate(struct gprs_ns2_vc *nsvc,
232 uint8_t pdu_type,
233 struct msgb *msg,
234 struct tlv_parsed *tp,
235 uint8_t *cause);
236
237/* SNS messages */
238int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
239 const struct gprs_ns_ie_ip4_elem *ip4_elems,
240 unsigned int num_ip4_elems,
241 const struct gprs_ns_ie_ip6_elem *ip6_elems,
242 unsigned int num_ip6_elems);
243int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
244 const struct gprs_ns_ie_ip4_elem *ip4_elems,
245 unsigned int num_ip4_elems,
246 const struct gprs_ns_ie_ip6_elem *ip6_elems,
247 unsigned int num_ip6_elems);
248int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
249int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
250 int ip4_ep_nr, int ip6_ep_nr);
251int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
252
253/* transmit message over a VC */
254int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
255int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
256
257int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
258int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
259
260int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
261int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
262
263int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
264int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
265
266int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
267 uint16_t bvci, uint8_t sducontrol,
268 struct msgb *msg);
269
270int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
271 uint16_t bvci, struct msgb *orig_msg);
272
273/* driver */
274struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
275 struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700276 const struct osmo_sockaddr *remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200277
278/* sns */
279int gprs_ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
280struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
281 const char *id);
282int ns2_sns_bss_fsm_start(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700283 const struct osmo_sockaddr *remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200284void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc);
285
286/* vc */
287struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
288 const char *id, bool initiate);
289int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100290int gprs_ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200291int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
292int gprs_ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
293int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
294
295/* vty.c */
296void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind);
297
298/* nse */
299void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);