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