blob: 4c0cdd01a85436b52f452a80d981c1b696f605de [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
Alexander Couzens90ee9632020-12-07 06:18:32 +010018#define NS_TIMERS_COUNT 10
Harald Welte3a44d172020-12-16 12:02:51 +010019#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries|tsns-prov|tsns-size-retries|tsns-config-retries)"
Alexander Couzens6a161492020-07-12 13:45:50 +020020#define NS_TIMERS_HELP \
21 "(un)blocking Timer (Tns-block) timeout\n" \
22 "(un)blocking Timer (Tns-block) number of retries\n" \
23 "Reset Timer (Tns-reset) timeout\n" \
24 "Reset Timer (Tns-reset) number of retries\n" \
25 "Test Timer (Tns-test) timeout\n" \
26 "Alive Timer (Tns-alive) timeout\n" \
27 "Alive Timer (Tns-alive) number of retries\n" \
Alexander Couzens90ee9632020-12-07 06:18:32 +010028 "SNS Provision Timer (Tsns-prov) timeout\n" \
29 "SNS Size number of retries\n" \
30 "SNS Config number of retries\n" \
Alexander Couzens6a161492020-07-12 13:45:50 +020031
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,
Alexander Couzens90ee9632020-12-07 06:18:32 +010045 NS_TOUT_TSNS_SIZE_RETRIES,
46 NS_TOUT_TSNS_CONFIG_RETRIES,
Alexander Couzens6a161492020-07-12 13:45:50 +020047};
48
49enum nsvc_timer_mode {
50 /* standard timers */
51 NSVC_TIMER_TNS_TEST,
52 NSVC_TIMER_TNS_ALIVE,
53 NSVC_TIMER_TNS_RESET,
54 _NSVC_TIMER_NR,
55};
56
57enum ns_stat {
58 NS_STAT_ALIVE_DELAY,
59};
60
Alexander Couzens6a161492020-07-12 13:45:50 +020061/*! Osmocom NS2 VC create status */
62enum gprs_ns2_cs {
63 GPRS_NS2_CS_CREATED, /*!< A NSVC object has been created */
64 GPRS_NS2_CS_FOUND, /*!< A NSVC object has been found */
65 GPRS_NS2_CS_REJECTED, /*!< Rejected and answered message */
66 GPRS_NS2_CS_SKIPPED, /*!< Skipped message */
67 GPRS_NS2_CS_ERROR, /*!< Failed to process message */
68};
69
70
71#define NSE_S_BLOCKED 0x0001
72#define NSE_S_ALIVE 0x0002
73#define NSE_S_RESET 0x0004
74
75#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
76#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
77#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
78
79/*! An instance of the NS protocol stack */
80struct gprs_ns2_inst {
81 /*! callback to the user for incoming UNIT DATA IND */
82 osmo_prim_cb cb;
83
84 /*! callback data */
85 void *cb_data;
86
87 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
88 struct llist_head binding;
89
90 /*! linked lists of all NSVC in this instance */
91 struct llist_head nse;
92
93 /*! create dynamic NSE on receiving packages */
94 bool create_nse;
95
96 uint16_t timeout[NS_TIMERS_COUNT];
97
98 /*! workaround for rate counter until rate counter accepts char str as index */
99 uint32_t rate_ctr_idx;
Harald Welte53a2fde2020-12-01 22:21:14 +0100100
101 /*! libmnl netlink socket for link state monitoring */
102 struct osmo_mnl *linkmon_mnl;
Alexander Couzens6a161492020-07-12 13:45:50 +0200103};
104
Harald Welte53a2fde2020-12-01 22:21:14 +0100105
Alexander Couzens6a161492020-07-12 13:45:50 +0200106/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
107struct gprs_ns2_nse {
108 uint16_t nsei;
109
110 /*! entry back to ns2_inst */
111 struct gprs_ns2_inst *nsi;
112
113 /*! llist entry for gprs_ns2_inst */
114 struct llist_head list;
115
116 /*! llist head to hold all nsvc */
117 struct llist_head nsvc;
118
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100119 /*! count all active NSVCs */
120 int nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100121
Alexander Couzens6a161492020-07-12 13:45:50 +0200122 /*! true if this NSE was created by VTY or pcu socket) */
123 bool persistent;
124
Alexander Couzensda0a2852020-10-01 23:24:07 +0200125 /*! true if this NSE wasn't yet alive at all.
126 * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */
127 bool first;
128
Alexander Couzens6a161492020-07-12 13:45:50 +0200129 /*! true if this NSE has at least one alive VC */
130 bool alive;
131
Alexander Couzensaac90162020-11-19 02:44:04 +0100132 /*! which link-layer are we based on? */
133 enum gprs_ns2_ll ll;
134
Alexander Couzensd923cff2020-12-01 01:03:52 +0100135 /*! which dialect does this NSE speaks? */
136 enum gprs_ns2_dialect dialect;
137
Alexander Couzens6a161492020-07-12 13:45:50 +0200138 struct osmo_fsm_inst *bss_sns_fi;
139};
140
141/*! Structure representing a single NS-VC */
142struct gprs_ns2_vc {
143 /*! list of NS-VCs within NSE */
144 struct llist_head list;
145
146 /*! list of NS-VCs within bind, bind is the owner! */
147 struct llist_head blist;
148
149 /*! pointer to NS Instance */
150 struct gprs_ns2_nse *nse;
151
152 /*! pointer to NS VL bind. bind own the memory of this instance */
153 struct gprs_ns2_vc_bind *bind;
154
155 /*! true if this NS was created by VTY or pcu socket) */
156 bool persistent;
157
158 /*! uniquely identifies NS-VC if VC contains nsvci */
159 uint16_t nsvci;
160
161 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
162 uint8_t sig_weight;
163
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100164 /*! signalling packet counter for the load sharing function */
165 uint8_t sig_counter;
166
167 /*! data weight. 0 = don't use for user data (BVCI != 0) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200168 uint8_t data_weight;
169
170 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
171 void *priv;
172
173 bool nsvci_is_valid;
174 bool sns_only;
175
176 struct rate_ctr_group *ctrg;
177 struct osmo_stat_item_group *statg;
178
Alexander Couzens6a161492020-07-12 13:45:50 +0200179 enum gprs_ns2_vc_mode mode;
180
181 struct osmo_fsm_inst *fi;
182};
183
184/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
185struct gprs_ns2_vc_bind {
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100186 /*! unique name */
187 const char *name;
Alexander Couzens6a161492020-07-12 13:45:50 +0200188 /*! list entry in nsi */
189 struct llist_head list;
190 /*! list of all VC */
191 struct llist_head nsvc;
192 /*! driver private structure */
193 void *priv;
194 /*! a pointer back to the nsi */
195 struct gprs_ns2_inst *nsi;
196 struct gprs_ns2_vc_driver *driver;
197
Alexander Couzensd923cff2020-12-01 01:03:52 +0100198 bool accept_ipaccess;
199 bool accept_sns;
Alexander Couzens6a161492020-07-12 13:45:50 +0200200
Alexander Couzensaac90162020-11-19 02:44:04 +0100201 /*! which link-layer are we based on? */
202 enum gprs_ns2_ll ll;
203
Alexander Couzens6a161492020-07-12 13:45:50 +0200204 /*! send a msg over a VC */
205 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
206
207 /*! free the vc priv data */
208 void (*free_vc)(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200209
Alexander Couzens22f34712020-10-02 02:34:39 +0200210 /*! allow to show information for the vty */
211 void (*dump_vty)(const struct gprs_ns2_vc_bind *bind,
212 struct vty *vty, bool stats);
213};
Alexander Couzens6a161492020-07-12 13:45:50 +0200214
215struct gprs_ns2_vc_driver {
216 const char *name;
217 void *priv;
218 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
219};
220
221enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
222 struct msgb *msg,
223 const char *logname,
224 struct msgb **reject,
225 struct gprs_ns2_vc **success);
226
Alexander Couzensffd49d02020-09-24 00:47:17 +0200227int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200228 struct msgb *msg);
229
230struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
231 struct gprs_ns2_nse *nse,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100232 bool initiater,
233 enum gprs_ns2_vc_mode vc_mode);
Alexander Couzens6a161492020-07-12 13:45:50 +0200234
235struct msgb *gprs_ns2_msgb_alloc(void);
236
Alexander Couzens412bc342020-11-19 05:24:37 +0100237void gprs_ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200238void gprs_ns2_sns_dump_vty(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200239void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100240 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200241 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200242 enum gprs_ns2_affecting_cause cause);
243void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
244
245/* message */
246int gprs_ns2_validate(struct gprs_ns2_vc *nsvc,
247 uint8_t pdu_type,
248 struct msgb *msg,
249 struct tlv_parsed *tp,
250 uint8_t *cause);
251
252/* SNS messages */
253int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
254 const struct gprs_ns_ie_ip4_elem *ip4_elems,
255 unsigned int num_ip4_elems,
256 const struct gprs_ns_ie_ip6_elem *ip6_elems,
257 unsigned int num_ip6_elems);
258int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
259 const struct gprs_ns_ie_ip4_elem *ip4_elems,
260 unsigned int num_ip4_elems,
261 const struct gprs_ns_ie_ip6_elem *ip6_elems,
262 unsigned int num_ip6_elems);
263int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
264int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
265 int ip4_ep_nr, int ip6_ep_nr);
266int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
267
268/* transmit message over a VC */
269int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
270int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
271
272int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
273int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
274
275int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
276int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
277
278int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
279int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
280
281int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
282 uint16_t bvci, uint8_t sducontrol,
283 struct msgb *msg);
284
285int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
286 uint16_t bvci, struct msgb *orig_msg);
287
288/* driver */
289struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
290 struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700291 const struct osmo_sockaddr *remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100292int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote);
293struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
294 struct osmo_sockaddr *remote,
295 int index);
Alexander Couzens6a161492020-07-12 13:45:50 +0200296
297/* sns */
298int gprs_ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
299struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
300 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200301void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc);
302
303/* vc */
304struct osmo_fsm_inst *gprs_ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
305 const char *id, bool initiate);
306int gprs_ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
Daniel Willmanned0c9822020-11-18 14:08:07 +0100307int gprs_ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200308int gprs_ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
309int gprs_ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
310int gprs_ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
311
Alexander Couzens6a161492020-07-12 13:45:50 +0200312/* nse */
313void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100314enum gprs_ns2_vc_mode gprs_ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect);